| 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 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 158 | def initialize_kafka_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 159 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 160 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 161 | self.harness.update_relation_data( |
| David Garcia | 4a0db7c | 2022-02-21 11:48:11 +0100 | [diff] [blame] | 162 | kafka_relation_id, "kafka", {"host": "kafka", "port": 9092} |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 163 | ) |
| 164 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 165 | def initialize_mongo_config(self): |
| 166 | self.harness.update_config({"mongodb_uri": "mongodb://mongo:27017"}) |
| 167 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 168 | def initialize_mongo_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 169 | mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 170 | self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 171 | self.harness.update_relation_data( |
| 172 | mongodb_relation_id, |
| 173 | "mongodb/0", |
| sousaedu | 4df5a46 | 2020-11-17 14:30:47 +0000 | [diff] [blame] | 174 | {"connection_string": "mongodb://mongo:27017"}, |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 175 | ) |
| 176 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 177 | def initialize_keystone_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 178 | keystone_relation_id = self.harness.add_relation("keystone", "keystone") |
| 179 | self.harness.add_relation_unit(keystone_relation_id, "keystone/0") |
| 180 | self.harness.update_relation_data( |
| 181 | keystone_relation_id, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 182 | "keystone", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 183 | { |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 184 | "host": "host", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 185 | "port": 5000, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 186 | "user_domain_name": "ud", |
| 187 | "project_domain_name": "pd", |
| 188 | "username": "u", |
| 189 | "password": "p", |
| 190 | "service": "s", |
| 191 | "keystone_db_password": "something", |
| 192 | "region_id": "something", |
| 193 | "admin_username": "something", |
| 194 | "admin_password": "something", |
| 195 | "admin_project_name": "something", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 196 | }, |
| 197 | ) |
| 198 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 199 | def initialize_prometheus_relation(self): |
| 200 | prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus") |
| 201 | self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0") |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 202 | self.harness.update_relation_data( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 203 | prometheus_relation_id, |
| 204 | "prometheus", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 205 | {"hostname": "prometheus", "port": 9090}, |
| 206 | ) |
| 207 | |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 208 | |
| 209 | if __name__ == "__main__": |
| 210 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 211 | |
| 212 | |
| 213 | # class TestCharm(unittest.TestCase): |
| 214 | # """Prometheus Charm unit tests.""" |
| 215 | |
| 216 | # def setUp(self) -> NoReturn: |
| 217 | # """Test setup""" |
| 218 | # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| 219 | # self.harness = Harness(NbiCharm) |
| 220 | # self.harness.set_leader(is_leader=True) |
| 221 | # self.harness.begin() |
| 222 | # self.config = { |
| 223 | # "enable_ng_ro": True, |
| 224 | # "database_commonkey": "commonkey", |
| 225 | # "log_level": "INFO", |
| 226 | # "vim_database": "db_name", |
| 227 | # "ro_database": "ro_db_name", |
| 228 | # "openmano_tenant": "mano", |
| 229 | # } |
| 230 | |
| 231 | # def test_config_changed_no_relations( |
| 232 | # self, |
| 233 | # ) -> NoReturn: |
| 234 | # """Test ingress resources without HTTP.""" |
| 235 | |
| 236 | # self.harness.charm.on.config_changed.emit() |
| 237 | |
| 238 | # # Assertions |
| 239 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 240 | # self.assertTrue( |
| 241 | # all( |
| 242 | # relation in self.harness.charm.unit.status.message |
| 243 | # for relation in ["mongodb", "kafka"] |
| 244 | # ) |
| 245 | # ) |
| 246 | |
| 247 | # # Disable ng-ro |
| 248 | # self.harness.update_config({"enable_ng_ro": False}) |
| 249 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 250 | # self.assertTrue( |
| 251 | # all( |
| 252 | # relation in self.harness.charm.unit.status.message |
| 253 | # for relation in ["mysql"] |
| 254 | # ) |
| 255 | # ) |
| 256 | |
| 257 | # def test_config_changed_non_leader( |
| 258 | # self, |
| 259 | # ) -> NoReturn: |
| 260 | # """Test ingress resources without HTTP.""" |
| 261 | # self.harness.set_leader(is_leader=False) |
| 262 | # self.harness.charm.on.config_changed.emit() |
| 263 | |
| 264 | # # Assertions |
| 265 | # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 266 | |
| 267 | # def test_with_relations_ng( |
| 268 | # self, |
| 269 | # ) -> NoReturn: |
| 270 | # "Test with relations (ng-ro)" |
| 271 | |
| 272 | # # Initializing the kafka relation |
| 273 | # kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 274 | # self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 275 | # self.harness.update_relation_data( |
| 276 | # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 277 | # ) |
| 278 | |
| 279 | # # Initializing the mongo relation |
| 280 | # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 281 | # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 282 | # self.harness.update_relation_data( |
| 283 | # mongodb_relation_id, |
| 284 | # "mongodb/0", |
| 285 | # {"connection_string": "mongodb://mongo:27017"}, |
| 286 | # ) |
| 287 | |
| 288 | # self.harness.charm.on.config_changed.emit() |
| 289 | |
| 290 | # # Verifying status |
| 291 | # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 292 | |
| 293 | |
| 294 | # if __name__ == "__main__": |
| 295 | # unittest.main() |