Add basic unit test

This commit is contained in:
Liam Young 2023-09-24 14:25:43 +00:00
parent 7deda62ef5
commit 12fdf64fe1
2 changed files with 40 additions and 78 deletions

View File

@ -1,75 +0,0 @@
# Copyright 2023 liam
# See LICENSE file for licensing details.
#
# Learn more about testing at: https://juju.is/docs/sdk/testing
import unittest
import ops.testing
from ops.model import ActiveStatus, BlockedStatus, WaitingStatus
from ops.testing import Harness
from charm import KeystoneLdapK8SCharm
class TestCharm(unittest.TestCase):
def setUp(self):
# Enable more accurate simulation of container networking.
# For more information, see https://juju.is/docs/sdk/testing#heading--simulate-can-connect
ops.testing.SIMULATE_CAN_CONNECT = True
self.addCleanup(setattr, ops.testing, "SIMULATE_CAN_CONNECT", False)
self.harness = Harness(KeystoneLdapK8SCharm)
self.addCleanup(self.harness.cleanup)
self.harness.begin()
def test_httpbin_pebble_ready(self):
# Expected plan after Pebble ready with default config
expected_plan = {
"services": {
"httpbin": {
"override": "replace",
"summary": "httpbin",
"command": "gunicorn -b 0.0.0.0:80 httpbin:app -k gevent",
"startup": "enabled",
"environment": {"GUNICORN_CMD_ARGS": "--log-level info"},
}
},
}
# Simulate the container coming up and emission of pebble-ready event
self.harness.container_pebble_ready("httpbin")
# Get the plan now we've run PebbleReady
updated_plan = self.harness.get_container_pebble_plan("httpbin").to_dict()
# Check we've got the plan we expected
self.assertEqual(expected_plan, updated_plan)
# Check the service was started
service = self.harness.model.unit.get_container("httpbin").get_service("httpbin")
self.assertTrue(service.is_running())
# Ensure we set an ActiveStatus with no message
self.assertEqual(self.harness.model.unit.status, ActiveStatus())
def test_config_changed_valid_can_connect(self):
# Ensure the simulated Pebble API is reachable
self.harness.set_can_connect("httpbin", True)
# Trigger a config-changed event with an updated value
self.harness.update_config({"log-level": "debug"})
# Get the plan now we've run PebbleReady
updated_plan = self.harness.get_container_pebble_plan("httpbin").to_dict()
updated_env = updated_plan["services"]["httpbin"]["environment"]
# Check the config change was effective
self.assertEqual(updated_env, {"GUNICORN_CMD_ARGS": "--log-level debug"})
self.assertEqual(self.harness.model.unit.status, ActiveStatus())
def test_config_changed_valid_cannot_connect(self):
# Trigger a config-changed event with an updated value
self.harness.update_config({"log-level": "debug"})
# Check the charm is in WaitingStatus
self.assertIsInstance(self.harness.model.unit.status, WaitingStatus)
def test_config_changed_invalid(self):
# Ensure the simulated Pebble API is reachable
self.harness.set_can_connect("httpbin", True)
# Trigger a config-changed event with an updated value
self.harness.update_config({"log-level": "foobar"})
# Check the charm is in BlockedStatus
self.assertIsInstance(self.harness.model.unit.status, BlockedStatus)

View File

@ -16,12 +16,11 @@
"""Define keystone tests."""
import base64
import json
import os
from unittest.mock import ANY, MagicMock
import mock
import ops_sunbeam.test_utils as test_utils
from ops.testing import Harness
import charm
@ -43,3 +42,41 @@ class _KeystoneLDAPK8SCharm(charm.KeystoneLDAPK8SCharm):
@property
def public_ingress_address(self) -> str:
return "10.0.0.10"
class TestKeystoneLDAPK8SCharm(test_utils.CharmTestCase):
def setUp(self):
"""Run test setup."""
self.harness = Harness(charm.KeystoneLDAPK8SCharm)
self.addCleanup(self.harness.cleanup)
self.harness.begin()
def test_charm(self):
"""Test pebble ready handler."""
self.harness.set_leader()
rel_id = self.harness.add_relation("domain-config", "keystone")
self.harness.add_relation_unit(rel_id, "keystone/0")
rel_data = self.harness.get_relation_data(rel_id, self.harness.charm.unit.app.name)
ldap_config_flags = json.dumps(
{
"group_tree_dn": "ou=groups,dc=test,dc=com",
"group_objectclass": "posixGroup",
"group_name_attribute": "cn",
"group_member_attribute": "memberUid",
"group_members_are_ids": "true",
}
)
self.harness.update_config(
{
"ldap-server": "ldap://10.1.176.184",
"ldap-user": "cn=admin,dc=test,dc=com",
"ldap-password": "crapper",
"ldap-suffix": "dc=test,dc=com",
"domain-name": "userdomain",
"ldap-config-flags": ldap_config_flags,
}
)
self.assertEqual("userdomain", rel_data["domain-name"])
contents = base64.b64decode(rel_data["config-contents"]).decode()
self.assertIn("password = crapper", contents)
self.assertIn("group_objectclass = posixGroup", contents)