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 pydantic
import ValidationError
24 from typing
import NoReturn
30 class TestPodSpec(unittest
.TestCase
):
31 """Pod spec unit tests."""
33 def test_make_pod_ports(self
) -> NoReturn
:
34 """Testing make pod ports."""
40 "containerPort": port
,
45 pod_ports
= pod_spec
._make
_pod
_ports
(port
)
47 self
.assertListEqual(expected_result
, pod_ports
)
49 def test_make_pod_envconfig(self
) -> NoReturn
:
50 """Testing make pod envconfig."""
52 "openstack_default_granularity": 300,
53 "global_request_timeout": 10,
55 "database_commonkey": "osm",
56 "collector_interval": 30,
57 "evaluator_interval": 30,
60 "vca_password": "secret",
64 "message_host": "kafka",
66 "database_uri": "mongodb://mongo",
67 "prometheus_host": "prometheus",
68 "prometheus_port": 9082,
72 "ALLOW_ANONYMOUS_LOGIN": "yes",
73 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
[
74 "openstack_default_granularity"
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"],
93 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
95 self
.assertDictEqual(expected_result
, pod_envconfig
)
97 def test_make_startup_probe(self
) -> NoReturn
:
98 """Testing make startup probe."""
100 "exec": {"command": ["/usr/bin/pgrep python3"]},
101 "initialDelaySeconds": 60,
105 startup_probe
= pod_spec
._make
_startup
_probe
()
107 self
.assertDictEqual(expected_result
, startup_probe
)
109 def test_make_readiness_probe(self
) -> NoReturn
:
110 """Testing make readiness probe."""
119 "successThreshold": 1,
120 "failureThreshold": 3,
123 readiness_probe
= pod_spec
._make
_readiness
_probe
(port
)
125 self
.assertDictEqual(expected_result
, readiness_probe
)
127 def test_make_liveness_probe(self
) -> NoReturn
:
128 """Testing make liveness probe."""
135 "initialDelaySeconds": 45,
138 "successThreshold": 1,
139 "failureThreshold": 3,
142 liveness_probe
= pod_spec
._make
_liveness
_probe
(port
)
144 self
.assertDictEqual(expected_result
, liveness_probe
)
146 def test_make_pod_spec(self
) -> NoReturn
:
147 """Testing make pod spec."""
148 image_info
= {"upstream-source": "opensourcemano/mon:8"}
151 "openstack_default_granularity": 300,
152 "global_request_timeout": 10,
154 "database_commonkey": "osm",
155 "collector_interval": 30,
156 "evaluator_interval": 30,
159 "vca_password": "secret",
163 "message_host": "kafka",
164 "message_port": 9090,
165 "database_uri": "mongodb://mongo",
166 "prometheus_host": "prometheus",
167 "prometheus_port": 9082,
177 "imageDetails": image_info
,
178 "imagePullPolicy": "Always",
182 "containerPort": port
,
187 "ALLOW_ANONYMOUS_LOGIN": "yes",
188 "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": config
[
189 "openstack_default_granularity"
191 "OSMMON_GLOBAL_REQUEST_TIMEOUT": config
[
192 "global_request_timeout"
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"],
211 "kubernetesResources": {"ingressResources": []},
214 spec
= pod_spec
.make_pod_spec(
215 image_info
, config
, relation_state
, app_name
, port
218 self
.assertDictEqual(expected_result
, spec
)
220 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
221 """Testing make pod spec without image_info."""
225 "openstack_default_granularity": 300,
226 "global_request_timeout": 10,
228 "database_commonkey": "osm",
229 "collector_interval": 30,
230 "evaluator_interval": 30,
233 "vca_password": "secret",
237 "message_host": "kafka",
238 "message_port": 9090,
239 "database_uri": "mongodb://mongo",
240 "prometheus_host": "prometheus",
241 "prometheus_port": 9082,
246 spec
= pod_spec
.make_pod_spec(
247 image_info
, config
, relation_state
, app_name
, port
250 self
.assertIsNone(spec
)
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"}
257 "message_host": "kafka",
258 "message_port": 9090,
259 "database_uri": "mongodb://mongo",
260 "prometheus_host": "prometheus",
261 "prometheus_port": 9082,
266 with self
.assertRaises(ValidationError
):
267 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
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"}
274 "openstack_default_granularity": 300,
275 "global_request_timeout": 10,
277 "database_commonkey": "osm",
278 "collector_interval": 30,
279 "evaluator_interval": 30,
282 "vca_password": "secret",
289 with self
.assertRaises(ValidationError
):
290 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
293 if __name__
== "__main__":