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