Refactoring MON Charm to use Operator Framework
[osm/devops.git] / installers / charm / mon / 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 pydantic import ValidationError
24 from typing import NoReturn
25 import unittest
26
27 import pod_spec
28
29
30 class TestPodSpec(unittest.TestCase):
31 """Pod spec unit tests."""
32
33 def test_make_pod_ports(self) -> NoReturn:
34 """Testing make pod ports."""
35 port = 8000
36
37 expected_result = [
38 {
39 "name": "mon",
40 "containerPort": port,
41 "protocol": "TCP",
42 }
43 ]
44
45 pod_ports = pod_spec._make_pod_ports(port)
46
47 self.assertListEqual(expected_result, pod_ports)
48
49 def test_make_pod_envconfig(self) -> NoReturn:
50 """Testing make pod envconfig."""
51 config = {
52 "openstack_default_granularity": 300,
53 "global_request_timeout": 10,
54 "log_level": "INFO",
55 "database_commonkey": "osm",
56 "collector_interval": 30,
57 "evaluator_interval": 30,
58 "vca_host": "admin",
59 "vca_user": "admin",
60 "vca_password": "secret",
61 "vca_cacert": "",
62 }
63 relation_state = {
64 "message_host": "kafka",
65 "message_port": 9090,
66 "database_uri": "mongodb://mongo",
67 "prometheus_host": "prometheus",
68 "prometheus_port": 9082,
69 }
70
71 expected_result = {
72 "ALLOW_ANONYMOUS_LOGIN": "yes",
73 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config[
74 "openstack_default_granularity"
75 ],
76 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config["global_request_timeout"],
77 "OSMMON_GLOBAL_LOGLEVEL": config["log_level"],
78 "OSMMON_COLLECTOR_INTERVAL": config["collector_interval"],
79 "OSMMON_EVALUATOR_INTERVAL": config["evaluator_interval"],
80 "OSMMON_MESSAGE_DRIVER": "kafka",
81 "OSMMON_MESSAGE_HOST": relation_state["message_host"],
82 "OSMMON_MESSAGE_PORT": relation_state["message_port"],
83 "OSMMON_DATABASE_DRIVER": "mongo",
84 "OSMMON_DATABASE_URI": relation_state["database_uri"],
85 "OSMMON_DATABASE_COMMONKEY": config["database_commonkey"],
86 "OSMMON_PROMETHEUS_URL": f"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}",
87 "OSMMON_VCA_HOST": config["vca_host"],
88 "OSMMON_VCA_USER": config["vca_user"],
89 "OSMMON_VCA_SECRET": config["vca_password"],
90 "OSMMON_VCA_CACERT": config["vca_cacert"],
91 }
92
93 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
94
95 self.assertDictEqual(expected_result, pod_envconfig)
96
97 def test_make_startup_probe(self) -> NoReturn:
98 """Testing make startup probe."""
99 expected_result = {
100 "exec": {"command": ["/usr/bin/pgrep python3"]},
101 "initialDelaySeconds": 60,
102 "timeoutSeconds": 5,
103 }
104
105 startup_probe = pod_spec._make_startup_probe()
106
107 self.assertDictEqual(expected_result, startup_probe)
108
109 def test_make_readiness_probe(self) -> NoReturn:
110 """Testing make readiness probe."""
111 port = 8000
112
113 expected_result = {
114 "tcpSocket": {
115 "port": port,
116 },
117 "periodSeconds": 10,
118 "timeoutSeconds": 5,
119 "successThreshold": 1,
120 "failureThreshold": 3,
121 }
122
123 readiness_probe = pod_spec._make_readiness_probe(port)
124
125 self.assertDictEqual(expected_result, readiness_probe)
126
127 def test_make_liveness_probe(self) -> NoReturn:
128 """Testing make liveness probe."""
129 port = 8000
130
131 expected_result = {
132 "tcpSocket": {
133 "port": port,
134 },
135 "initialDelaySeconds": 45,
136 "periodSeconds": 10,
137 "timeoutSeconds": 5,
138 "successThreshold": 1,
139 "failureThreshold": 3,
140 }
141
142 liveness_probe = pod_spec._make_liveness_probe(port)
143
144 self.assertDictEqual(expected_result, liveness_probe)
145
146 def test_make_pod_spec(self) -> NoReturn:
147 """Testing make pod spec."""
148 image_info = {"upstream-source": "opensourcemano/mon:8"}
149 config = {
150 "site_url": "",
151 "openstack_default_granularity": 300,
152 "global_request_timeout": 10,
153 "log_level": "INFO",
154 "database_commonkey": "osm",
155 "collector_interval": 30,
156 "evaluator_interval": 30,
157 "vca_host": "admin",
158 "vca_user": "admin",
159 "vca_password": "secret",
160 "vca_cacert": "",
161 }
162 relation_state = {
163 "message_host": "kafka",
164 "message_port": 9090,
165 "database_uri": "mongodb://mongo",
166 "prometheus_host": "prometheus",
167 "prometheus_port": 9082,
168 }
169 app_name = "mon"
170 port = 8000
171
172 expected_result = {
173 "version": 3,
174 "containers": [
175 {
176 "name": app_name,
177 "imageDetails": image_info,
178 "imagePullPolicy": "Always",
179 "ports": [
180 {
181 "name": app_name,
182 "containerPort": port,
183 "protocol": "TCP",
184 }
185 ],
186 "envConfig": {
187 "ALLOW_ANONYMOUS_LOGIN": "yes",
188 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config[
189 "openstack_default_granularity"
190 ],
191 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config[
192 "global_request_timeout"
193 ],
194 "OSMMON_GLOBAL_LOGLEVEL": config["log_level"],
195 "OSMMON_COLLECTOR_INTERVAL": config["collector_interval"],
196 "OSMMON_EVALUATOR_INTERVAL": config["evaluator_interval"],
197 "OSMMON_MESSAGE_DRIVER": "kafka",
198 "OSMMON_MESSAGE_HOST": relation_state["message_host"],
199 "OSMMON_MESSAGE_PORT": relation_state["message_port"],
200 "OSMMON_DATABASE_DRIVER": "mongo",
201 "OSMMON_DATABASE_URI": relation_state["database_uri"],
202 "OSMMON_DATABASE_COMMONKEY": config["database_commonkey"],
203 "OSMMON_PROMETHEUS_URL": f"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}",
204 "OSMMON_VCA_HOST": config["vca_host"],
205 "OSMMON_VCA_USER": config["vca_user"],
206 "OSMMON_VCA_SECRET": config["vca_password"],
207 "OSMMON_VCA_CACERT": config["vca_cacert"],
208 },
209 }
210 ],
211 "kubernetesResources": {"ingressResources": []},
212 }
213
214 spec = pod_spec.make_pod_spec(
215 image_info, config, relation_state, app_name, port
216 )
217
218 self.assertDictEqual(expected_result, spec)
219
220 def test_make_pod_spec_without_image_info(self) -> NoReturn:
221 """Testing make pod spec without image_info."""
222 image_info = None
223 config = {
224 "site_url": "",
225 "openstack_default_granularity": 300,
226 "global_request_timeout": 10,
227 "log_level": "INFO",
228 "database_commonkey": "osm",
229 "collector_interval": 30,
230 "evaluator_interval": 30,
231 "vca_host": "admin",
232 "vca_user": "admin",
233 "vca_password": "secret",
234 "vca_cacert": "",
235 }
236 relation_state = {
237 "message_host": "kafka",
238 "message_port": 9090,
239 "database_uri": "mongodb://mongo",
240 "prometheus_host": "prometheus",
241 "prometheus_port": 9082,
242 }
243 app_name = "mon"
244 port = 8000
245
246 spec = pod_spec.make_pod_spec(
247 image_info, config, relation_state, app_name, port
248 )
249
250 self.assertIsNone(spec)
251
252 def test_make_pod_spec_without_config(self) -> NoReturn:
253 """Testing make pod spec without config."""
254 image_info = {"upstream-source": "opensourcemano/mon:8"}
255 config = {}
256 relation_state = {
257 "message_host": "kafka",
258 "message_port": 9090,
259 "database_uri": "mongodb://mongo",
260 "prometheus_host": "prometheus",
261 "prometheus_port": 9082,
262 }
263 app_name = "mon"
264 port = 8000
265
266 with self.assertRaises(ValidationError):
267 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
268
269 def test_make_pod_spec_without_relation_state(self) -> NoReturn:
270 """Testing make pod spec without relation_state."""
271 image_info = {"upstream-source": "opensourcemano/mon:8"}
272 config = {
273 "site_url": "",
274 "openstack_default_granularity": 300,
275 "global_request_timeout": 10,
276 "log_level": "INFO",
277 "database_commonkey": "osm",
278 "collector_interval": 30,
279 "evaluator_interval": 30,
280 "vca_host": "admin",
281 "vca_user": "admin",
282 "vca_password": "secret",
283 "vca_cacert": "",
284 }
285 relation_state = {}
286 app_name = "mon"
287 port = 8000
288
289 with self.assertRaises(ValidationError):
290 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
291
292
293 if __name__ == "__main__":
294 unittest.main()