Refactoring RO Charm to use Operator Framework
[osm/devops.git] / installers / charm / ro / tests / test_pod_spec.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 from typing import NoReturn
24 import unittest
25
26 import pod_spec
27
28
29 class TestPodSpec(unittest.TestCase):
30 """Pod spec unit tests."""
31
32 def test_make_pod_ports(self) -> NoReturn:
33 """Testing make pod ports."""
34 port = 9090
35
36 expected_result = [
37 {
38 "name": "ro",
39 "containerPort": port,
40 "protocol": "TCP",
41 }
42 ]
43
44 pod_ports = pod_spec._make_pod_ports(port)
45
46 self.assertListEqual(expected_result, pod_ports)
47
48 def test_make_pod_envconfig_ng_ro(self) -> NoReturn:
49 """Teting make pod envconfig."""
50 config = {
51 "enable_ng_ro": True,
52 "database_commonkey": "osm",
53 "log_level": "INFO",
54 }
55 relation_state = {
56 "kafka_host": "kafka",
57 "kafka_port": 9090,
58 "mongodb_connection_string": "mongodb://mongo",
59 }
60
61 expected_result = {
62 "OSMRO_LOG_LEVEL": config["log_level"],
63 "OSMRO_MESSAGE_DRIVER": "kafka",
64 "OSMRO_MESSAGE_HOST": relation_state["kafka_host"],
65 "OSMRO_MESSAGE_PORT": relation_state["kafka_port"],
66 "OSMRO_DATABASE_DRIVER": "mongo",
67 "OSMRO_DATABASE_URI": relation_state["mongodb_connection_string"],
68 "OSMRO_DATABASE_COMMONKEY": config["database_commonkey"],
69 }
70
71 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
72
73 self.assertDictEqual(expected_result, pod_envconfig)
74
75 def test_make_pod_envconfig_no_ng_ro(self) -> NoReturn:
76 """Teting make pod envconfig."""
77 config = {
78 "log_level": "INFO",
79 "enable_ng_ro": False,
80 "vim_database": "mano_vim_db",
81 "ro_database": "mano_db",
82 "openmano_tenant": "osm",
83 }
84 relation_state = {
85 "mysql_host": "mysql",
86 "mysql_port": 3306,
87 "mysql_user": "mano",
88 "mysql_password": "manopw",
89 "mysql_root_password": "rootmanopw",
90 }
91
92 expected_result = {
93 "OSMRO_LOG_LEVEL": config["log_level"],
94 "RO_DB_HOST": relation_state["mysql_host"],
95 "RO_DB_OVIM_HOST": relation_state["mysql_host"],
96 "RO_DB_PORT": relation_state["mysql_port"],
97 "RO_DB_OVIM_PORT": relation_state["mysql_port"],
98 "RO_DB_USER": relation_state["mysql_user"],
99 "RO_DB_OVIM_USER": relation_state["mysql_user"],
100 "RO_DB_PASSWORD": relation_state["mysql_password"],
101 "RO_DB_OVIM_PASSWORD": relation_state["mysql_password"],
102 "RO_DB_ROOT_PASSWORD": relation_state["mysql_root_password"],
103 "RO_DB_OVIM_ROOT_PASSWORD": relation_state["mysql_root_password"],
104 "RO_DB_NAME": config["ro_database"],
105 "RO_DB_OVIM_NAME": config["vim_database"],
106 "OPENMANO_TENANT": config["openmano_tenant"],
107 }
108
109 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
110
111 self.assertDictEqual(expected_result, pod_envconfig)
112
113 def test_make_startup_probe(self) -> NoReturn:
114 """Testing make startup probe."""
115 expected_result = {
116 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
117 "initialDelaySeconds": 60,
118 "timeoutSeconds": 5,
119 }
120
121 startup_probe = pod_spec._make_startup_probe()
122
123 self.assertDictEqual(expected_result, startup_probe)
124
125 def test_make_readiness_probe(self) -> NoReturn:
126 """Testing make readiness probe."""
127 port = 9090
128
129 expected_result = {
130 "httpGet": {
131 "path": "/openmano/tenants",
132 "port": port,
133 },
134 "periodSeconds": 10,
135 "timeoutSeconds": 5,
136 "successThreshold": 1,
137 "failureThreshold": 3,
138 }
139
140 readiness_probe = pod_spec._make_readiness_probe(port)
141
142 self.assertDictEqual(expected_result, readiness_probe)
143
144 def test_make_liveness_probe(self) -> NoReturn:
145 """Testing make liveness probe."""
146 port = 9090
147
148 expected_result = {
149 "httpGet": {
150 "path": "/openmano/tenants",
151 "port": port,
152 },
153 "initialDelaySeconds": 600,
154 "periodSeconds": 10,
155 "timeoutSeconds": 5,
156 "successThreshold": 1,
157 "failureThreshold": 3,
158 }
159
160 liveness_probe = pod_spec._make_liveness_probe(port)
161
162 self.assertDictEqual(expected_result, liveness_probe)
163
164 def test_make_pod_spec_ng_ro(self) -> NoReturn:
165 """Testing make pod spec."""
166 image_info = {"upstream-source": "opensourcemano/ro:8"}
167 config = {
168 "database_commonkey": "osm",
169 "log_level": "INFO",
170 "enable_ng_ro": True,
171 }
172 relation_state = {
173 "kafka_host": "kafka",
174 "kafka_port": 9090,
175 "mongodb_connection_string": "mongodb://mongo",
176 }
177 app_name = "ro"
178 port = 9090
179
180 expected_result = {
181 "version": 3,
182 "containers": [
183 {
184 "name": app_name,
185 "imageDetails": image_info,
186 "imagePullPolicy": "Always",
187 "ports": [
188 {
189 "name": app_name,
190 "containerPort": port,
191 "protocol": "TCP",
192 }
193 ],
194 "envConfig": {
195 "OSMRO_LOG_LEVEL": config["log_level"],
196 "OSMRO_MESSAGE_DRIVER": "kafka",
197 "OSMRO_MESSAGE_HOST": relation_state["kafka_host"],
198 "OSMRO_MESSAGE_PORT": relation_state["kafka_port"],
199 "OSMRO_DATABASE_DRIVER": "mongo",
200 "OSMRO_DATABASE_URI": relation_state[
201 "mongodb_connection_string"
202 ],
203 "OSMRO_DATABASE_COMMONKEY": config["database_commonkey"],
204 },
205 "kubernetes": {
206 "startupProbe": {
207 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
208 "initialDelaySeconds": 60,
209 "timeoutSeconds": 5,
210 },
211 "readinessProbe": {
212 "httpGet": {
213 "path": "/openmano/tenants",
214 "port": port,
215 },
216 "periodSeconds": 10,
217 "timeoutSeconds": 5,
218 "successThreshold": 1,
219 "failureThreshold": 3,
220 },
221 "livenessProbe": {
222 "httpGet": {
223 "path": "/openmano/tenants",
224 "port": port,
225 },
226 "initialDelaySeconds": 600,
227 "periodSeconds": 10,
228 "timeoutSeconds": 5,
229 "successThreshold": 1,
230 "failureThreshold": 3,
231 },
232 },
233 }
234 ],
235 "kubernetesResources": {"ingressResources": []},
236 }
237
238 spec = pod_spec.make_pod_spec(
239 image_info, config, relation_state, app_name, port
240 )
241
242 self.assertDictEqual(expected_result, spec)
243
244 def test_make_pod_spec_no_ng_ro(self) -> NoReturn:
245 """Testing make pod spec."""
246 image_info = {"upstream-source": "opensourcemano/ro:8"}
247 config = {
248 "log_level": "INFO",
249 "enable_ng_ro": False,
250 "vim_database": "mano_vim_db",
251 "ro_database": "mano_db",
252 "openmano_tenant": "osm",
253 }
254 relation_state = {
255 "mysql_host": "mysql",
256 "mysql_port": 3306,
257 "mysql_user": "mano",
258 "mysql_password": "manopw",
259 "mysql_root_password": "rootmanopw",
260 }
261 app_name = "ro"
262 port = 9090
263
264 expected_result = {
265 "version": 3,
266 "containers": [
267 {
268 "name": app_name,
269 "imageDetails": image_info,
270 "imagePullPolicy": "Always",
271 "ports": [
272 {
273 "name": app_name,
274 "containerPort": port,
275 "protocol": "TCP",
276 }
277 ],
278 "envConfig": {
279 "OSMRO_LOG_LEVEL": config["log_level"],
280 "RO_DB_HOST": relation_state["mysql_host"],
281 "RO_DB_OVIM_HOST": relation_state["mysql_host"],
282 "RO_DB_PORT": relation_state["mysql_port"],
283 "RO_DB_OVIM_PORT": relation_state["mysql_port"],
284 "RO_DB_USER": relation_state["mysql_user"],
285 "RO_DB_OVIM_USER": relation_state["mysql_user"],
286 "RO_DB_PASSWORD": relation_state["mysql_password"],
287 "RO_DB_OVIM_PASSWORD": relation_state["mysql_password"],
288 "RO_DB_ROOT_PASSWORD": relation_state["mysql_root_password"],
289 "RO_DB_OVIM_ROOT_PASSWORD": relation_state[
290 "mysql_root_password"
291 ],
292 "RO_DB_NAME": config["ro_database"],
293 "RO_DB_OVIM_NAME": config["vim_database"],
294 "OPENMANO_TENANT": config["openmano_tenant"],
295 },
296 "kubernetes": {
297 "startupProbe": {
298 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
299 "initialDelaySeconds": 60,
300 "timeoutSeconds": 5,
301 },
302 "readinessProbe": {
303 "httpGet": {
304 "path": "/openmano/tenants",
305 "port": port,
306 },
307 "periodSeconds": 10,
308 "timeoutSeconds": 5,
309 "successThreshold": 1,
310 "failureThreshold": 3,
311 },
312 "livenessProbe": {
313 "httpGet": {
314 "path": "/openmano/tenants",
315 "port": port,
316 },
317 "initialDelaySeconds": 600,
318 "periodSeconds": 10,
319 "timeoutSeconds": 5,
320 "successThreshold": 1,
321 "failureThreshold": 3,
322 },
323 },
324 }
325 ],
326 "kubernetesResources": {"ingressResources": []},
327 }
328
329 spec = pod_spec.make_pod_spec(
330 image_info, config, relation_state, app_name, port
331 )
332
333 self.assertDictEqual(expected_result, spec)
334
335 def test_make_pod_spec_without_image_info(self) -> NoReturn:
336 """Testing make pod spec without image_info."""
337 image_info = None
338 config = {
339 "enable_ng_ro": True,
340 "database_commonkey": "osm",
341 "log_level": "INFO",
342 }
343 relation_state = {
344 "kafka_host": "kafka",
345 "kafka_port": 9090,
346 "mongodb_connection_string": "mongodb://mongo",
347 }
348 app_name = "ro"
349 port = 9090
350
351 spec = pod_spec.make_pod_spec(
352 image_info, config, relation_state, app_name, port
353 )
354
355 self.assertIsNone(spec)
356
357 def test_make_pod_spec_without_config(self) -> NoReturn:
358 """Testing make pod spec without config."""
359 image_info = {"upstream-source": "opensourcemano/ro:8"}
360 config = {}
361 relation_state = {
362 "kafka_host": "kafka",
363 "kafka_port": 9090,
364 "mongodb_connection_string": "mongodb://mongo",
365 }
366 app_name = "ro"
367 port = 9090
368
369 with self.assertRaises(ValueError):
370 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
371
372 def test_make_pod_spec_without_relation_state(self) -> NoReturn:
373 """Testing make pod spec without relation_state."""
374 image_info = {"upstream-source": "opensourcemano/ro:8"}
375 config = {
376 "enable_ng_ro": True,
377 "database_commonkey": "osm",
378 "log_level": "INFO",
379 }
380 relation_state = {}
381 app_name = "ro"
382 port = 9090
383
384 with self.assertRaises(ValueError):
385 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
386
387
388 if __name__ == "__main__":
389 unittest.main()