| 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", |
| 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", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 51 | } |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 52 | self.harness.update_config(self.config) |
| 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 | def test_config_changed_no_relations( |
| 55 | self, |
| 56 | ) -> NoReturn: |
| 57 | """Test ingress resources without HTTP.""" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 58 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 59 | self.harness.charm.on.config_changed.emit() |
| 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 | # 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 | ) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 68 | ) |
| 69 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 76 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 77 | # Assertions |
| 78 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 79 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 87 | # Verifying status |
| 88 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 89 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 90 | def test_with_relations_keystone_missing( |
| 91 | self, |
| 92 | ) -> NoReturn: |
| 93 | "Test with relations (keystone)" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 94 | self.harness.update_config({"auth_backend": "keystone"}) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 101 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 113 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 114 | def initialize_kafka_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 121 | def initialize_mongo_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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", |
| sousaedu | 4df5a46 | 2020-11-17 14:30:47 +0000 | [diff] [blame] | 127 | {"connection_string": "mongodb://mongo:27017"}, |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 128 | ) |
| 129 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 130 | def initialize_keystone_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 135 | "keystone", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 136 | { |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 137 | "host": "host", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 138 | "port": 5000, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 149 | }, |
| 150 | ) |
| 151 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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") |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 155 | self.harness.update_relation_data( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 156 | prometheus_relation_id, |
| 157 | "prometheus", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 158 | {"hostname": "prometheus", "port": 9090}, |
| 159 | ) |
| 160 | |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 161 | |
| 162 | if __name__ == "__main__": |
| 163 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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() |