2 # Copyright 2020 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
23 from typing
import NoReturn
29 class TestPodSpec(unittest
.TestCase
):
30 """Pod spec unit tests."""
32 def test_make_pod_ports(self
) -> NoReturn
:
33 """Testing make pod ports."""
39 "containerPort": port
,
44 pod_ports
= pod_spec
._make
_pod
_ports
(port
)
46 self
.assertListEqual(expected_result
, pod_ports
)
48 def test_make_pod_envconfig(self
) -> NoReturn
:
49 """Testing make pod envconfig."""
51 "openstack_default_granularity": 300,
52 "global_request_timeout": 10,
54 "database_commonkey": "osm",
55 "collector_interval": 30,
56 "evaluator_interval": 30,
59 "vca_password": "secret",
63 "message_host": "kafka",
65 "database_uri": "mongodb://mongo",
66 "prometheus_host": "prometheus",
67 "prometheus_port": 9082,
71 "ALLOW_ANONYMOUS_LOGIN": "yes",
72 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
[
73 "openstack_default_granularity"
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"],
92 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
94 self
.assertDictEqual(expected_result
, pod_envconfig
)
96 def test_make_startup_probe(self
) -> NoReturn
:
97 """Testing make startup probe."""
99 "exec": {"command": ["/usr/bin/pgrep python3"]},
100 "initialDelaySeconds": 60,
104 startup_probe
= pod_spec
._make
_startup
_probe
()
106 self
.assertDictEqual(expected_result
, startup_probe
)
108 def test_make_readiness_probe(self
) -> NoReturn
:
109 """Testing make readiness probe."""
118 "successThreshold": 1,
119 "failureThreshold": 3,
122 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
124 self
.assertDictEqual(expected_result
, readiness_probe
)
126 def test_make_liveness_probe(self
) -> NoReturn
:
127 """Testing make liveness probe."""
134 "initialDelaySeconds": 45,
137 "successThreshold": 1,
138 "failureThreshold": 3,
141 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
143 self
.assertDictEqual(expected_result
, liveness_probe
)
145 def test_make_pod_spec(self
) -> NoReturn
:
146 """Testing make pod spec."""
147 image_info
= {"upstream-source": "opensourcemano/mon:8"}
150 "openstack_default_granularity": 300,
151 "global_request_timeout": 10,
153 "database_commonkey": "osm",
154 "collector_interval": 30,
155 "evaluator_interval": 30,
158 "vca_password": "secret",
162 "message_host": "kafka",
163 "message_port": 9090,
164 "database_uri": "mongodb://mongo",
165 "prometheus_host": "prometheus",
166 "prometheus_port": 9082,
176 "imageDetails": image_info
,
177 "imagePullPolicy": "Always",
181 "containerPort": port
,
186 "ALLOW_ANONYMOUS_LOGIN": "yes",
187 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
[
188 "openstack_default_granularity"
190 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
[
191 "global_request_timeout"
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": (
203 f
"http://{relation_state['prometheus_host']}:{relation_state['prometheus_port']}"
205 "OSMMON_VCA_HOST": config
["vca_host"],
206 "OSMMON_VCA_USER": config
["vca_user"],
207 "OSMMON_VCA_SECRET": config
["vca_password"],
208 "OSMMON_VCA_CACERT": config
["vca_cacert"],
212 "kubernetesResources": {"ingressResources": []},
215 spec
= pod_spec
.make_pod_spec(
216 image_info
, config
, relation_state
, app_name
, port
219 self
.assertDictEqual(expected_result
, spec
)
221 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
222 """Testing make pod spec without image_info."""
226 "openstack_default_granularity": 300,
227 "global_request_timeout": 10,
229 "database_commonkey": "osm",
230 "collector_interval": 30,
231 "evaluator_interval": 30,
234 "vca_password": "secret",
238 "message_host": "kafka",
239 "message_port": 9090,
240 "database_uri": "mongodb://mongo",
241 "prometheus_host": "prometheus",
242 "prometheus_port": 9082,
247 spec
= pod_spec
.make_pod_spec(
248 image_info
, config
, relation_state
, app_name
, port
251 self
.assertIsNone(spec
)
253 def test_make_pod_spec_without_config(self
) -> NoReturn
:
254 """Testing make pod spec without config."""
255 image_info
= {"upstream-source": "opensourcemano/mon:8"}
258 "message_host": "kafka",
259 "message_port": 9090,
260 "database_uri": "mongodb://mongo",
261 "prometheus_host": "prometheus",
262 "prometheus_port": 9082,
267 with self
.assertRaises(ValueError):
268 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
270 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
271 """Testing make pod spec without relation_state."""
272 image_info
= {"upstream-source": "opensourcemano/mon:8"}
275 "openstack_default_granularity": 300,
276 "global_request_timeout": 10,
278 "database_commonkey": "osm",
279 "collector_interval": 30,
280 "evaluator_interval": 30,
283 "vca_password": "secret",
290 with self
.assertRaises(ValueError):
291 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
294 if __name__
== "__main__":