Merge ~mthaddon/charm-k8s-unifi-poller/+git/charm-k8s-unifi-poller:master into charm-k8s-unifi-poller:master

Proposed by Tom Haddon
Status: Merged
Approved by: Tom Haddon
Approved revision: 0f9d4bf0f9f6b50fab5b5771a4df675c70b8bb14
Merged at revision: c40952d1bfcdfa00e71f5f5c3d928911e4945f00
Proposed branch: ~mthaddon/charm-k8s-unifi-poller/+git/charm-k8s-unifi-poller:master
Merge into: charm-k8s-unifi-poller:master
Diff against target: 48 lines (+15/-1)
2 files modified
src/charm.py (+5/-1)
tests/unit/test_charm.py (+10/-0)
Reviewer Review Type Date Requested Status
Gareth Woolridge Approve
Canonical IS Reviewers Pending
Review via email: mp+390545@code.launchpad.net

Commit message

Prevent duplicate entries in extra_configs

Description of the change

Prevent duplicate entries in extra_configs

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
Gareth Woolridge (moon127) wrote :

Agreed we should be checking for duplicate config options in the extra_config data.

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

Change successfully merged at revision c40952d1bfcdfa00e71f5f5c3d928911e4945f00

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/charm.py b/src/charm.py
index 20a530a..96b72c2 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -71,10 +71,14 @@ class UnifiPollerCharm(CharmBase):
71 """Check extra_config configuration_settings."""71 """Check extra_config configuration_settings."""
72 config = self.model.config72 config = self.model.config
7373
74 errors = []74 errors, config_keys = [], []
75 if config['extra_configs']:75 if config['extra_configs']:
76 for c in yaml.safe_load(config['extra_configs']):76 for c in yaml.safe_load(config['extra_configs']):
77 k, value = c.split('=', 1)77 k, value = c.split('=', 1)
78 if k in config_keys:
79 errors.append('{} is duplicated'.format(k))
80 else:
81 config_keys.append(k)
78 if not re.match(r'^UP_', k.upper()):82 if not re.match(r'^UP_', k.upper()):
79 errors.append(k)83 errors.append(k)
80 if re.match(r'.*_URL', k.upper()):84 if re.match(r'.*_URL', k.upper()):
diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
index 42b6066..50cfd9e 100644
--- a/tests/unit/test_charm.py
+++ b/tests/unit/test_charm.py
@@ -92,6 +92,10 @@ CONFIG_INVALID_EXTRA_CONFIG_NO_NETLOC = {
92 'extra_configs': "[up_unifi_controller_1_url=http://valid-url, up_unifi_controller_2_url=https:no-netloc]",92 'extra_configs': "[up_unifi_controller_1_url=http://valid-url, up_unifi_controller_2_url=https:no-netloc]",
93}93}
9494
95CONFIG_INVALID_EXTRA_CONFIG_DUPLICATES = {
96 'extra_configs': "[up_dup=1, up_dup=2]",
97}
98
9599
96class TestCharm(unittest.TestCase):100class TestCharm(unittest.TestCase):
97 def setUp(self):101 def setUp(self):
@@ -122,6 +126,12 @@ class TestCharm(unittest.TestCase):
122 expected = 'invalid extra_config setting(s): up_unifi_controller_2_url'126 expected = 'invalid extra_config setting(s): up_unifi_controller_2_url'
123 self.assertEqual(self.harness.charm._check_for_config_problems(), expected)127 self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
124128
129 def test_check_for_invalid_extra_configs_duplicates(self):
130 """Check for correctly reports duplicate entries in extra_configs options."""
131 self.harness.update_config(CONFIG_INVALID_EXTRA_CONFIG_DUPLICATES)
132 expected = ['up_dup is duplicated']
133 self.assertEqual(self.harness.charm._bad_extra_config_charm_settings(), expected)
134
125 def test_check_for_missing_image_invalid_extra_config(self):135 def test_check_for_missing_image_invalid_extra_config(self):
126 """Check for correctly reports invalid extra_configs options."""136 """Check for correctly reports invalid extra_configs options."""
127 self.harness.update_config(CONFIG_INVALID_EXTRA_CONFIG_NO_IMAGE)137 self.harness.update_config(CONFIG_INVALID_EXTRA_CONFIG_NO_IMAGE)

Subscribers

People subscribed via source and target branches