← Back to team overview

wordpress-charmers team mailing list archive

[Merge] ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:maintenance-status into charm-k8s-wordpress:master

 

Tom Haddon has proposed merging ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:maintenance-status into charm-k8s-wordpress:master.

Commit message:
Fix unit status to stay in 'active' once configured

Requested reviews:
  Wordpress Charmers (wordpress-charmers)

For more details, see:
https://code.launchpad.net/~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress/+merge/396065

Fix unit status to stay in active once configured.

Without this change, the site gets configured by the leader in the `on_wordpress_initialise` function. At this point that unit's status is set to ActiveStatus. However, at the next run of the update_status hook, the `configure_pod` function runs. Whenever this runs on the leader, it ends up setting the status to MaintenanceStatus.

This change correctly recognises when a service has been initialised, using StoredState, and sets status to ActiveState in the `configure_pod` function if so.
-- 
Your team Wordpress Charmers is requested to review the proposed merge of ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:maintenance-status into charm-k8s-wordpress:master.
diff --git a/src/charm.py b/src/charm.py
index edade47..47b2db3 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -174,9 +174,14 @@ class WordpressCharm(CharmBase):
             self.model.unit.status = MaintenanceStatus(msg)
             self.model.pod.set_spec(spec)
 
-            msg = "Pod configured"
-            logger.info(msg)
-            self.model.unit.status = MaintenanceStatus(msg)
+            if self.state.initialised:
+                msg = "Pod configured"
+                logger.info(msg)
+                self.model.unit.status = ActiveStatus(msg)
+            else:
+                msg = "Pod configured, but WordPress configuration pending"
+                logger.info(msg)
+                self.model.unit.status = MaintenanceStatus(msg)
         else:
             logger.info("Spec changes ignored by non-leader")
 
diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
index 1840a4c..4d74ee7 100644
--- a/tests/unit/test_charm.py
+++ b/tests/unit/test_charm.py
@@ -7,7 +7,11 @@ import unittest
 from charm import WordpressCharm, create_wordpress_secrets, gather_wordpress_secrets
 from wordpress import WORDPRESS_SECRETS
 from ops import testing
-from ops.model import BlockedStatus
+from ops.model import (
+    ActiveStatus,
+    BlockedStatus,
+    MaintenanceStatus,
+)
 
 from test_wordpress import TEST_MODEL_CONFIG
 
@@ -124,3 +128,31 @@ class TestWordpressCharm(unittest.TestCase):
             }
         }
         self.assertEqual(self.harness.charm.make_pod_resources(), expected)
+
+    @mock.patch("charm._leader_set")
+    @mock.patch("charm._leader_get")
+    def test_configure_pod(self, _leader_get_func, _leader_set_func):
+        leadership_data = TestLeadershipData()
+        _leader_set_func.side_effect = leadership_data._leader_set
+        _leader_get_func.side_effect = leadership_data._leader_get
+
+        # First of all, test with leader set, but not initialised.
+        self.harness.set_leader(True)
+        self.assertEqual(self.harness.charm.state.initialised, False)
+        self.harness.charm.configure_pod()
+        expected_msg = "Pod configured, but WordPress configuration pending"
+        self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
+        self.assertLogs(expected_msg, level="INFO")
+        self.assertIsInstance(self.harness.charm.unit.status, MaintenanceStatus)
+        # Now with state initialised.
+        self.harness.charm.state.initialised = True
+        self.harness.charm.configure_pod()
+        expected_msg = "Pod configured"
+        self.assertEqual(self.harness.charm.unit.status.message, expected_msg)
+        self.assertLogs(expected_msg, level="INFO")
+        self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
+        # And now test with non-leader.
+        self.harness.set_leader(False)
+        self.harness.charm.configure_pod()
+        expected_msg = "Spec changes ignored by non-leader"
+        self.assertLogs(expected_msg, level="INFO")

Follow ups