Major improvement in OSM charms
[osm/devops.git] / installers / charm / ro / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2020 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
23 import sys
24 from typing import NoReturn
25 import unittest
26 from ops.model import ActiveStatus, BlockedStatus
27 from ops.testing import Harness
28
29 from charm import RoCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(RoCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "enable_ng_ro": True,
43 "database_commonkey": "commonkey",
44 "log_level": "INFO",
45 "vim_database": "db_name",
46 "ro_database": "ro_db_name",
47 "openmano_tenant": "mano",
48 }
49 self.harness.update_config(self.config)
50
51 def test_config_changed_no_relations(
52 self,
53 ) -> NoReturn:
54 """Test ingress resources without HTTP."""
55
56 self.harness.charm.on.config_changed.emit()
57
58 # Assertions
59 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
60 self.assertTrue(
61 all(
62 relation in self.harness.charm.unit.status.message
63 for relation in ["mongodb", "kafka"]
64 )
65 )
66
67 # Disable ng-ro
68 self.harness.update_config({"enable_ng_ro": False})
69 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
70 self.assertTrue(
71 all(
72 relation in self.harness.charm.unit.status.message
73 for relation in ["mysql"]
74 )
75 )
76
77 def test_config_changed_non_leader(
78 self,
79 ) -> NoReturn:
80 """Test ingress resources without HTTP."""
81 self.harness.set_leader(is_leader=False)
82 self.harness.charm.on.config_changed.emit()
83
84 # Assertions
85 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
86
87 def test_with_relations_ng(
88 self,
89 ) -> NoReturn:
90 "Test with relations (ng-ro)"
91
92 # Initializing the kafka relation
93 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
94 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
95 self.harness.update_relation_data(
96 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
97 )
98
99 # Initializing the mongo relation
100 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
101 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
102 self.harness.update_relation_data(
103 mongodb_relation_id,
104 "mongodb/0",
105 {"connection_string": "mongodb://mongo:27017"},
106 )
107
108 # Verifying status
109 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
110
111
112 if __name__ == "__main__":
113 unittest.main()
114
115 # class TestCharm(unittest.TestCase):
116 # """RO Charm unit tests."""
117
118 # def setUp(self) -> NoReturn:
119 # """Test setup"""
120 # self.harness = Harness(RoCharm)
121 # self.harness.set_leader(is_leader=True)
122 # self.harness.begin()
123
124 # def test_on_start_without_relations_ng_ro(self) -> NoReturn:
125 # """Test installation without any relation."""
126 # self.harness.charm.on.start.emit()
127
128 # # Verifying status
129 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
130
131 # # Verifying status message
132 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
133 # self.assertTrue(
134 # self.harness.charm.unit.status.message.startswith("Waiting for ")
135 # )
136 # self.assertIn("kafka", self.harness.charm.unit.status.message)
137 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
138 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
139
140 # def test_on_start_without_relations_no_ng_ro(self) -> NoReturn:
141 # """Test installation without any relation."""
142 # self.harness.update_config({"enable_ng_ro": False})
143
144 # self.harness.charm.on.start.emit()
145
146 # # Verifying status
147 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
148
149 # # Verifying status message
150 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
151 # self.assertTrue(
152 # self.harness.charm.unit.status.message.startswith("Waiting for ")
153 # )
154 # self.assertIn("mysql", self.harness.charm.unit.status.message)
155 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
156
157 # def test_on_start_with_relations_ng_ro(self) -> NoReturn:
158 # """Test deployment with NG-RO."""
159 # expected_result = {
160 # "version": 3,
161 # "containers": [
162 # {
163 # "name": "ro",
164 # "imageDetails": self.harness.charm.image.fetch(),
165 # "imagePullPolicy": "Always",
166 # "ports": [
167 # {
168 # "name": "ro",
169 # "containerPort": 9090,
170 # "protocol": "TCP",
171 # }
172 # ],
173 # "envConfig": {
174 # "OSMRO_LOG_LEVEL": "INFO",
175 # "OSMRO_MESSAGE_DRIVER": "kafka",
176 # "OSMRO_MESSAGE_HOST": "kafka",
177 # "OSMRO_MESSAGE_PORT": "9090",
178 # "OSMRO_DATABASE_DRIVER": "mongo",
179 # "OSMRO_DATABASE_URI": "mongodb://mongo",
180 # "OSMRO_DATABASE_COMMONKEY": "osm",
181 # },
182 # "kubernetes": {
183 # "startupProbe": {
184 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
185 # "initialDelaySeconds": 60,
186 # "timeoutSeconds": 5,
187 # },
188 # "readinessProbe": {
189 # "httpGet": {
190 # "path": "/openmano/tenants",
191 # "port": 9090,
192 # },
193 # "periodSeconds": 10,
194 # "timeoutSeconds": 5,
195 # "successThreshold": 1,
196 # "failureThreshold": 3,
197 # },
198 # "livenessProbe": {
199 # "httpGet": {
200 # "path": "/openmano/tenants",
201 # "port": 9090,
202 # },
203 # "initialDelaySeconds": 600,
204 # "periodSeconds": 10,
205 # "timeoutSeconds": 5,
206 # "successThreshold": 1,
207 # "failureThreshold": 3,
208 # },
209 # },
210 # }
211 # ],
212 # "kubernetesResources": {"ingressResources": []},
213 # }
214
215 # self.harness.charm.on.start.emit()
216
217 # # Initializing the kafka relation
218 # relation_id = self.harness.add_relation("kafka", "kafka")
219 # self.harness.add_relation_unit(relation_id, "kafka/0")
220 # self.harness.update_relation_data(
221 # relation_id,
222 # "kafka/0",
223 # {
224 # "host": "kafka",
225 # "port": "9090",
226 # },
227 # )
228
229 # # Initializing the mongodb relation
230 # relation_id = self.harness.add_relation("mongodb", "mongodb")
231 # self.harness.add_relation_unit(relation_id, "mongodb/0")
232 # self.harness.update_relation_data(
233 # relation_id,
234 # "mongodb/0",
235 # {
236 # "connection_string": "mongodb://mongo",
237 # },
238 # )
239
240 # # Verifying status
241 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
242
243 # pod_spec, _ = self.harness.get_pod_spec()
244
245 # self.assertDictEqual(expected_result, pod_spec)
246
247 # def test_on_start_with_relations_no_ng_ro(self) -> NoReturn:
248 # """Test deployment with old RO."""
249 # self.harness.update_config({"enable_ng_ro": False})
250
251 # expected_result = {
252 # "version": 3,
253 # "containers": [
254 # {
255 # "name": "ro",
256 # "imageDetails": self.harness.charm.image.fetch(),
257 # "imagePullPolicy": "Always",
258 # "ports": [
259 # {
260 # "name": "ro",
261 # "containerPort": 9090,
262 # "protocol": "TCP",
263 # }
264 # ],
265 # "envConfig": {
266 # "OSMRO_LOG_LEVEL": "INFO",
267 # "RO_DB_HOST": "mysql",
268 # "RO_DB_OVIM_HOST": "mysql",
269 # "RO_DB_PORT": 3306,
270 # "RO_DB_OVIM_PORT": 3306,
271 # "RO_DB_USER": "mano",
272 # "RO_DB_OVIM_USER": "mano",
273 # "RO_DB_PASSWORD": "manopw",
274 # "RO_DB_OVIM_PASSWORD": "manopw",
275 # "RO_DB_ROOT_PASSWORD": "rootmanopw",
276 # "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
277 # "RO_DB_NAME": "mano_db",
278 # "RO_DB_OVIM_NAME": "mano_vim_db",
279 # "OPENMANO_TENANT": "osm",
280 # },
281 # "kubernetes": {
282 # "startupProbe": {
283 # "exec": {"command": ["/usr/bin/pgrep", "python3"]},
284 # "initialDelaySeconds": 60,
285 # "timeoutSeconds": 5,
286 # },
287 # "readinessProbe": {
288 # "httpGet": {
289 # "path": "/openmano/tenants",
290 # "port": 9090,
291 # },
292 # "periodSeconds": 10,
293 # "timeoutSeconds": 5,
294 # "successThreshold": 1,
295 # "failureThreshold": 3,
296 # },
297 # "livenessProbe": {
298 # "httpGet": {
299 # "path": "/openmano/tenants",
300 # "port": 9090,
301 # },
302 # "initialDelaySeconds": 600,
303 # "periodSeconds": 10,
304 # "timeoutSeconds": 5,
305 # "successThreshold": 1,
306 # "failureThreshold": 3,
307 # },
308 # },
309 # }
310 # ],
311 # "kubernetesResources": {"ingressResources": []},
312 # }
313
314 # self.harness.charm.on.start.emit()
315
316 # # Initializing the mysql relation
317 # relation_id = self.harness.add_relation("mysql", "mysql")
318 # self.harness.add_relation_unit(relation_id, "mysql/0")
319 # self.harness.update_relation_data(
320 # relation_id,
321 # "mysql/0",
322 # {
323 # "host": "mysql",
324 # "port": 3306,
325 # "user": "mano",
326 # "password": "manopw",
327 # "root_password": "rootmanopw",
328 # },
329 # )
330
331 # # Verifying status
332 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
333
334 # pod_spec, _ = self.harness.get_pod_spec()
335
336 # self.assertDictEqual(expected_result, pod_spec)
337
338 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
339 # """Test to see if kafka relation is updated."""
340 # self.harness.charm.on.start.emit()
341
342 # relation_id = self.harness.add_relation("kafka", "kafka")
343 # self.harness.add_relation_unit(relation_id, "kafka/0")
344 # self.harness.update_relation_data(
345 # relation_id,
346 # "kafka/0",
347 # {
348 # "host": "kafka",
349 # "port": 9090,
350 # },
351 # )
352
353 # # Verifying status
354 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
355
356 # # Verifying status message
357 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
358 # self.assertTrue(
359 # self.harness.charm.unit.status.message.startswith("Waiting for ")
360 # )
361 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
362 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
363
364 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
365 # """Test to see if mongodb relation is updated."""
366 # self.harness.charm.on.start.emit()
367
368 # relation_id = self.harness.add_relation("mongodb", "mongodb")
369 # self.harness.add_relation_unit(relation_id, "mongodb/0")
370 # self.harness.update_relation_data(
371 # relation_id,
372 # "mongodb/0",
373 # {
374 # "connection_string": "mongodb://mongo",
375 # },
376 # )
377
378 # # Verifying status
379 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
380
381 # # Verifying status message
382 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
383 # self.assertTrue(
384 # self.harness.charm.unit.status.message.startswith("Waiting for ")
385 # )
386 # self.assertIn("kafka", self.harness.charm.unit.status.message)
387 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
388
389 # def test_on_mysql_unit_relation_changed(self) -> NoReturn:
390 # """Test to see if mysql relation is updated."""
391 # self.harness.charm.on.start.emit()
392
393 # relation_id = self.harness.add_relation("mysql", "mysql")
394 # self.harness.add_relation_unit(relation_id, "mysql/0")
395 # self.harness.update_relation_data(
396 # relation_id,
397 # "mysql/0",
398 # {
399 # "host": "mysql",
400 # "port": 3306,
401 # "user": "mano",
402 # "password": "manopw",
403 # "root_password": "rootmanopw",
404 # },
405 # )
406
407 # # Verifying status
408 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
409
410 # # Verifying status message
411 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
412 # self.assertTrue(
413 # self.harness.charm.unit.status.message.startswith("Waiting for ")
414 # )
415 # self.assertIn("kafka", self.harness.charm.unit.status.message)
416 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
417 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
418
419 # def test_publish_ro_info(self) -> NoReturn:
420 # """Test to see if ro relation is updated."""
421 # expected_result = {
422 # "host": "ro",
423 # "port": "9090",
424 # }
425
426 # self.harness.charm.on.start.emit()
427
428 # relation_id = self.harness.add_relation("ro", "lcm")
429 # self.harness.add_relation_unit(relation_id, "lcm/0")
430 # relation_data = self.harness.get_relation_data(relation_id, "ro")
431
432 # self.assertDictEqual(expected_result, relation_data)
433
434
435 if __name__ == "__main__":
436 unittest.main()