Merge ~peppepetra/charm-juju-controller:blacken-20.08 into charm-juju-controller:master
- Git
- lp:~peppepetra/charm-juju-controller
- blacken-20.08
- Merge into master
Proposed by
Giuseppe Petralia
Status: | Merged |
---|---|
Approved by: | Alvaro Uria |
Approved revision: | 9943788a0d0adf8c399f5190ca4a29642dd61fa2 |
Merged at revision: | bdbd4d486c92fae360b4ce2b9e7c96ff78715323 |
Proposed branch: | ~peppepetra/charm-juju-controller:blacken-20.08 |
Merge into: | charm-juju-controller:master |
Prerequisite: | ~peppepetra/charm-juju-controller:makefile-20.08 |
Diff against target: |
655 lines (+116/-159) 8 files modified
src/files/plugins/check_jujucontroller.py (+15/-25) src/lib/lib_juju_controller.py (+27/-28) src/reactive/juju_controller.py (+17/-17) src/tests/functional/tests/tests_juju_controller.py (+6/-17) src/tests/unit/conftest.py (+19/-32) src/tests/unit/test_check_jujucontroller.py (+18/-25) src/tests/unit/test_lib_juju_controller.py (+12/-12) src/tox.ini (+2/-3) |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Alvaro Uria (community) | Approve | ||
Review via email: mp+388744@code.launchpad.net |
Commit message
Blackened repository to 88 lines
Description of the change
To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Unable to determine commit message from repository - please click "Set commit message" and enter the commit message manually.
Revision history for this message
Alvaro Uria (aluria) wrote : | # |
+1 I tested this together with the other 2 MPs (blacken and linting) and all tests passed.
review:
Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote : | # |
Change successfully merged at revision bdbd4d486c92fae
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | diff --git a/src/files/plugins/check_jujucontroller.py b/src/files/plugins/check_jujucontroller.py |
2 | index 8f919e7..0dc0946 100755 |
3 | --- a/src/files/plugins/check_jujucontroller.py |
4 | +++ b/src/files/plugins/check_jujucontroller.py |
5 | @@ -21,20 +21,20 @@ import yaml |
6 | # trivial, as that file's path depends on the application name. This could be |
7 | # solved by making this whole script a jinja template rendered at charm |
8 | # installation, but this in turn would make unit testing more complex. |
9 | -CACERTPATH = '/run/juju-controller-ca.crt' |
10 | +CACERTPATH = "/run/juju-controller-ca.crt" |
11 | # This file will only contain non-secret values extracted from the controller |
12 | # agent.conf. |
13 | # At the moment these are only the juju version and the API port. |
14 | -PUBAGENTCONFPATH = '/run/juju-controller-public-agent.conf' |
15 | +PUBAGENTCONFPATH = "/run/juju-controller-public-agent.conf" |
16 | |
17 | |
18 | # see: |
19 | # - https://github.com/juju/juju/pull/11096 |
20 | # - https://bugs.launchpad.net/juju/+bug/1797848 |
21 | -MINVER = '2.7.2' |
22 | +MINVER = "2.7.2" |
23 | # Known response bodies that may be returned by the health endpoint |
24 | -OKSTATUSES = ['running'] |
25 | -WARNSTATUSES = ['starting', 'stopping'] |
26 | +OKSTATUSES = ["running"] |
27 | +WARNSTATUSES = ["starting", "stopping"] |
28 | |
29 | |
30 | class States(Enum): |
31 | @@ -51,7 +51,7 @@ def _supported(version): |
32 | |
33 | |
34 | def _get_agent_conf(path=PUBAGENTCONFPATH): |
35 | - with open(path, 'r') as agentconffile: |
36 | + with open(path, "r") as agentconffile: |
37 | return yaml.safe_load(agentconffile) |
38 | |
39 | |
40 | @@ -60,36 +60,26 @@ def check_endpoint(agentconf=PUBAGENTCONFPATH): |
41 | try: |
42 | agentconf = _get_agent_conf(agentconf) |
43 | except FileNotFoundError: |
44 | - msg = ( |
45 | - '{} not found. Cannot query the endpoint' |
46 | - ''.format(PUBAGENTCONFPATH) |
47 | - ) |
48 | + msg = "{} not found. Cannot query the endpoint".format(PUBAGENTCONFPATH) |
49 | return States.UNKNOWN, msg |
50 | |
51 | - endpoint = ( |
52 | - 'https://localhost:{}/health' |
53 | - ''.format(agentconf.get('apiport')) |
54 | - ) |
55 | + endpoint = "https://localhost:{}/health".format(agentconf.get("apiport")) |
56 | |
57 | - controller_version = agentconf.get('upgradedToVersion') |
58 | + controller_version = agentconf.get("upgradedToVersion") |
59 | if not _supported(controller_version): |
60 | - msg = ( |
61 | - 'juju version {} does not offer a monitoring endpoint' |
62 | - ''.format(controller_version) |
63 | + msg = "juju version {} does not offer a monitoring endpoint".format( |
64 | + controller_version |
65 | ) |
66 | return States.UNKNOWN, msg |
67 | |
68 | try: |
69 | r = requests.get(endpoint, verify=CACERTPATH, timeout=15) |
70 | except (RequestException, OSError) as error: |
71 | - msg = 'cannot connect to the health endpoint. Error: {}'.format(error) |
72 | + msg = "cannot connect to the health endpoint. Error: {}".format(error) |
73 | return States.CRITICAL, msg |
74 | |
75 | if not r.ok: |
76 | - msg = ( |
77 | - 'the health endpoint returned status code {}' |
78 | - ''.format(r.status_code) |
79 | - ) |
80 | + msg = "the health endpoint returned status code {}".format(r.status_code) |
81 | return States.UNKNOWN, msg |
82 | |
83 | controller_status = r.text.strip() |
84 | @@ -107,10 +97,10 @@ def check_endpoint(agentconf=PUBAGENTCONFPATH): |
85 | def main(): |
86 | """Print the status of the controller and return the proper RC.""" |
87 | state, msg = check_endpoint() |
88 | - formatted_output = '{}: {}'.format(state.name, msg) |
89 | + formatted_output = "{}: {}".format(state.name, msg) |
90 | print(formatted_output) |
91 | sys.exit(state.value) |
92 | |
93 | |
94 | -if __name__ == '__main__': |
95 | +if __name__ == "__main__": |
96 | main() |
97 | diff --git a/src/lib/lib_juju_controller.py b/src/lib/lib_juju_controller.py |
98 | index df9ecea..2b45272 100644 |
99 | --- a/src/lib/lib_juju_controller.py |
100 | +++ b/src/lib/lib_juju_controller.py |
101 | @@ -7,12 +7,12 @@ import shutil |
102 | from charmhelpers.core import hookenv |
103 | import yaml |
104 | |
105 | -AGENTDIRGLOB = '/var/lib/juju/agents/machine-*' |
106 | -CACERTPATH = '/run/juju-controller-ca.crt' |
107 | -PUBAGENTCONFPATH = '/run/juju-controller-public-agent.conf' |
108 | +AGENTDIRGLOB = "/var/lib/juju/agents/machine-*" |
109 | +CACERTPATH = "/run/juju-controller-ca.crt" |
110 | +PUBAGENTCONFPATH = "/run/juju-controller-public-agent.conf" |
111 | |
112 | |
113 | -class JujuControllerHelper(): |
114 | +class JujuControllerHelper: |
115 | """A small helper lib for the juju controller charm.""" |
116 | |
117 | def __init__(self): |
118 | @@ -20,65 +20,64 @@ class JujuControllerHelper(): |
119 | self.charm_config = hookenv.config() |
120 | |
121 | def _machine_id(self): |
122 | - machine = os.path.basename( |
123 | - glob.glob(AGENTDIRGLOB)[0] |
124 | - ) |
125 | - return machine.split('-').pop() |
126 | + machine = os.path.basename(glob.glob(AGENTDIRGLOB)[0]) |
127 | + return machine.split("-").pop() |
128 | |
129 | def dump_ca(self, path=CACERTPATH): |
130 | - """Extract the CA cert from agent.conf and place it in a nagios-readable file.""" |
131 | - with open(path, 'w') as cafile: |
132 | + """Extract CA cert from agent.conf and place it in a nagios-readable file.""" |
133 | + with open(path, "w") as cafile: |
134 | cafile.write(self.cacert) |
135 | try: |
136 | - shutil.chown(path, user='nagios') |
137 | + shutil.chown(path, user="nagios") |
138 | except LookupError as error: |
139 | - msg = 'cannot chown {} to user nagios. {}'.format(path, error) |
140 | + msg = "cannot chown {} to user nagios. {}".format(path, error) |
141 | hookenv.log(msg, level=hookenv.WARNING) |
142 | |
143 | def dump_public_agent_conf(self, path=PUBAGENTCONFPATH): |
144 | - """Save version and API port from agent.conf and place them in a nagios-readable file.""" |
145 | - with open(path, 'w') as pubagentconffile: |
146 | + """Provide version and api port to nagios. |
147 | + |
148 | + Get version and API port from agent.conf |
149 | + and place them in a nagios-readable file. |
150 | + """ |
151 | + with open(path, "w") as pubagentconffile: |
152 | public_data = { |
153 | - 'upgradedToVersion': self.version, |
154 | - 'apiport': self.apiport, |
155 | - } |
156 | + "upgradedToVersion": self.version, |
157 | + "apiport": self.apiport, |
158 | + } |
159 | yaml.dump(public_data, pubagentconffile) |
160 | try: |
161 | - shutil.chown(path, user='nagios') |
162 | + shutil.chown(path, user="nagios") |
163 | except LookupError as error: |
164 | - msg = 'cannot chown {} to user nagios. {}'.format(path, error) |
165 | + msg = "cannot chown {} to user nagios. {}".format(path, error) |
166 | hookenv.log(msg, level=hookenv.WARNING) |
167 | |
168 | @property |
169 | def services(self): |
170 | """Return the list of juju services on this machine.""" |
171 | - return ['juju-db', 'jujud-machine-{}'.format(self._machine_id())] |
172 | + return ["juju-db", "jujud-machine-{}".format(self._machine_id())] |
173 | |
174 | @property |
175 | def agent_conf_path(self): |
176 | """Return the path of the controller agent.conf file.""" |
177 | - return os.path.join( |
178 | - glob.glob(AGENTDIRGLOB)[0], |
179 | - 'agent.conf' |
180 | - ) |
181 | + return os.path.join(glob.glob(AGENTDIRGLOB)[0], "agent.conf") |
182 | |
183 | @property |
184 | def agent_conf(self): |
185 | """Return the content of the controller agent.conf file as a map.""" |
186 | - with open(self.agent_conf_path, 'r') as agentconffile: |
187 | + with open(self.agent_conf_path, "r") as agentconffile: |
188 | return yaml.safe_load(agentconffile) |
189 | |
190 | @property |
191 | def version(self): |
192 | """Return the version of the juju controller.""" |
193 | - return self.agent_conf.get('upgradedToVersion') |
194 | + return self.agent_conf.get("upgradedToVersion") |
195 | |
196 | @property |
197 | def apiport(self): |
198 | """Return the tcp port the API is listening at.""" |
199 | - return self.agent_conf.get('apiport') |
200 | + return self.agent_conf.get("apiport") |
201 | |
202 | @property |
203 | def cacert(self): |
204 | """Return the CA certificate needed to verify the juju controller cert.""" |
205 | - return self.agent_conf.get('cacert') |
206 | + return self.agent_conf.get("cacert") |
207 | diff --git a/src/reactive/juju_controller.py b/src/reactive/juju_controller.py |
208 | index 96005df..5905b53 100644 |
209 | --- a/src/reactive/juju_controller.py |
210 | +++ b/src/reactive/juju_controller.py |
211 | @@ -19,10 +19,10 @@ from lib_juju_controller import JujuControllerHelper |
212 | |
213 | helper = JujuControllerHelper() |
214 | |
215 | -PLUGINS_DIR = '/usr/local/lib/nagios/plugins/' |
216 | +PLUGINS_DIR = "/usr/local/lib/nagios/plugins/" |
217 | |
218 | |
219 | -@when_not('juju-controller.installed') |
220 | +@when_not("juju-controller.installed") |
221 | def install_juju_controller(): |
222 | """Install the juju controller charm. This is a no-op for now.""" |
223 | # Do your setup here. |
224 | @@ -36,12 +36,12 @@ def install_juju_controller(): |
225 | # * https://jujucharms.com/docs/devel/developer-getting-started |
226 | # * https://github.com/juju-solutions/layer-basic#overview |
227 | # |
228 | - set_flag('juju-controller.installed') |
229 | - hookenv.status_set('active', 'Ready') |
230 | + set_flag("juju-controller.installed") |
231 | + hookenv.status_set("active", "Ready") |
232 | |
233 | |
234 | -@when('nrpe-external-master.available') |
235 | -@when('nrpe-plugins.installed') |
236 | +@when("nrpe-external-master.available") |
237 | +@when("nrpe-plugins.installed") |
238 | def update_nrpe_config(): |
239 | """Configure nrpe checks.""" |
240 | hostname = nrpe.get_nagios_hostname() |
241 | @@ -53,35 +53,35 @@ def update_nrpe_config(): |
242 | |
243 | # Health endpoint |
244 | nrpe_setup.add_check( |
245 | - shortname='juju_health', |
246 | - description='Juju health check for the built-in health endpoint', |
247 | - check_cmd=os.path.join(PLUGINS_DIR, 'check_jujucontroller.py') |
248 | + shortname="juju_health", |
249 | + description="Juju health check for the built-in health endpoint", |
250 | + check_cmd=os.path.join(PLUGINS_DIR, "check_jujucontroller.py"), |
251 | ) |
252 | # cache agent.conf data (required by the endpoint check) |
253 | cache_agent_conf() |
254 | |
255 | nrpe_setup.write() |
256 | - hookenv.status_set('active', 'NRPE configured') |
257 | + hookenv.status_set("active", "NRPE configured") |
258 | |
259 | |
260 | -@when_not('nrpe-plugins.installed') |
261 | +@when_not("nrpe-plugins.installed") |
262 | def update_plugins(): |
263 | """Copy nrpe plugins to the system path.""" |
264 | - src_dir = os.path.join(hookenv.charm_dir(), 'files', 'plugins/') |
265 | + src_dir = os.path.join(hookenv.charm_dir(), "files", "plugins/") |
266 | nrpe.copy_nrpe_checks(src_dir) |
267 | - set_flag('nrpe-plugins.installed') |
268 | + set_flag("nrpe-plugins.installed") |
269 | |
270 | |
271 | @when_file_changed(helper.agent_conf_path) |
272 | -@when('nrpe-external-master.available') |
273 | +@when("nrpe-external-master.available") |
274 | def cache_agent_conf(): |
275 | """Cache parts of agent.conf in a file the nagios user can read.""" |
276 | helper.dump_ca() |
277 | helper.dump_public_agent_conf() |
278 | |
279 | |
280 | -@hook('upgrade-charm') |
281 | +@hook("upgrade-charm") |
282 | def upgrade_charm(): |
283 | """Unset all custom flags to prepare for charm upgrade.""" |
284 | - clear_flag('nrpe-plugins.installed') |
285 | - clear_flag('juju-controller.installed') |
286 | + clear_flag("nrpe-plugins.installed") |
287 | + clear_flag("juju-controller.installed") |
288 | diff --git a/src/tests/functional/tests/tests_juju_controller.py b/src/tests/functional/tests/tests_juju_controller.py |
289 | index bdefa2b..3fd5229 100644 |
290 | --- a/src/tests/functional/tests/tests_juju_controller.py |
291 | +++ b/src/tests/functional/tests/tests_juju_controller.py |
292 | @@ -27,9 +27,7 @@ async def juju_status(unit, inner_model, filter=""): |
293 | return data |
294 | |
295 | |
296 | -async def check_for_inner_status( |
297 | - unit, inner_model, inner_app, target="active" |
298 | -): |
299 | +async def check_for_inner_status(unit, inner_model, inner_app, target="active"): |
300 | """Check if status of an app is equal to target.""" |
301 | status = await juju_status(unit, inner_model) |
302 | apps = status.get("applications", {}) |
303 | @@ -74,31 +72,22 @@ class JujuControllerTestBase(unittest.TestCase): |
304 | out = model.run_on_unit( |
305 | cls.unit, "sudo -u ubuntu -H /snap/bin/juju enable-ha -n 3" |
306 | ) |
307 | - assert out["Code"] == "0", "Error enabling ha: {}".format( |
308 | - out["Stderr"] |
309 | - ) |
310 | + assert out["Code"] == "0", "Error enabling ha: {}".format(out["Stderr"]) |
311 | |
312 | @classmethod |
313 | def run_remote_juju(cls, cmd, opts): |
314 | """Run juju command on controller model.""" |
315 | - cmd = "sudo -u ubuntu -H /snap/bin/juju {} -m controller {}".format( |
316 | - cmd, opts |
317 | - ) |
318 | + cmd = "sudo -u ubuntu -H /snap/bin/juju {} -m controller {}".format(cmd, opts) |
319 | out = model.run_on_unit(cls.unit, cmd) |
320 | - assert out["Code"] == "0", "Error running {}: {}".format( |
321 | - cmd, out["Stderr"] |
322 | - ) |
323 | + assert out["Code"] == "0", "Error running {}: {}".format(cmd, out["Stderr"]) |
324 | return out["Stdout"] |
325 | |
326 | @classmethod |
327 | def deploy_juju_controller(cls): |
328 | """Deploy juju-controller charm.""" |
329 | - model.scp_to_unit( |
330 | - cls.unit, str(BUILT_CHARM), "/home/ubuntu", scp_opts="-r" |
331 | - ) |
332 | + model.scp_to_unit(cls.unit, str(BUILT_CHARM), "/home/ubuntu", scp_opts="-r") |
333 | cls.run_remote_juju( |
334 | - "deploy", |
335 | - "-n 3 --to 0,1,2 /home/ubuntu/juju-controller juju-controller", |
336 | + "deploy", "-n 3 --to 0,1,2 /home/ubuntu/juju-controller juju-controller", |
337 | ) |
338 | block_until_inner_app(cls.unit, "controller", "juju-controller") |
339 | |
340 | diff --git a/src/tests/unit/conftest.py b/src/tests/unit/conftest.py |
341 | index 34aebc2..b67d094 100644 |
342 | --- a/src/tests/unit/conftest.py |
343 | +++ b/src/tests/unit/conftest.py |
344 | @@ -15,19 +15,20 @@ import yaml |
345 | def mock_layers(monkeypatch): |
346 | """Mock layers.""" |
347 | import sys |
348 | - sys.modules['charms.layer'] = mock.Mock() |
349 | - sys.modules['reactive'] = mock.Mock() |
350 | + |
351 | + sys.modules["charms.layer"] = mock.Mock() |
352 | + sys.modules["reactive"] = mock.Mock() |
353 | # Mock any functions in layers that need to be mocked here |
354 | |
355 | def options(layer): |
356 | # mock options for layers here |
357 | - if layer == 'example-layer': |
358 | - options = {'port': 9999} |
359 | + if layer == "example-layer": |
360 | + options = {"port": 9999} |
361 | return options |
362 | else: |
363 | return None |
364 | |
365 | - monkeypatch.setattr('lib_juju_controller.layer.options', options) |
366 | + monkeypatch.setattr("lib_juju_controller.layer.options", options) |
367 | |
368 | |
369 | @pytest.fixture |
370 | @@ -37,29 +38,25 @@ def mock_hookenv_config(monkeypatch): |
371 | |
372 | def mock_config(): |
373 | cfg = {} |
374 | - charm_config_path = os.path.join( |
375 | - os.path.dirname(__file__), |
376 | - '../../config.yaml' |
377 | - ) |
378 | + charm_config_path = os.path.join(os.path.dirname(__file__), "../../config.yaml") |
379 | yml = yaml.safe_load(open(charm_config_path)) |
380 | |
381 | # Load all defaults |
382 | - for key, value in yml['options'].items(): |
383 | - cfg[key] = value['default'] |
384 | + for key, value in yml["options"].items(): |
385 | + cfg[key] = value["default"] |
386 | |
387 | # Manually add cfg from other layers |
388 | # cfg['my-other-layer'] = 'mock' |
389 | return cfg |
390 | |
391 | - monkeypatch.setattr('lib_juju_controller.hookenv.config', mock_config) |
392 | + monkeypatch.setattr("lib_juju_controller.hookenv.config", mock_config) |
393 | |
394 | |
395 | @pytest.fixture |
396 | def mock_remote_unit(monkeypatch): |
397 | """Mock hookenv.remote_unit.""" |
398 | monkeypatch.setattr( |
399 | - 'lib_juju_controller.hookenv.remote_unit', |
400 | - lambda: 'unit-mock/0' |
401 | + "lib_juju_controller.hookenv.remote_unit", lambda: "unit-mock/0" |
402 | ) |
403 | |
404 | |
405 | @@ -67,8 +64,7 @@ def mock_remote_unit(monkeypatch): |
406 | def mock_charm_dir(monkeypatch): |
407 | """Provide a mocked hookenv.charm_dir.""" |
408 | monkeypatch.setattr( |
409 | - 'lib_juju_controller.hookenv.charm_dir', |
410 | - lambda: '/mock/charm/dir' |
411 | + "lib_juju_controller.hookenv.charm_dir", lambda: "/mock/charm/dir" |
412 | ) |
413 | |
414 | |
415 | @@ -83,47 +79,38 @@ def mock_unit_db(monkeypatch): |
416 | @pytest.fixture |
417 | def agent_conf_sample_path(): |
418 | """Provide a sample agent.conf (path only).""" |
419 | - return os.path.join( |
420 | - os.path.dirname(__file__), |
421 | - '../samples/agent.conf' |
422 | - ) |
423 | + return os.path.join(os.path.dirname(__file__), "../samples/agent.conf") |
424 | |
425 | |
426 | @pytest.fixture |
427 | def agent_conf_sample(agent_conf_sample_path): |
428 | """Provide a map representing the content of the sample agent.conf.""" |
429 | - with open(agent_conf_sample_path, 'r') as agentconffile: |
430 | + with open(agent_conf_sample_path, "r") as agentconffile: |
431 | return yaml.safe_load(agentconffile) |
432 | |
433 | |
434 | @pytest.fixture |
435 | def jujucontroller( |
436 | - mock_hookenv_config, |
437 | - monkeypatch, |
438 | - agent_conf_sample, |
439 | + mock_hookenv_config, monkeypatch, agent_conf_sample, |
440 | ): |
441 | """Mock the JujuControllerHelper. |
442 | |
443 | Properties are initialized based on the contents of the agent.conf sample |
444 | """ |
445 | from lib_juju_controller import JujuControllerHelper |
446 | + |
447 | helper = JujuControllerHelper() |
448 | |
449 | monkeypatch.setattr( |
450 | - 'lib_juju_controller.glob.glob', |
451 | - lambda _: ['/var/lib/juju/agents/machine-99'] |
452 | + "lib_juju_controller.glob.glob", lambda _: ["/var/lib/juju/agents/machine-99"] |
453 | ) |
454 | |
455 | monkeypatch.setattr( |
456 | - 'lib_juju_controller.JujuControllerHelper.agent_conf', |
457 | - agent_conf_sample |
458 | + "lib_juju_controller.JujuControllerHelper.agent_conf", agent_conf_sample |
459 | ) |
460 | |
461 | # Any other functions that load helper will get this version |
462 | - monkeypatch.setattr( |
463 | - 'lib_juju_controller.JujuControllerHelper', |
464 | - lambda: helper |
465 | - ) |
466 | + monkeypatch.setattr("lib_juju_controller.JujuControllerHelper", lambda: helper) |
467 | |
468 | return helper |
469 | |
470 | diff --git a/src/tests/unit/test_check_jujucontroller.py b/src/tests/unit/test_check_jujucontroller.py |
471 | index ffba1ef..e6cad4a 100644 |
472 | --- a/src/tests/unit/test_check_jujucontroller.py |
473 | +++ b/src/tests/unit/test_check_jujucontroller.py |
474 | @@ -6,50 +6,43 @@ import sys |
475 | import pytest |
476 | import responses |
477 | |
478 | -PLUGINS_DIR = os.path.join( |
479 | - os.path.dirname(__file__), |
480 | - '../../files/plugins' |
481 | -) |
482 | +PLUGINS_DIR = os.path.join(os.path.dirname(__file__), "../../files/plugins") |
483 | sys.path.append(PLUGINS_DIR) |
484 | import check_jujucontroller as nrpecheck # noqa: E402,I100,I202 |
485 | |
486 | |
487 | -class TestCheckJujuController(): |
488 | +class TestCheckJujuController: |
489 | """Class containing all tests for the controller NRPE check.""" |
490 | |
491 | - @pytest.mark.parametrize( |
492 | - "version,expected", |
493 | - [('2.8.1', True), ('2.5.6', False)] |
494 | - ) |
495 | + @pytest.mark.parametrize("version,expected", [("2.8.1", True), ("2.5.6", False)]) |
496 | def test_supported_function(self, version, expected): |
497 | """Ensure the version check cuts off at 2.7.2.""" |
498 | assert nrpecheck._supported(version) is expected |
499 | |
500 | def test_no_pubagentconf(self, monkeypatch): |
501 | """Return UNKNOWN if the required auxiliary files are not in place.""" |
502 | - state, _ = nrpecheck.check_endpoint('/this/file/does/not/exist') |
503 | + state, _ = nrpecheck.check_endpoint("/this/file/does/not/exist") |
504 | assert state == nrpecheck.States.UNKNOWN |
505 | |
506 | def test_unsupported(self, monkeypatch, agent_conf_sample_path): |
507 | """Checking an unsupported version should return UNKNOWN.""" |
508 | - monkeypatch.setattr( |
509 | - 'check_jujucontroller._supported', |
510 | - lambda _: False |
511 | - ) |
512 | + monkeypatch.setattr("check_jujucontroller._supported", lambda _: False) |
513 | state, _ = nrpecheck.check_endpoint(agent_conf_sample_path) |
514 | assert state == nrpecheck.States.UNKNOWN |
515 | |
516 | def test_connection_error(self, monkeypatch, agent_conf_sample_path): |
517 | """Connection exceptions should be reported as CRITICAL.""" |
518 | monkeypatch.setattr( |
519 | - 'check_jujucontroller.requests.get', |
520 | - lambda *args, **kwargs: exec('raise ConnectionError("foo")') |
521 | + "check_jujucontroller.requests.get", |
522 | + lambda *args, **kwargs: exec('raise ConnectionError("foo")'), |
523 | ) |
524 | state, msg = nrpecheck.check_endpoint(agent_conf_sample_path) |
525 | assert state == nrpecheck.States.CRITICAL |
526 | - assert msg == 'cannot connect to the health endpoint. Error: foo' |
527 | + assert msg == "cannot connect to the health endpoint. Error: foo" |
528 | |
529 | - def test_status_codes(self, jujucontroller, mocked_responses, agent_conf_sample_path): |
530 | + def test_status_codes( |
531 | + self, jujucontroller, mocked_responses, agent_conf_sample_path |
532 | + ): |
533 | """The endpoint should always return 200, regardless of controller state. |
534 | |
535 | Any other HTTP status should be considered an UNKNOWN state. |
536 | @@ -57,8 +50,8 @@ class TestCheckJujuController(): |
537 | """ |
538 | mocked_responses.add( |
539 | responses.GET, |
540 | - 'https://localhost:{}/health'.format(jujucontroller.apiport), |
541 | - body='foo', |
542 | + "https://localhost:{}/health".format(jujucontroller.apiport), |
543 | + body="foo", |
544 | status=404, |
545 | ) |
546 | state, _ = nrpecheck.check_endpoint(agent_conf_sample_path) |
547 | @@ -66,9 +59,9 @@ class TestCheckJujuController(): |
548 | |
549 | @pytest.mark.parametrize( |
550 | "response,expected_state", |
551 | - [(res, nrpecheck.States.OK) for res in nrpecheck.OKSTATUSES] + |
552 | - [(res, nrpecheck.States.WARNING) for res in nrpecheck.WARNSTATUSES] + |
553 | - [('fubar', nrpecheck.States.CRITICAL)] |
554 | + [(res, nrpecheck.States.OK) for res in nrpecheck.OKSTATUSES] |
555 | + + [(res, nrpecheck.States.WARNING) for res in nrpecheck.WARNSTATUSES] |
556 | + + [("fubar", nrpecheck.States.CRITICAL)], |
557 | ) |
558 | def test_endpoint_response( |
559 | self, |
560 | @@ -76,12 +69,12 @@ class TestCheckJujuController(): |
561 | expected_state, |
562 | mocked_responses, |
563 | agent_conf_sample_path, |
564 | - jujucontroller |
565 | + jujucontroller, |
566 | ): |
567 | """Check that all known reponse bodies cause the related NRPE status.""" |
568 | mocked_responses.add( |
569 | responses.GET, |
570 | - 'https://localhost:{}/health'.format(jujucontroller.apiport), |
571 | + "https://localhost:{}/health".format(jujucontroller.apiport), |
572 | body=response, |
573 | status=200, |
574 | ) |
575 | diff --git a/src/tests/unit/test_lib_juju_controller.py b/src/tests/unit/test_lib_juju_controller.py |
576 | index e35d436..03bae5b 100644 |
577 | --- a/src/tests/unit/test_lib_juju_controller.py |
578 | +++ b/src/tests/unit/test_lib_juju_controller.py |
579 | @@ -2,11 +2,11 @@ |
580 | """Pytest test cases for lib_juju_controller.py.""" |
581 | |
582 | EXPECTED = { |
583 | - 'machine-id': '99', |
584 | - 'services': ['juju-db', 'jujud-machine-99'], |
585 | - 'version': '2.7.6', |
586 | - 'apiport': 17070, |
587 | - 'cacert': '''-----BEGIN CERTIFICATE----- |
588 | + "machine-id": "99", |
589 | + "services": ["juju-db", "jujud-machine-99"], |
590 | + "version": "2.7.6", |
591 | + "apiport": 17070, |
592 | + "cacert": """-----BEGIN CERTIFICATE----- |
593 | AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
594 | BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB |
595 | CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC |
596 | @@ -33,29 +33,29 @@ EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE |
597 | FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF |
598 | GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG |
599 | -----END CERTIFICATE----- |
600 | -''' |
601 | +""", |
602 | } |
603 | |
604 | |
605 | -class TestJujuControllerLib(): |
606 | +class TestJujuControllerLib: |
607 | """Class collecting all tests for the charm library.""" |
608 | |
609 | def test_machine_id(self, jujucontroller): |
610 | """Test parsing the machine id.""" |
611 | - assert jujucontroller._machine_id() == EXPECTED['machine-id'] |
612 | + assert jujucontroller._machine_id() == EXPECTED["machine-id"] |
613 | |
614 | def test_services(self, jujucontroller): |
615 | """Test collecting all juju services.""" |
616 | - assert jujucontroller.services == EXPECTED['services'] |
617 | + assert jujucontroller.services == EXPECTED["services"] |
618 | |
619 | def test_version(self, jujucontroller): |
620 | """Test reporting the controller version.""" |
621 | - assert jujucontroller.version == EXPECTED['version'] |
622 | + assert jujucontroller.version == EXPECTED["version"] |
623 | |
624 | def test_apiport(self, jujucontroller): |
625 | """Test reporting the API port.""" |
626 | - assert jujucontroller.apiport == EXPECTED['apiport'] |
627 | + assert jujucontroller.apiport == EXPECTED["apiport"] |
628 | |
629 | def test_cacert(self, jujucontroller): |
630 | """Test reporting the CA certificate that signed the controller cert.""" |
631 | - assert jujucontroller.cacert == EXPECTED['cacert'] |
632 | + assert jujucontroller.cacert == EXPECTED["cacert"] |
633 | diff --git a/src/tox.ini b/src/tox.ini |
634 | index fc17be2..463f0a4 100644 |
635 | --- a/src/tox.ini |
636 | +++ b/src/tox.ini |
637 | @@ -25,7 +25,7 @@ passenv = |
638 | [testenv:lint] |
639 | commands = |
640 | flake8 |
641 | -#TODO black --check --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" . |
642 | + black --check --exclude "/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/" . |
643 | deps = |
644 | black |
645 | flake8 |
646 | @@ -43,8 +43,7 @@ exclude = |
647 | mod, |
648 | .build |
649 | |
650 | -max-line-length = 100 |
651 | -#TODO max-line-length = 88 |
652 | +max-line-length = 88 |
653 | max-complexity = 10 |
654 | |
655 | [testenv:black] |
This merge proposal is being monitored by mergebot. Change the status to Approved to merge.