Merge ~ballot/charm-telegraf/+git/telegraf-charm:snap_daemon_user into ~sajoupa/charm-telegraf:snap_daemon_user

Proposed by Benjamin Allot
Status: Superseded
Proposed branch: ~ballot/charm-telegraf/+git/telegraf-charm:snap_daemon_user
Merge into: ~sajoupa/charm-telegraf:snap_daemon_user
Diff against target: 169 lines (+40/-28)
4 files modified
src/reactive/telegraf.py (+11/-1)
src/tests/unit/requirements.txt (+1/-0)
src/tests/unit/test_telegraf.py (+26/-25)
src/tox.ini (+2/-2)
Reviewer Review Type Date Requested Status
Laurent Sesquès Pending
Review via email: mp+397818@code.launchpad.net
To post a comment you must log in.
abc2234... by Benjamin Allot

Add xdist to allow faster test execution

6ef444a... by Laurent Sesquès

when using the snap, set config files ownership to snap_daemon https://snapcraft.io/docs/services-and-daemons

Reviewed-on: https://code.launchpad.net/~sajoupa/charm-telegraf/+git/telegraf-charm/+merge/397492
Reviewed-by: Benjamin Allot <email address hidden>

Unmerged commits

abc2234... by Benjamin Allot

Add xdist to allow faster test execution

2790fd2... by Benjamin Allot

Add parametrization for the install_method

Until all the tests are fixed, only the test_write_telegraf_file tests
is parametrized with both install_method.
Also, remove reference to unittests and convert to pure pytest.

dcc064a... by Benjamin Allot

Add a way to customize args upon tox calls for unit tests

6ef444a... by Laurent Sesquès

when using the snap, set config files ownership to snap_daemon https://snapcraft.io/docs/services-and-daemons

Reviewed-on: https://code.launchpad.net/~sajoupa/charm-telegraf/+git/telegraf-charm/+merge/397492
Reviewed-by: Benjamin Allot <email address hidden>

dabf651... by Haw Loeung

Add showing charm source version/commit in juju status output

Reviewed-on: https://code.launchpad.net/~hloeung/charm-telegraf/+git/charm-telegraf/+merge/397477
Reviewed-by: Joe Guo <email address hidden>

007ce0a... by Haw Loeung

