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 """Teting make pod envconfig."""
54 "message_host": "kafka",
56 "database_uri": "mongodb://mongo",
60 "ALLOW_ANONYMOUS_LOGIN": "yes",
61 "OSMPOL_GLOBAL_LOGLEVEL": config
["log_level"],
62 "OSMPOL_MESSAGE_HOST": relation_state
["message_host"],
63 "OSMPOL_MESSAGE_DRIVER": "kafka",
64 "OSMPOL_MESSAGE_PORT": relation_state
["message_port"],
65 "OSMPOL_DATABASE_DRIVER": "mongo",
66 "OSMPOL_DATABASE_URI": relation_state
["database_uri"],
69 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
71 self
.assertDictEqual(expected_result
, pod_envconfig
)
73 def test_make_startup_probe(self
) -> NoReturn
:
74 """Testing make startup probe."""
76 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
77 "initialDelaySeconds": 60,
81 startup_probe
= pod_spec
._make
_startup
_probe
()
83 self
.assertDictEqual(expected_result
, startup_probe
)
85 def test_make_readiness_probe(self
) -> NoReturn
:
86 """Testing make readiness probe."""
89 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
93 "successThreshold": 1,
94 "failureThreshold": 3,
97 readiness_probe
= pod_spec
._make
_readiness
_probe
()
99 self
.assertDictEqual(expected_result
, readiness_probe
)
101 def test_make_liveness_probe(self
) -> NoReturn
:
102 """Testing make liveness probe."""
105 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
107 "initialDelaySeconds": 45,
110 "successThreshold": 1,
111 "failureThreshold": 3,
114 liveness_probe
= pod_spec
._make
_liveness
_probe
()
116 self
.assertDictEqual(expected_result
, liveness_probe
)
118 def test_make_pod_spec(self
) -> NoReturn
:
119 """Testing make pod spec."""
120 image_info
= {"upstream-source": "opensourcemano/pol:8"}
125 "message_host": "kafka",
126 "message_port": 9090,
127 "database_uri": "mongodb://mongo",
137 "imageDetails": image_info
,
138 "imagePullPolicy": "Always",
142 "containerPort": port
,
147 "ALLOW_ANONYMOUS_LOGIN": "yes",
148 "OSMPOL_GLOBAL_LOGLEVEL": config
["log_level"],
149 "OSMPOL_MESSAGE_HOST": relation_state
["message_host"],
150 "OSMPOL_MESSAGE_DRIVER": "kafka",
151 "OSMPOL_MESSAGE_PORT": relation_state
["message_port"],
152 "OSMPOL_DATABASE_DRIVER": "mongo",
153 "OSMPOL_DATABASE_URI": relation_state
["database_uri"],
157 "kubernetesResources": {"ingressResources": []},
160 spec
= pod_spec
.make_pod_spec(
161 image_info
, config
, relation_state
, app_name
, port
164 self
.assertDictEqual(expected_result
, spec
)
166 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
167 """Testing make pod spec without image_info."""
173 "message_host": "kafka",
174 "message_port": 9090,
175 "database_uri": "mongodb://mongo",
180 spec
= pod_spec
.make_pod_spec(
181 image_info
, config
, relation_state
, app_name
, port
184 self
.assertIsNone(spec
)
186 def test_make_pod_spec_without_config(self
) -> NoReturn
:
187 """Testing make pod spec without config."""
188 image_info
= {"upstream-source": "opensourcemano/pol:8"}
191 "message_host": "kafka",
192 "message_port": 9090,
193 "database_uri": "mongodb://mongo",
198 with self
.assertRaises(ValueError):
199 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
201 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
202 """Testing make pod spec without relation_state."""
203 image_info
= {"upstream-source": "opensourcemano/pol:8"}
211 with self
.assertRaises(ValueError):
212 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
215 if __name__
== "__main__":