Major improvement in OSM charms
[osm/devops.git] / installers / charm / nbi / tests / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2021 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 import sys
24 from typing import NoReturn
25 import unittest
26 from ops.model import ActiveStatus, BlockedStatus
27 from ops.testing import Harness
28
29 from charm import NbiCharm
30
31
32 class TestCharm(unittest.TestCase):
33 """Prometheus Charm unit tests."""
34
35 def setUp(self) -> NoReturn:
36 """Test setup"""
37 self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
38 self.harness = Harness(NbiCharm)
39 self.harness.set_leader(is_leader=True)
40 self.harness.begin()
41 self.config = {
42 "enable_test": False,
43 "auth_backend": "internal",
44 "database_commonkey": "key",
45 "log_level": "INFO",
46 "max_file_size": 0,
47 "ingress_whitelist_source_range": "",
48 "tls_secret_name": "",
49 "site_url": "https://nbi.192.168.100.100.xip.io",
50 }
51 self.harness.update_config(self.config)
52
53 def test_config_changed_no_relations(
54 self,
55 ) -> NoReturn:
56 """Test ingress resources without HTTP."""
57
58 self.harness.charm.on.config_changed.emit()
59
60 # Assertions
61 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
62 self.assertTrue(
63 all(
64 relation in self.harness.charm.unit.status.message
65 for relation in ["mongodb", "kafka", "prometheus"]
66 )
67 )
68
69 def test_config_changed_non_leader(
70 self,
71 ) -> NoReturn:
72 """Test ingress resources without HTTP."""
73 self.harness.set_leader(is_leader=False)
74 self.harness.charm.on.config_changed.emit()
75
76 # Assertions
77 self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
78
79 def test_with_relations_internal(
80 self,
81 ) -> NoReturn:
82 "Test with relations (internal)"
83 self.initialize_kafka_relation()
84 self.initialize_mongo_relation()
85 self.initialize_prometheus_relation()
86 # Verifying status
87 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
88
89 def test_with_relations_keystone_missing(
90 self,
91 ) -> NoReturn:
92 "Test with relations (keystone)"
93 self.harness.update_config({"auth_backend": "keystone"})
94 self.initialize_kafka_relation()
95 self.initialize_mongo_relation()
96 self.initialize_prometheus_relation()
97 # Verifying status
98 self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
99 self.assertTrue("keystone" in self.harness.charm.unit.status.message)
100
101 def test_with_relations_keystone(
102 self,
103 ) -> NoReturn:
104 "Test with relations (keystone)"
105 self.harness.update_config({"auth_backend": "keystone"})
106 self.initialize_kafka_relation()
107 self.initialize_mongo_relation()
108 self.initialize_prometheus_relation()
109 self.initialize_keystone_relation()
110 # Verifying status
111 self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
112
113 def initialize_kafka_relation(self):
114 kafka_relation_id = self.harness.add_relation("kafka", "kafka")
115 self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
116 self.harness.update_relation_data(
117 kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
118 )
119
120 def initialize_mongo_relation(self):
121 mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
122 self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
123 self.harness.update_relation_data(
124 mongodb_relation_id,
125 "mongodb/0",
126 {"connection_string": "mongodb://mongo:27017"},
127 )
128
129 def initialize_keystone_relation(self):
130 keystone_relation_id = self.harness.add_relation("keystone", "keystone")
131 self.harness.add_relation_unit(keystone_relation_id, "keystone/0")
132 self.harness.update_relation_data(
133 keystone_relation_id,
134 "keystone",
135 {
136 "host": "host",
137 "port": 5000,
138 "user_domain_name": "ud",
139 "project_domain_name": "pd",
140 "username": "u",
141 "password": "p",
142 "service": "s",
143 "keystone_db_password": "something",
144 "region_id": "something",
145 "admin_username": "something",
146 "admin_password": "something",
147 "admin_project_name": "something",
148 },
149 )
150
151 def initialize_prometheus_relation(self):
152 prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus")
153 self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0")
154 self.harness.update_relation_data(
155 prometheus_relation_id,
156 "prometheus",
157 {"hostname": "prometheus", "port": 9090},
158 )
159
160
161 if __name__ == "__main__":
162 unittest.main()
163
164
165 # class TestCharm(unittest.TestCase):
166 # """Prometheus Charm unit tests."""
167
168 # def setUp(self) -> NoReturn:
169 # """Test setup"""
170 # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch()
171 # self.harness = Harness(NbiCharm)
172 # self.harness.set_leader(is_leader=True)
173 # self.harness.begin()
174 # self.config = {
175 # "enable_ng_ro": True,
176 # "database_commonkey": "commonkey",
177 # "log_level": "INFO",
178 # "vim_database": "db_name",
179 # "ro_database": "ro_db_name",
180 # "openmano_tenant": "mano",
181 # }
182
183 # def test_config_changed_no_relations(
184 # self,
185 # ) -> NoReturn:
186 # """Test ingress resources without HTTP."""
187
188 # self.harness.charm.on.config_changed.emit()
189
190 # # Assertions
191 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
192 # self.assertTrue(
193 # all(
194 # relation in self.harness.charm.unit.status.message
195 # for relation in ["mongodb", "kafka"]
196 # )
197 # )
198
199 # # Disable ng-ro
200 # self.harness.update_config({"enable_ng_ro": False})
201 # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus)
202 # self.assertTrue(
203 # all(
204 # relation in self.harness.charm.unit.status.message
205 # for relation in ["mysql"]
206 # )
207 # )
208
209 # def test_config_changed_non_leader(
210 # self,
211 # ) -> NoReturn:
212 # """Test ingress resources without HTTP."""
213 # self.harness.set_leader(is_leader=False)
214 # self.harness.charm.on.config_changed.emit()
215
216 # # Assertions
217 # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus)
218
219 # def test_with_relations_ng(
220 # self,
221 # ) -> NoReturn:
222 # "Test with relations (ng-ro)"
223
224 # # Initializing the kafka relation
225 # kafka_relation_id = self.harness.add_relation("kafka", "kafka")
226 # self.harness.add_relation_unit(kafka_relation_id, "kafka/0")
227 # self.harness.update_relation_data(
228 # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092}
229 # )
230
231 # # Initializing the mongo relation
232 # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb")
233 # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0")
234 # self.harness.update_relation_data(
235 # mongodb_relation_id,
236 # "mongodb/0",
237 # {"connection_string": "mongodb://mongo:27017"},
238 # )
239
240 # self.harness.charm.on.config_changed.emit()
241
242 # # Verifying status
243 # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus)
244
245
246 # if __name__ == "__main__":
247 # unittest.main()