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
27 from charm
import PolCharm
28 from ops
.model
import ActiveStatus
, BlockedStatus
29 from ops
.testing
import Harness
32 class TestCharm(unittest
.TestCase
):
33 """Pol Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.image_info
= sys
.modules
["oci_image"].OCIImageResource().fetch()
38 self
.harness
= Harness(PolCharm
)
39 self
.harness
.set_leader(is_leader
=True)
44 self
.harness
.update_config(self
.config
)
46 def test_config_changed_no_relations(
49 """Test ingress resources without HTTP."""
51 self
.harness
.charm
.on
.config_changed
.emit()
54 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
57 relation
in self
.harness
.charm
.unit
.status
.message
58 for relation
in ["mongodb", "kafka"]
62 def test_config_changed_non_leader(
65 """Test ingress resources without HTTP."""
66 self
.harness
.set_leader(is_leader
=False)
67 self
.harness
.charm
.on
.config_changed
.emit()
70 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, ActiveStatus
)
72 def test_with_relations(
75 "Test with relations (internal)"
76 self
.initialize_kafka_relation()
77 self
.initialize_mongo_relation()
79 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
81 def initialize_kafka_relation(self
):
82 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
83 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
84 self
.harness
.update_relation_data(
85 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
88 def initialize_mongo_relation(self
):
89 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
90 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
91 self
.harness
.update_relation_data(
94 {"connection_string": "mongodb://mongo:27017"},
98 if __name__
== "__main__":
102 # class TestCharm(unittest.TestCase):
103 # """POL Charm unit tests."""
105 # def setUp(self) -> NoReturn:
107 # self.harness = Harness(PolCharm)
108 # self.harness.set_leader(is_leader=True)
109 # self.harness.begin()
111 # def test_on_start_without_relations(self) -> NoReturn:
112 # """Test installation without any relation."""
113 # self.harness.charm.on.start.emit()
116 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
118 # # Verifying status message
119 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
121 # self.harness.charm.unit.status.message.startswith("Waiting for ")
123 # self.assertIn("kafka", self.harness.charm.unit.status.message)
124 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
125 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations"))
127 # def test_on_start_with_relations(self) -> NoReturn:
128 # """Test deployment without keystone."""
129 # expected_result = {
134 # "imageDetails": self.harness.charm.image.fetch(),
135 # "imagePullPolicy": "Always",
139 # "containerPort": 80,
144 # "ALLOW_ANONYMOUS_LOGIN": "yes",
145 # "OSMPOL_GLOBAL_LOGLEVEL": "INFO",
146 # "OSMPOL_MESSAGE_HOST": "kafka",
147 # "OSMPOL_MESSAGE_DRIVER": "kafka",
148 # "OSMPOL_MESSAGE_PORT": 9092,
149 # "OSMPOL_DATABASE_DRIVER": "mongo",
150 # "OSMPOL_DATABASE_URI": "mongodb://mongo:27017",
154 # "kubernetesResources": {"ingressResources": []},
157 # self.harness.charm.on.start.emit()
159 # # Check if kafka datastore is initialized
160 # self.assertIsNone(self.harness.charm.state.message_host)
161 # self.assertIsNone(self.harness.charm.state.message_port)
163 # # Check if mongodb datastore is initialized
164 # self.assertIsNone(self.harness.charm.state.database_uri)
166 # # Initializing the kafka relation
167 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
168 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
169 # self.harness.update_relation_data(
170 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
173 # # Initializing the mongo relation
174 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
175 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
176 # self.harness.update_relation_data(
177 # mongodb_relation_id,
179 # {"connection_string": "mongodb://mongo:27017"},
182 # # Checking if kafka data is stored
183 # self.assertEqual(self.harness.charm.state.message_host, "kafka")
184 # self.assertEqual(self.harness.charm.state.message_port, 9092)
186 # # Checking if mongodb data is stored
187 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
190 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
192 # pod_spec, _ = self.harness.get_pod_spec()
194 # self.assertDictEqual(expected_result, pod_spec)
196 # def test_on_kafka_unit_relation_changed(self) -> NoReturn:
197 # """Test to see if kafka relation is updated."""
198 # self.harness.charm.on.start.emit()
200 # self.assertIsNone(self.harness.charm.state.message_host)
201 # self.assertIsNone(self.harness.charm.state.message_port)
203 # relation_id = self.harness.add_relation("kafka", "kafka")
204 # self.harness.add_relation_unit(relation_id, "kafka/0")
205 # self.harness.update_relation_data(
206 # relation_id, "kafka/0", {"host": "kafka", "port": 9092}
209 # self.assertEqual(self.harness.charm.state.message_host, "kafka")
210 # self.assertEqual(self.harness.charm.state.message_port, 9092)
213 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
215 # # Verifying status message
216 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
218 # self.harness.charm.unit.status.message.startswith("Waiting for ")
220 # self.assertNotIn("kafka", self.harness.charm.unit.status.message)
221 # self.assertIn("mongodb", self.harness.charm.unit.status.message)
222 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
224 # def test_on_mongodb_unit_relation_changed(self) -> NoReturn:
225 # """Test to see if mongodb relation is updated."""
226 # self.harness.charm.on.start.emit()
228 # self.assertIsNone(self.harness.charm.state.database_uri)
230 # relation_id = self.harness.add_relation("mongodb", "mongodb")
231 # self.harness.add_relation_unit(relation_id, "mongodb/0")
232 # self.harness.update_relation_data(
233 # relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
236 # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
239 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
241 # # Verifying status message
242 # self.assertGreater(len(self.harness.charm.unit.status.message), 0)
244 # self.harness.charm.unit.status.message.startswith("Waiting for ")
246 # self.assertIn("kafka", self.harness.charm.unit.status.message)
247 # self.assertNotIn("mongodb", self.harness.charm.unit.status.message)
248 # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation"))
251 # if __name__ == "__main__":