From 359a104829b77147de665cb0b21af3cc08bc46d6 Mon Sep 17 00:00:00 2001 From: arturo-seijas <102022572+arturo-seijas@users.noreply.github.com> Date: Thu, 23 Mar 2023 17:26:29 +0100 Subject: [PATCH] Show Discourse version in juju status (#50) --- src/charm.py | 8 ++++ tests/unit/test_charm.py | 81 ++++++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/src/charm.py b/src/charm.py index 8baa6ef8..f3435415 100755 --- a/src/charm.py +++ b/src/charm.py @@ -387,6 +387,14 @@ def _set_up_discourse(self, event: HookEvent) -> None: user="discourse", ) precompile_process.wait_output() + get_version_process = container.exec( + [f"{DISCOURSE_PATH}/bin/rails", "runner", "puts Discourse::VERSION::STRING"], + environment=env_settings, + working_dir=DISCOURSE_PATH, + user="discourse", + ) + version, _ = get_version_process.wait_output() + self.unit.set_workload_version(version) except ExecError as cmd_err: logger.exception("Setting up discourse failed with code %d.", cmd_err.exit_code) raise diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index 2d249407..07a6e5e5 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -37,7 +37,10 @@ def tearDown(self): def _patch_exec(self, fail: bool = False) -> typing.Generator[unittest.mock.Mock, None, None]: """Patch the ops.model.Container.exec method. - When fail argument is true, the execution will fail + When fail argument is true, the execution will fail. + + Yields: + Mock for the exec method. """ exec_process_mock = unittest.mock.MagicMock() if not fail: @@ -473,8 +476,7 @@ def test_anonymize_user(self, mock_exec): environment=charm._create_discourse_environment_settings(), ) - @patch.object(Container, "exec") - def test_install_when_leader(self, mock_exec): + def test_install_when_leader(self): """ arrange: given a deployed discourse charm with all the required relations act: trigger the install event on a leader unit @@ -483,26 +485,32 @@ def test_install_when_leader(self, mock_exec): self.harness.begin() self.harness.set_leader(True) self._add_database_relations() - self.harness.container_pebble_ready("discourse") - self.harness.charm.on.install.emit() - - updated_plan = self.harness.get_container_pebble_plan("discourse").to_dict() - updated_plan_env = updated_plan["services"]["discourse"]["environment"] - mock_exec.assert_any_call( - [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "--trace", "db:migrate"], - environment=updated_plan_env, - working_dir=DISCOURSE_PATH, - user="discourse", - ) - mock_exec.assert_any_call( - [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "assets:precompile"], - environment=updated_plan_env, - working_dir=DISCOURSE_PATH, - user="discourse", - ) + with self._patch_exec() as mock_exec: + self.harness.container_pebble_ready("discourse") + self.harness.charm.on.install.emit() + + updated_plan = self.harness.get_container_pebble_plan("discourse").to_dict() + updated_plan_env = updated_plan["services"]["discourse"]["environment"] + mock_exec.assert_any_call( + [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "--trace", "db:migrate"], + environment=updated_plan_env, + working_dir=DISCOURSE_PATH, + user="discourse", + ) + mock_exec.assert_any_call( + [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "assets:precompile"], + environment=updated_plan_env, + working_dir=DISCOURSE_PATH, + user="discourse", + ) + mock_exec.assert_any_call( + [f"{DISCOURSE_PATH}/bin/rails", "runner", "puts Discourse::VERSION::STRING"], + environment=updated_plan_env, + working_dir=DISCOURSE_PATH, + user="discourse", + ) - @patch.object(Container, "exec") - def test_install_when_not_leader(self, mock_exec): + def test_install_when_not_leader(self): """ arrange: given a deployed discourse charm with all the required relations act: trigger the install event on a leader unit @@ -511,17 +519,24 @@ def test_install_when_not_leader(self, mock_exec): self.harness.begin() self.harness.set_leader(False) self._add_database_relations() - self.harness.container_pebble_ready("discourse") - self.harness.charm.on.install.emit() - - updated_plan = self.harness.get_container_pebble_plan("discourse").to_dict() - updated_plan_env = updated_plan["services"]["discourse"]["environment"] - mock_exec.assert_any_call( - [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "assets:precompile"], - environment=updated_plan_env, - working_dir=DISCOURSE_PATH, - user="discourse", - ) + with self._patch_exec() as mock_exec: + self.harness.container_pebble_ready("discourse") + self.harness.charm.on.install.emit() + + updated_plan = self.harness.get_container_pebble_plan("discourse").to_dict() + updated_plan_env = updated_plan["services"]["discourse"]["environment"] + mock_exec.assert_any_call( + [f"{DISCOURSE_PATH}/bin/bundle", "exec", "rake", "assets:precompile"], + environment=updated_plan_env, + working_dir=DISCOURSE_PATH, + user="discourse", + ) + mock_exec.assert_any_call( + [f"{DISCOURSE_PATH}/bin/rails", "runner", "puts Discourse::VERSION::STRING"], + environment=updated_plan_env, + working_dir=DISCOURSE_PATH, + user="discourse", + ) def _add_postgres_relation(self): "Add postgresql relation and relation data to the charm."