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 LcmCharm
32 class TestCharm(unittest
.TestCase
):
33 """LCM Charm unit tests."""
35 def setUp(self
) -> NoReturn
:
37 self
.harness
= Harness(LcmCharm
)
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
.assertIn("ro", self
.harness
.charm
.unit
.status
.message
)
56 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
58 def test_on_start_with_relations(self
) -> NoReturn
:
59 """Test deployment without keystone."""
65 "imageDetails": self
.harness
.charm
.image
.fetch(),
66 "imagePullPolicy": "Always",
70 "containerPort": 9999,
75 "ALLOW_ANONYMOUS_LOGIN": "yes",
76 "OSMLCM_GLOBAL_LOGLEVEL": "INFO",
77 "OSMLCM_RO_HOST": "ro",
78 "OSMLCM_RO_PORT": 9090,
79 "OSMLCM_RO_TENANT": "osm",
80 "OSMLCM_MESSAGE_DRIVER": "kafka",
81 "OSMLCM_MESSAGE_HOST": "kafka",
82 "OSMLCM_MESSAGE_PORT": 9092,
83 "OSMLCM_DATABASE_DRIVER": "mongo",
84 "OSMLCM_DATABASE_URI": "mongodb://mongo:27017",
85 "OSMLCM_DATABASE_COMMONKEY": "osm",
86 "OSMLCM_STORAGE_DRIVER": "mongo",
87 "OSMLCM_STORAGE_PATH": "/app/storage",
88 "OSMLCM_STORAGE_COLLECTION": "files",
89 "OSMLCM_STORAGE_URI": "mongodb://mongo:27017",
90 "OSMLCM_VCA_HOST": "admin",
91 "OSMLCM_VCA_PORT": 17070,
92 "OSMLCM_VCA_USER": "admin",
93 "OSMLCM_VCA_PUBKEY": "secret",
94 "OSMLCM_VCA_SECRET": "secret",
95 "OSMLCM_VCA_CACERT": "",
96 "OSMLCM_VCA_CLOUD": "localhost",
97 "OSMLCM_VCA_K8S_CLOUD": "k8scloud",
101 "kubernetesResources": {"ingressResources": []},
104 self
.harness
.charm
.on
.start
.emit()
106 # Check if kafka datastore is initialized
107 self
.assertIsNone(self
.harness
.charm
.state
.message_host
)
108 self
.assertIsNone(self
.harness
.charm
.state
.message_port
)
110 # Check if mongodb datastore is initialized
111 self
.assertIsNone(self
.harness
.charm
.state
.database_uri
)
113 # Check if RO datastore is initialized
114 self
.assertIsNone(self
.harness
.charm
.state
.ro_host
)
115 self
.assertIsNone(self
.harness
.charm
.state
.ro_port
)
117 # Initializing the kafka relation
118 kafka_relation_id
= self
.harness
.add_relation("kafka", "kafka")
119 self
.harness
.add_relation_unit(kafka_relation_id
, "kafka/0")
120 self
.harness
.update_relation_data(
121 kafka_relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
124 # Initializing the mongo relation
125 mongodb_relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
126 self
.harness
.add_relation_unit(mongodb_relation_id
, "mongodb/0")
127 self
.harness
.update_relation_data(
130 {"connection_string": "mongodb://mongo:27017"},
133 # Initializing the RO relation
134 ro_relation_id
= self
.harness
.add_relation("ro", "ro")
135 self
.harness
.add_relation_unit(ro_relation_id
, "ro/0")
136 self
.harness
.update_relation_data(
137 ro_relation_id
, "ro/0", {"host": "ro", "port": 9090}
140 # Checking if kafka data is stored
141 self
.assertEqual(self
.harness
.charm
.state
.message_host
, "kafka")
142 self
.assertEqual(self
.harness
.charm
.state
.message_port
, 9092)
144 # Checking if mongodb data is stored
145 self
.assertEqual(self
.harness
.charm
.state
.database_uri
, "mongodb://mongo:27017")
147 # Checking if RO data is stored
148 self
.assertEqual(self
.harness
.charm
.state
.ro_host
, "ro")
149 self
.assertEqual(self
.harness
.charm
.state
.ro_port
, 9090)
152 self
.assertNotIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
154 pod_spec
, _
= self
.harness
.get_pod_spec()
156 self
.assertDictEqual(expected_result
, pod_spec
)
158 def test_on_kafka_relation_unit_changed(self
) -> NoReturn
:
159 """Test to see if kafka relation is updated."""
160 self
.harness
.charm
.on
.start
.emit()
162 self
.assertIsNone(self
.harness
.charm
.state
.message_host
)
163 self
.assertIsNone(self
.harness
.charm
.state
.message_port
)
165 relation_id
= self
.harness
.add_relation("kafka", "kafka")
166 self
.harness
.add_relation_unit(relation_id
, "kafka/0")
167 self
.harness
.update_relation_data(
168 relation_id
, "kafka/0", {"host": "kafka", "port": 9092}
171 self
.assertEqual(self
.harness
.charm
.state
.message_host
, "kafka")
172 self
.assertEqual(self
.harness
.charm
.state
.message_port
, 9092)
175 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
177 # Verifying status message
178 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
180 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
182 self
.assertNotIn("kafka", self
.harness
.charm
.unit
.status
.message
)
183 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
184 self
.assertIn("ro", self
.harness
.charm
.unit
.status
.message
)
185 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
187 def test_on_mongodb_unit_relation_changed(self
) -> NoReturn
:
188 """Test to see if mongodb relation is updated."""
189 self
.harness
.charm
.on
.start
.emit()
191 self
.assertIsNone(self
.harness
.charm
.state
.database_uri
)
193 relation_id
= self
.harness
.add_relation("mongodb", "mongodb")
194 self
.harness
.add_relation_unit(relation_id
, "mongodb/0")
195 self
.harness
.update_relation_data(
196 relation_id
, "mongodb/0", {"connection_string": "mongodb://mongo:27017"}
199 self
.assertEqual(self
.harness
.charm
.state
.database_uri
, "mongodb://mongo:27017")
202 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
204 # Verifying status message
205 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
207 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
209 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
210 self
.assertNotIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
211 self
.assertIn("ro", self
.harness
.charm
.unit
.status
.message
)
212 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
214 def test_on_ro_unit_relation_changed(self
) -> NoReturn
:
215 """Test to see if RO relation is updated."""
216 self
.harness
.charm
.on
.start
.emit()
218 self
.assertIsNone(self
.harness
.charm
.state
.ro_host
)
219 self
.assertIsNone(self
.harness
.charm
.state
.ro_port
)
221 relation_id
= self
.harness
.add_relation("ro", "ro")
222 self
.harness
.add_relation_unit(relation_id
, "ro/0")
223 self
.harness
.update_relation_data(
224 relation_id
, "ro/0", {"host": "ro", "port": 9090}
227 self
.assertEqual(self
.harness
.charm
.state
.ro_host
, "ro")
228 self
.assertEqual(self
.harness
.charm
.state
.ro_port
, 9090)
231 self
.assertIsInstance(self
.harness
.charm
.unit
.status
, BlockedStatus
)
233 # Verifying status message
234 self
.assertGreater(len(self
.harness
.charm
.unit
.status
.message
), 0)
236 self
.harness
.charm
.unit
.status
.message
.startswith("Waiting for ")
238 self
.assertIn("kafka", self
.harness
.charm
.unit
.status
.message
)
239 self
.assertIn("mongodb", self
.harness
.charm
.unit
.status
.message
)
240 self
.assertNotIn("ro", self
.harness
.charm
.unit
.status
.message
)
241 self
.assertTrue(self
.harness
.charm
.unit
.status
.message
.endswith(" relations"))
244 if __name__
== "__main__":