eb5f5cf3d2b911d21b58a5b2c037ac126c12d178
[osm/devops.git] / installers / charm / pol / 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 = 80
35
36 expected_result = [
37 {
38 "name": "pol",
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 """Teting make pod envconfig."""
50 config = {
51 "log_level": "INFO",
52 }
53 relation_state = {
54 "message_host": "kafka",
55 "message_port": 9090,
56 "database_uri": "mongodb://mongo",
57 }
58
59 expected_result = {
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"],
67 }
68
69 pod_envconfig = pod_spec._make_pod_envconfig(config, relation_state)
70
71 self.assertDictEqual(expected_result, pod_envconfig)
72
73 def test_make_startup_probe(self) -> NoReturn:
74 """Testing make startup probe."""
75 expected_result = {
76 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
77 "initialDelaySeconds": 60,
78 "timeoutSeconds": 5,
79 }
80
81 startup_probe = pod_spec._make_startup_probe()
82
83 self.assertDictEqual(expected_result, startup_probe)
84
85 def test_make_readiness_probe(self) -> NoReturn:
86 """Testing make readiness probe."""
87 expected_result = {
88 "exec": {
89 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
90 },
91 "periodSeconds": 10,
92 "timeoutSeconds": 5,
93 "successThreshold": 1,
94 "failureThreshold": 3,
95 }
96
97 readiness_probe = pod_spec._make_readiness_probe()
98
99 self.assertDictEqual(expected_result, readiness_probe)
100
101 def test_make_liveness_probe(self) -> NoReturn:
102 """Testing make liveness probe."""
103 expected_result = {
104 "exec": {
105 "command": ["sh", "-c", "osm-pol-healthcheck || exit 1"],
106 },
107 "initialDelaySeconds": 45,
108 "periodSeconds": 10,
109 "timeoutSeconds": 5,
110 "successThreshold": 1,
111 "failureThreshold": 3,
112 }
113
114 liveness_probe = pod_spec._make_liveness_probe()
115
116 self.assertDictEqual(expected_result, liveness_probe)
117
118 def test_make_pod_spec(self) -> NoReturn:
119 """Testing make pod spec."""
120 image_info = {"upstream-source": "opensourcemano/pol:8"}
121 config = {
122 "log_level": "INFO",
123 }
124 relation_state = {
125 "message_host": "kafka",
126 "message_port": 9090,
127 "database_uri": "mongodb://mongo",
128 }
129 app_name = "pol"
130 port = 80
131
132 expected_result = {
133 "version": 3,
134 "containers": [
135 {
136 "name": app_name,
137 "imageDetails": image_info,
138 "imagePullPolicy": "Always",
139 "ports": [
140 {
141 "name": app_name,
142 "containerPort": port,
143 "protocol": "TCP",
144 }
145 ],
146 "envConfig": {
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"],
154 },
155 }
156 ],
157 "kubernetesResources": {"ingressResources": []},
158 }
159
160 spec = pod_spec.make_pod_spec(
161 image_info, config, relation_state, app_name, port
162 )
163
164 self.assertDictEqual(expected_result, spec)
165
166 def test_make_pod_spec_without_image_info(self) -> NoReturn:
167 """Testing make pod spec without image_info."""
168 image_info = None
169 config = {
170 "log_level": "INFO",
171 }
172 relation_state = {
173 "message_host": "kafka",
174 "message_port": 9090,
175 "database_uri": "mongodb://mongo",
176 }
177 app_name = "pol"
178 port = 80
179
180 spec = pod_spec.make_pod_spec(
181 image_info, config, relation_state, app_name, port
182 )
183
184 self.assertIsNone(spec)
185
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"}
189 config = {}
190 relation_state = {
191 "message_host": "kafka",
192 "message_port": 9090,
193 "database_uri": "mongodb://mongo",
194 }
195 app_name = "pol"
196 port = 80
197
198 with self.assertRaises(ValueError):
199 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
200
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"}
204 config = {
205 "log_level": "INFO",
206 }
207 relation_state = {}
208 app_name = "pol"
209 port = 80
210
211 with self.assertRaises(ValueError):
212 pod_spec.make_pod_spec(image_info, config, relation_state, app_name, port)
213
214
215 if __name__ == "__main__":
216 unittest.main()