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

Subscribers

People subscribed via source and target branches

to all changes: