From 366a762c79aeb5947c2747d9f7755caecea4b5a7 Mon Sep 17 00:00:00 2001 From: Niels Robin-Aubertin Date: Mon, 15 Jan 2024 13:56:10 -0500 Subject: [PATCH] Add unit tests --- tests/unit/test_charm.py | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py index dc4f66cf..b65ee0d8 100644 --- a/tests/unit/test_charm.py +++ b/tests/unit/test_charm.py @@ -8,7 +8,7 @@ import secrets import typing -from unittest.mock import MagicMock +from unittest.mock import MagicMock, patch import ops import pytest @@ -691,3 +691,37 @@ def test_http_proxy_env(monkeypatch): assert created_env["https_proxy"] == "http://httpsproxy.test" assert created_env["NO_PROXY"] == "noproxy.test" assert created_env["no_proxy"] == "noproxy.test" + + +def test_acquire_lock_on_upgrade(): + """ + arrange: given a deployed discourse charm with postgresql/redis related + act: emit an upgrade event + assert: it should try to acquire_lock from the RollingOpsManager + """ + harness = helpers.start_harness(with_postgres=True, with_redis=True) + harness.container_pebble_ready("discourse") + assert harness.model.unit.status == ActiveStatus() + + with patch( + "charms.rolling_ops.v0.rollingops.RollingOpsManager._on_acquire_lock" + ) as acquire_lock: + harness.charm.on.upgrade_charm.emit() + acquire_lock.assert_called_once() + + +def test_setup_and_activate_on_upgrade(monkeypatch: pytest.MonkeyPatch): + """ + arrange: given a deployed discourse charm with postgresql/redis related + act: emit an upgrade event + assert: it should launch _setup_and_activate + """ + harness = helpers.start_harness(with_postgres=True, with_redis=True) + harness.container_pebble_ready("discourse") + harness.set_leader(True) + assert harness.model.unit.status == ActiveStatus() + + setup_and_activate = MagicMock() + monkeypatch.setattr(harness.charm, "_setup_and_activate", setup_and_activate) + harness.charm.on.upgrade_charm.emit() + setup_and_activate.assert_called_once()