Merge ~sajoupa/charm-telegraf:snap-support into charm-telegraf:master

Proposed by Laurent Sesquès
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)
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)

To post a comment you must log in.
Revision history for this message
Laurent Sesquès (sajoupa) wrote :

Successfully passes `make test`.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Stuart Bishop (stub) wrote :

Looks great. Some minor comments inline, but nothing requiring changes.

Revision history for this message
Paul Goins (vultaire) wrote :

LGTM

review: Approve
Revision history for this message
Xav Paice (xavpaice) wrote :

Needs a rebase against master to resolve merge conflicts.

Tests all pass, including the focal/snap test.

review: Needs Fixing
Revision history for this message
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`

Revision history for this message
Laurent Sesquès (sajoupa) wrote :

replied to the inline comments.

Revision history for this message
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/test_telegraf.py::setup:

monkeypatch.setattr(telegraf.host, 'service_pause', lambda svc: None)

review: Needs Fixing
Revision history for this message
Joe Guo (guoqiao) wrote :

Other than above, both unit and functional tests passed.

Revision history for this message
Stuart Bishop (stub) wrote :

Yup. Some trivial formatting issues, inline.

review: Approve
Revision history for this message
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/test_telegraf.py::setup:
>
> monkeypatch.setattr(telegraf.host, 'service_pause', lambda svc: None)

Hi Joe,

I've always run it successfully as a normal user.
But your suggestion makes sense, I added it.

Revision history for this message
Laurent Sesquès (sajoupa) wrote :

Pushed updates addressing guoqiao's and stub's comments.
Still passes `make test` successfully.

Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision ac0af7b8cb76e77adc913ec67c2f28272f77980d

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/README.md b/README.md
2index 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
7 # Configuration
8
9+## Installation method: deb or snap
10+
11+The charm can deploy using either a deb (default) or a snap installation.
12+ juju config telegraf install_method=deb # to use the debian package
13+ juju config telegraf install_method=snap # to use the snap
14+Only the needed package (deb or snap) will be installed.
15+
16+To change the snap channel:
17+ juju config telegraf snap_channel=beta
18+
19 ## Prometheus Output
20
21 By default, Prometheus output is presented available for scraping.
22diff --git a/config.yaml b/config.yaml
23index 215baff..454e461 100644
24--- a/config.yaml
25+++ b/config.yaml
26@@ -97,6 +97,16 @@ options:
27 # description and type comes from the apt layer.
28 default: |
29 - null
30+ install_method:
31+ type: string
32+ default: "deb"
33+ description: >
34+ Installation method. Valid values are "deb" or "snap".
35+ snap_channel:
36+ default: "stable"
37+ type: string
38+ description: |
39+ Snap Store channel from which to install the telegraf snap.
40 extra_options:
41 default: ""
42 type: string
43@@ -125,7 +135,7 @@ options:
44 type: string
45 description: |
46 Extra plugins, manually configured. This is expected to be a string
47- and will be saved "as is" in /etc/telegraf/telegraf.d/extra_plugins.conf
48+ and will be saved "as is" in telegraf.d/extra_plugins.conf
49 disabled_plugins:
50 type: string
51 default: ""
52diff --git a/files/telegraf_exec_metrics.py b/files/telegraf_exec_metrics.py
53index 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 name_override = "{name}"
58 data_format = "json"
59 tag_keys = ["namespace", "id"]
60- interval = 600
61+ interval = "600s"
62 ''')
63
64 def parse(self, content):
65diff --git a/layer.yaml b/layer.yaml
66index 346e4d8..057a4c2 100644
67--- a/layer.yaml
68+++ b/layer.yaml
69@@ -20,6 +20,7 @@ includes:
70 - layer:leadership
71 - layer:ntpmon
72 - layer:promreg-client
73+ - layer:snap
74 - interface:elasticsearch
75 - interface:grafana-dashboard
76 - interface:http
77@@ -41,7 +42,4 @@ options:
78 - sysstat
79 - python3-yaml
80 - python3-netifaces
81- apt:
82- packages:
83- - telegraf
84 repo: https://git.launchpad.net/telegraf-charm
85diff --git a/reactive/telegraf.py b/reactive/telegraf.py
86index 28ee023..d62efd2 100644
87--- a/reactive/telegraf.py
88+++ b/reactive/telegraf.py
89@@ -28,6 +28,8 @@ import re
90 import netifaces
91 import subprocess
92
93+from charms import apt
94+from charms.layer import snap
95 from charms.reactive import (
96 endpoint_from_flag,
97 helpers,
98@@ -45,11 +47,11 @@ from charmhelpers.core import hookenv, host, unitdata
99 from charmhelpers.core.templating import render
100 from charmhelpers.core.host import is_container
101 from charmhelpers.contrib.charmsupport import nrpe
102-from charmhelpers.fetch import apt_cache
103 import charms.promreg
104 from jinja2 import Template, Environment, FileSystemLoader, exceptions
105
106-BASE_DIR = '/etc/telegraf'
107+DEB_BASE_DIR = '/etc/telegraf'
108+SNAP_BASE_DIR = '/var/snap/telegraf/current'
109
110 CONFIG_FILE = 'telegraf.conf'
111
112@@ -59,16 +61,51 @@ GRAFANA_DASHBOARD_TELEGRAF_FILE_NAME = 'Telegraf.json.j2'
113
114 GRAFANA_DASHBOARD_NAME = 'telegraf'
115
116+SNAP_SERVICE = 'snap.telegraf.telegraf'
117+DEB_SERVICE = 'telegraf'
118+
119 # Utilities #
120
121
122-def exec_timeout_supported():
123- cache = apt_cache()
124- pkg = cache['telegraf']
125- timeout_support = True
126- if '0.12' in pkg.current_ver.ver_str:
127- timeout_support = False
128- return timeout_support
129+class InvalidInstallMethod(Exception):
130+ pass
131+
132+
133+def get_install_method():
134+ config = hookenv.config()
135+ if config['install_method'] in ['deb', 'snap']:
136+ return config['install_method']
137+ else:
138+ hookenv.log(
139+ "Invalid install_method for telegraf: {}".format(config['install_method']),
140+ level=hookenv.ERROR)
141+ raise InvalidInstallMethod()
142+
143+
144+def get_base_dir():
145+ config = hookenv.config()
146+ if config['install_method'] == 'deb':
147+ return DEB_BASE_DIR
148+ elif config['install_method'] == 'snap':
149+ return SNAP_BASE_DIR
150+ else:
151+ hookenv.log(
152+ "Invalid install_method for telegraf: {}".format(config['install_method']),
153+ level=hookenv.ERROR)
154+ raise InvalidInstallMethod()
155+
156+
157+def get_service():
158+ config = hookenv.config()
159+ if config['install_method'] == 'deb':
160+ return DEB_SERVICE
161+ elif config['install_method'] == 'snap':
162+ return SNAP_SERVICE
163+ else:
164+ hookenv.log(
165+ "Invalid install_method for telegraf: {}".format(config['install_method']),
166+ level=hookenv.ERROR)
167+ raise InvalidInstallMethod()
168
169
170 def get_templates_dir():
171@@ -76,11 +113,11 @@ def get_templates_dir():
172
173
174 def get_main_config_path():
175- return os.path.join(BASE_DIR, CONFIG_FILE)
176+ return os.path.join(get_base_dir(), CONFIG_FILE)
177
178
179 def get_configs_dir():
180- return os.path.join(BASE_DIR, CONFIG_DIR)
181+ return os.path.join(get_base_dir(), CONFIG_DIR)
182
183
184 def get_files_dir():
185@@ -314,13 +351,15 @@ def update_sysstat_config_with_sdac_xall(path='/etc/sysstat/sysstat'):
186 hookenv.log("sysstat config file not found: {}".format(path), level=hookenv.WARNING)
187
188
189-# States
190-
191-@when('apt.installed.telegraf')
192-@when_not('telegraf.configured')
193-def configure_telegraf(): # noqa: C901 (compexity 15)
194+def configure_telegraf(): # noqa: C901
195 update_sysstat_config_with_sdac_xall()
196- config_path = get_main_config_path()
197+ config = hookenv.config()
198+ context = config.copy()
199+ try:
200+ config_path = get_main_config_path()
201+ except InvalidInstallMethod:
202+ hookenv.status_set('blocked', 'Wrong install_method provided: {!r}'.format(config['install_method']))
203+ return
204 if get_remote_unit_name() is None:
205 hookenv.status_set('waiting', 'Waiting for juju-info relation')
206 # if UNIT_NAME in hostname config and relation not yet available,
207@@ -329,8 +368,6 @@ def configure_telegraf(): # noqa: C901 (compexity 15)
208 os.unlink(config_path)
209 return
210
211- config = hookenv.config()
212- context = config.copy()
213 inputs = config.get('inputs_config', '')
214 outputs = config.get('outputs_config', '')
215 # just for the migration out of base64
216@@ -370,8 +407,10 @@ def configure_telegraf(): # noqa: C901 (compexity 15)
217 context["hostname"] = get_hostname_label()
218
219 logfile_path = os.path.normpath(context["logfile"])
220- if context["logfile"] and not logfile_path.startswith("/var/log/"):
221- # only allow logging in /var/log or in syslog
222+ if (context["logfile"] and not logfile_path.startswith("/var/log/") and not
223+ (config["install_method"] == 'snap' and
224+ logfile_path.startswith("/var/snap/telegraf/common/"))):
225+ # only allow logging in /var/log, syslog, or /var/snap/telegraf/common
226 hookenv.log("logfile value reset to stderr."
227 " Original value: {}".format(context["logfile"]),
228 hookenv.DEBUG)
229@@ -401,10 +440,64 @@ def configure_telegraf(): # noqa: C901 (compexity 15)
230 hookenv.log("Updating main config file")
231 render(source='telegraf.conf.tmpl', templates_dir=get_templates_dir(),
232 target=config_path, context=context)
233- host.service_resume('telegraf') # unmask and enable service, start it if not yet
234- host.service_reload('telegraf') # reload or restart service, to apply config changes
235+ for service in [DEB_SERVICE, SNAP_SERVICE]:
236+ if service == get_service():
237+ host.service_resume(service)
238+ host.service_reload(service)
239+ else:
240+ try:
241+ host.service_pause(service)
242+ except ValueError:
243+ # On machines not supporting snaps, it won't find the service
244+ # If we're using the snap, and the deb failed to install, it's fine too
245+ pass
246 set_flag('telegraf.configured')
247 set_flag('telegraf.needs_reload')
248+ if config["install_method"] == 'deb':
249+ set_flag('telegraf.apt.configured')
250+ else:
251+ set_flag('telegraf.snap.configured')
252+
253+
254+# States
255+
256+@when_not('telegraf.installed')
257+def install_telegraf():
258+ try:
259+ install_method = get_install_method()
260+ except InvalidInstallMethod:
261+ hookenv.status_set('blocked', "Wrong install_method provided. Expected either 'deb' or 'snap'.")
262+ return
263+ if install_method == 'deb':
264+ try:
265+ snap.remove('telegraf')
266+ except Exception:
267+ # the snap may already be absent, or snaps may not even be supported in this environment
268+ pass
269+ apt.queue_install(['telegraf'])
270+ elif install_method == 'snap':
271+ apt.purge('telegraf')
272+ config = hookenv.config()
273+ snap_channel = config.get('snap_channel')
274+ snap.install('telegraf', channel=snap_channel, classic=True)
275+ if install_method:
276+ set_flag('telegraf.installed')
277+
278+
279+@when('telegraf.installed')
280+@when('apt.installed.telegraf')
281+@when_not('telegraf.configured')
282+@when_not('telegraf.apt.configured')
283+def configure_telegraf_deb():
284+ configure_telegraf()
285+
286+
287+@when('telegraf.installed')
288+@when('snap.installed.telegraf')
289+@when_not('telegraf.configured')
290+@when_not('telegraf.snap.configured')
291+def configure_telegraf_snap():
292+ configure_telegraf()
293
294
295 @hook('upgrade-charm')
296@@ -413,6 +506,8 @@ def upgrade_charm():
297 clear_flag('plugins.{}.configured'.format(plugin))
298 clear_flag('extra_plugins.configured')
299 clear_flag('telegraf.configured')
300+ clear_flag('telegraf.apt.configured')
301+ clear_flag('telegraf.snap.configured')
302
303
304 @when('config.changed')
305@@ -424,7 +519,11 @@ def handle_config_changes():
306 # if something else changed, let's reconfigure telegraf itself just in case
307 if config.changed('extra_plugins'):
308 clear_flag('extra_plugins.configured')
309+ if config.changed('install_method') or config.changed('snap_channel') or config.changed('install_sources'):
310+ clear_flag('telegraf.installed')
311 clear_flag('telegraf.configured')
312+ clear_flag('telegraf.apt.configured')
313+ clear_flag('telegraf.snap.configured')
314 clear_flag('telegraf.nagios-setup.complete')
315
316
317@@ -788,11 +887,8 @@ def exec_input(exec_rel):
318 if not commands:
319 hookenv.log("No Commands defined in the exec relation, doing nothing.")
320 return
321- timeout_support = exec_timeout_supported()
322 pre_proc_cmds = []
323 for command in commands:
324- if not timeout_support:
325- command.pop('timeout')
326 run_on_this_unit = command.pop('run_on_this_unit')
327 if run_on_this_unit:
328 pre_proc_cmds.append(command)
329@@ -1089,12 +1185,13 @@ def start_or_restart():
330 states = sorted([k for k in get_states().keys()
331 if k.startswith('plugins') or k.startswith('extra_plugins')])
332
333+ service = get_service()
334 config_files_changed = helpers.any_file_changed(list_config_files())
335 active_plugins_changed = helpers.data_changed('active_plugins', states or '')
336- if not host.service_running('telegraf') \
337+ if not host.service_running(service) \
338 or config_files_changed or active_plugins_changed:
339 hookenv.log("Restarting telegraf")
340- host.service_restart('telegraf')
341+ host.service_restart(service)
342 else:
343 hookenv.log("Not restarting: active_plugins_changed={} | "
344 "config_files_changed={}".format(active_plugins_changed,
345@@ -1102,10 +1199,10 @@ def start_or_restart():
346
347 # Give telegraf time to restart.
348 timeout = time.time() + 15
349- while not host.service_running('telegraf') and time.time() < timeout:
350+ while not host.service_running(service) and time.time() < timeout:
351 time.sleep(0.1)
352
353- if host.service_running('telegraf'):
354+ if host.service_running(service):
355 hookenv.status_set('active',
356 'Monitoring {}'.format(get_remote_unit_name()))
357 clear_flag('telegraf.needs_reload')
358@@ -1130,10 +1227,12 @@ def update_status():
359 hash_type='sha256')
360 if changed:
361 clear_flag('telegraf.configured')
362+ clear_flag('telegraf.apt.configured')
363+ clear_flag('telegraf.snap.configured')
364
365
366 @when('nrpe-external-master.available')
367-@when('apt.installed.telegraf')
368+@when('telegraf.installed')
369 @when('telegraf.configured')
370 @when_not('telegraf.nagios-setup.complete')
371 def configure_nagios(nagios):
372diff --git a/tests/bundles/focal-snap.yaml b/tests/bundles/focal-snap.yaml
373new file mode 100644
374index 0000000..cfd7e2a
375--- /dev/null
376+++ b/tests/bundles/focal-snap.yaml
377@@ -0,0 +1,14 @@
378+series: focal
379+
380+applications:
381+ telegraf:
382+ num_units: 0
383+ options:
384+ install_method: 'snap'
385+ ubuntu:
386+ charm: cs:ubuntu
387+ num_units: 1
388+
389+relations:
390+ - - ubuntu:juju-info
391+ - telegraf:juju-info
392diff --git a/tests/test_telegraf.py b/tests/test_telegraf.py
393index 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 DEFAULT_RETRIES = 12
398 DEFAULT_RETRIES_TIMEOUT = 5
399 DEFAULT_TELEGRAF_EXPORTER_PORT = "9103"
400-TELEGRAF_CONF = "/etc/telegraf/telegraf.conf"
401+DEB_TELEGRAF_CONF = "/etc/telegraf/telegraf.conf"
402+SNAP_TELEGRAF_CONF = "/var/snap/telegraf/current/telegraf.conf"
403
404
405 class BaseTelegrafTest(unittest.TestCase):
406@@ -53,11 +54,6 @@ class TestTelegraf(BaseTelegrafTest):
407 # findall returns a list, [] when no match
408 self.assertTrue(re.findall(re_pattern, text, flags=re.M))
409
410- def check_re_pattern(self, re_pattern, text):
411- logging.info('checking metrics %s', re_pattern)
412- # findall returns a list, [] when no match
413- self.assertTrue(re.findall(re_pattern, text, flags=re.M))
414-
415 def test_01_service(self):
416 principal_units = (model.get_units(app)
417 for app in juju.get_principle_applications(self.application_name))
418@@ -99,15 +95,35 @@ class TestTelegraf(BaseTelegrafTest):
419 self.fail(msg)
420
421 def test_02_telegraf_logfile(self):
422- """Check logfile parameter exists in /etc/telegraf/telegraf.conf."""
423- cmd = "cat {}".format(TELEGRAF_CONF)
424+ """Check logfile parameter exists in ${BASE_DIR}/telegraf.conf."""
425+ install_method = model.get_application_config('telegraf')['install_method']['value']
426+ if install_method == 'deb':
427+ telegraf_conf = DEB_TELEGRAF_CONF
428+ else:
429+ telegraf_conf = SNAP_TELEGRAF_CONF
430+ cmd = "cat {}".format(telegraf_conf)
431 response = model.run_on_unit(self.lead_unit_name, cmd)
432 if response["Code"] != "0":
433- self.fail("test_02_telegraf_logfile: could not read file {}".format(TELEGRAF_CONF))
434+ self.fail("test_02_telegraf_logfile: could not read file {}".format(telegraf_conf))
435
436 for line in response["Stdout"].splitlines():
437 if line.strip() == 'logfile = "/var/log/telegraf/telegraf.log"':
438 logging.info("test_02_telegraf_logfile: logfile config parameter found")
439 return
440
441- self.fail("test_02_telegraf_logfile: logfile parameter not found in {}".format(TELEGRAF_CONF))
442+ self.fail("test_02_telegraf_logfile: logfile parameter not found in {}".format(telegraf_conf))
443+
444+ def test_03_system_service(self):
445+ """Check that the right service is running, e.g. either the deb's or the snap's."""
446+ install_method = model.get_application_config('telegraf')['install_method']['value']
447+ services = {'deb': 'telegraf', 'snap': 'snap.telegraf.telegraf'}
448+ for method in services.keys():
449+ service = services[method]
450+ cmd = "service {} status".format(service)
451+ response = model.run_on_unit(self.lead_unit_name, cmd)
452+ if install_method == method and response["Code"] != "0":
453+ self.fail("test_03_system_service: service {} should be running on {} but is not. "
454+ "install_method is {}.".format(service, self.lead_unit_name, install_method))
455+ elif install_method != method and response["Code"] == "0":
456+ self.fail("test_03_system_service: service {} is running on {} but shouldn't. "
457+ "install_method is {}.".format(service, self.lead_unit_name, install_method))
458diff --git a/tests/tests.yaml b/tests/tests.yaml
459index 57e7325..2230082 100644
460--- a/tests/tests.yaml
461+++ b/tests/tests.yaml
462@@ -6,6 +6,7 @@ gate_bundles:
463 - bionic-postgres
464 - bionic-monitoring
465 - focal
466+ - focal-snap
467 smoke_bundles:
468 - bionic-postgres
469 - focal
470diff --git a/unit_tests/test_mysql.py b/unit_tests/test_mysql.py
471index 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 @patch('reactive.telegraf.render_extra_options')
476 @patch('charmhelpers.core.host.write_file')
477 @patch('reactive.telegraf.get_templates_dir')
478- def test_render_mysql_tmpl(self, get_templates_dir, write_file,
479+ @patch('reactive.telegraf.get_base_dir')
480+ def test_render_mysql_tmpl(self, get_base_dir, get_templates_dir, write_file,
481 extra_options):
482+ get_base_dir.return_value = '/etc/telegraf'
483 get_templates_dir.return_value = os.path.join(
484 os.path.dirname(__file__), '..', 'templates')
485 extra_options.return_value = ''
486@@ -81,8 +83,10 @@ class TestMySQL(unittest.TestCase):
487
488 @patch('os.unlink')
489 @patch('os.path.exists')
490- def test_render_mysql_tmpl_no_context(self, exists, unlink):
491+ @patch('reactive.telegraf.get_base_dir')
492+ def test_render_mysql_tmpl_no_context(self, get_base_dir, exists, unlink):
493 exists.return_value = False
494+ get_base_dir.return_value = '/etc/telegraf'
495 reactive.telegraf.render_mysql_tmpl([])
496 exists.assert_called_once_with(
497 '/etc/telegraf/telegraf.d/mysql.conf'
498diff --git a/unit_tests/test_postgresql.py b/unit_tests/test_postgresql.py
499index 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
504 # Mock layer modules
505 import charms
506+apt = MagicMock()
507+layer = MagicMock()
508 promreg = MagicMock()
509+charms.apt = apt
510+charms.layer = layer
511 charms.promreg = promreg
512+sys.modules['charms.apt'] = apt
513+sys.modules['charms.layer'] = layer
514 sys.modules['charms.promreg'] = promreg
515
516 import reactive
517@@ -88,9 +94,11 @@ class TestPostgreSQL(unittest.TestCase):
518
519 @patch('charmhelpers.core.host.write_file')
520 @patch('reactive.telegraf.get_templates_dir')
521- def test_render_postgresql_tmpl(self, get_templates_dir, write_file):
522+ @patch('reactive.telegraf.get_base_dir')
523+ def test_render_postgresql_tmpl(self, get_base_dir, get_templates_dir, write_file):
524 get_templates_dir.return_value = os.path.join(
525 os.path.dirname(__file__), '..', 'templates')
526+ get_base_dir.return_value = '/etc/telegraf'
527
528 reactive.telegraf.render_postgresql_tmpl([
529 {'replica': 'master',
530@@ -104,10 +112,12 @@ class TestPostgreSQL(unittest.TestCase):
531
532 @patch('charmhelpers.core.host.write_file')
533 @patch('reactive.telegraf.get_templates_dir')
534- def test_render_postgresql_tmpl_extra_options(self, get_templates_dir,
535+ @patch('reactive.telegraf.get_base_dir')
536+ def test_render_postgresql_tmpl_extra_options(self, get_base_dir, get_templates_dir,
537 write_file):
538 get_templates_dir.return_value = os.path.join(
539 os.path.dirname(__file__), '..', 'templates')
540+ get_base_dir.return_value = '/etc/telegraf'
541
542 pg_extra_opts = {
543 "tagexclude": ["server"],
544@@ -144,8 +154,10 @@ class TestPostgreSQL(unittest.TestCase):
545
546 @patch('os.unlink')
547 @patch('os.path.exists')
548- def test_render_postgresql_tmpl_no_context(self, exists, unlink):
549+ @patch('reactive.telegraf.get_base_dir')
550+ def test_render_postgresql_tmpl_no_context(self, get_base_dir, exists, unlink):
551 exists.return_value = False
552+ get_base_dir.return_value = '/etc/telegraf'
553 reactive.telegraf.render_postgresql_tmpl([])
554 exists.assert_called_once_with(
555 '/etc/telegraf/telegraf.d/postgresql.conf'
556diff --git a/unit_tests/test_telegraf.py b/unit_tests/test_telegraf.py
557index 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 UNIT_TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
562 UNIT_TESTS_DATA_DIR = os.path.join(UNIT_TESTS_DIR, 'data')
563
564+
565 @pytest.fixture(autouse=True)
566 def setup(monkeypatch, tmpdir):
567 monkeypatch.setitem(os.environ, 'JUJU_UNIT_NAME', 'telegraf/0')
568 monkeypatch.setitem(os.environ, 'JUJU_MODEL_NAME', 'telegraf-test-model')
569 monkeypatch.setattr(telegraf, 'get_remote_unit_name', lambda: 'remote-unit/0')
570- monkeypatch.setattr(telegraf, 'exec_timeout_supported', lambda: True)
571 # mock this to avoid permission errors
572 monkeypatch.setattr(telegraf, 'update_sysstat_config_with_sdac_xall', lambda: True)
573 monkeypatch.setattr(telegraf.hookenv, 'status_set', lambda status, msg: None)
574 monkeypatch.setattr(telegraf.hookenv, 'log', lambda msg, **kw: None)
575 monkeypatch.setattr(telegraf.host, 'service_resume', lambda svc: None)
576 monkeypatch.setattr(telegraf.host, 'service_reload', lambda svc: None)
577+ monkeypatch.setattr(telegraf.host, 'service_pause', lambda svc: None)
578
579 # patch host.write for non-root
580 user = getpass.getuser()
581@@ -113,7 +114,7 @@ def temp_charm_dir(monkeypatch, tmpdir):
582 def temp_config_dir(monkeypatch, tmpdir):
583 base_dir = tmpdir.mkdir("etc_telegraf")
584 base_dir.mkdir(telegraf.CONFIG_DIR)
585- monkeypatch.setattr(telegraf, 'BASE_DIR', base_dir.strpath)
586+ monkeypatch.setattr(telegraf, 'DEB_BASE_DIR', base_dir.strpath)
587
588
589 @pytest.fixture(autouse=True)
590@@ -127,7 +128,7 @@ def config(monkeypatch, temp_charm_dir):
591
592 # utilities
593 def base_dir():
594- return py.path.local(telegraf.BASE_DIR)
595+ return py.path.local(telegraf.DEB_BASE_DIR)
596
597
598 def configs_dir():
599@@ -872,24 +873,6 @@ def test_exec_input_leader_no_more(mocker, monkeypatch):
600 assert not configs_dir().join('exec.conf').exists()
601
602
603-def test_exec_input_no_timeout_support(mocker, monkeypatch):
604- interface = mocker.Mock(spec=RelationBase)
605- interface.commands = mocker.Mock()
606- commands = [{'commands': ['/srv/bin/test.sh', '/bin/true'],
607- 'data_format': 'json',
608- 'timeout': '5s',
609- 'run_on_this_unit': True}]
610- interface.commands.return_value = commands
611- expected = """
612-[[inputs.exec]]
613- commands = ['/srv/bin/test.sh', '/bin/true']
614- data_format = "json"
615-"""
616- monkeypatch.setattr(telegraf, 'exec_timeout_supported', lambda: False)
617- telegraf.exec_input(interface)
618- assert configs_dir().join('exec.conf').read().strip() == expected.strip()
619-
620-
621 def test_exec_input_departed(mocker, monkeypatch):
622 configs_dir().join('exec.conf').write('empty')
623 relations = [1]
624@@ -969,7 +952,7 @@ def test_basic_config(mocker, monkeypatch, config):
625 bus.dispatch()
626 assert 'telegraf.configured' in bus.get_states().keys()
627 assert base_dir().join('telegraf.conf').exists()
628- service_restart.assert_called_once_with('telegraf')
629+ service_restart.assert_called_once_with(telegraf.DEB_SERVICE)
630 check_prometheus_port.assert_called_once_with('prometheus_output', 9103)
631
632
633@@ -984,7 +967,7 @@ def test_config_no_prometheus(mocker, monkeypatch, config):
634 bus.dispatch()
635 assert 'telegraf.configured' in bus.get_states().keys()
636 assert base_dir().join('telegraf.conf').exists()
637- service_restart.assert_called_once_with('telegraf')
638+ service_restart.assert_called_once_with(telegraf.DEB_SERVICE)
639 assert not check_prometheus_port.called
640
641
642@@ -996,7 +979,7 @@ def test_config_changed_apt(mocker, monkeypatch, config):
643 set_flag('apt.installed.telegraf')
644 set_flag('config.changed')
645 bus.dispatch()
646- service_restart.assert_called_once_with('telegraf')
647+ service_restart.assert_called_once_with(telegraf.DEB_SERVICE)
648
649
650 def test_config_changed_extra_options(mocker, monkeypatch, config):
651@@ -1004,7 +987,7 @@ def test_config_changed_extra_options(mocker, monkeypatch, config):
652 monkeypatch.setattr(telegraf.host, 'service_running',
653 lambda n: True)
654 mocker.patch('reactive.telegraf.check_prometheus_port')
655- set_flag('telegraf.installed')
656+ set_flag('apt.telegraf.installed')
657 set_flag('plugins.haproxy.configured')
658 config.save()
659 config.load_previous()
660@@ -1013,7 +996,7 @@ def test_config_changed_extra_options(mocker, monkeypatch, config):
661 set_flag('config.changed')
662 bus.dispatch()
663 assert 'plugins.haproxy.configured' not in bus.get_states().keys()
664- service_restart.assert_called_once_with('telegraf')
665+ service_restart.assert_called_once_with(telegraf.DEB_SERVICE)
666
667
668 def test_config_changed_extra_plugins(mocker, monkeypatch, config):
669@@ -1021,7 +1004,7 @@ def test_config_changed_extra_plugins(mocker, monkeypatch, config):
670 monkeypatch.setattr(telegraf.host, 'service_running',
671 lambda n: True)
672 mocker.patch('reactive.telegraf.check_prometheus_port')
673- set_flag('telegraf.installed')
674+ set_flag('apt.telegraf.installed')
675 assert not configs_dir().join('extra_plugins.conf').exists()
676 config.save()
677 config.load_previous()
678@@ -1037,7 +1020,7 @@ def test_config_changed_extra_plugins(mocker, monkeypatch, config):
679 bus.dispatch()
680 assert configs_dir().join('extra_plugins.conf').exists()
681 assert configs_dir().join('extra_plugins.conf').read() == config['extra_plugins']
682- service_restart.assert_called_with('telegraf')
683+ service_restart.assert_called_with(telegraf.DEB_SERVICE)
684
685
686 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 return_value={"ingress_addresses": ["1.2.3.4"]})
689 config['prometheus_output_port'] = ''
690 bus.discover()
691- set_flag('telegraf.installed')
692+ set_flag('snap.telegraf.installed')
693 set_flag('telegraf.configured')
694 interface = mocker.Mock(spec=RelationBase)
695 interface.configure = mocker.Mock()
696@@ -1065,7 +1048,7 @@ def test_restart_on_output_plugin_relation_departed(mocker, monkeypatch, config)
697 # dispatch, file should be gone and telegraf restarted.
698 bus.dispatch()
699 assert not configs_dir().join('prometheus-client.conf').exists()
700- service_restart.assert_called_with('telegraf')
701+ service_restart.assert_called_with(telegraf.DEB_SERVICE)
702
703
704 def test_upgrade_charm(mocker):
705@@ -1074,10 +1057,12 @@ def test_upgrade_charm(mocker):
706 list_plugins.return_value = ['foo', 'bar']
707 telegraf.upgrade_charm()
708 clear_flag.assert_any_call('telegraf.configured')
709+ clear_flag.assert_any_call('telegraf.apt.configured')
710+ clear_flag.assert_any_call('telegraf.snap.configured')
711 clear_flag.assert_any_call('plugins.foo.configured')
712 clear_flag.assert_any_call('plugins.bar.configured')
713 clear_flag.assert_any_call('extra_plugins.configured')
714- assert clear_flag.call_count == 4
715+ assert clear_flag.call_count == 6
716
717
718 def test_get_socket_listener_port(monkeypatch, config):

Subscribers

People subscribed via source and target branches

to all changes: