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