Merge ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:unit-tests into charm-k8s-bind:master

Proposed by Barry Price
Status: Merged
Approved by: Barry Price
Approved revision: 77454805fc091b701992c868ab91f54a001dfd6b
Merged at revision: 9e15f5e7d7169fc589f53cd626a8834f312bedae
Proposed branch: ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:unit-tests
Merge into: charm-k8s-bind:master
Prerequisite: ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:charm-fixes
Diff against target: 169 lines (+127/-3)
2 files modified
src/charm.py (+2/-1)
tests/unit/test_charm.py (+125/-2)
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Canonical IS Reviewers Pending
Review via email: mp+388452@code.launchpad.net

Commit message

Add more unit tests. This gets us to 90% coverage.

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
Tom Haddon (mthaddon) wrote :

Can you add docstrings for all tests please? And one question inline about whether we need the make_pod_resources method

Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM, but please add trailing "."s to the docstrings

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/charm.py b/src/charm.py
2index f4922e3..8754cfa 100755
3--- a/src/charm.py
4+++ b/src/charm.py
5@@ -69,7 +69,8 @@ class BindK8sCharm(CharmBase):
6 self.model.unit.status = ActiveStatus()
7
8 def make_pod_resources(self):
9- resources = {}
10+ """LP#1889746: We should define a manual ingress here to work around LP#1889703."""
11+ resources = {} # TODO
12 out = io.StringIO()
13 pprint(resources, out)
14 logger.info("This is the Kubernetes Pod resources <<EOM\n{}\nEOM".format(out.getvalue()))
15diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
16index 4b3ae7e..dc53ac0 100644
17--- a/tests/unit/test_charm.py
18+++ b/tests/unit/test_charm.py
19@@ -6,6 +6,7 @@ import unittest
20 from charm import BindK8sCharm
21
22 from ops import testing
23+from ops.model import ActiveStatus
24
25 CONFIG_EMPTY = {
26 'bind_image_path': '',
27@@ -27,18 +28,140 @@ CONFIG_IMAGE_PASSWORD_MISSING = {
28 'https_proxy': '',
29 }
30
31+CONFIG_VALID = {
32+ 'bind_image_path': 'example.com/bind:v1',
33+ 'bind_image_username': '',
34+ 'bind_image_password': '',
35+ 'container_config': '',
36+ 'container_secrets': '',
37+ 'custom_config_repo': '',
38+ 'https_proxy': '',
39+}
40+
41+CONFIG_VALID_WITH_CONTAINER_CONFIG = {
42+ 'bind_image_path': 'example.com/bind:v1',
43+ 'bind_image_username': '',
44+ 'bind_image_password': '',
45+ 'container_config': '"magic_number": 123',
46+ 'container_secrets': '',
47+ 'custom_config_repo': '',
48+ 'https_proxy': '',
49+}
50+
51+CONFIG_VALID_WITH_CONTAINER_CONFIG_AND_SECRETS = {
52+ 'bind_image_path': 'example.com/bind:v1',
53+ 'bind_image_username': '',
54+ 'bind_image_password': '',
55+ 'container_config': '"magic_number": 123',
56+ 'container_secrets': '"secret_password": "xyzzy"',
57+ 'custom_config_repo': '',
58+ 'https_proxy': '',
59+}
60+
61+
62+class TestBindK8s(unittest.TestCase):
63+ maxDiff = None
64
65-class TestBindK8sCharmHooksDisabled(unittest.TestCase):
66 def setUp(self):
67 self.harness = testing.Harness(BindK8sCharm)
68 self.harness.begin()
69 self.harness.disable_hooks()
70
71- def test_check_for_config_problems(self):
72+ def test_check_for_config_problems_empty_image_path(self):
73+ """Confirm that we generate an error if we're not told what image to use."""
74 self.harness.update_config(CONFIG_EMPTY)
75 expected = 'required setting(s) empty: bind_image_path'
76 self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
77
78+ def test_check_for_config_problems_empty_image_password(self):
79+ """Confirm that we generate an error if we're not given valid registry creds."""
80 self.harness.update_config(CONFIG_IMAGE_PASSWORD_MISSING)
81 expected = 'required setting(s) empty: bind_image_password'
82 self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
83+
84+ def test_check_for_config_problems_none(self):
85+ """Confirm that we accept valid config."""
86+ self.harness.update_config(CONFIG_VALID)
87+ expected = ''
88+ self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
89+
90+ def test_make_pod_resources(self):
91+ """Confirm that we generate the expected pod resources (see LP#1889746)."""
92+ expected = {}
93+ self.assertEqual(self.harness.charm.make_pod_resources(), expected)
94+
95+ def test_make_pod_spec_basic(self):
96+ """Confirm that we generate the expected pod spec from valid config."""
97+ self.harness.update_config(CONFIG_VALID)
98+ expected = {
99+ 'version': 2,
100+ 'containers': [
101+ {
102+ 'name': 'bind',
103+ 'imageDetails': {'imagePath': 'example.com/bind:v1'},
104+ 'ports': [
105+ {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
106+ {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
107+ ],
108+ 'config': {},
109+ 'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
110+ }
111+ ],
112+ }
113+ self.assertEqual(self.harness.charm.make_pod_spec(), expected)
114+
115+ def test_make_pod_spec_with_extra_config(self):
116+ """Confirm that we generate the expected pod spec from a more involved valid config."""
117+ self.harness.update_config(CONFIG_VALID_WITH_CONTAINER_CONFIG)
118+ expected = {
119+ 'version': 2,
120+ 'containers': [
121+ {
122+ 'name': 'bind',
123+ 'imageDetails': {'imagePath': 'example.com/bind:v1'},
124+ 'ports': [
125+ {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
126+ {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
127+ ],
128+ 'config': {'magic_number': 123},
129+ 'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
130+ }
131+ ],
132+ }
133+ self.assertEqual(self.harness.charm.make_pod_spec(), expected)
134+
135+ def test_make_pod_spec_with_extra_config_and_secrets(self):
136+ """Confirm that we generate the expected pod spec from a more involved valid config that includes secrets."""
137+ self.harness.update_config(CONFIG_VALID_WITH_CONTAINER_CONFIG_AND_SECRETS)
138+ expected = {
139+ 'version': 2,
140+ 'containers': [
141+ {
142+ 'name': 'bind',
143+ 'imageDetails': {'imagePath': 'example.com/bind:v1'},
144+ 'ports': [
145+ {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
146+ {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
147+ ],
148+ 'config': {'magic_number': 123, 'secret_password': 'xyzzy'},
149+ 'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
150+ }
151+ ],
152+ }
153+ self.assertEqual(self.harness.charm.make_pod_spec(), expected)
154+
155+ def test_configure_pod_as_leader(self):
156+ """Confirm that our status is set correctly when we're the leader."""
157+ self.harness.enable_hooks()
158+ self.harness.set_leader(True)
159+ self.harness.update_config(CONFIG_VALID)
160+ expected = ActiveStatus('Pod configured')
161+ self.assertEqual(self.harness.model.unit.status, expected)
162+
163+ def test_configure_pod_as_non_leader(self):
164+ """Confirm that our status is set correctly when we're not the leader."""
165+ self.harness.enable_hooks()
166+ self.harness.set_leader(False)
167+ self.harness.update_config(CONFIG_VALID)
168+ expected = ActiveStatus()
169+ self.assertEqual(self.harness.model.unit.status, expected)

Subscribers

People subscribed via source and target branches

to all changes: