Add POL side-car charm
[osm/devops.git] / installers / charm / osm-pol / 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, OsmPolCharm, check_service_active
30
31 container_name = "pol"
32 service_name = "pol"
33
34
35 @pytest.fixture
36 def harness(mocker: MockerFixture):
37 harness = Harness(OsmPolCharm)
38 harness.begin()
39 yield harness
40 harness.cleanup()
41
42
43 def test_missing_relations(harness: Harness):
44 harness.charm.on.config_changed.emit()
45 assert type(harness.charm.unit.status) == BlockedStatus
46 assert all(
47 relation in harness.charm.unit.status.message for relation in ["mongodb", "kafka", "mysql"]
48 )
49
50
51 def test_ready(harness: Harness):
52 _add_relations(harness)
53 assert harness.charm.unit.status == ActiveStatus()
54
55
56 def test_container_stops_after_relation_broken(harness: Harness):
57 harness.charm.on[container_name].pebble_ready.emit(container_name)
58 container = harness.charm.unit.get_container(container_name)
59 relation_ids = _add_relations(harness)
60 check_service_active(container, service_name)
61 harness.remove_relation(relation_ids[0])
62 with pytest.raises(CharmError):
63 check_service_active(container, service_name)
64
65
66 def _add_relations(harness: Harness):
67 relation_ids = []
68 # Add mongo relation
69 relation_id = harness.add_relation("mongodb", "mongodb")
70 harness.add_relation_unit(relation_id, "mongodb/0")
71 harness.update_relation_data(
72 relation_id, "mongodb/0", {"connection_string": "mongodb://:1234"}
73 )
74 relation_ids.append(relation_id)
75 # Add kafka relation
76 relation_id = harness.add_relation("kafka", "kafka")
77 harness.add_relation_unit(relation_id, "kafka/0")
78 harness.update_relation_data(relation_id, "kafka", {"host": "kafka", "port": 9092})
79 relation_ids.append(relation_id)
80 # Add mysql relation
81 relation_id = harness.add_relation("mysql", "mysql")
82 harness.add_relation_unit(relation_id, "mysql/0")
83 harness.update_relation_data(
84 relation_id,
85 "mysql/0",
86 {
87 "host": "mysql",
88 "port": 3306,
89 "user": "mano",
90 "password": "manopw",
91 "root_password": "rootmanopw",
92 },
93 )
94 relation_ids.append(relation_id)
95 return relation_ids