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 typing
import NoReturn
25 from ops
.model
import BlockedStatus
27 from ops
.testing
import Harness
29 from charm
import RoCharm
32 class TestCharm(unittest
.TestCase
):
33 """RO Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(RoCharm
)
38 self
.harness
.set_leader(is_leader
=True)
41 def test_on_start_without_relations_ng_ro(self
) -> NoReturn
:
42 """Test installation without any relation."""
43 self
.harness
.charm
.on
.start
.emit()
46 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
48 # Verifying status message
49 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
51 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
53 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
54 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
55 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
57 def test_on_start_without_relations_no_ng_ro(self
) -> NoReturn
:
58 """Test installation without any relation."""
59 self
.harness
.update_config({"enable_ng_ro": False})
61 self
.harness
.charm
.on
.start
.emit()
64 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
66 # Verifying status message
67 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
69 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
71 self
.assertIn("mysql", self
.harness
.charm
.unit
.status
.message
)
72 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
74 def test_on_start_with_relations_ng_ro(self
) -> NoReturn
:
75 """Test deployment with NG-RO."""
81 "imageDetails": self
.harness
.charm
.image
.fetch(),
82 "imagePullPolicy": "Always",
86 "containerPort": 9090,
91 "OSMRO_LOG_LEVEL": "INFO",
92 "OSMRO_MESSAGE_DRIVER": "kafka",
93 "OSMRO_MESSAGE_HOST": "kafka",
94 "OSMRO_MESSAGE_PORT": "9090",
95 "OSMRO_DATABASE_DRIVER": "mongo",
96 "OSMRO_DATABASE_URI": "mongodb://mongo",
97 "OSMRO_DATABASE_COMMONKEY": "osm",
101 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
102 "initialDelaySeconds": 60,
107 "path": "/openmano/tenants",
112 "successThreshold": 1,
113 "failureThreshold": 3,
117 "path": "/openmano/tenants",
120 "initialDelaySeconds": 600,
123 "successThreshold": 1,
124 "failureThreshold": 3,
129 "kubernetesResources": {"ingressResources": []},
132 self
.harness
.charm
.on
.start
.emit()
134 # Initializing the kafka relation
135 relation_id
= self
.harness
.add_relation("kafka", "kafka")
136 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
137 self
.harness
.update_relation_data(
146 # Initializing the mongodb relation
147 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
148 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
149 self
.harness
.update_relation_data(
153 "connection_string": "mongodb://mongo",
158 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
160 pod_spec
, _
= self
.harness
.get_pod_spec()
162 self
.assertDictEqual(expected_result
, pod_spec
)
164 def test_on_start_with_relations_no_ng_ro(self
) -> NoReturn
:
165 """Test deployment with old RO."""
166 self
.harness
.update_config({"enable_ng_ro": False})
173 "imageDetails": self
.harness
.charm
.image
.fetch(),
174 "imagePullPolicy": "Always",
178 "containerPort": 9090,
183 "OSMRO_LOG_LEVEL": "INFO",
184 "RO_DB_HOST": "mysql",
185 "RO_DB_OVIM_HOST": "mysql",
187 "RO_DB_OVIM_PORT": 3306,
188 "RO_DB_USER": "mano",
189 "RO_DB_OVIM_USER": "mano",
190 "RO_DB_PASSWORD": "manopw",
191 "RO_DB_OVIM_PASSWORD": "manopw",
192 "RO_DB_ROOT_PASSWORD": "rootmanopw",
193 "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw",
194 "RO_DB_NAME": "mano_db",
195 "RO_DB_OVIM_NAME": "mano_vim_db",
196 "OPENMANO_TENANT": "osm",
200 "exec": {"command": ["/usr/bin/pgrep", "python3"]},
201 "initialDelaySeconds": 60,
206 "path": "/openmano/tenants",
211 "successThreshold": 1,
212 "failureThreshold": 3,
216 "path": "/openmano/tenants",
219 "initialDelaySeconds": 600,
222 "successThreshold": 1,
223 "failureThreshold": 3,
228 "kubernetesResources": {"ingressResources": []},
231 self
.harness
.charm
.on
.start
.emit()
233 # Initializing the mysql relation
234 relation_id
= self
.harness
.add_relation("mysql", "mysql")
235 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
236 self
.harness
.update_relation_data(
243 "password": "manopw",
244 "root_password": "rootmanopw",
249 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
251 pod_spec
, _
= self
.harness
.get_pod_spec()
253 self
.assertDictEqual(expected_result
, pod_spec
)
255 def test_on_kafka_unit_relation_changed(self
) -> NoReturn
:
256 """Test to see if kafka relation is updated."""
257 self
.harness
.charm
.on
.start
.emit()
259 relation_id
= self
.harness
.add_relation("kafka", "kafka")
260 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
261 self
.harness
.update_relation_data(
271 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
273 # Verifying status message
274 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
276 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
278 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
279 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
281 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
282 """Test to see if mongodb relation is updated."""
283 self
.harness
.charm
.on
.start
.emit()
285 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
286 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
287 self
.harness
.update_relation_data(
291 "connection_string": "mongodb://mongo",
296 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
298 # Verifying status message
299 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
301 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
303 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
304 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
306 def test_on_mysql_unit_relation_changed(self
) -> NoReturn
:
307 """Test to see if mysql relation is updated."""
308 self
.harness
.charm
.on
.start
.emit()
310 relation_id
= self
.harness
.add_relation("mysql", "mysql")
311 self
.harness
.add_relation_unit(relation_id
, "mysql/0")
312 self
.harness
.update_relation_data(
319 "password": "manopw",
320 "root_password": "rootmanopw",
325 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
327 # Verifying status message
328 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
330 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
332 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
333 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
334 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
336 def test_publish_ro_info(self
) -> NoReturn
:
337 """Test to see if ro relation is updated."""
343 self
.harness
.charm
.on
.start
.emit()
345 relation_id
= self
.harness
.add_relation("ro", "lcm")
346 self
.harness
.add_relation_unit(relation_id
, "lcm/0")
347 relation_data
= self
.harness
.get_relation_data(relation_id
, "ro")
349 self
.assertDictEqual(expected_result
, relation_data
)
352 if __name__
== "__main__":