Add RO sidecar charm
[osm/devops.git] / installers / charm / osm-ro / tests / unit / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2022 Canonical Ltd.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 #
22 # Learn more about testing at: https://juju.is/docs/sdk/testing
23
24 import pytest
25 from ops.model import ActiveStatus, BlockedStatus
26 from ops.testing import Harness
27 from pytest_mock import MockerFixture
28
29 from charm import CharmError, OsmRoCharm, check_service_active
30
31 container_name = "ro"
32 service_name = "ro"
33
34
35 @pytest.fixture
36 def harness(mocker: MockerFixture):
37 mocker.patch("charm.KubernetesServicePatch", lambda x, y: None)
38 harness = Harness(OsmRoCharm)
39 harness.begin()
40 yield harness
41 harness.cleanup()
42
43
44 def test_missing_relations(harness: Harness):
45 harness.charm.on.config_changed.emit()
46 assert type(harness.charm.unit.status) == BlockedStatus
47 assert all(relation in harness.charm.unit.status.message for relation in ["mongodb", "kafka"])
48
49
50 def test_ready(harness: Harness):
51 _add_relations(harness)
52 assert harness.charm.unit.status == ActiveStatus()
53
54
55 def test_container_stops_after_relation_broken(harness: Harness):
56 harness.charm.on[container_name].pebble_ready.emit(container_name)
57 container = harness.charm.unit.get_container(container_name)
58 relation_ids = _add_relations(harness)
59 check_service_active(container, service_name)
60 harness.remove_relation(relation_ids[0])
61 with pytest.raises(CharmError):
62 check_service_active(container, service_name)
63
64
65 def test_ro_relation_joined(harness: Harness):
66 harness.set_leader(True)
67 _add_relations(harness)
68 relation_id = harness.add_relation("ro", "lcm")
69 harness.add_relation_unit(relation_id, "lcm/0")
70 relation_data = harness.get_relation_data(relation_id, harness.charm.app.name)
71 assert harness.charm.unit.status == ActiveStatus()
72 assert relation_data == {"host": harness.charm.app.name, "port": "9090"}
73
74
75 def test_certificates(harness: Harness):
76 # aGVsbG8K: "hello\n"
77 # aGVsbG8gYWdhaW4K: "hello again\n"
78 _add_relations(harness)
79 harness.update_config({"certificates": "cert1:aGVsbG8K,cert2:aGVsbG8gYWdhaW4K"})
80 for cert_name, content in {"cert1": "hello\n", "cert2": "hello again\n"}.items():
81 assert harness.charm.container.exists(f"/certs/{cert_name}")
82 assert harness.charm.container.pull(f"/certs/{cert_name}").read() == content
83
84
85 def _add_relations(harness: Harness):
86 relation_ids = []
87 # Add mongo relation
88 relation_id = harness.add_relation("mongodb", "mongodb")
89 harness.add_relation_unit(relation_id, "mongodb/0")
90 harness.update_relation_data(
91 relation_id, "mongodb/0", {"connection_string": "mongodb://:1234"}
92 )
93 relation_ids.append(relation_id)
94 # Add kafka relation
95 relation_id = harness.add_relation("kafka", "kafka")
96 harness.add_relation_unit(relation_id, "kafka/0")
97 harness.update_relation_data(relation_id, "kafka", {"host": "kafka", "port": "9092"})
98 relation_ids.append(relation_id)
99 return relation_ids