Skip to content

Commit

Permalink
simplify charm and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess committed Dec 5, 2023
1 parent a3cc8a7 commit 872b710
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 4 additions & 10 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions tests/integration/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
),
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 872b710

Please sign in to comment.