| 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 | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 26 | from ops.model import ActiveStatus, BlockedStatus |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 27 | from ops.testing import Harness |
| 28 | |
| 29 | from charm import NbiCharm |
| 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 33 | """Prometheus Charm unit tests.""" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 37 | self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 38 | self.harness = Harness(NbiCharm) |
| 39 | self.harness.set_leader(is_leader=True) |
| 40 | self.harness.begin() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 50 | } |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 51 | self.harness.update_config(self.config) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 52 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 53 | def test_config_changed_no_relations( |
| 54 | self, |
| 55 | ) -> NoReturn: |
| 56 | """Test ingress resources without HTTP.""" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 57 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 58 | self.harness.charm.on.config_changed.emit() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 59 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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 | ) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 67 | ) |
| 68 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 75 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 76 | # Assertions |
| 77 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 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 | 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() |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 86 | # Verifying status |
| 87 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 88 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 89 | def test_with_relations_keystone_missing( |
| 90 | self, |
| 91 | ) -> NoReturn: |
| 92 | "Test with relations (keystone)" |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 93 | self.harness.update_config({"auth_backend": "keystone"}) |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 100 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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) |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 112 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 113 | def initialize_kafka_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 120 | def initialize_mongo_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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", |
| sousaedu | 4df5a46 | 2020-11-17 14:30:47 +0000 | [diff] [blame] | 126 | {"connection_string": "mongodb://mongo:27017"}, |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 127 | ) |
| 128 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 129 | def initialize_keystone_relation(self): |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 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, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 134 | "keystone", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 135 | { |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 136 | "host": "host", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 137 | "port": 5000, |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 148 | }, |
| 149 | ) |
| 150 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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") |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 154 | self.harness.update_relation_data( |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 155 | prometheus_relation_id, |
| 156 | "prometheus", |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 157 | {"hostname": "prometheus", "port": 9090}, |
| 158 | ) |
| 159 | |
| sousaedu | 6248fe6 | 2020-10-13 23:46:51 +0100 | [diff] [blame] | 160 | |
| 161 | if __name__ == "__main__": |
| 162 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 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() |