2d16cd84a7a6aaabda69d93451881cd77ec7c91e
[osm/devops.git] / installers / charm / osm-mon / 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, OsmMonCharm, check_service_active
30
31 container_name = "mon"
32 service_name = "mon"
33 url_configs = ["grafana-url", "prometheus-url"]
34
35
36 @pytest.fixture
37 def harness(mocker: MockerFixture):
38 mocker.patch("charm.KubernetesServicePatch", lambda x, y: None)
39 harness = Harness(OsmMonCharm)
40 harness.begin()
41 harness.container_pebble_ready(container_name)
42 yield harness
43 harness.cleanup()
44
45
46 def _set_mandatory_config(harness: Harness):
47 harness.update_config(
48 {
49 "grafana-url": "http://grafana:1234",
50 "grafana-user": "user",
51 "grafana-password": "password",
52 "prometheus-url": "http://someurl",
53 }
54 )
55
56
57 def test_default_config_is_invalid_charm_is_blocked(harness: Harness):
58 harness.charm.on.config_changed.emit()
59 assert isinstance(harness.charm.unit.status, BlockedStatus)
60 assert (
61 "need grafana-url, grafana-user, grafana-password, prometheus-url config"
62 == harness.charm.unit.status.message
63 )
64
65
66 def test_missing_relations(harness: Harness):
67 _set_mandatory_config(harness)
68 harness.charm.on.config_changed.emit()
69 assert isinstance(harness.charm.unit.status, BlockedStatus)
70 assert all(
71 relation in harness.charm.unit.status.message
72 for relation in ["mongodb", "kafka", "prometheus", "keystone"]
73 )
74
75
76 @pytest.mark.parametrize("config_param", url_configs)
77 def test_url_config_without_schema_block_status(harness: Harness, config_param):
78 _set_mandatory_config(harness)
79 _add_relations(harness)
80 harness.update_config({config_param: "foo.com"})
81 assert isinstance(harness.charm.unit.status, BlockedStatus)
82 assert (
83 f"Invalid value for {config_param} config: 'foo.com'" in harness.charm.unit.status.message
84 )
85
86
87 @pytest.mark.parametrize("config_param", url_configs)
88 def test_url_config_with_port_without_schema_block_status(harness: Harness, config_param):
89 _set_mandatory_config(harness)
90 _add_relations(harness)
91 harness.update_config({config_param: "foo.com:9090"})
92 assert isinstance(harness.charm.unit.status, BlockedStatus)
93 assert (
94 f"Invalid value for {config_param} config: 'foo.com:9090'"
95 in harness.charm.unit.status.message
96 )
97
98
99 @pytest.mark.parametrize("config_param", url_configs)
100 def test_url_config_without_port_is_valid(harness: Harness, config_param):
101 _set_mandatory_config(harness)
102 _add_relations(harness)
103 harness.update_config({config_param: "http://foo"})
104 assert harness.charm.unit.status == ActiveStatus()
105
106
107 @pytest.mark.parametrize("config_param", url_configs)
108 def test_url_config_with_port_is_valid(harness: Harness, config_param):
109 _set_mandatory_config(harness)
110 _add_relations(harness)
111 harness.update_config({config_param: "http://foo:90"})
112 assert harness.charm.unit.status == ActiveStatus()
113
114
115 def test_ready(harness: Harness):
116 _set_mandatory_config(harness)
117 _add_relations(harness)
118 assert harness.charm.unit.status == ActiveStatus()
119
120
121 def test_container_stops_after_relation_broken(harness: Harness):
122 _set_mandatory_config(harness)
123 harness.charm.on[container_name].pebble_ready.emit(container_name)
124 container = harness.charm.unit.get_container(container_name)
125 relation_ids = _add_relations(harness)
126 check_service_active(container, service_name)
127 harness.remove_relation(relation_ids[0])
128 with pytest.raises(CharmError):
129 check_service_active(container, service_name)
130
131
132 def _add_relations(harness: Harness):
133 relation_ids = []
134 # Add mongo relation
135 relation_id = harness.add_relation("mongodb", "mongodb")
136 harness.add_relation_unit(relation_id, "mongodb/0")
137 harness.update_relation_data(
138 relation_id,
139 "mongodb",
140 {"uris": "mongodb://:1234", "username": "user", "password": "password"},
141 )
142 relation_ids.append(relation_id)
143 # Add kafka relation
144 relation_id = harness.add_relation("kafka", "kafka")
145 harness.add_relation_unit(relation_id, "kafka/0")
146 harness.update_relation_data(relation_id, "kafka", {"host": "kafka", "port": "9092"})
147 relation_ids.append(relation_id)
148 # Add prometheus relation
149 relation_id = harness.add_relation("metrics-endpoint", "prometheus")
150 relation_ids.append(relation_id)
151 # Add keystone relation
152 relation_id = harness.add_relation("keystone", "keystone")
153 harness.add_relation_unit(relation_id, "keystone/0")
154 harness.update_relation_data(
155 relation_id,
156 "keystone",
157 {
158 "host": "host",
159 "port": "port",
160 "user_domain_name": "user_domain_name",
161 "project_domain_name": "project_domain_name",
162 "username": "username",
163 "password": "password",
164 "service": "service",
165 "keystone_db_password": "keystone_db_password",
166 "region_id": "region_id",
167 "admin_username": "admin_username",
168 "admin_password": "admin_password",
169 "admin_project_name": "admin_project_name",
170 },
171 )
172 relation_ids.append(relation_id)
173 return relation_ids