25e2cd649afee30bcb143338cf85bc7deda5c52c
[osm/devops.git] / installers / charm / lcm / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2020 Canonical Ltd.
3 #
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
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
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
14 # under the License.
15 #
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
18 #
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
21 ##
22
23 from typing import NoReturn
24 import unittest
25 from ops.model import BlockedStatus
26
27 from ops.testing import Harness
28
29 from charm import LcmCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """LCM Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.harness = Harness(LcmCharm)
38 self.harness.set_leader(is_leader=True)
39 self.harness.begin()
40
41 def test_on_start_without_relations(self) -> NoReturn:
42 """Test installation without any relation."""
43 self.harness.charm.on.start.emit()
44
45 # Verifying status
46 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
47
48 # Verifying status message
49 self.assertGreater(len(self.harness.charm.unit.status.message), 0)
50 self.assertTrue(
51 self.harness.charm.unit.status.message.startswith("Waiting for ")
52 )
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"))
57
58 def test_on_start_with_relations(self) -> NoReturn:
59 """Test deployment without keystone."""
60 expected_result = {
61 "version": 3,
62 "containers": [
63 {
64 "name": "lcm",
65 "imageDetails": self.harness.charm.image.fetch(),
66 "imagePullPolicy": "Always",
67 "ports": [
68 {
69 "name": "lcm",
70 "containerPort": 9999,
71 "protocol": "TCP",
72 }
73 ],
74 "envConfig": {
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",
98 },
99 }
100 ],
101 "kubernetesResources": {"ingressResources": []},
102 }
103
104 self.harness.charm.on.start.emit()
105
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)
109
110 # Check if mongodb datastore is initialized
111 self.assertIsNone(self.harness.charm.state.database_uri)
112
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)
116
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}
122 )
123
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(
128 mongodb_relation_id,
129 "mongodb/0",
130 {"connection_string": "mongodb://mongo:27017"},
131 )
132
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}
138 )
139
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)
143
144 # Checking if mongodb data is stored
145 self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
146
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)
150
151 # Verifying status
152 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
153
154 pod_spec, _ = self.harness.get_pod_spec()
155
156 self.assertDictEqual(expected_result, pod_spec)
157
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()
161
162 self.assertIsNone(self.harness.charm.state.message_host)
163 self.assertIsNone(self.harness.charm.state.message_port)
164
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}
169 )
170
171 self.assertEqual(self.harness.charm.state.message_host, "kafka")
172 self.assertEqual(self.harness.charm.state.message_port, 9092)
173
174 # Verifying status
175 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
176
177 # Verifying status message
178 self.assertGreater(len(self.harness.charm.unit.status.message), 0)
179 self.assertTrue(
180 self.harness.charm.unit.status.message.startswith("Waiting for ")
181 )
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"))
186
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()
190
191 self.assertIsNone(self.harness.charm.state.database_uri)
192
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"}
197 )
198
199 self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017")
200
201 # Verifying status
202 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
203
204 # Verifying status message
205 self.assertGreater(len(self.harness.charm.unit.status.message), 0)
206 self.assertTrue(
207 self.harness.charm.unit.status.message.startswith("Waiting for ")
208 )
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"))
213
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()
217
218 self.assertIsNone(self.harness.charm.state.ro_host)
219 self.assertIsNone(self.harness.charm.state.ro_port)
220
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}
225 )
226
227 self.assertEqual(self.harness.charm.state.ro_host, "ro")
228 self.assertEqual(self.harness.charm.state.ro_port, 9090)
229
230 # Verifying status
231 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
232
233 # Verifying status message
234 self.assertGreater(len(self.harness.charm.unit.status.message), 0)
235 self.assertTrue(
236 self.harness.charm.unit.status.message.startswith("Waiting for ")
237 )
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"))
242
243
244 if __name__ == "__main__":
245 unittest.main()