Merge ~sajoupa/charm-telegraf:snap-support into charm-telegraf:master
- Git
- lp:~sajoupa/charm-telegraf
- snap-support
- Merge into master
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | Joe Guo | ||||
Approved revision: | ae53ba8ebee05c43a650a791e394fe31276ee239 | ||||
Merged at revision: | ac0af7b8cb76e77adc913ec67c2f28272f77980d | ||||
Proposed branch: | ~sajoupa/charm-telegraf:snap-support | ||||
Merge into: | charm-telegraf:master | ||||
Diff against target: |
718 lines (+231/-82) 11 files modified
README.md (+10/-0) config.yaml (+11/-1) files/telegraf_exec_metrics.py (+1/-1) layer.yaml (+1/-3) reactive/telegraf.py (+130/-31) tests/bundles/focal-snap.yaml (+14/-0) tests/test_telegraf.py (+26/-10) tests/tests.yaml (+1/-0) unit_tests/test_mysql.py (+6/-2) unit_tests/test_postgresql.py (+15/-3) unit_tests/test_telegraf.py (+16/-31) |
||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Stuart Bishop (community) | Approve | ||
Joe Guo (community) | Needs Fixing | ||
Xav Paice (community) | Needs Fixing | ||
Paul Goins | Approve | ||
Canonical IS Reviewers | Pending | ||
Review via email: mp+386570@code.launchpad.net |
Commit message
Add support for either deb or snap install (defaults to deb)
Description of the change
Laurent Sesquès (sajoupa) wrote : | # |
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.
Stuart Bishop (stub) wrote : | # |
Looks great. Some minor comments inline, but nothing requiring changes.
Xav Paice (xavpaice) wrote : | # |
Needs a rebase against master to resolve merge conflicts.
Tests all pass, including the focal/snap test.
Laurent Sesquès (sajoupa) wrote : | # |
- rebase done
- fixed a few regressions introduced in recent commits in master
- followed stub's advice for {!r}
- successfully passes `make test`
Laurent Sesquès (sajoupa) wrote : | # |
replied to the inline comments.
Joe Guo (guoqiao) wrote : | # |
Hi Laurent,
Did you run `make test` as root?
I just ran `make unittest` as non-root, it will pop up dialogues to ask for authentication.
You may need to add this line in unit_tests/
monkeypatch.
Joe Guo (guoqiao) wrote : | # |
Other than above, both unit and functional tests passed.
Stuart Bishop (stub) wrote : | # |
Yup. Some trivial formatting issues, inline.
Laurent Sesquès (sajoupa) wrote : | # |
> Hi Laurent,
>
> Did you run `make test` as root?
> I just ran `make unittest` as non-root, it will pop up dialogues to ask for
> authentication.
> You may need to add this line in unit_tests/
>
> monkeypatch.
Hi Joe,
I've always run it successfully as a normal user.
But your suggestion makes sense, I added it.
Laurent Sesquès (sajoupa) wrote : | # |
Pushed updates addressing guoqiao's and stub's comments.
Still passes `make test` successfully.
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change successfully merged at revision ac0af7b8cb76e77
Preview Diff
1 | diff --git a/README.md b/README.md | |||
2 | index d5ae81e..636dcda 100644 | |||
3 | --- a/README.md | |||
4 | +++ b/README.md | |||
5 | @@ -28,6 +28,16 @@ address rather than the unit's private address. | |||
6 | 28 | 28 | ||
7 | 29 | # Configuration | 29 | # Configuration |
8 | 30 | 30 | ||
9 | 31 | ## Installation method: deb or snap | ||
10 | 32 | |||
11 | 33 | The charm can deploy using either a deb (default) or a snap installation. | ||
12 | 34 | juju config telegraf install_method=deb # to use the debian package | ||
13 | 35 | juju config telegraf install_method=snap # to use the snap | ||
14 | 36 | Only the needed package (deb or snap) will be installed. | ||
15 | 37 | |||
16 | 38 | To change the snap channel: | ||
17 | 39 | juju config telegraf snap_channel=beta | ||
18 | 40 | |||
19 | 31 | ## Prometheus Output | 41 | ## Prometheus Output |
20 | 32 | 42 | ||
21 | 33 | By default, Prometheus output is presented available for scraping. | 43 | By default, Prometheus output is presented available for scraping. |
22 | diff --git a/config.yaml b/config.yaml | |||
23 | index 215baff..454e461 100644 | |||
24 | --- a/config.yaml | |||
25 | +++ b/config.yaml | |||
26 | @@ -97,6 +97,16 @@ options: | |||
27 | 97 | # description and type comes from the apt layer. | 97 | # description and type comes from the apt layer. |
28 | 98 | default: | | 98 | default: | |
29 | 99 | - null | 99 | - null |
30 | 100 | install_method: | ||
31 | 101 | type: string | ||
32 | 102 | default: "deb" | ||
33 | 103 | description: > | ||
34 | 104 | Installation method. Valid values are "deb" or "snap". | ||
35 | 105 | snap_channel: | ||
36 | 106 | default: "stable" | ||
37 | 107 | type: string | ||
38 | 108 | description: | | ||
39 | 109 | Snap Store channel from which to install the telegraf snap. | ||
40 | 100 | extra_options: | 110 | extra_options: |
41 | 101 | default: "" | 111 | default: "" |
42 | 102 | type: string | 112 | type: string |
43 | @@ -125,7 +135,7 @@ options: | |||
44 | 125 | type: string | 135 | type: string |
45 | 126 | description: | | 136 | description: | |
46 | 127 | Extra plugins, manually configured. This is expected to be a string | 137 | Extra plugins, manually configured. This is expected to be a string |
48 | 128 | and will be saved "as is" in /etc/telegraf/telegraf.d/extra_plugins.conf | 138 | and will be saved "as is" in telegraf.d/extra_plugins.conf |
49 | 129 | disabled_plugins: | 139 | disabled_plugins: |
50 | 130 | type: string | 140 | type: string |
51 | 131 | default: "" | 141 | default: "" |
52 | diff --git a/files/telegraf_exec_metrics.py b/files/telegraf_exec_metrics.py | |||
53 | index 8525e0e..0a08c13 100755 | |||
54 | --- a/files/telegraf_exec_metrics.py | |||
55 | +++ b/files/telegraf_exec_metrics.py | |||
56 | @@ -203,7 +203,7 @@ class NetNSCmdMetric(CmdMetric): | |||
57 | 203 | name_override = "{name}" | 203 | name_override = "{name}" |
58 | 204 | data_format = "json" | 204 | data_format = "json" |
59 | 205 | tag_keys = ["namespace", "id"] | 205 | tag_keys = ["namespace", "id"] |
61 | 206 | interval = 600 | 206 | interval = "600s" |
62 | 207 | ''') | 207 | ''') |
63 | 208 | 208 | ||
64 | 209 | def parse(self, content): | 209 | def parse(self, content): |
65 | diff --git a/layer.yaml b/layer.yaml | |||
66 | index 346e4d8..057a4c2 100644 | |||
67 | --- a/layer.yaml | |||
68 | +++ b/layer.yaml | |||
69 | @@ -20,6 +20,7 @@ includes: | |||
70 | 20 | - layer:leadership | 20 | - layer:leadership |
71 | 21 | - layer:ntpmon | 21 | - layer:ntpmon |
72 | 22 | - layer:promreg-client | 22 | - layer:promreg-client |
73 | 23 | - layer:snap | ||
74 | 23 | - interface:elasticsearch | 24 | - interface:elasticsearch |
75 | 24 | - interface:grafana-dashboard | 25 | - interface:grafana-dashboard |
76 | 25 | - interface:http | 26 | - interface:http |
77 | @@ -41,7 +42,4 @@ options: | |||
78 | 41 | - sysstat | 42 | - sysstat |
79 | 42 | - python3-yaml | 43 | - python3-yaml |
80 | 43 | - python3-netifaces | 44 | - python3-netifaces |
81 | 44 | apt: | ||
82 | 45 | packages: | ||
83 | 46 | - telegraf | ||
84 | 47 | repo: https://git.launchpad.net/telegraf-charm | 45 | repo: https://git.launchpad.net/telegraf-charm |
85 | diff --git a/reactive/telegraf.py b/reactive/telegraf.py | |||
86 | index 28ee023..d62efd2 100644 | |||
87 | --- a/reactive/telegraf.py | |||
88 | +++ b/reactive/telegraf.py | |||
89 | @@ -28,6 +28,8 @@ import re | |||
90 | 28 | import netifaces | 28 | import netifaces |
91 | 29 | import subprocess | 29 | import subprocess |
92 | 30 | 30 | ||
93 | 31 | from charms import apt | ||
94 | 32 | from charms.layer import snap | ||
95 | 31 | from charms.reactive import ( | 33 | from charms.reactive import ( |
96 | 32 | endpoint_from_flag, | 34 | endpoint_from_flag, |
97 | 33 | helpers, | 35 | helpers, |
98 | @@ -45,11 +47,11 @@ from charmhelpers.core import hookenv, host, unitdata | |||
99 | 45 | from charmhelpers.core.templating import render | 47 | from charmhelpers.core.templating import render |
100 | 46 | from charmhelpers.core.host import is_container | 48 | from charmhelpers.core.host import is_container |
101 | 47 | from charmhelpers.contrib.charmsupport import nrpe | 49 | from charmhelpers.contrib.charmsupport import nrpe |
102 | 48 | from charmhelpers.fetch import apt_cache | ||
103 | 49 | import charms.promreg | 50 | import charms.promreg |
104 | 50 | from jinja2 import Template, Environment, FileSystemLoader, exceptions | 51 | from jinja2 import Template, Environment, FileSystemLoader, exceptions |
105 | 51 | 52 | ||
107 | 52 | BASE_DIR = '/etc/telegraf' | 53 | DEB_BASE_DIR = '/etc/telegraf' |
108 | 54 | SNAP_BASE_DIR = '/var/snap/telegraf/current' | ||
109 | 53 | 55 | ||
110 | 54 | CONFIG_FILE = 'telegraf.conf' | 56 | CONFIG_FILE = 'telegraf.conf' |
111 | 55 | 57 | ||
112 | @@ -59,16 +61,51 @@ GRAFANA_DASHBOARD_TELEGRAF_FILE_NAME = 'Telegraf.json.j2' | |||
113 | 59 | 61 | ||
114 | 60 | GRAFANA_DASHBOARD_NAME = 'telegraf' | 62 | GRAFANA_DASHBOARD_NAME = 'telegraf' |
115 | 61 | 63 | ||
116 | 64 | SNAP_SERVICE = 'snap.telegraf.telegraf' | ||
117 | 65 | DEB_SERVICE = 'telegraf' | ||
118 | 66 | |||
119 | 62 | # Utilities # | 67 | # Utilities # |
120 | 63 | 68 | ||
121 | 64 | 69 | ||
129 | 65 | def exec_timeout_supported(): | 70 | class InvalidInstallMethod(Exception): |
130 | 66 | cache = apt_cache() | 71 | pass |
131 | 67 | pkg = cache['telegraf'] | 72 | |
132 | 68 | timeout_support = True | 73 | |
133 | 69 | if '0.12' in pkg.current_ver.ver_str: | 74 | def get_install_method(): |
134 | 70 | timeout_support = False | 75 | config = hookenv.config() |
135 | 71 | return timeout_support | 76 | if config['install_method'] in ['deb', 'snap']: |
136 | 77 | return config['install_method'] | ||
137 | 78 | else: | ||
138 | 79 | hookenv.log( | ||
139 | 80 | "Invalid install_method for telegraf: {}".format(config['install_method']), | ||
140 | 81 | level=hookenv.ERROR) | ||
141 | 82 | raise InvalidInstallMethod() | ||
142 | 83 | |||
143 | 84 | |||
144 | 85 | def get_base_dir(): | ||
145 | 86 | config = hookenv.config() | ||
146 | 87 | if config['install_method'] == 'deb': | ||
147 | 88 | return DEB_BASE_DIR | ||
148 | 89 | elif config['install_method'] == 'snap': | ||
149 | 90 | return SNAP_BASE_DIR | ||
150 | 91 | else: | ||
151 | 92 | hookenv.log( | ||
152 | 93 | "Invalid install_method for telegraf: {}".format(config['install_method']), | ||
153 | 94 | level=hookenv.ERROR) | ||
154 | 95 | raise InvalidInstallMethod() | ||
155 | 96 | |||
156 | 97 | |||
157 | 98 | def get_service(): | ||
158 | 99 | config = hookenv.config() | ||
159 | 100 | if config['install_method'] == 'deb': | ||
160 | 101 | return DEB_SERVICE | ||
161 | 102 | elif config['install_method'] == 'snap': | ||
162 | 103 | return SNAP_SERVICE | ||
163 | 104 | else: | ||
164 | 105 | hookenv.log( | ||
165 | 106 | "Invalid install_method for telegraf: {}".format(config['install_method']), | ||
166 | 107 | level=hookenv.ERROR) | ||
167 | 108 | raise InvalidInstallMethod() | ||
168 | 72 | 109 | ||
169 | 73 | 110 | ||
170 | 74 | def get_templates_dir(): | 111 | def get_templates_dir(): |
171 | @@ -76,11 +113,11 @@ def get_templates_dir(): | |||
172 | 76 | 113 | ||
173 | 77 | 114 | ||
174 | 78 | def get_main_config_path(): | 115 | def get_main_config_path(): |
176 | 79 | return os.path.join(BASE_DIR, CONFIG_FILE) | 116 | return os.path.join(get_base_dir(), CONFIG_FILE) |
177 | 80 | 117 | ||
178 | 81 | 118 | ||
179 | 82 | def get_configs_dir(): | 119 | def get_configs_dir(): |
181 | 83 | return os.path.join(BASE_DIR, CONFIG_DIR) | 120 | return os.path.join(get_base_dir(), CONFIG_DIR) |
182 | 84 | 121 | ||
183 | 85 | 122 | ||
184 | 86 | def get_files_dir(): | 123 | def get_files_dir(): |
185 | @@ -314,13 +351,15 @@ def update_sysstat_config_with_sdac_xall(path='/etc/sysstat/sysstat'): | |||
186 | 314 | hookenv.log("sysstat config file not found: {}".format(path), level=hookenv.WARNING) | 351 | hookenv.log("sysstat config file not found: {}".format(path), level=hookenv.WARNING) |
187 | 315 | 352 | ||
188 | 316 | 353 | ||
194 | 317 | # States | 354 | def configure_telegraf(): # noqa: C901 |
190 | 318 | |||
191 | 319 | @when('apt.installed.telegraf') | ||
192 | 320 | @when_not('telegraf.configured') | ||
193 | 321 | def configure_telegraf(): # noqa: C901 (compexity 15) | ||
195 | 322 | update_sysstat_config_with_sdac_xall() | 355 | update_sysstat_config_with_sdac_xall() |
197 | 323 | config_path = get_main_config_path() | 356 | config = hookenv.config() |
198 | 357 | context = config.copy() | ||
199 | 358 | try: | ||
200 | 359 | config_path = get_main_config_path() | ||
201 | 360 | except InvalidInstallMethod: | ||
202 | 361 | hookenv.status_set('blocked', 'Wrong install_method provided: {!r}'.format(config['install_method'])) | ||
203 | 362 | return | ||
204 | 324 | if get_remote_unit_name() is None: | 363 | if get_remote_unit_name() is None: |
205 | 325 | hookenv.status_set('waiting', 'Waiting for juju-info relation') | 364 | hookenv.status_set('waiting', 'Waiting for juju-info relation') |
206 | 326 | # if UNIT_NAME in hostname config and relation not yet available, | 365 | # if UNIT_NAME in hostname config and relation not yet available, |
207 | @@ -329,8 +368,6 @@ def configure_telegraf(): # noqa: C901 (compexity 15) | |||
208 | 329 | os.unlink(config_path) | 368 | os.unlink(config_path) |
209 | 330 | return | 369 | return |
210 | 331 | 370 | ||
211 | 332 | config = hookenv.config() | ||
212 | 333 | context = config.copy() | ||
213 | 334 | inputs = config.get('inputs_config', '') | 371 | inputs = config.get('inputs_config', '') |
214 | 335 | outputs = config.get('outputs_config', '') | 372 | outputs = config.get('outputs_config', '') |
215 | 336 | # just for the migration out of base64 | 373 | # just for the migration out of base64 |
216 | @@ -370,8 +407,10 @@ def configure_telegraf(): # noqa: C901 (compexity 15) | |||
217 | 370 | context["hostname"] = get_hostname_label() | 407 | context["hostname"] = get_hostname_label() |
218 | 371 | 408 | ||
219 | 372 | logfile_path = os.path.normpath(context["logfile"]) | 409 | logfile_path = os.path.normpath(context["logfile"]) |
222 | 373 | if context["logfile"] and not logfile_path.startswith("/var/log/"): | 410 | if (context["logfile"] and not logfile_path.startswith("/var/log/") and not |
223 | 374 | # only allow logging in /var/log or in syslog | 411 | (config["install_method"] == 'snap' and |
224 | 412 | logfile_path.startswith("/var/snap/telegraf/common/"))): | ||
225 | 413 | # only allow logging in /var/log, syslog, or /var/snap/telegraf/common | ||
226 | 375 | hookenv.log("logfile value reset to stderr." | 414 | hookenv.log("logfile value reset to stderr." |
227 | 376 | " Original value: {}".format(context["logfile"]), | 415 | " Original value: {}".format(context["logfile"]), |
228 | 377 | hookenv.DEBUG) | 416 | hookenv.DEBUG) |
229 | @@ -401,10 +440,64 @@ def configure_telegraf(): # noqa: C901 (compexity 15) | |||
230 | 401 | hookenv.log("Updating main config file") | 440 | hookenv.log("Updating main config file") |
231 | 402 | render(source='telegraf.conf.tmpl', templates_dir=get_templates_dir(), | 441 | render(source='telegraf.conf.tmpl', templates_dir=get_templates_dir(), |
232 | 403 | target=config_path, context=context) | 442 | target=config_path, context=context) |
235 | 404 | host.service_resume('telegraf') # unmask and enable service, start it if not yet | 443 | for service in [DEB_SERVICE, SNAP_SERVICE]: |
236 | 405 | host.service_reload('telegraf') # reload or restart service, to apply config changes | 444 | if service == get_service(): |
237 | 445 | host.service_resume(service) | ||
238 | 446 | host.service_reload(service) | ||
239 | 447 | else: | ||
240 | 448 | try: | ||
241 | 449 | host.service_pause(service) | ||
242 | 450 | except ValueError: | ||
243 | 451 | # On machines not supporting snaps, it won't find the service | ||
244 | 452 | # If we're using the snap, and the deb failed to install, it's fine too | ||
245 | 453 | pass | ||
246 | 406 | set_flag('telegraf.configured') | 454 | set_flag('telegraf.configured') |
247 | 407 | set_flag('telegraf.needs_reload') | 455 | set_flag('telegraf.needs_reload') |
248 | 456 | if config["install_method"] == 'deb': | ||
249 | 457 | set_flag('telegraf.apt.configured') | ||
250 | 458 | else: | ||
251 | 459 | set_flag('telegraf.snap.configured') | ||
252 | 460 | |||
253 | 461 | |||
254 | 462 | # States | ||
255 | 463 | |||
256 | 464 | @when_not('telegraf.installed') | ||
257 | 465 | def install_telegraf(): | ||
258 | 466 | try: | ||
259 | 467 | install_method = get_install_method() | ||
260 | 468 | except InvalidInstallMethod: | ||
261 | 469 | hookenv.status_set('blocked', "Wrong install_method provided. Expected either 'deb' or 'snap'.") | ||
262 | 470 | return | ||
263 | 471 | if install_method == 'deb': | ||
264 | 472 | try: | ||
265 | 473 | snap.remove('telegraf') | ||
266 | 474 | except Exception: | ||
267 | 475 | # the snap may already be absent, or snaps may not even be supported in this environment | ||
268 | 476 | pass | ||
269 | 477 | apt.queue_install(['telegraf']) | ||
270 | 478 | elif install_method == 'snap': | ||
271 | 479 | apt.purge('telegraf') | ||
272 | 480 | config = hookenv.config() | ||
273 | 481 | snap_channel = config.get('snap_channel') | ||
274 | 482 | snap.install('telegraf', channel=snap_channel, classic=True) | ||
275 | 483 | if install_method: | ||
276 | 484 | set_flag('telegraf.installed') | ||
277 | 485 | |||
278 | 486 | |||
279 | 487 | @when('telegraf.installed') | ||
280 | 488 | @when('apt.installed.telegraf') | ||
281 | 489 | @when_not('telegraf.configured') | ||
282 | 490 | @when_not('telegraf.apt.configured') | ||
283 | 491 | def configure_telegraf_deb(): | ||
284 | 492 | configure_telegraf() | ||
285 | 493 | |||
286 | 494 | |||
287 | 495 | @when('telegraf.installed') | ||
288 | 496 | @when('snap.installed.telegraf') | ||
289 | 497 | @when_not('telegraf.configured') | ||
290 | 498 | @when_not('telegraf.snap.configured') | ||
291 | 499 | def configure_telegraf_snap(): | ||
292 | 500 | configure_telegraf() | ||
293 | 408 | 501 | ||
294 | 409 | 502 | ||
295 | 410 | @hook('upgrade-charm') | 503 | @hook('upgrade-charm') |
296 | @@ -413,6 +506,8 @@ def upgrade_charm(): | |||
297 | 413 | clear_flag('plugins.{}.configured'.format(plugin)) | 506 | clear_flag('plugins.{}.configured'.format(plugin)) |
298 | 414 | clear_flag('extra_plugins.configured') | 507 | clear_flag('extra_plugins.configured') |
299 | 415 | clear_flag('telegraf.configured') | 508 | clear_flag('telegraf.configured') |
300 | 509 | clear_flag('telegraf.apt.configured') | ||
301 | 510 | clear_flag('telegraf.snap.configured') | ||
302 | 416 | 511 | ||
303 | 417 | 512 | ||
304 | 418 | @when('config.changed') | 513 | @when('config.changed') |
305 | @@ -424,7 +519,11 @@ def handle_config_changes(): | |||
306 | 424 | # if something else changed, let's reconfigure telegraf itself just in case | 519 | # if something else changed, let's reconfigure telegraf itself just in case |
307 | 425 | if config.changed('extra_plugins'): | 520 | if config.changed('extra_plugins'): |
308 | 426 | clear_flag('extra_plugins.configured') | 521 | clear_flag('extra_plugins.configured') |
309 | 522 | if config.changed('install_method') or config.changed('snap_channel') or config.changed('install_sources'): | ||
310 | 523 | clear_flag('telegraf.installed') | ||
311 | 427 | clear_flag('telegraf.configured') | 524 | clear_flag('telegraf.configured') |
312 | 525 | clear_flag('telegraf.apt.configured') | ||
313 | 526 | clear_flag('telegraf.snap.configured') | ||
314 | 428 | clear_flag('telegraf.nagios-setup.complete') | 527 | clear_flag('telegraf.nagios-setup.complete') |
315 | 429 | 528 | ||
316 | 430 | 529 | ||
317 | @@ -788,11 +887,8 @@ def exec_input(exec_rel): | |||
318 | 788 | if not commands: | 887 | if not commands: |
319 | 789 | hookenv.log("No Commands defined in the exec relation, doing nothing.") | 888 | hookenv.log("No Commands defined in the exec relation, doing nothing.") |
320 | 790 | return | 889 | return |
321 | 791 | timeout_support = exec_timeout_supported() | ||
322 | 792 | pre_proc_cmds = [] | 890 | pre_proc_cmds = [] |
323 | 793 | for command in commands: | 891 | for command in commands: |
324 | 794 | if not timeout_support: | ||
325 | 795 | command.pop('timeout') | ||
326 | 796 | run_on_this_unit = command.pop('run_on_this_unit') | 892 | run_on_this_unit = command.pop('run_on_this_unit') |
327 | 797 | if run_on_this_unit: | 893 | if run_on_this_unit: |
328 | 798 | pre_proc_cmds.append(command) | 894 | pre_proc_cmds.append(command) |
329 | @@ -1089,12 +1185,13 @@ def start_or_restart(): | |||
330 | 1089 | states = sorted([k for k in get_states().keys() | 1185 | states = sorted([k for k in get_states().keys() |
331 | 1090 | if k.startswith('plugins') or k.startswith('extra_plugins')]) | 1186 | if k.startswith('plugins') or k.startswith('extra_plugins')]) |
332 | 1091 | 1187 | ||
333 | 1188 | service = get_service() | ||
334 | 1092 | config_files_changed = helpers.any_file_changed(list_config_files()) | 1189 | config_files_changed = helpers.any_file_changed(list_config_files()) |
335 | 1093 | active_plugins_changed = helpers.data_changed('active_plugins', states or '') | 1190 | active_plugins_changed = helpers.data_changed('active_plugins', states or '') |
337 | 1094 | if not host.service_running('telegraf') \ | 1191 | if not host.service_running(service) \ |
338 | 1095 | or config_files_changed or active_plugins_changed: | 1192 | or config_files_changed or active_plugins_changed: |
339 | 1096 | hookenv.log("Restarting telegraf") | 1193 | hookenv.log("Restarting telegraf") |
341 | 1097 | host.service_restart('telegraf') | 1194 | host.service_restart(service) |
342 | 1098 | else: | 1195 | else: |
343 | 1099 | hookenv.log("Not restarting: active_plugins_changed={} | " | 1196 | hookenv.log("Not restarting: active_plugins_changed={} | " |
344 | 1100 | "config_files_changed={}".format(active_plugins_changed, | 1197 | "config_files_changed={}".format(active_plugins_changed, |
345 | @@ -1102,10 +1199,10 @@ def start_or_restart(): | |||
346 | 1102 | 1199 | ||
347 | 1103 | # Give telegraf time to restart. | 1200 | # Give telegraf time to restart. |
348 | 1104 | timeout = time.time() + 15 | 1201 | timeout = time.time() + 15 |
350 | 1105 | while not host.service_running('telegraf') and time.time() < timeout: | 1202 | while not host.service_running(service) and time.time() < timeout: |
351 | 1106 | time.sleep(0.1) | 1203 | time.sleep(0.1) |
352 | 1107 | 1204 | ||
354 | 1108 | if host.service_running('telegraf'): | 1205 | if host.service_running(service): |
355 | 1109 | hookenv.status_set('active', | 1206 | hookenv.status_set('active', |
356 | 1110 | 'Monitoring {}'.format(get_remote_unit_name())) | 1207 | 'Monitoring {}'.format(get_remote_unit_name())) |
357 | 1111 | clear_flag('telegraf.needs_reload') | 1208 | clear_flag('telegraf.needs_reload') |
358 | @@ -1130,10 +1227,12 @@ def update_status(): | |||
359 | 1130 | hash_type='sha256') | 1227 | hash_type='sha256') |
360 | 1131 | if changed: | 1228 | if changed: |
361 | 1132 | clear_flag('telegraf.configured') | 1229 | clear_flag('telegraf.configured') |
362 | 1230 | clear_flag('telegraf.apt.configured') | ||
363 | 1231 | clear_flag('telegraf.snap.configured') | ||
364 | 1133 | 1232 | ||
365 | 1134 | 1233 | ||
366 | 1135 | @when('nrpe-external-master.available') | 1234 | @when('nrpe-external-master.available') |
368 | 1136 | @when('apt.installed.telegraf') | 1235 | @when('telegraf.installed') |
369 | 1137 | @when('telegraf.configured') | 1236 | @when('telegraf.configured') |
370 | 1138 | @when_not('telegraf.nagios-setup.complete') | 1237 | @when_not('telegraf.nagios-setup.complete') |
371 | 1139 | def configure_nagios(nagios): | 1238 | def configure_nagios(nagios): |
372 | diff --git a/tests/bundles/focal-snap.yaml b/tests/bundles/focal-snap.yaml | |||
373 | 1140 | new file mode 100644 | 1239 | new file mode 100644 |
374 | index 0000000..cfd7e2a | |||
375 | --- /dev/null | |||
376 | +++ b/tests/bundles/focal-snap.yaml | |||
377 | @@ -0,0 +1,14 @@ | |||
378 | 1 | series: focal | ||
379 | 2 | |||
380 | 3 | applications: | ||
381 | 4 | telegraf: | ||
382 | 5 | num_units: 0 | ||
383 | 6 | options: | ||
384 | 7 | install_method: 'snap' | ||
385 | 8 | ubuntu: | ||
386 | 9 | charm: cs:ubuntu | ||
387 | 10 | num_units: 1 | ||
388 | 11 | |||
389 | 12 | relations: | ||
390 | 13 | - - ubuntu:juju-info | ||
391 | 14 | - telegraf:juju-info | ||
392 | diff --git a/tests/test_telegraf.py b/tests/test_telegraf.py | |||
393 | index 592f415..19feb41 100644 | |||
394 | --- a/tests/test_telegraf.py | |||
395 | +++ b/tests/test_telegraf.py | |||
396 | @@ -29,7 +29,8 @@ DEFAULT_HTTPGET_TIMEOUT = 10 | |||
397 | 29 | DEFAULT_RETRIES = 12 | 29 | DEFAULT_RETRIES = 12 |
398 | 30 | DEFAULT_RETRIES_TIMEOUT = 5 | 30 | DEFAULT_RETRIES_TIMEOUT = 5 |
399 | 31 | DEFAULT_TELEGRAF_EXPORTER_PORT = "9103" | 31 | DEFAULT_TELEGRAF_EXPORTER_PORT = "9103" |
401 | 32 | TELEGRAF_CONF = "/etc/telegraf/telegraf.conf" | 32 | DEB_TELEGRAF_CONF = "/etc/telegraf/telegraf.conf" |
402 | 33 | SNAP_TELEGRAF_CONF = "/var/snap/telegraf/current/telegraf.conf" | ||
403 | 33 | 34 | ||
404 | 34 | 35 | ||
405 | 35 | class BaseTelegrafTest(unittest.TestCase): | 36 | class BaseTelegrafTest(unittest.TestCase): |
406 | @@ -53,11 +54,6 @@ class TestTelegraf(BaseTelegrafTest): | |||
407 | 53 | # findall returns a list, [] when no match | 54 | # findall returns a list, [] when no match |
408 | 54 | self.assertTrue(re.findall(re_pattern, text, flags=re.M)) | 55 | self.assertTrue(re.findall(re_pattern, text, flags=re.M)) |
409 | 55 | 56 | ||
410 | 56 | def check_re_pattern(self, re_pattern, text): | ||
411 | 57 | logging.info('checking metrics %s', re_pattern) | ||
412 | 58 | # findall returns a list, [] when no match | ||
413 | 59 | self.assertTrue(re.findall(re_pattern, text, flags=re.M)) | ||
414 | 60 | |||
415 | 61 | def test_01_service(self): | 57 | def test_01_service(self): |
416 | 62 | principal_units = (model.get_units(app) | 58 | principal_units = (model.get_units(app) |
417 | 63 | for app in juju.get_principle_applications(self.application_name)) | 59 | for app in juju.get_principle_applications(self.application_name)) |
418 | @@ -99,15 +95,35 @@ class TestTelegraf(BaseTelegrafTest): | |||
419 | 99 | self.fail(msg) | 95 | self.fail(msg) |
420 | 100 | 96 | ||
421 | 101 | def test_02_telegraf_logfile(self): | 97 | def test_02_telegraf_logfile(self): |
424 | 102 | """Check logfile parameter exists in /etc/telegraf/telegraf.conf.""" | 98 | """Check logfile parameter exists in ${BASE_DIR}/telegraf.conf.""" |
425 | 103 | cmd = "cat {}".format(TELEGRAF_CONF) | 99 | install_method = model.get_application_config('telegraf')['install_method']['value'] |
426 | 100 | if install_method == 'deb': | ||
427 | 101 | telegraf_conf = DEB_TELEGRAF_CONF | ||
428 | 102 | else: | ||
429 | 103 | telegraf_conf = SNAP_TELEGRAF_CONF | ||
430 | 104 | cmd = "cat {}".format(telegraf_conf) | ||
431 | 104 | response = model.run_on_unit(self.lead_unit_name, cmd) | 105 | response = model.run_on_unit(self.lead_unit_name, cmd) |
432 | 105 | if response["Code"] != "0": | 106 | if response["Code"] != "0": |
434 | 106 | self.fail("test_02_telegraf_logfile: could not read file {}".format(TELEGRAF_CONF)) | 107 | self.fail("test_02_telegraf_logfile: could not read file {}".format(telegraf_conf)) |
435 | 107 | 108 | ||
436 | 108 | for line in response["Stdout"].splitlines(): | 109 | for line in response["Stdout"].splitlines(): |
437 | 109 | if line.strip() == 'logfile = "/var/log/telegraf/telegraf.log"': | 110 | if line.strip() == 'logfile = "/var/log/telegraf/telegraf.log"': |
438 | 110 | logging.info("test_02_telegraf_logfile: logfile config parameter found") | 111 | logging.info("test_02_telegraf_logfile: logfile config parameter found") |
439 | 111 | return | 112 | return |
440 | 112 | 113 | ||
442 | 113 | self.fail("test_02_telegraf_logfile: logfile parameter not found in {}".format(TELEGRAF_CONF)) | 114 | self.fail("test_02_telegraf_logfile: logfile parameter not found in {}".format(telegraf_conf)) |
443 | 115 | |||
444 | 116 | def test_03_system_service(self): | ||
445 | 117 | """Check that the right service is running, e.g. either the deb's or the snap's.""" | ||
446 | 118 | install_method = model.get_application_config('telegraf')['install_method']['value'] | ||
447 | 119 | services = {'deb': 'telegraf', 'snap': 'snap.telegraf.telegraf'} | ||
448 | 120 | for method in services.keys(): | ||
449 | 121 | service = services[method] | ||
450 | 122 | cmd = "service {} status".format(service) | ||
451 | 123 | response = model.run_on_unit(self.lead_unit_name, cmd) | ||
452 | 124 | if install_method == method and response["Code"] != "0": | ||
453 | 125 | self.fail("test_03_system_service: service {} should be running on {} but is not. " | ||
454 | 126 | "install_method is {}.".format(service, self.lead_unit_name, install_method)) | ||
455 | 127 | elif install_method != method and response["Code"] == "0": | ||
456 | 128 | self.fail("test_03_system_service: service {} is running on {} but shouldn't. " | ||
457 | 129 | "install_method is {}.".format(service, self.lead_unit_name, install_method)) | ||
458 | diff --git a/tests/tests.yaml b/tests/tests.yaml | |||
459 | index 57e7325..2230082 100644 | |||
460 | --- a/tests/tests.yaml | |||
461 | +++ b/tests/tests.yaml | |||
462 | @@ -6,6 +6,7 @@ gate_bundles: | |||
463 | 6 | - bionic-postgres | 6 | - bionic-postgres |
464 | 7 | - bionic-monitoring | 7 | - bionic-monitoring |
465 | 8 | - focal | 8 | - focal |
466 | 9 | - focal-snap | ||
467 | 9 | smoke_bundles: | 10 | smoke_bundles: |
468 | 10 | - bionic-postgres | 11 | - bionic-postgres |
469 | 11 | - focal | 12 | - focal |
470 | diff --git a/unit_tests/test_mysql.py b/unit_tests/test_mysql.py | |||
471 | index e28973c..0904e94 100644 | |||
472 | --- a/unit_tests/test_mysql.py | |||
473 | +++ b/unit_tests/test_mysql.py | |||
474 | @@ -61,8 +61,10 @@ class TestMySQL(unittest.TestCase): | |||
475 | 61 | @patch('reactive.telegraf.render_extra_options') | 61 | @patch('reactive.telegraf.render_extra_options') |
476 | 62 | @patch('charmhelpers.core.host.write_file') | 62 | @patch('charmhelpers.core.host.write_file') |
477 | 63 | @patch('reactive.telegraf.get_templates_dir') | 63 | @patch('reactive.telegraf.get_templates_dir') |
479 | 64 | def test_render_mysql_tmpl(self, get_templates_dir, write_file, | 64 | @patch('reactive.telegraf.get_base_dir') |
480 | 65 | def test_render_mysql_tmpl(self, get_base_dir, get_templates_dir, write_file, | ||
481 | 65 | extra_options): | 66 | extra_options): |
482 | 67 | get_base_dir.return_value = '/etc/telegraf' | ||
483 | 66 | get_templates_dir.return_value = os.path.join( | 68 | get_templates_dir.return_value = os.path.join( |
484 | 67 | os.path.dirname(__file__), '..', 'templates') | 69 | os.path.dirname(__file__), '..', 'templates') |
485 | 68 | extra_options.return_value = '' | 70 | extra_options.return_value = '' |
486 | @@ -81,8 +83,10 @@ class TestMySQL(unittest.TestCase): | |||
487 | 81 | 83 | ||
488 | 82 | @patch('os.unlink') | 84 | @patch('os.unlink') |
489 | 83 | @patch('os.path.exists') | 85 | @patch('os.path.exists') |
491 | 84 | def test_render_mysql_tmpl_no_context(self, exists, unlink): | 86 | @patch('reactive.telegraf.get_base_dir') |
492 | 87 | def test_render_mysql_tmpl_no_context(self, get_base_dir, exists, unlink): | ||
493 | 85 | exists.return_value = False | 88 | exists.return_value = False |
494 | 89 | get_base_dir.return_value = '/etc/telegraf' | ||
495 | 86 | reactive.telegraf.render_mysql_tmpl([]) | 90 | reactive.telegraf.render_mysql_tmpl([]) |
496 | 87 | exists.assert_called_once_with( | 91 | exists.assert_called_once_with( |
497 | 88 | '/etc/telegraf/telegraf.d/mysql.conf' | 92 | '/etc/telegraf/telegraf.d/mysql.conf' |
498 | diff --git a/unit_tests/test_postgresql.py b/unit_tests/test_postgresql.py | |||
499 | index 1c56a0c..a0423c7 100644 | |||
500 | --- a/unit_tests/test_postgresql.py | |||
501 | +++ b/unit_tests/test_postgresql.py | |||
502 | @@ -21,8 +21,14 @@ from unittest.mock import ANY, call, MagicMock, patch, sentinel | |||
503 | 21 | 21 | ||
504 | 22 | # Mock layer modules | 22 | # Mock layer modules |
505 | 23 | import charms | 23 | import charms |
506 | 24 | apt = MagicMock() | ||
507 | 25 | layer = MagicMock() | ||
508 | 24 | promreg = MagicMock() | 26 | promreg = MagicMock() |
509 | 27 | charms.apt = apt | ||
510 | 28 | charms.layer = layer | ||
511 | 25 | charms.promreg = promreg | 29 | charms.promreg = promreg |
512 | 30 | sys.modules['charms.apt'] = apt | ||
513 | 31 | sys.modules['charms.layer'] = layer | ||
514 | 26 | sys.modules['charms.promreg'] = promreg | 32 | sys.modules['charms.promreg'] = promreg |
515 | 27 | 33 | ||
516 | 28 | import reactive | 34 | import reactive |
517 | @@ -88,9 +94,11 @@ class TestPostgreSQL(unittest.TestCase): | |||
518 | 88 | 94 | ||
519 | 89 | @patch('charmhelpers.core.host.write_file') | 95 | @patch('charmhelpers.core.host.write_file') |
520 | 90 | @patch('reactive.telegraf.get_templates_dir') | 96 | @patch('reactive.telegraf.get_templates_dir') |
522 | 91 | def test_render_postgresql_tmpl(self, get_templates_dir, write_file): | 97 | @patch('reactive.telegraf.get_base_dir') |
523 | 98 | def test_render_postgresql_tmpl(self, get_base_dir, get_templates_dir, write_file): | ||
524 | 92 | get_templates_dir.return_value = os.path.join( | 99 | get_templates_dir.return_value = os.path.join( |
525 | 93 | os.path.dirname(__file__), '..', 'templates') | 100 | os.path.dirname(__file__), '..', 'templates') |
526 | 101 | get_base_dir.return_value = '/etc/telegraf' | ||
527 | 94 | 102 | ||
528 | 95 | reactive.telegraf.render_postgresql_tmpl([ | 103 | reactive.telegraf.render_postgresql_tmpl([ |
529 | 96 | {'replica': 'master', | 104 | {'replica': 'master', |
530 | @@ -104,10 +112,12 @@ class TestPostgreSQL(unittest.TestCase): | |||
531 | 104 | 112 | ||
532 | 105 | @patch('charmhelpers.core.host.write_file') | 113 | @patch('charmhelpers.core.host.write_file') |
533 | 106 | @patch('reactive.telegraf.get_templates_dir') | 114 | @patch('reactive.telegraf.get_templates_dir') |
535 | 107 | def test_render_postgresql_tmpl_extra_options(self, get_templates_dir, | 115 | @patch('reactive.telegraf.get_base_dir') |
536 | 116 | def test_render_postgresql_tmpl_extra_options(self, get_base_dir, get_templates_dir, | ||
537 | 108 | write_file): | 117 | write_file): |
538 | 109 | get_templates_dir.return_value = os.path.join( | 118 | get_templates_dir.return_value = os.path.join( |
539 | 110 | os.path.dirname(__file__), '..', 'templates') | 119 | os.path.dirname(__file__), '..', 'templates') |
540 | 120 | get_base_dir.return_value = '/etc/telegraf' | ||
541 | 111 | 121 | ||
542 | 112 | pg_extra_opts = { | 122 | pg_extra_opts = { |
543 | 113 | "tagexclude": ["server"], | 123 | "tagexclude": ["server"], |
544 | @@ -144,8 +154,10 @@ class TestPostgreSQL(unittest.TestCase): | |||
545 | 144 | 154 | ||
546 | 145 | @patch('os.unlink') | 155 | @patch('os.unlink') |
547 | 146 | @patch('os.path.exists') | 156 | @patch('os.path.exists') |
549 | 147 | def test_render_postgresql_tmpl_no_context(self, exists, unlink): | 157 | @patch('reactive.telegraf.get_base_dir') |
550 | 158 | def test_render_postgresql_tmpl_no_context(self, get_base_dir, exists, unlink): | ||
551 | 148 | exists.return_value = False | 159 | exists.return_value = False |
552 | 160 | get_base_dir.return_value = '/etc/telegraf' | ||
553 | 149 | reactive.telegraf.render_postgresql_tmpl([]) | 161 | reactive.telegraf.render_postgresql_tmpl([]) |
554 | 150 | exists.assert_called_once_with( | 162 | exists.assert_called_once_with( |
555 | 151 | '/etc/telegraf/telegraf.d/postgresql.conf' | 163 | '/etc/telegraf/telegraf.d/postgresql.conf' |
556 | diff --git a/unit_tests/test_telegraf.py b/unit_tests/test_telegraf.py | |||
557 | index b4860ac..71af731 100644 | |||
558 | --- a/unit_tests/test_telegraf.py | |||
559 | +++ b/unit_tests/test_telegraf.py | |||
560 | @@ -46,18 +46,19 @@ from reactive import telegraf | |||
561 | 46 | UNIT_TESTS_DIR = os.path.dirname(os.path.abspath(__file__)) | 46 | UNIT_TESTS_DIR = os.path.dirname(os.path.abspath(__file__)) |
562 | 47 | UNIT_TESTS_DATA_DIR = os.path.join(UNIT_TESTS_DIR, 'data') | 47 | UNIT_TESTS_DATA_DIR = os.path.join(UNIT_TESTS_DIR, 'data') |
563 | 48 | 48 | ||
564 | 49 | |||
565 | 49 | @pytest.fixture(autouse=True) | 50 | @pytest.fixture(autouse=True) |
566 | 50 | def setup(monkeypatch, tmpdir): | 51 | def setup(monkeypatch, tmpdir): |
567 | 51 | monkeypatch.setitem(os.environ, 'JUJU_UNIT_NAME', 'telegraf/0') | 52 | monkeypatch.setitem(os.environ, 'JUJU_UNIT_NAME', 'telegraf/0') |
568 | 52 | monkeypatch.setitem(os.environ, 'JUJU_MODEL_NAME', 'telegraf-test-model') | 53 | monkeypatch.setitem(os.environ, 'JUJU_MODEL_NAME', 'telegraf-test-model') |
569 | 53 | monkeypatch.setattr(telegraf, 'get_remote_unit_name', lambda: 'remote-unit/0') | 54 | monkeypatch.setattr(telegraf, 'get_remote_unit_name', lambda: 'remote-unit/0') |
570 | 54 | monkeypatch.setattr(telegraf, 'exec_timeout_supported', lambda: True) | ||
571 | 55 | # mock this to avoid permission errors | 55 | # mock this to avoid permission errors |
572 | 56 | monkeypatch.setattr(telegraf, 'update_sysstat_config_with_sdac_xall', lambda: True) | 56 | monkeypatch.setattr(telegraf, 'update_sysstat_config_with_sdac_xall', lambda: True) |
573 | 57 | monkeypatch.setattr(telegraf.hookenv, 'status_set', lambda status, msg: None) | 57 | monkeypatch.setattr(telegraf.hookenv, 'status_set', lambda status, msg: None) |
574 | 58 | monkeypatch.setattr(telegraf.hookenv, 'log', lambda msg, **kw: None) | 58 | monkeypatch.setattr(telegraf.hookenv, 'log', lambda msg, **kw: None) |
575 | 59 | monkeypatch.setattr(telegraf.host, 'service_resume', lambda svc: None) | 59 | monkeypatch.setattr(telegraf.host, 'service_resume', lambda svc: None) |
576 | 60 | monkeypatch.setattr(telegraf.host, 'service_reload', lambda svc: None) | 60 | monkeypatch.setattr(telegraf.host, 'service_reload', lambda svc: None) |
577 | 61 | monkeypatch.setattr(telegraf.host, 'service_pause', lambda svc: None) | ||
578 | 61 | 62 | ||
579 | 62 | # patch host.write for non-root | 63 | # patch host.write for non-root |
580 | 63 | user = getpass.getuser() | 64 | user = getpass.getuser() |
581 | @@ -113,7 +114,7 @@ def temp_charm_dir(monkeypatch, tmpdir): | |||
582 | 113 | def temp_config_dir(monkeypatch, tmpdir): | 114 | def temp_config_dir(monkeypatch, tmpdir): |
583 | 114 | base_dir = tmpdir.mkdir("etc_telegraf") | 115 | base_dir = tmpdir.mkdir("etc_telegraf") |
584 | 115 | base_dir.mkdir(telegraf.CONFIG_DIR) | 116 | base_dir.mkdir(telegraf.CONFIG_DIR) |
586 | 116 | monkeypatch.setattr(telegraf, 'BASE_DIR', base_dir.strpath) | 117 | monkeypatch.setattr(telegraf, 'DEB_BASE_DIR', base_dir.strpath) |
587 | 117 | 118 | ||
588 | 118 | 119 | ||
589 | 119 | @pytest.fixture(autouse=True) | 120 | @pytest.fixture(autouse=True) |
590 | @@ -127,7 +128,7 @@ def config(monkeypatch, temp_charm_dir): | |||
591 | 127 | 128 | ||
592 | 128 | # utilities | 129 | # utilities |
593 | 129 | def base_dir(): | 130 | def base_dir(): |
595 | 130 | return py.path.local(telegraf.BASE_DIR) | 131 | return py.path.local(telegraf.DEB_BASE_DIR) |
596 | 131 | 132 | ||
597 | 132 | 133 | ||
598 | 133 | def configs_dir(): | 134 | def configs_dir(): |
599 | @@ -872,24 +873,6 @@ def test_exec_input_leader_no_more(mocker, monkeypatch): | |||
600 | 872 | assert not configs_dir().join('exec.conf').exists() | 873 | assert not configs_dir().join('exec.conf').exists() |
601 | 873 | 874 | ||
602 | 874 | 875 | ||
603 | 875 | def test_exec_input_no_timeout_support(mocker, monkeypatch): | ||
604 | 876 | interface = mocker.Mock(spec=RelationBase) | ||
605 | 877 | interface.commands = mocker.Mock() | ||
606 | 878 | commands = [{'commands': ['/srv/bin/test.sh', '/bin/true'], | ||
607 | 879 | 'data_format': 'json', | ||
608 | 880 | 'timeout': '5s', | ||
609 | 881 | 'run_on_this_unit': True}] | ||
610 | 882 | interface.commands.return_value = commands | ||
611 | 883 | expected = """ | ||
612 | 884 | [[inputs.exec]] | ||
613 | 885 | commands = ['/srv/bin/test.sh', '/bin/true'] | ||
614 | 886 | data_format = "json" | ||
615 | 887 | """ | ||
616 | 888 | monkeypatch.setattr(telegraf, 'exec_timeout_supported', lambda: False) | ||
617 | 889 | telegraf.exec_input(interface) | ||
618 | 890 | assert configs_dir().join('exec.conf').read().strip() == expected.strip() | ||
619 | 891 | |||
620 | 892 | |||
621 | 893 | def test_exec_input_departed(mocker, monkeypatch): | 876 | def test_exec_input_departed(mocker, monkeypatch): |
622 | 894 | configs_dir().join('exec.conf').write('empty') | 877 | configs_dir().join('exec.conf').write('empty') |
623 | 895 | relations = [1] | 878 | relations = [1] |
624 | @@ -969,7 +952,7 @@ def test_basic_config(mocker, monkeypatch, config): | |||
625 | 969 | bus.dispatch() | 952 | bus.dispatch() |
626 | 970 | assert 'telegraf.configured' in bus.get_states().keys() | 953 | assert 'telegraf.configured' in bus.get_states().keys() |
627 | 971 | assert base_dir().join('telegraf.conf').exists() | 954 | assert base_dir().join('telegraf.conf').exists() |
629 | 972 | service_restart.assert_called_once_with('telegraf') | 955 | service_restart.assert_called_once_with(telegraf.DEB_SERVICE) |
630 | 973 | check_prometheus_port.assert_called_once_with('prometheus_output', 9103) | 956 | check_prometheus_port.assert_called_once_with('prometheus_output', 9103) |
631 | 974 | 957 | ||
632 | 975 | 958 | ||
633 | @@ -984,7 +967,7 @@ def test_config_no_prometheus(mocker, monkeypatch, config): | |||
634 | 984 | bus.dispatch() | 967 | bus.dispatch() |
635 | 985 | assert 'telegraf.configured' in bus.get_states().keys() | 968 | assert 'telegraf.configured' in bus.get_states().keys() |
636 | 986 | assert base_dir().join('telegraf.conf').exists() | 969 | assert base_dir().join('telegraf.conf').exists() |
638 | 987 | service_restart.assert_called_once_with('telegraf') | 970 | service_restart.assert_called_once_with(telegraf.DEB_SERVICE) |
639 | 988 | assert not check_prometheus_port.called | 971 | assert not check_prometheus_port.called |
640 | 989 | 972 | ||
641 | 990 | 973 | ||
642 | @@ -996,7 +979,7 @@ def test_config_changed_apt(mocker, monkeypatch, config): | |||
643 | 996 | set_flag('apt.installed.telegraf') | 979 | set_flag('apt.installed.telegraf') |
644 | 997 | set_flag('config.changed') | 980 | set_flag('config.changed') |
645 | 998 | bus.dispatch() | 981 | bus.dispatch() |
647 | 999 | service_restart.assert_called_once_with('telegraf') | 982 | service_restart.assert_called_once_with(telegraf.DEB_SERVICE) |
648 | 1000 | 983 | ||
649 | 1001 | 984 | ||
650 | 1002 | def test_config_changed_extra_options(mocker, monkeypatch, config): | 985 | def test_config_changed_extra_options(mocker, monkeypatch, config): |
651 | @@ -1004,7 +987,7 @@ def test_config_changed_extra_options(mocker, monkeypatch, config): | |||
652 | 1004 | monkeypatch.setattr(telegraf.host, 'service_running', | 987 | monkeypatch.setattr(telegraf.host, 'service_running', |
653 | 1005 | lambda n: True) | 988 | lambda n: True) |
654 | 1006 | mocker.patch('reactive.telegraf.check_prometheus_port') | 989 | mocker.patch('reactive.telegraf.check_prometheus_port') |
656 | 1007 | set_flag('telegraf.installed') | 990 | set_flag('apt.telegraf.installed') |
657 | 1008 | set_flag('plugins.haproxy.configured') | 991 | set_flag('plugins.haproxy.configured') |
658 | 1009 | config.save() | 992 | config.save() |
659 | 1010 | config.load_previous() | 993 | config.load_previous() |
660 | @@ -1013,7 +996,7 @@ def test_config_changed_extra_options(mocker, monkeypatch, config): | |||
661 | 1013 | set_flag('config.changed') | 996 | set_flag('config.changed') |
662 | 1014 | bus.dispatch() | 997 | bus.dispatch() |
663 | 1015 | assert 'plugins.haproxy.configured' not in bus.get_states().keys() | 998 | assert 'plugins.haproxy.configured' not in bus.get_states().keys() |
665 | 1016 | service_restart.assert_called_once_with('telegraf') | 999 | service_restart.assert_called_once_with(telegraf.DEB_SERVICE) |
666 | 1017 | 1000 | ||
667 | 1018 | 1001 | ||
668 | 1019 | def test_config_changed_extra_plugins(mocker, monkeypatch, config): | 1002 | def test_config_changed_extra_plugins(mocker, monkeypatch, config): |
669 | @@ -1021,7 +1004,7 @@ def test_config_changed_extra_plugins(mocker, monkeypatch, config): | |||
670 | 1021 | monkeypatch.setattr(telegraf.host, 'service_running', | 1004 | monkeypatch.setattr(telegraf.host, 'service_running', |
671 | 1022 | lambda n: True) | 1005 | lambda n: True) |
672 | 1023 | mocker.patch('reactive.telegraf.check_prometheus_port') | 1006 | mocker.patch('reactive.telegraf.check_prometheus_port') |
674 | 1024 | set_flag('telegraf.installed') | 1007 | set_flag('apt.telegraf.installed') |
675 | 1025 | assert not configs_dir().join('extra_plugins.conf').exists() | 1008 | assert not configs_dir().join('extra_plugins.conf').exists() |
676 | 1026 | config.save() | 1009 | config.save() |
677 | 1027 | config.load_previous() | 1010 | config.load_previous() |
678 | @@ -1037,7 +1020,7 @@ def test_config_changed_extra_plugins(mocker, monkeypatch, config): | |||
679 | 1037 | bus.dispatch() | 1020 | bus.dispatch() |
680 | 1038 | assert configs_dir().join('extra_plugins.conf').exists() | 1021 | assert configs_dir().join('extra_plugins.conf').exists() |
681 | 1039 | assert configs_dir().join('extra_plugins.conf').read() == config['extra_plugins'] | 1022 | assert configs_dir().join('extra_plugins.conf').read() == config['extra_plugins'] |
683 | 1040 | service_restart.assert_called_with('telegraf') | 1023 | service_restart.assert_called_with(telegraf.DEB_SERVICE) |
684 | 1041 | 1024 | ||
685 | 1042 | 1025 | ||
686 | 1043 | def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config): | 1026 | def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config): |
687 | @@ -1056,7 +1039,7 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config) | |||
688 | 1056 | return_value={"ingress_addresses": ["1.2.3.4"]}) | 1039 | return_value={"ingress_addresses": ["1.2.3.4"]}) |
689 | 1057 | config['prometheus_output_port'] = '' | 1040 | config['prometheus_output_port'] = '' |
690 | 1058 | bus.discover() | 1041 | bus.discover() |
692 | 1059 | set_flag('telegraf.installed') | 1042 | set_flag('snap.telegraf.installed') |
693 | 1060 | set_flag('telegraf.configured') | 1043 | set_flag('telegraf.configured') |
694 | 1061 | interface = mocker.Mock(spec=RelationBase) | 1044 | interface = mocker.Mock(spec=RelationBase) |
695 | 1062 | interface.configure = mocker.Mock() | 1045 | interface.configure = mocker.Mock() |
696 | @@ -1065,7 +1048,7 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config) | |||
697 | 1065 | # dispatch, file should be gone and telegraf restarted. | 1048 | # dispatch, file should be gone and telegraf restarted. |
698 | 1066 | bus.dispatch() | 1049 | bus.dispatch() |
699 | 1067 | assert not configs_dir().join('prometheus-client.conf').exists() | 1050 | assert not configs_dir().join('prometheus-client.conf').exists() |
701 | 1068 | service_restart.assert_called_with('telegraf') | 1051 | service_restart.assert_called_with(telegraf.DEB_SERVICE) |
702 | 1069 | 1052 | ||
703 | 1070 | 1053 | ||
704 | 1071 | def test_upgrade_charm(mocker): | 1054 | def test_upgrade_charm(mocker): |
705 | @@ -1074,10 +1057,12 @@ def test_upgrade_charm(mocker): | |||
706 | 1074 | list_plugins.return_value = ['foo', 'bar'] | 1057 | list_plugins.return_value = ['foo', 'bar'] |
707 | 1075 | telegraf.upgrade_charm() | 1058 | telegraf.upgrade_charm() |
708 | 1076 | clear_flag.assert_any_call('telegraf.configured') | 1059 | clear_flag.assert_any_call('telegraf.configured') |
709 | 1060 | clear_flag.assert_any_call('telegraf.apt.configured') | ||
710 | 1061 | clear_flag.assert_any_call('telegraf.snap.configured') | ||
711 | 1077 | clear_flag.assert_any_call('plugins.foo.configured') | 1062 | clear_flag.assert_any_call('plugins.foo.configured') |
712 | 1078 | clear_flag.assert_any_call('plugins.bar.configured') | 1063 | clear_flag.assert_any_call('plugins.bar.configured') |
713 | 1079 | clear_flag.assert_any_call('extra_plugins.configured') | 1064 | clear_flag.assert_any_call('extra_plugins.configured') |
715 | 1080 | assert clear_flag.call_count == 4 | 1065 | assert clear_flag.call_count == 6 |
716 | 1081 | 1066 | ||
717 | 1082 | 1067 | ||
718 | 1083 | def test_get_socket_listener_port(monkeypatch, config): | 1068 | def test_get_socket_listener_port(monkeypatch, config): |
Successfully passes `make test`.