Files
sunbeam-charms/charms/sunbeam-clusterd/tests/unit/test_charm.py
Guillaume Boutry 619976fe43 Implement sunbeam-clusterd
Sunbeam-clusterd can scale up and down.
Currently, the external address in bound to the peers relationship.
Exposes the action `get-credentials` which returns the URL. In the long
term, it will return the credentials to access securely clusterd.

Change-Id: I8b91efe6d96198f5ad3634b9747161225381ded6
2024-01-25 12:30:54 +01:00

97 lines
2.8 KiB
Python

# Copyright 2024 Canonical Ltd.
# See LICENSE file for licensing details.
#
# Learn more about testing at: https://juju.is/docs/sdk/testing
"""Unit tests."""
import pathlib
import charm
import ops
import ops.testing as testing
import ops_sunbeam.test_utils as test_utils
import yaml
from mock import (
patch,
)
class _SunbeamClusterdCharm(charm.SunbeamClusterdCharm):
"""Clusterd test charm."""
def __init__(self, framework):
"""Setup event logging."""
self.seen_events = []
super().__init__(framework)
charmcraft = (
pathlib.Path(__file__).parents[2] / "charmcraft.yaml"
).read_text()
config = yaml.dump(yaml.safe_load(charmcraft)["config"])
actions = yaml.dump(yaml.safe_load(charmcraft)["actions"])
class TestCharm(test_utils.CharmTestCase):
"""Test the charm."""
PATCHES = ["snap", "clusterd"]
def setUp(self):
"""Setup charm and harness."""
super().setUp(charm, self.PATCHES)
self.harness = testing.Harness(
_SunbeamClusterdCharm,
meta=charmcraft,
config=config,
actions=actions,
)
ensure_snap_present = patch(
"charm.SunbeamClusterdCharm.ensure_snap_present"
)
self.ensure_snap_present = ensure_snap_present.start()
self.addCleanup(ensure_snap_present.stop)
self.addCleanup(self.harness.cleanup)
def initial_setup(self):
"""Common setup code for charm tests."""
self.harness.add_network("10.0.0.10")
self.harness.begin_with_initial_hooks()
def test_initial_bootstrap(self):
"""Test charm is bootstrapped."""
self.initial_setup()
self.harness.set_leader()
self.harness.charm.on.config_changed.emit()
self.assertEqual(self.harness.charm.unit.status, ops.ActiveStatus())
self.ensure_snap_present.assert_called()
self.harness.charm._clusterd.bootstrap.assert_called_once()
def test_initial_bootstrap_no_leader(self):
"""Test charm is bootstrapped."""
self.initial_setup()
self.harness.charm.on.config_changed.emit()
self.assertEqual(
self.harness.charm.unit.status,
ops.WaitingStatus("(workload) Leader not ready"),
)
self.harness.charm._clusterd.bootstrap.assert_not_called()
def test_config(self):
"""Test config update."""
self.initial_setup()
self.harness.set_leader()
self.harness.update_config({"snap-channel": "edge"})
self.ensure_snap_present.assert_called()
def test_get_credentials(self):
"""Test get credentials action."""
self.initial_setup()
self.harness.set_leader()
self.harness.charm.on.config_changed.emit()
output = self.harness.run_action("get-credentials")
self.assertEqual({"url": "https://10.0.0.10:7000"}, output.results)