| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 2 | # Copyright 2021 Canonical Ltd. |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 23 | import sys |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 24 | from typing import NoReturn |
| 25 | import unittest |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 26 | |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 27 | |
| 28 | from charm import NbiCharm |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 29 | from ops.model import ActiveStatus, BlockedStatus |
| 30 | from ops.testing import Harness |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class TestCharm(unittest.TestCase): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 34 | """Prometheus Charm unit tests.""" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 35 | |
| 36 | def setUp(self) -> NoReturn: |
| 37 | """Test setup""" |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 38 | self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 39 | self.harness = Harness(NbiCharm) |
| 40 | self.harness.set_leader(is_leader=True) |
| 41 | self.harness.begin() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 42 | self.config = { |
| 43 | "enable_test": False, |
| 44 | "auth_backend": "internal", |
| 45 | "database_commonkey": "key", |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 46 | "mongodb_uri": "", |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 47 | "log_level": "INFO", |
| 48 | "max_file_size": 0, |
| 49 | "ingress_whitelist_source_range": "", |
| 50 | "tls_secret_name": "", |
| David Garcia | c395a45 | 2021-05-05 14:22:26 +0200 | [diff] [blame] | 51 | "site_url": "https://nbi.192.168.100.100.nip.io", |
| sousaedu | 3cc0316 | 2021-04-29 16:53:12 +0200 | [diff] [blame] | 52 | "cluster_issuer": "vault-issuer", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 53 | } |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 54 | self.harness.update_config(self.config) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 55 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 56 | def test_config_changed_no_relations( |
| 57 | self, |
| 58 | ) -> NoReturn: |
| 59 | """Test ingress resources without HTTP.""" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 60 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 61 | self.harness.charm.on.config_changed.emit() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 62 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 63 | # Assertions |
| 64 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 65 | self.assertTrue( |
| 66 | all( |
| 67 | relation in self.harness.charm.unit.status.message |
| 68 | for relation in ["mongodb", "kafka", "prometheus"] |
| 69 | ) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 70 | ) |
| 71 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 72 | def test_config_changed_non_leader( |
| 73 | self, |
| 74 | ) -> NoReturn: |
| 75 | """Test ingress resources without HTTP.""" |
| 76 | self.harness.set_leader(is_leader=False) |
| 77 | self.harness.charm.on.config_changed.emit() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 78 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 79 | # Assertions |
| 80 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 81 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 82 | def test_with_relations_internal_and_mongodb_config( |
| 83 | self, |
| 84 | ) -> NoReturn: |
| 85 | "Test with relations and mongodb config (internal)" |
| 86 | self.initialize_kafka_relation() |
| 87 | self.initialize_mongo_config() |
| 88 | self.initialize_prometheus_relation() |
| 89 | # Verifying status |
| 90 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 91 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 92 | def test_with_relations_internal( |
| 93 | self, |
| 94 | ) -> NoReturn: |
| 95 | "Test with relations (internal)" |
| 96 | self.initialize_kafka_relation() |
| 97 | self.initialize_mongo_relation() |
| 98 | self.initialize_prometheus_relation() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 99 | # Verifying status |
| 100 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 101 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 102 | def test_with_relations_and_mongodb_config_with_keystone_missing( |
| 103 | self, |
| 104 | ) -> NoReturn: |
| 105 | "Test with relations and mongodb config (keystone)" |
| 106 | self.harness.update_config({"auth_backend": "keystone"}) |
| 107 | self.initialize_kafka_relation() |
| 108 | self.initialize_mongo_config() |
| 109 | self.initialize_prometheus_relation() |
| 110 | # Verifying status |
| 111 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 112 | self.assertTrue("keystone" in self.harness.charm.unit.status.message) |
| 113 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 114 | def test_with_relations_keystone_missing( |
| 115 | self, |
| 116 | ) -> NoReturn: |
| 117 | "Test with relations (keystone)" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 118 | self.harness.update_config({"auth_backend": "keystone"}) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 119 | self.initialize_kafka_relation() |
| 120 | self.initialize_mongo_relation() |
| 121 | self.initialize_prometheus_relation() |
| 122 | # Verifying status |
| 123 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 124 | self.assertTrue("keystone" in self.harness.charm.unit.status.message) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 125 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 126 | def test_with_relations_and_mongodb_config_with_keystone( |
| 127 | self, |
| 128 | ) -> NoReturn: |
| 129 | "Test with relations (keystone)" |
| 130 | self.harness.update_config({"auth_backend": "keystone"}) |
| 131 | self.initialize_kafka_relation() |
| 132 | self.initialize_mongo_config() |
| 133 | self.initialize_prometheus_relation() |
| 134 | self.initialize_keystone_relation() |
| 135 | # Verifying status |
| 136 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 137 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 138 | def test_with_relations_keystone( |
| 139 | self, |
| 140 | ) -> NoReturn: |
| 141 | "Test with relations (keystone)" |
| 142 | self.harness.update_config({"auth_backend": "keystone"}) |
| 143 | self.initialize_kafka_relation() |
| 144 | self.initialize_mongo_relation() |
| 145 | self.initialize_prometheus_relation() |
| 146 | self.initialize_keystone_relation() |
| 147 | # Verifying status |
| 148 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 149 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 150 | def test_mongodb_exception_relation_and_config( |
| 151 | self, |
| 152 | ) -> NoReturn: |
| 153 | self.initialize_mongo_config() |
| 154 | self.initialize_mongo_relation() |
| 155 | # Verifying status |
| 156 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 157 | |
| Guillermo Calvino | 4a46c6e | 2023-03-23 13:40:33 +0100 | [diff] [blame] | 158 | def test_config_liveness_probe_with_relations( |
| 159 | self, |
| 160 | ) -> NoReturn: |
| 161 | "Test configuration of liveness probe with relations" |
| 162 | self.initialize_nbi_with_keystone() |
| 163 | self.config_liveness_probe() |
| 164 | # Verifying status |
| 165 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 166 | |
| 167 | def test_wrong_config_liveness_probe_with_relations( |
| 168 | self, |
| 169 | ) -> NoReturn: |
| 170 | "Test wrong configuration of liveness probe with relations" |
| 171 | self.initialize_nbi_with_keystone() |
| 172 | self.wrong_config_liveness_probe() |
| 173 | # Verifying status |
| 174 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 175 | |
| 176 | def test_config_readiness_probe_with_relations( |
| 177 | self, |
| 178 | ) -> NoReturn: |
| 179 | "Test configuration of readiness probe with relations" |
| 180 | self.initialize_nbi_with_keystone() |
| 181 | self.config_readiness_probe() |
| 182 | # Verifying status |
| 183 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 184 | |
| 185 | def test_wrong_config_readiness_probe_with_relations( |
| 186 | self, |
| 187 | ) -> NoReturn: |
| 188 | "Test wrong configuration of readiness probe with relations" |
| 189 | self.initialize_nbi_with_keystone() |
| 190 | self.wrong_config_readiness_probe() |
| 191 | # Verifying status |
| 192 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 193 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 194 | def initialize_kafka_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 195 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 196 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 197 | self.harness.update_relation_data( |
| David Garcia | 4a0db7c | 2022-02-21 11:48:11 +0100 | [diff] [blame] | 198 | kafka_relation_id, "kafka", {"host": "kafka", "port": 9092} |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 199 | ) |
| 200 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 201 | def initialize_mongo_config(self): |
| 202 | self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"}) |
| 203 | |
| Guillermo Calvino | 4a46c6e | 2023-03-23 13:40:33 +0100 | [diff] [blame] | 204 | def initialize_nbi_with_keystone(self): |
| 205 | self.harness.update_config({"auth_backend": "keystone"}) |
| 206 | self.initialize_kafka_relation() |
| 207 | self.initialize_mongo_relation() |
| 208 | self.initialize_prometheus_relation() |
| 209 | self.initialize_keystone_relation() |
| 210 | |
| 211 | def config_liveness_probe(self): |
| 212 | self.harness.update_config( |
| 213 | {"tcpsocket_liveness_probe": '{"initial_delay_seconds": 10}'} |
| 214 | ) |
| 215 | |
| 216 | def wrong_config_liveness_probe(self): |
| 217 | self.harness.update_config( |
| 218 | {"tcpsocket_liveness_probe": "{initial_delay_seconds: 10}"} |
| 219 | ) |
| 220 | |
| 221 | def wrong_config_readiness_probe(self): |
| 222 | self.harness.update_config( |
| 223 | {"tcpsocket_readiness_probe": "{initial_delay_seconds: 10}"} |
| 224 | ) |
| 225 | |
| 226 | def config_readiness_probe(self): |
| 227 | self.harness.update_config( |
| 228 | {"tcpsocket_readiness_probe": '{"initial_delay_seconds": 10}'} |
| 229 | ) |
| 230 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 231 | def initialize_mongo_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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", |
| sousaedu | 4df5a46 | 2020-11-17 14:30:47 +0000 | [diff] [blame] | 237 | {"connection_string": "mongodb://mongo:27017"}, |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 238 | ) |
| 239 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 240 | def initialize_keystone_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 241 | keystone_relation_id = self.harness.add_relation("keystone", "keystone") |
| 242 | self.harness.add_relation_unit(keystone_relation_id, "keystone/0") |
| 243 | self.harness.update_relation_data( |
| 244 | keystone_relation_id, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 245 | "keystone", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 246 | { |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 247 | "host": "host", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 248 | "port": 5000, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 249 | "user_domain_name": "ud", |
| 250 | "project_domain_name": "pd", |
| 251 | "username": "u", |
| 252 | "password": "p", |
| 253 | "service": "s", |
| 254 | "keystone_db_password": "something", |
| 255 | "region_id": "something", |
| 256 | "admin_username": "something", |
| 257 | "admin_password": "something", |
| 258 | "admin_project_name": "something", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 259 | }, |
| 260 | ) |
| 261 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 262 | def initialize_prometheus_relation(self): |
| 263 | prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus") |
| 264 | self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0") |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 265 | self.harness.update_relation_data( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 266 | prometheus_relation_id, |
| 267 | "prometheus", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 268 | {"hostname": "prometheus", "port": 9090}, |
| 269 | ) |
| 270 | |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 271 | |
| 272 | if __name__ == "__main__": |
| 273 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 274 | |
| 275 | |
| 276 | # class TestCharm(unittest.TestCase): |
| 277 | # """Prometheus Charm unit tests.""" |
| 278 | |
| 279 | # def setUp(self) -> NoReturn: |
| 280 | # """Test setup""" |
| 281 | # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| 282 | # self.harness = Harness(NbiCharm) |
| 283 | # self.harness.set_leader(is_leader=True) |
| 284 | # self.harness.begin() |
| 285 | # self.config = { |
| 286 | # "enable_ng_ro": True, |
| 287 | # "database_commonkey": "commonkey", |
| 288 | # "log_level": "INFO", |
| 289 | # "vim_database": "db_name", |
| 290 | # "ro_database": "ro_db_name", |
| 291 | # "openmano_tenant": "mano", |
| 292 | # } |
| 293 | |
| 294 | # def test_config_changed_no_relations( |
| 295 | # self, |
| 296 | # ) -> NoReturn: |
| 297 | # """Test ingress resources without HTTP.""" |
| 298 | |
| 299 | # self.harness.charm.on.config_changed.emit() |
| 300 | |
| 301 | # # Assertions |
| 302 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 303 | # self.assertTrue( |
| 304 | # all( |
| 305 | # relation in self.harness.charm.unit.status.message |
| 306 | # for relation in ["mongodb", "kafka"] |
| 307 | # ) |
| 308 | # ) |
| 309 | |
| 310 | # # Disable ng-ro |
| 311 | # self.harness.update_config({"enable_ng_ro": False}) |
| 312 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 313 | # self.assertTrue( |
| 314 | # all( |
| 315 | # relation in self.harness.charm.unit.status.message |
| 316 | # for relation in ["mysql"] |
| 317 | # ) |
| 318 | # ) |
| 319 | |
| 320 | # def test_config_changed_non_leader( |
| 321 | # self, |
| 322 | # ) -> NoReturn: |
| 323 | # """Test ingress resources without HTTP.""" |
| 324 | # self.harness.set_leader(is_leader=False) |
| 325 | # self.harness.charm.on.config_changed.emit() |
| 326 | |
| 327 | # # Assertions |
| 328 | # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 329 | |
| 330 | # def test_with_relations_ng( |
| 331 | # self, |
| 332 | # ) -> NoReturn: |
| 333 | # "Test with relations (ng-ro)" |
| 334 | |
| 335 | # # Initializing the kafka relation |
| 336 | # kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 337 | # self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 338 | # self.harness.update_relation_data( |
| 339 | # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 340 | # ) |
| 341 | |
| 342 | # # Initializing the mongo relation |
| 343 | # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 344 | # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 345 | # self.harness.update_relation_data( |
| 346 | # mongodb_relation_id, |
| 347 | # "mongodb/0", |
| 348 | # {"connection_string": "mongodb://mongo:27017"}, |
| 349 | # ) |
| 350 | |
| 351 | # self.harness.charm.on.config_changed.emit() |
| 352 | |
| 353 | # # Verifying status |
| 354 | # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 355 | |
| 356 | |
| 357 | # if __name__ == "__main__": |
| 358 | # unittest.main() |