Merge ~jfguedez/charm-juju-lint:bug/1942947 into charm-juju-lint:master

Proposed by Jose Guedez
Status: Merged
Approved by: James Troup
Approved revision: 2e5ded981a952154b9fda8ac1dd266083fa60bb1
Merged at revision: 31f543f9e3880535fb16f282eedd20546c223efc
Proposed branch: ~jfguedez/charm-juju-lint:bug/1942947
Merge into: charm-juju-lint:master
Diff against target: 125 lines (+79/-9)
3 files modified
scripts/auto_lint.py (+16/-7)
tests/unit/fixtures.py (+32/-0)
tests/unit/test_auto_lint.py (+31/-2)
Reviewer Review Type Date Requested Status
Edin S (community) Approve
BootStack Reviewers Pending
BootStack Reviewers Pending
Review via email: mp+408263@code.launchpad.net

Commit message

Collect default charm settings, add unit tests

To post a comment you must log in.
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
Edin S (exsdev) :
review: Approve
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 31f543f9e3880535fb16f282eedd20546c223efc

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/scripts/auto_lint.py b/scripts/auto_lint.py
2index 6446a01..944dcf5 100755
3--- a/scripts/auto_lint.py
4+++ b/scripts/auto_lint.py
5@@ -21,6 +21,7 @@ License: Apache License 2.0
6
7 from base64 import b64decode
8 import json
9+import logging
10 from os.path import join
11 import subprocess
12 from sys import exit
13@@ -160,13 +161,21 @@ async def add_charm_config_to_juju_status(juju_status, model):
14 model_app = model.applications[app_name]
15 model_app_config = await model_app.get_config()
16
17- # collect only the manual settings to match bundle behaviour
18- # which is what juju-lint expects for charm configuration
19- status_app_config = {
20- c: d["value"]
21- for c, d in model_app_config.items()
22- if d["source"] != "default"
23- }
24+ # Collect all manual/default config settings
25+ # this similar to juju export-bundle --include-charm-defaults
26+ status_app_config = {}
27+ for k, v in model_app_config.items():
28+ if v["source"] != "unset":
29+ try:
30+ status_app_config[k] = v["value"]
31+ except KeyError as error:
32+ logging.error(
33+ 'missing info for app: "{}", key: "{}", val: "{}"'.format(
34+ app_name, k, v
35+ )
36+ )
37+ raise error
38+
39 juju_status["applications"][app_name]["options"] = status_app_config
40
41 return juju_status
42diff --git a/tests/unit/fixtures.py b/tests/unit/fixtures.py
43index 344586f..5e25c17 100644
44--- a/tests/unit/fixtures.py
45+++ b/tests/unit/fixtures.py
46@@ -419,3 +419,35 @@ class MockModel:
47 get_config=AsyncMock(return_value={"swap": {"value": "", "source": "user"}})
48 ),
49 }
50+
51+
52+class MockModelUnset:
53+ applications = {
54+ "juju-lint": Mock(
55+ get_config=AsyncMock(return_value={"model-uuid": {"source": "unset"}})
56+ ),
57+ "nagios": Mock(
58+ get_config=AsyncMock(
59+ return_value={"check_timeout": {"value": "10", "source": "default"}}
60+ )
61+ ),
62+ "nrpe": Mock(
63+ get_config=AsyncMock(return_value={"swap": {"value": "", "source": "user"}})
64+ ),
65+ }
66+
67+
68+class MockModelMissingValue:
69+ applications = {
70+ "juju-lint": Mock(
71+ get_config=AsyncMock(return_value={"model-uuid": {"source": "user"}})
72+ ),
73+ "nagios": Mock(
74+ get_config=AsyncMock(
75+ return_value={"check_timeout": {"value": "10", "source": "default"}}
76+ )
77+ ),
78+ "nrpe": Mock(
79+ get_config=AsyncMock(return_value={"swap": {"value": "", "source": "user"}})
80+ ),
81+ }
82diff --git a/tests/unit/test_auto_lint.py b/tests/unit/test_auto_lint.py
83index a0f5e05..00189ae 100644
84--- a/tests/unit/test_auto_lint.py
85+++ b/tests/unit/test_auto_lint.py
86@@ -85,8 +85,37 @@ class TestAutoLint(unittest.TestCase):
87 juju_status = loop.run(
88 add_charm_config_to_juju_status(self.juju_status, fixtures.MockModel)
89 )
90- for app_dict in juju_status["applications"].values():
91- assert "options" in app_dict
92+ for app_name, app_dict in juju_status["applications"].items():
93+ options = app_dict["options"]
94+ if app_name == "juju-lint":
95+ self.assertEqual(options["model-uuid"], "1")
96+ elif app_name == "nagios":
97+ self.assertEqual(options["check_timeout"], "10")
98+ elif app_name == "nrpe":
99+ self.assertEqual(options["swap"], "")
100+ else:
101+ raise Exception("unexpected application: {}".format(app_name))
102+
103+ def test_add_config_to_status_unset(self):
104+ """Tests that the unset options are ignored."""
105+ juju_status = loop.run(
106+ add_charm_config_to_juju_status(self.juju_status, fixtures.MockModelUnset)
107+ )
108+ for app_name, app_dict in juju_status["applications"].items():
109+ if app_name == "juju-lint":
110+ self.assertEqual(app_dict["options"], {})
111+
112+ def test_add_config_to_status_value_present(self):
113+ """Tests handling the case where an option is missing a value."""
114+
115+ def test_helper():
116+ loop.run(
117+ add_charm_config_to_juju_status(
118+ self.juju_status, fixtures.MockModelMissingValue
119+ )
120+ )
121+
122+ self.assertRaises(KeyError, test_helper)
123
124 @mock.patch("scripts.auto_lint.write_file")
125 @mock.patch("scripts.auto_lint.subprocess.check_output")

Subscribers

People subscribed via source and target branches

to all changes: