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