From 872b7104b8943e04369ced2510135458bbb8da15 Mon Sep 17 00:00:00 2001 From: Adam Dyess Date: Tue, 5 Dec 2023 08:01:42 -0600 Subject: [PATCH] simplify charm and testing --- metadata.yaml | 2 +- src/charm.py | 14 ++++---------- tests/integration/test_charm.py | 14 +++++++++++--- tests/unit/test_base.py | 9 +++++++-- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/metadata.yaml b/metadata.yaml index bd3d69d3..5f080aa7 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -15,7 +15,7 @@ maintainers: source: https://github.com/canonical/k8s-operator assumes: - - juju > 3.1 + - juju >= 3.1 description: | A machine charm which operates a complete Kubernetes cluster. diff --git a/src/charm.py b/src/charm.py index 3ee19aec..ac699f18 100755 --- a/src/charm.py +++ b/src/charm.py @@ -33,21 +33,15 @@ def __init__(self, *args): args: Arguments passed to the CharmBase parent constructor. """ super().__init__(*args) - self.framework.observe(self.on.config_changed, self._on_config_changed) + self.framework.observe(self.on.update_status, self._on_update_status) - def _on_config_changed(self, _event: ops.ConfigChangedEvent): - """Handle changed configuration. - - Change this example to suit your needs. If you don't need to handle config, you can remove - this method. - - Learn more about config at https://juju.is/docs/sdk/config + def _on_update_status(self, _event: ops.UpdateStatusEvent): + """Handle update-status event. Args: _event: event triggering the handler. """ - # Fetch an invalid config name - self.model.config["log-level"].lower() + self.unit.status = ops.ActiveStatus("Ready") if __name__ == "__main__": # pragma: nocover diff --git a/tests/integration/test_charm.py b/tests/integration/test_charm.py index d75ba3e6..290118dc 100644 --- a/tests/integration/test_charm.py +++ b/tests/integration/test_charm.py @@ -13,7 +13,7 @@ import yaml from pytest_operator.plugin import OpsTest -logger = logging.getLogger(__name__) +log = logging.getLogger(__name__) METADATA = yaml.safe_load(Path("./metadata.yaml").read_text(encoding="utf-8")) APP_NAME = METADATA["name"] @@ -26,10 +26,18 @@ async def test_build_and_deploy(ops_test: OpsTest, pytestconfig: pytest.Config): Assert on the unit status before any relations/configurations take place. """ # Deploy the charm and wait for active/idle status - charm = pytestconfig.getoption("--charm-file") + if charm := pytestconfig.getoption("--charm-file"): + charm = Path(charm) + log.info("Specific charm-file %s...", charm) + if not charm: + log.info("Search for charm...") + charm = next(Path.cwd().glob("*.charm"), None) + if not charm: + log.info("Build charm...") + charm = await ops_test.build_charm(".") assert ops_test.model await asyncio.gather( - ops_test.model.deploy(f"./{charm}", application_name=APP_NAME, series="jammy"), + ops_test.model.deploy(charm.absolute(), application_name=APP_NAME, series="jammy"), ops_test.model.wait_for_idle( apps=[APP_NAME], status="active", raise_on_blocked=True, timeout=1000 ), diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index f6acc1a0..1f7b50e4 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -23,6 +23,11 @@ def harness(): def test_config_changed_invalid(harness): - # Trigger a config-changed event with an updated value + # Trigger a config-changed event with an unknown-config option with pytest.raises(ValueError): - harness.update_config({"log-level": "foobar"}) + harness.update_config({"unknown-config": "foobar"}) + + +def test_update_status(harness): + harness.charm.on.update_status.emit() + assert harness.model.unit.status == ops.ActiveStatus("Ready")