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 PolCharm
32 class TestCharm(unittest
.TestCase
):
33 """POL Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(PolCharm
)
38 self
.harness
.set_leader(is_leader
=True)
41 def test_on_start_without_relations(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_with_relations(self
) -> NoReturn
:
58 """Test deployment without keystone."""
64 "imageDetails": self
.harness
.charm
.image
.fetch(),
65 "imagePullPolicy": "Always",
74 "ALLOW_ANONYMOUS_LOGIN": "yes",
75 "OSMPOL_GLOBAL_LOGLEVEL": "INFO",
76 "OSMPOL_MESSAGE_HOST": "kafka",
77 "OSMPOL_MESSAGE_DRIVER": "kafka",
78 "OSMPOL_MESSAGE_PORT": 9092,
79 "OSMPOL_DATABASE_DRIVER": "mongo",
80 "OSMPOL_DATABASE_URI": "mongodb://mongo:27017",
84 "kubernetesResources": {"ingressResources": []},
87 self
.harness
.charm
.on
.start
.emit()
89 # Check if kafka datastore is initialized
90 self
.assertIsNone(self
.harness
.charm
.state
.message_host
)
91 self
.assertIsNone(self
.harness
.charm
.state
.message_port
)
93 # Check if mongodb datastore is initialized
94 self
.assertIsNone(self
.harness
.charm
.state
.database_uri
)
96 # Initializing the kafka relation
97 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
98 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
99 self
.harness
.update_relation_data(
100 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
103 # Initializing the mongo relation
104 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
105 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
106 self
.harness
.update_relation_data(
109 {"connection_string": "mongodb://mongo:27017"},
112 # Checking if kafka data is stored
113 self
.assertEqual(self
.harness
.charm
.state
.message_host
, "kafka")
114 self
.assertEqual(self
.harness
.charm
.state
.message_port
, 9092)
116 # Checking if mongodb data is stored
117 self
.assertEqual(self
.harness
.charm
.state
.database_uri
, "mongodb://mongo:27017")
120 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
122 pod_spec
, _
= self
.harness
.get_pod_spec()
124 self
.assertDictEqual(expected_result
, pod_spec
)
126 def test_on_kafka_unit_relation_changed(self
) -> NoReturn
:
127 """Test to see if kafka relation is updated."""
128 self
.harness
.charm
.on
.start
.emit()
130 self
.assertIsNone(self
.harness
.charm
.state
.message_host
)
131 self
.assertIsNone(self
.harness
.charm
.state
.message_port
)
133 relation_id
= self
.harness
.add_relation("kafka", "kafka")
134 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
135 self
.harness
.update_relation_data(
136 relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
139 self
.assertEqual(self
.harness
.charm
.state
.message_host
, "kafka")
140 self
.assertEqual(self
.harness
.charm
.state
.message_port
, 9092)
143 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
145 # Verifying status message
146 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
148 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
150 self
.assertNotIn("kafka", self
.harness
.charm
.unit
.status
.message
)
151 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
152 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
154 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
155 """Test to see if mongodb relation is updated."""
156 self
.harness
.charm
.on
.start
.emit()
158 self
.assertIsNone(self
.harness
.charm
.state
.database_uri
)
160 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
161 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
162 self
.harness
.update_relation_data(
163 relation_id
, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
166 self
.assertEqual(self
.harness
.charm
.state
.database_uri
, "mongodb://mongo:27017")
169 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
171 # Verifying status message
172 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
174 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
176 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
177 self
.assertNotIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
178 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relation"))
181 if __name__
== "__main__":