sunbeam-charms/charms/horizon-k8s/tests/unit/test_dashboard_charm.py
James Page be89e68454 actions: retrieve dashboard URL
Allow sunbeam to retrieve the public ingress URL for the
dashboard to avoid the user having to use juju commands
to interrogate the model directly.

Change-Id: Ia35f66ecfd282c70f329d955413fa26f464a08a2
2023-05-22 14:35:55 +01:00

121 lines
3.8 KiB
Python

#!/usr/bin/env python3
# Copyright 2021 Canonical Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for Horizon operator."""
import mock
import ops_sunbeam.test_utils as test_utils
import charm
class _DashboardOperatorCharm(charm.OpenstackDashboardOperatorCharm):
"""Test Operator Charm for Horizon Operator."""
def __init__(self, framework):
self.seen_events = []
super().__init__(framework)
def _log_event(self, event):
self.seen_events.append(type(event).__name__)
def configure_charm(self, event):
super().configure_charm(event)
self._log_event(event)
@property
def public_ingress_address(self):
return "dashboard.juju"
class TestDashboardOperatorCharm(test_utils.CharmTestCase):
"""Unit tests for Horizon Operator."""
PATCHES = []
@mock.patch(
"charms.observability_libs.v1.kubernetes_service_patch."
"KubernetesServicePatch"
)
def setUp(self, mock_patch):
"""Setup environment for unit test."""
super().setUp(charm, self.PATCHES)
self.harness = test_utils.get_harness(
_DashboardOperatorCharm, container_calls=self.container_calls
)
# clean up events that were dynamically defined,
# otherwise we get issues because they'll be redefined,
# which is not allowed.
from charms.data_platform_libs.v0.database_requires import (
DatabaseEvents,
)
for attr in (
"database_database_created",
"database_endpoints_changed",
"database_read_only_endpoints_changed",
):
try:
delattr(DatabaseEvents, attr)
except AttributeError:
pass
self.addCleanup(self.harness.cleanup)
self.harness.begin()
def test_pebble_ready_handler(self):
"""Test pebble ready handler."""
self.assertEqual(self.harness.charm.seen_events, [])
test_utils.set_all_pebbles_ready(self.harness)
self.assertEqual(self.harness.charm.seen_events, ["PebbleReadyEvent"])
def test_all_relations(self):
"""Test all integrations for Operator."""
self.harness.set_leader()
test_utils.set_all_pebbles_ready(self.harness)
test_utils.add_all_relations(self.harness)
test_utils.add_complete_ingress_relation(self.harness)
setup_cmds = [
["a2dissite", "000-default"],
["a2ensite", "wsgi-openstack-dashboard"],
[
"python3",
"/usr/share/openstack-dashboard/manage.py",
"migrate",
"--noinput",
],
]
for cmd in setup_cmds:
self.assertIn(
cmd, self.container_calls.execute["openstack-dashboard"]
)
self.check_file(
"openstack-dashboard",
"/etc/apache2/sites-available/wsgi-openstack-dashboard.conf",
)
self.check_file(
"openstack-dashboard", "/etc/openstack-dashboard/local_settings.py"
)
def test_get_dashboard_url_action(self):
"""Test admin account action."""
action_event = mock.MagicMock()
self.harness.charm._get_dashboard_url_action(action_event)
action_event.set_results.assert_called_with(
{"url": "http://dashboard.juju:80"}
)