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 """Teting make pod envconfig."""
55 "message_host": "kafka",
57 "database_uri": "mongodb://mongo",
61 "ALLOW_ANONYMOUS_LOGIN": "yes",
62 "OSMPOL_GLOBAL_LOGLEVEL": config
["log_level"],
63 "OSMPOL_MESSAGE_HOST": relation_state
["message_host"],
64 "OSMPOL_MESSAGE_DRIVER": "kafka",
65 "OSMPOL_MESSAGE_PORT": relation_state
["message_port"],
66 "OSMPOL_DATABASE_DRIVER": "mongo",
67 "OSMPOL_DATABASE_URI": relation_state
["database_uri"],
70 pod_envconfig
= pod_spec
._make
_pod
_envconfig
(config
, relation_state
)
72 self
.assertDictEqual(expected_result
, pod_envconfig
)
74 def test_make_startup_probe(self
) -> NoReturn
:
75 """Testing make startup probe."""
77 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
78 "initialDelaySeconds": 60,
82 startup_probe
= pod_spec
._make
_startup
_probe
()
84 self
.assertDictEqual(expected_result
, startup_probe
)
86 def test_make_readiness_probe(self
) -> NoReturn
:
87 """Testing make readiness probe."""
90 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
94 "successThreshold": 1,
95 "failureThreshold": 3,
98 readiness_probe
= pod_spec
._make
_readiness
_probe
()
100 self
.assertDictEqual(expected_result
, readiness_probe
)
102 def test_make_liveness_probe(self
) -> NoReturn
:
103 """Testing make liveness probe."""
106 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
108 "initialDelaySeconds": 45,
111 "successThreshold": 1,
112 "failureThreshold": 3,
115 liveness_probe
= pod_spec
._make
_liveness
_probe
()
117 self
.assertDictEqual(expected_result
, liveness_probe
)
119 def test_make_pod_spec(self
) -> NoReturn
:
120 """Testing make pod spec."""
121 image_info
= {"upstream-source": "opensourcemano/pol:8"}
126 "message_host": "kafka",
127 "message_port": 9090,
128 "database_uri": "mongodb://mongo",
138 "imageDetails": image_info
,
139 "imagePullPolicy": "Always",
143 "containerPort": port
,
148 "ALLOW_ANONYMOUS_LOGIN": "yes",
149 "OSMPOL_GLOBAL_LOGLEVEL": config
["log_level"],
150 "OSMPOL_MESSAGE_HOST": relation_state
["message_host"],
151 "OSMPOL_MESSAGE_DRIVER": "kafka",
152 "OSMPOL_MESSAGE_PORT": relation_state
["message_port"],
153 "OSMPOL_DATABASE_DRIVER": "mongo",
154 "OSMPOL_DATABASE_URI": relation_state
["database_uri"],
158 "kubernetesResources": {"ingressResources": []},
161 spec
= pod_spec
.make_pod_spec(
162 image_info
, config
, relation_state
, app_name
, port
165 self
.assertDictEqual(expected_result
, spec
)
167 def test_make_pod_spec_without_image_info(self
) -> NoReturn
:
168 """Testing make pod spec without image_info."""
174 "message_host": "kafka",
175 "message_port": 9090,
176 "database_uri": "mongodb://mongo",
181 spec
= pod_spec
.make_pod_spec(
182 image_info
, config
, relation_state
, app_name
, port
185 self
.assertIsNone(spec
)
187 def test_make_pod_spec_without_config(self
) -> NoReturn
:
188 """Testing make pod spec without config."""
189 image_info
= {"upstream-source": "opensourcemano/pol:8"}
192 "message_host": "kafka",
193 "message_port": 9090,
194 "database_uri": "mongodb://mongo",
199 with self
.assertRaises(ValidationError
):
200 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
202 def test_make_pod_spec_without_relation_state(self
) -> NoReturn
:
203 """Testing make pod spec without relation_state."""
204 image_info
= {"upstream-source": "opensourcemano/pol:8"}
212 with self
.assertRaises(ValidationError
):
213 pod_spec
.make_pod_spec(image_info
, config
, relation_state
, app_name
, port
)
216 if __name__
== "__main__":