Add showing charm source version/commit in juju status output

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/reactive/telegraf.py b/src/reactive/telegraf.py
2index e8cfa01..d8fd268 100644
3--- a/src/reactive/telegraf.py
4+++ b/src/reactive/telegraf.py
5@@ -1439,7 +1439,17 @@ def start_or_restart():
6 time.sleep(0.1)
7
8 if host.service_running(service):
9- hookenv.status_set("active", "Monitoring {}".format(get_remote_unit_name()))
10+ revision = ''
11+ if os.path.exists('version'):
12+ with open('version') as f:
13+ line = f.readline().strip()
14+ # We only want the first 8 characters, that's enough to tell
15+ # which version of the charm we're using.
16+ if len(line) > 8:
17+ revision = ' (source version/commit {}…)'.format(line[:8])
18+ else:
19+ revision = ' (source version/commit {})'.format(line)
20+ hookenv.status_set("active", "Monitoring {}{}".format(get_remote_unit_name(), revision))
21 clear_flag("telegraf.needs_reload")
22 else:
23 hookenv.status_set("blocked", "Telegraf failed to start. Check config.")
24diff --git a/src/tests/unit/requirements.txt b/src/tests/unit/requirements.txt
25index a03b163..2f1db93 100644
26--- a/src/tests/unit/requirements.txt
27+++ b/src/tests/unit/requirements.txt
28@@ -3,6 +3,7 @@ flake8
29 pytest
30 pytest-cov
31 pytest-mock
32+pytest-xdist
33 netifaces
34 insights-core
35 charms.reactive>=1.3.0
36diff --git a/src/tests/unit/test_telegraf.py b/src/tests/unit/test_telegraf.py
37index 62a5595..39959bf 100644
38--- a/src/tests/unit/test_telegraf.py
39+++ b/src/tests/unit/test_telegraf.py
40@@ -142,10 +142,18 @@ def temp_config_dir(monkeypatch, tmpdir):
41 monkeypatch.setattr(telegraf, "SUDOERS_DIR", sudoers_dir.strpath)
42
43
44+# For now, too many tests fails if we set also "snap" method
45+# TODO: fix all tests so every single one is called once with each install_method
46+@pytest.fixture(params=["deb"])
47+def install_method(request):
48+ return request.param
49+
50+
51 @pytest.fixture(autouse=True)
52-def config(monkeypatch, temp_charm_dir):
53+def config(monkeypatch, temp_charm_dir, install_method):
54 raw_config = yaml.full_load(open("config.yaml", "r"))
55 data = dict((k, v["default"]) for k, v in raw_config["options"].items())
56+ data["install_method"] = install_method
57 config = Config(data)
58 monkeypatch.setattr(telegraf.hookenv, "config", lambda: config)
59
60@@ -190,25 +198,22 @@ def check_sysstat_config(original, expected):
61 assert actual == expected
62
63
64-def test_write_telegraf_file(mocker, config):
65- config["install_method"] = "deb"
66- write_file = mocker.patch("charmhelpers.core.host.write_file")
67- telegraf.write_telegraf_file("the-path", "the-content áéíóú")
68- write_file.assert_called_once_with(
69- "the-path",
70- "the-content áéíóú".encode("utf-8"),
71- owner="root",
72- group="telegraf",
73- perms=0o640,
74- )
75- config["install_method"] = "snap"
76+@pytest.mark.parametrize("install_method", ["deb", "snap"])
77+def test_write_telegraf_file(mocker, install_method):
78+
79+ if install_method == "deb":
80+ user = telegraf.DEB_USER
81+ group = telegraf.DEB_GROUP
82+ elif install_method == "snap":
83+ user = telegraf.SNAP_USER
84+ group = telegraf.SNAP_GROUP
85 write_file = mocker.patch("charmhelpers.core.host.write_file")
86 telegraf.write_telegraf_file("the-path", "the-content áéíóú")
87 write_file.assert_called_once_with(
88 "the-path",
89 "the-content áéíóú".encode("utf-8"),
90- owner="snap_daemon",
91- group="snap_daemon",
92+ owner=user,
93+ group=group,
94 perms=0o640,
95 )
96
97@@ -1323,13 +1328,13 @@ def test_inputs_internal_memstats(monkeypatch, config):
98 assert expected in config_file.read()
99
100
101-class TestGetHostnameLabel(unittest.TestCase):
102+class TestGetHostnameLabel():
103 @patch("reactive.telegraf.get_remote_unit_name")
104 @patch("charmhelpers.core.hookenv.config")
105 def test_legacy(self, config, get_remote_unit_name):
106 config.return_value = dict(hostname="UNIT_NAME")
107 get_remote_unit_name.return_value = "remote/10"
108- self.assertEqual(telegraf.get_hostname_label(), "remote-10")
109+ assert telegraf.get_hostname_label() == "remote-10"
110
111 @patch("reactive.telegraf.get_remote_unit_name")
112 @patch("charmhelpers.core.hookenv.config")
113@@ -1338,9 +1343,7 @@ class TestGetHostnameLabel(unittest.TestCase):
114 env = dict(JUJU_ENV_NAME="env_name", JUJU_ENV_UUID="env_uuid")
115 config.return_value = dict(hostname="{uuid}:{model}:{unit}")
116 with patch.dict(os.environ, env):
117- self.assertEqual(
118- telegraf.get_hostname_label(), "env_uuid:env_name:remote-10"
119- )
120+ assert telegraf.get_hostname_label() == "env_uuid:env_name:remote-10"
121
122 @patch("reactive.telegraf.get_remote_unit_name")
123 @patch("charmhelpers.core.hookenv.config")
124@@ -1349,12 +1352,10 @@ class TestGetHostnameLabel(unittest.TestCase):
125 env = dict(JUJU_MODEL_NAME="model_name", JUJU_MODEL_UUID="model_uuid")
126 config.return_value = dict(hostname="{uuid}:{model}:{unit}")
127 with patch.dict(os.environ, env):
128- self.assertEqual(
129- telegraf.get_hostname_label(), "model_uuid:model_name:remote-10"
130- )
131+ assert telegraf.get_hostname_label() == "model_uuid:model_name:remote-10"
132
133
134-class TestTelegrafNagios(unittest.TestCase):
135+class TestTelegrafNagios():
136 @mock.patch("charmhelpers.contrib.charmsupport.nrpe.NRPE")
137 @mock.patch("charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname")
138 def test_update_nrpe_config(self, mock_get_nagios_hostname, mock_nrpe, *args):
139@@ -1373,7 +1374,7 @@ class TestTelegrafNagios(unittest.TestCase):
140 mock_nrpe.assert_has_calls(calls)
141
142
143-class TestGrafanaDashboard(unittest.TestCase):
144+class TestGrafanaDashboard():
145 @patch("reactive.telegraf.render_custom")
146 @patch("reactive.telegraf.endpoint_from_flag")
147 @patch("reactive.telegraf.hookenv")
148diff --git a/src/tox.ini b/src/tox.ini
149index 4977737..ce51462 100644
150--- a/src/tox.ini
151+++ b/src/tox.ini
152@@ -56,7 +56,7 @@ deps =
153
154 [testenv:unit]
155 commands =
156- pytest -v --ignore {toxinidir}/tests/functional \
157+ pytest {posargs:-v -n auto --ignore {toxinidir}/tests/functional \
158 --cov=lib \
159 --cov=reactive \
160 --cov=actions \
161@@ -64,7 +64,7 @@ commands =
162 --cov=src \
163 --cov-report=term \
164 --cov-report=annotate:report/annotated \
165- --cov-report=html:report/html
166+ --cov-report=html:report/html}
167 deps = -r{toxinidir}/tests/unit/requirements.txt
168
169 [testenv:func]

Subscribers

People subscribed via source and target branches