2 # Copyright 2021 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
24 from typing
import NoReturn
26 from ops
.model
import ActiveStatus
, BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import LcmCharm
32 class TestCharm(unittest
.TestCase
):
33 """Prometheus Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(LcmCharm
)
39 self
.harness
.set_leader(is_leader
=True)
42 "vca_host": "192.168.0.13",
45 "vca_password": "admin",
47 "vca_cacert": "cacert",
49 "vca_k8s_cloud": "k8scloud",
50 "database_commonkey": "commonkey",
53 self
.harness
.update_config(self
.config
)
55 def test_config_changed_no_relations(
58 """Test ingress resources without HTTP."""
60 self
.harness
.charm
.on
.config_changed
.emit()
63 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
66 relation
in self
.harness
.charm
.unit
.status
.message
67 for relation
in ["mongodb", "kafka", "ro"]
71 def test_config_changed_non_leader(
74 """Test ingress resources without HTTP."""
75 self
.harness
.set_leader(is_leader
=False)
76 self
.harness
.charm
.on
.config_changed
.emit()
79 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
81 def test_with_relations(
84 "Test with relations (internal)"
85 self
.initialize_kafka_relation()
86 self
.initialize_mongo_relation()
87 self
.initialize_ro_relation()
89 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
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}
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(
104 {"connection_string": "mongodb://mongo:27017"},
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(
113 {"host": "ro", "port": 9090},
117 if __name__
== "__main__":
121 # class TestCharm(unittest.TestCase):
122 # """LCM Charm unit tests."""
124 # def setUp(self) -> NoReturn:
126 # self.harness = Harness(LcmCharm)
127 # self.harness.set_leader(is_leader=True)
128 # self.harness.begin()
130 # def test_on_start_without_relations(self) -> NoReturn:
131 # """Test installation without any relation."""
132 # self.harness.charm.on.start.emit()
135 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
137 # # Verifying status message
138 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
140 # self.harness.charm.unit.status.message.startswith("Waiting for ")
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"))
147 # def test_on_start_with_relations(self) -> NoReturn:
148 # """Test deployment without keystone."""
149 # expected_result = {
154 # "imageDetails": self.harness.charm.image.fetch(),
155 # "imagePullPolicy": "Always",
159 # "containerPort": 9999,
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",
190 # "kubernetesResources": {"ingressResources": []},
193 # self.harness.charm.on.start.emit()
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)
199 # # Check if mongodb datastore is initialized
200 # self.assertIsNone(self.harness.charm.state.database_uri)
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)
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}
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,
219 # {"connection_string": "mongodb://mongo:27017"},
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}
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)
233 # # Checking if mongodb data is stored
234 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
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)
241 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
243 # pod_spec, _ = self.harness.get_pod_spec()
245 # self.assertDictEqual(expected_result, pod_spec)
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()
251 # self.assertIsNone(self.harness.charm.state.message_host)
252 # self.assertIsNone(self.harness.charm.state.message_port)
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}
260 # self.assertEqual(self.harness.charm.state.message_host, "kafka")
261 # self.assertEqual(self.harness.charm.state.message_port, 9092)
264 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
266 # # Verifying status message
267 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
269 # self.harness.charm.unit.status.message.startswith("Waiting for ")
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"))
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()
280 # self.assertIsNone(self.harness.charm.state.database_uri)
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"}
288 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
291 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
293 # # Verifying status message
294 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
296 # self.harness.charm.unit.status.message.startswith("Waiting for ")
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"))
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()
307 # self.assertIsNone(self.harness.charm.state.ro_host)
308 # self.assertIsNone(self.harness.charm.state.ro_port)
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}
316 # self.assertEqual(self.harness.charm.state.ro_host, "ro")
317 # self.assertEqual(self.harness.charm.state.ro_port, 9090)
320 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
322 # # Verifying status message
323 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
325 # self.harness.charm.unit.status.message.startswith("Waiting for ")
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"))
333 # if __name__ == "__main__":