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