← Back to team overview

bind-charmers team mailing list archive

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

 

Barry Price has proposed merging ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:unit-tests into charm-k8s-bind:master with ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:charm-fixes as a prerequisite.

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

Requested reviews:
  Bind Charmers (bind-charmers)

For more details, see:
https://code.launchpad.net/~barryprice/charm-k8s-bind/+git/charm-k8s-bind/+merge/388452
-- 
Your team Bind Charmers is requested to review the proposed merge of ~barryprice/charm-k8s-bind/+git/charm-k8s-bind:unit-tests into charm-k8s-bind:master.
diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
index 4b3ae7e..544c812 100644
--- a/tests/unit/test_charm.py
+++ b/tests/unit/test_charm.py
@@ -1,18 +1,15 @@
-# Copyright 2020 Canonical Ltd.
-# Licensed under the GPLv3, see LICENCE file for details.
-
 import unittest
 
 from charm import BindK8sCharm
 
 from ops import testing
+from ops.model import ActiveStatus
 
 CONFIG_EMPTY = {
     'bind_image_path': '',
     'bind_image_username': '',
     'bind_image_password': '',
     'container_config': '',
-    'container_secrets': '',
     'custom_config_repo': '',
     'https_proxy': '',
 }
@@ -22,23 +19,135 @@ CONFIG_IMAGE_PASSWORD_MISSING = {
     'bind_image_username': 'username',
     'bind_image_password': '',
     'container_config': '',
+    'custom_config_repo': '',
+    'https_proxy': '',
+}
+
+CONFIG_VALID = {
+    'bind_image_path': 'example.com/bind:v1',
+    'bind_image_username': '',
+    'bind_image_password': '',
+    'container_config': '',
+    'container_secrets': '',
+    'custom_config_repo': '',
+    'https_proxy': '',
+}
+
+CONFIG_VALID_WITH_CONTAINER_CONFIG = {
+    'bind_image_path': 'example.com/bind:v1',
+    'bind_image_username': '',
+    'bind_image_password': '',
+    'container_config': '"magic_number": 123',
     'container_secrets': '',
     'custom_config_repo': '',
     'https_proxy': '',
 }
 
+CONFIG_VALID_WITH_CONTAINER_CONFIG_AND_SECRETS = {
+    'bind_image_path': 'example.com/bind:v1',
+    'bind_image_username': '',
+    'bind_image_password': '',
+    'container_config': '"magic_number": 123',
+    'container_secrets': '"secret_password": "xyzzy"',
+    'custom_config_repo': '',
+    'https_proxy': '',
+}
+
+
+class TestBindK8s(unittest.TestCase):
+    maxDiff = None
 
-class TestBindK8sCharmHooksDisabled(unittest.TestCase):
     def setUp(self):
         self.harness = testing.Harness(BindK8sCharm)
         self.harness.begin()
         self.harness.disable_hooks()
 
-    def test_check_for_config_problems(self):
+    def test_check_for_config_problems_empty_image_path(self):
         self.harness.update_config(CONFIG_EMPTY)
         expected = 'required setting(s) empty: bind_image_path'
         self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
 
+    def test_check_for_config_problems_empty_image_password(self):
         self.harness.update_config(CONFIG_IMAGE_PASSWORD_MISSING)
         expected = 'required setting(s) empty: bind_image_password'
         self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
+
+    def test_check_for_config_problems_none(self):
+        self.harness.update_config(CONFIG_VALID)
+        expected = ''
+        self.assertEqual(self.harness.charm._check_for_config_problems(), expected)
+
+    def test_make_pod_resources(self):
+        expected = {}
+        self.assertEqual(self.harness.charm.make_pod_resources(), expected)
+
+    def test_make_pod_spec_basic(self):
+        self.harness.update_config(CONFIG_VALID)
+        expected = {
+            'version': 2,
+            'containers': [
+                {
+                    'name': 'bind',
+                    'imageDetails': {'imagePath': 'example.com/bind:v1'},
+                    'ports': [
+                        {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
+                        {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
+                    ],
+                    'config': {},
+                    'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
+                }
+            ],
+        }
+        self.assertEqual(self.harness.charm.make_pod_spec(), expected)
+
+    def test_make_pod_spec_with_extra_config(self):
+        self.harness.update_config(CONFIG_VALID_WITH_CONTAINER_CONFIG)
+        expected = {
+            'version': 2,
+            'containers': [
+                {
+                    'name': 'bind',
+                    'imageDetails': {'imagePath': 'example.com/bind:v1'},
+                    'ports': [
+                        {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
+                        {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
+                    ],
+                    'config': {'magic_number': 123},
+                    'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
+                }
+            ],
+        }
+        self.assertEqual(self.harness.charm.make_pod_spec(), expected)
+
+    def test_make_pod_spec_with_extra_config_and_secrets(self):
+        self.harness.update_config(CONFIG_VALID_WITH_CONTAINER_CONFIG_AND_SECRETS)
+        expected = {
+            'version': 2,
+            'containers': [
+                {
+                    'name': 'bind',
+                    'imageDetails': {'imagePath': 'example.com/bind:v1'},
+                    'ports': [
+                        {'containerPort': 53, 'name': 'domain-tcp', 'protocol': 'TCP'},
+                        {'containerPort': 53, 'name': 'domain-udp', 'protocol': 'UDP'},
+                    ],
+                    'config': {'magic_number': 123, 'secret_password': 'xyzzy'},
+                    'kubernetes': {'readinessProbe': {'exec': {'command': ['/usr/local/bin/dns-check.sh']}}},
+                }
+            ],
+        }
+        self.assertEqual(self.harness.charm.make_pod_spec(), expected)
+
+    def test_configure_pod_as_leader(self):
+        self.harness.enable_hooks()
+        self.harness.set_leader(True)
+        self.harness.update_config(CONFIG_VALID)
+        expected = ActiveStatus('Pod configured')
+        self.assertEqual(self.harness.model.unit.status, expected)
+
+    def test_configure_pod_as_non_leader(self):
+        self.harness.enable_hooks()
+        self.harness.set_leader(False)
+        self.harness.update_config(CONFIG_VALID)
+        expected = ActiveStatus()
+        self.assertEqual(self.harness.model.unit.status, expected)

Follow ups