Ahmad Hassan b3387c4ff1 [openstack-hypervisor] error out on snap installation failure
When the openstack-hypervisor snap fails to install, then set
the unit status to error instead of blocked.

Closes-Bug: #2065944.

Change-Id: I0b3817c8e8321e5bfd8eaabe63646c9cd48cdf36
2025-01-28 11:21:39 +05:00

108 lines
3.3 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
from unittest.mock import (
patch,
)
import charm
import ops
import ops.testing as testing
import ops_sunbeam.test_utils as test_utils
import yaml
from charms.operator_libs_linux.v2 import (
snap,
)
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_openstack_snap_not_installed(self):
"""Check action raises SnapNotFoundError if openstack snap is not installed."""
self.harness.begin()
self.ensure_snap_present.side_effect = snap.SnapNotFoundError
with self.assertRaises(snap.SnapNotFoundError):
self.harness.charm.on.install.emit()
def test_initial_bootstrap(self):
"""Test charm is bootstrapped."""
self.initial_setup()
self.harness.set_leader()
self.harness.charm.on.config_changed.emit()
self.harness.evaluate_status()
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)