Major improvement in OSM charms
[osm/devops.git] / installers / charm / lcm / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 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 import sys
24 from typing import NoReturn
25 import unittest
26 from ops.model import ActiveStatus, BlockedStatus
27 from ops.testing import Harness
28
29 from charm import LcmCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(LcmCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "vca_host": "192.168.0.13",
43 "vca_port": 17070,
44 "vca_user": "admin",
45 "vca_password": "admin",
46 "vca_pubkey": "key",
47 "vca_cacert": "cacert",
48 "vca_cloud": "cloud",
49 "vca_k8s_cloud": "k8scloud",
50 "database_commonkey": "commonkey",
51 "log_level": "INFO",
52 }
53 self.harness.update_config(self.config)
54
55 def test_config_changed_no_relations(
56 self,
57 ) -> NoReturn:
58 """Test ingress resources without HTTP."""
59
60 self.harness.charm.on.config_changed.emit()
61
62 # Assertions
63 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
64 self.assertTrue(
65 all(
66 relation in self.harness.charm.unit.status.message
67 for relation in ["mongodb", "kafka", "ro"]
68 )
69 )
70
71 def test_config_changed_non_leader(
72 self,
73 ) -> NoReturn:
74 """Test ingress resources without HTTP."""
75 self.harness.set_leader(is_leader=False)
76 self.harness.charm.on.config_changed.emit()
77
78 # Assertions
79 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
80
81 def test_with_relations(
82 self,
83 ) -> NoReturn:
84 "Test with relations (internal)"
85 self.initialize_kafka_relation()
86 self.initialize_mongo_relation()
87 self.initialize_ro_relation()
88 # Verifying status
89 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
90
91 def initialize_kafka_relation(self):
92 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
93 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
94 self.harness.update_relation_data(
95 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
96 )
97
98 def initialize_mongo_relation(self):
99 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
100 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
101 self.harness.update_relation_data(
102 mongodb_relation_id,
103 "mongodb/0",
104 {"connection_string": "mongodb://mongo:27017"},
105 )
106
107 def initialize_ro_relation(self):
108 http_relation_id = self.harness.add_relation("ro", "ro")
109 self.harness.add_relation_unit(http_relation_id, "ro")
110 self.harness.update_relation_data(
111 http_relation_id,
112 "ro",
113 {"host": "ro", "port": 9090},
114 )
115
116
117 if __name__ == "__main__":
118 unittest.main()
119
120
121 # class TestCharm(unittest.TestCase):
122 # """LCM Charm unit tests."""
123
124 # def setUp(self) -> NoReturn:
125 # """Test setup"""
126 # self.harness = Harness(LcmCharm)
127 # self.harness.set_leader(is_leader=True)
128 # self.harness.begin()
129
130 # def test_on_start_without_relations(self) -> NoReturn:
131 # """Test installation without any relation."""
132 # self.harness.charm.on.start.emit()
133
134 # # Verifying status
135 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
136
137 # # Verifying status message
138 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
139 # self.assertTrue(
140 # self.harness.charm.unit.status.message.startswith("Waiting for ")
141 # )
142 # self.assertIn("kafka", self.harness.charm.unit.status.message)
143 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
144 # self.assertIn("ro", self.harness.charm.unit.status.message)
145 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
146
147 # def test_on_start_with_relations(self) -> NoReturn:
148 # """Test deployment without keystone."""
149 # expected_result = {
150 # "version": 3,
151 # "containers": [
152 # {
153 # "name": "lcm",
154 # "imageDetails": self.harness.charm.image.fetch(),
155 # "imagePullPolicy": "Always",
156 # "ports": [
157 # {
158 # "name": "lcm",
159 # "containerPort": 9999,
160 # "protocol": "TCP",
161 # }
162 # ],
163 # "envConfig": {
164 # "ALLOW_ANONYMOUS_LOGIN": "yes",
165 # "OSMLCM_GLOBAL_LOGLEVEL": "INFO",
166 # "OSMLCM_RO_HOST": "ro",
167 # "OSMLCM_RO_PORT": 9090,
168 # "OSMLCM_RO_TENANT": "osm",
169 # "OSMLCM_MESSAGE_DRIVER": "kafka",
170 # "OSMLCM_MESSAGE_HOST": "kafka",
171 # "OSMLCM_MESSAGE_PORT": 9092,
172 # "OSMLCM_DATABASE_DRIVER": "mongo",
173 # "OSMLCM_DATABASE_URI": "mongodb://mongo:27017",
174 # "OSMLCM_DATABASE_COMMONKEY": "osm",
175 # "OSMLCM_STORAGE_DRIVER": "mongo",
176 # "OSMLCM_STORAGE_PATH": "/app/storage",
177 # "OSMLCM_STORAGE_COLLECTION": "files",
178 # "OSMLCM_STORAGE_URI": "mongodb://mongo:27017",
179 # "OSMLCM_VCA_HOST": "admin",
180 # "OSMLCM_VCA_PORT": 17070,
181 # "OSMLCM_VCA_USER": "admin",
182 # "OSMLCM_VCA_PUBKEY": "secret",
183 # "OSMLCM_VCA_SECRET": "secret",
184 # "OSMLCM_VCA_CACERT": "",
185 # "OSMLCM_VCA_CLOUD": "localhost",
186 # "OSMLCM_VCA_K8S_CLOUD": "k8scloud",
187 # },
188 # }
189 # ],
190 # "kubernetesResources": {"ingressResources": []},
191 # }
192
193 # self.harness.charm.on.start.emit()
194
195 # # Check if kafka datastore is initialized
196 # self.assertIsNone(self.harness.charm.state.message_host)
197 # self.assertIsNone(self.harness.charm.state.message_port)
198
199 # # Check if mongodb datastore is initialized
200 # self.assertIsNone(self.harness.charm.state.database_uri)
201
202 # # Check if RO datastore is initialized
203 # self.assertIsNone(self.harness.charm.state.ro_host)
204 # self.assertIsNone(self.harness.charm.state.ro_port)
205
206 # # Initializing the kafka relation
207 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
208 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
209 # self.harness.update_relation_data(
210 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
211 # )
212
213 # # Initializing the mongo relation
214 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
215 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
216 # self.harness.update_relation_data(
217 # mongodb_relation_id,
218 # "mongodb/0",
219 # {"connection_string": "mongodb://mongo:27017"},
220 # )
221
222 # # Initializing the RO relation
223 # ro_relation_id = self.harness.add_relation("ro", "ro")
224 # self.harness.add_relation_unit(ro_relation_id, "ro/0")
225 # self.harness.update_relation_data(
226 # ro_relation_id, "ro/0", {"host": "ro", "port": 9090}
227 # )
228
229 # # Checking if kafka data is stored
230 # self.assertEqual(self.harness.charm.state.message_host, "kafka")
231 # self.assertEqual(self.harness.charm.state.message_port, 9092)
232
233 # # Checking if mongodb data is stored
234 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
235
236 # # Checking if RO data is stored
237 # self.assertEqual(self.harness.charm.state.ro_host, "ro")
238 # self.assertEqual(self.harness.charm.state.ro_port, 9090)
239
240 # # Verifying status
241 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
242
243 # pod_spec, _ = self.harness.get_pod_spec()
244
245 # self.assertDictEqual(expected_result, pod_spec)
246
247 # def test_on_kafka_relation_unit_changed(self) -> NoReturn:
248 # """Test to see if kafka relation is updated."""
249 # self.harness.charm.on.start.emit()
250
251 # self.assertIsNone(self.harness.charm.state.message_host)
252 # self.assertIsNone(self.harness.charm.state.message_port)
253
254 # relation_id = self.harness.add_relation("kafka", "kafka")
255 # self.harness.add_relation_unit(relation_id, "kafka/0")
256 # self.harness.update_relation_data(
257 # relation_id, "kafka/0", {"host": "kafka", "port": 9092}
258 # )
259
260 # self.assertEqual(self.harness.charm.state.message_host, "kafka")
261 # self.assertEqual(self.harness.charm.state.message_port, 9092)
262
263 # # Verifying status
264 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
265
266 # # Verifying status message
267 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
268 # self.assertTrue(
269 # self.harness.charm.unit.status.message.startswith("Waiting for ")
270 # )
271 # self.assertNotIn("kafka", self.harness.charm.unit.status.message)
272 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
273 # self.assertIn("ro", self.harness.charm.unit.status.message)
274 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
275
276 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
277 # """Test to see if mongodb relation is updated."""
278 # self.harness.charm.on.start.emit()
279
280 # self.assertIsNone(self.harness.charm.state.database_uri)
281
282 # relation_id = self.harness.add_relation("mongodb", "mongodb")
283 # self.harness.add_relation_unit(relation_id, "mongodb/0")
284 # self.harness.update_relation_data(
285 # relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
286 # )
287
288 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
289
290 # # Verifying status
291 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
292
293 # # Verifying status message
294 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
295 # self.assertTrue(
296 # self.harness.charm.unit.status.message.startswith("Waiting for ")
297 # )
298 # self.assertIn("kafka", self.harness.charm.unit.status.message)
299 # self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
300 # self.assertIn("ro", self.harness.charm.unit.status.message)
301 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
302
303 # def test_on_ro_unit_relation_changed(self) -> NoReturn:
304 # """Test to see if RO relation is updated."""
305 # self.harness.charm.on.start.emit()
306
307 # self.assertIsNone(self.harness.charm.state.ro_host)
308 # self.assertIsNone(self.harness.charm.state.ro_port)
309
310 # relation_id = self.harness.add_relation("ro", "ro")
311 # self.harness.add_relation_unit(relation_id, "ro/0")
312 # self.harness.update_relation_data(
313 # relation_id, "ro/0", {"host": "ro", "port": 9090}
314 # )
315
316 # self.assertEqual(self.harness.charm.state.ro_host, "ro")
317 # self.assertEqual(self.harness.charm.state.ro_port, 9090)
318
319 # # Verifying status
320 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
321
322 # # Verifying status message
323 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
324 # self.assertTrue(
325 # self.harness.charm.unit.status.message.startswith("Waiting for ")
326 # )
327 # self.assertIn("kafka", self.harness.charm.unit.status.message)
328 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
329 # self.assertNotIn("ro", self.harness.charm.unit.status.message)
330 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
331
332
333 # if __name__ == "__main__":
334 # unittest.main()