Skip to content

Commit

Permalink
Show Discourse version in juju status (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-seijas authored Mar 23, 2023
1 parent 6c1f536 commit 359a104
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 33 deletions.
8 changes: 8 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 48 additions & 33 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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."
Expand Down

0 comments on commit 359a104

Please sign in to comment.