From 7263dcefaaff00595908440f1c5d190047039ff4 Mon Sep 17 00:00:00 2001 From: Chi Wai Chan Date: Wed, 10 Jul 2024 15:38:18 +0800 Subject: [PATCH] Set tempest-ready to `False` when the unit start The peer relation data `tempest-ready` persists across a reboot of the host machine, and this can cause the inconsistency between the actual pod state and the relation data. For example, relation data `tempest-ready` can still be `True` before and after the host is rebooted, but the tempest workspace directory created during tempest init will be gone because the pod is recreated. So when the host is rebooted we need to consider tempest to be no longer ready, so that in the follow up config-changed hook, the charm will re-init tempest. Bug: #2069767 Change-Id: I1db615e5094922b35da3b07552b0c045997abbfc --- charms/tempest-k8s/src/charm.py | 14 ++++++++++++++ .../src/templates/tempest-run-wrapper.j2 | 4 +++- .../tempest-k8s/tests/unit/test_tempest_charm.py | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/charms/tempest-k8s/src/charm.py b/charms/tempest-k8s/src/charm.py index 70d79263..275a7118 100755 --- a/charms/tempest-k8s/src/charm.py +++ b/charms/tempest-k8s/src/charm.py @@ -118,6 +118,7 @@ class TempestOperatorCharm(sunbeam_charm.OSBaseOperatorCharmK8S): self.framework.observe( self.on.get_lists_action, self._on_get_lists_action ) + self.framework.observe(self.on.start, self._on_start) self.framework.observe(self.on.upgrade_charm, self._on_upgrade_charm) @property @@ -361,6 +362,19 @@ class TempestOperatorCharm(sunbeam_charm.OSBaseOperatorCharmK8S): """Get the pebble handler.""" return self.get_named_pebble_handler(CONTAINER) + def _on_start(self, event: ops.charm.StartEvent) -> None: + """Called on charm start.""" + # Mark tempest as being unready when it is started or rebooted. This is + # because peer relation data `tempest-ready` persists across a reboot + # of the host machine, and this can cause the inconsistency between the + # actual pod state and the relation data. For example, relation data + # `tempest-ready` can still be `True` before and after the host is + # rebooted, but the tempest workspace directory created during tempest + # init will be gone because the pod is recreated. So when the host is + # rebooted we need to consider tempest to be no longer ready, so that + # in the follow up config-changed hook, the charm will re-init tempest. + self.set_tempest_ready(False) + def _on_upgrade_charm(self, event: ops.charm.UpgradeCharmEvent) -> None: """Called on charm upgrade.""" # When a charm is upgraded, consider tempest to no longer be ready, diff --git a/charms/tempest-k8s/src/templates/tempest-run-wrapper.j2 b/charms/tempest-k8s/src/templates/tempest-run-wrapper.j2 index 858f204f..1aef0d6d 100644 --- a/charms/tempest-k8s/src/templates/tempest-run-wrapper.j2 +++ b/charms/tempest-k8s/src/templates/tempest-run-wrapper.j2 @@ -35,7 +35,9 @@ sed $'s/\033\[[0-9;]*m//g' -i "$TMP_FILE" # After everything, move it to the actual output. # This ensures we don't have issues with logging libs pushing partial files, # if we were to stream to the final output. -mv "$TMP_FILE" "$TEMPEST_OUTPUT" +# The `mkdir` is to ensure the tempest workspace directory exists, and the +# temp file can be moved to that directory. +mkdir -p $TEMPEST_WORKSPACE && mv "$TMP_FILE" "$TEMPEST_OUTPUT" SUMMARY="$(awk '/^Totals$/,/Sum of execute/ { print }' < "$TEMPEST_OUTPUT")" if [[ -n "$SUMMARY" ]]; then diff --git a/charms/tempest-k8s/tests/unit/test_tempest_charm.py b/charms/tempest-k8s/tests/unit/test_tempest_charm.py index ffa389cd..19baa1f6 100644 --- a/charms/tempest-k8s/tests/unit/test_tempest_charm.py +++ b/charms/tempest-k8s/tests/unit/test_tempest_charm.py @@ -582,6 +582,14 @@ class TestTempestOperatorCharm(test_utils.CharmTestCase): self.harness.charm.init_tempest() self.harness.charm.set_tempest_ready.assert_not_called() + def test_start(self): + """Test start charm updates things as required.""" + test_utils.set_all_pebbles_ready(self.harness) + + self.harness.charm.set_tempest_ready = Mock() + self.harness.charm._on_start(Mock()) + self.harness.charm.set_tempest_ready.assert_called_once_with(False) + def test_upgrade_charm(self): """Test upgrade charm updates things as required.""" test_utils.set_all_pebbles_ready(self.harness)