| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +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. |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +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 |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 24 | from typing import NoReturn |
| 25 | import unittest |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 26 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 27 | |
| 28 | from charm import KeystoneCharm |
| 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 |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class TestCharm(unittest.TestCase): |
| 34 | """Keystone Charm unit tests.""" |
| 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() |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 39 | self.harness = Harness(KeystoneCharm) |
| 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 | "region_id": "str", |
| 44 | "keystone_db_password": "str", |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 45 | "mysql_host": "", |
| 46 | "mysql_port": 3306, |
| 47 | "mysql_root_password": "manopw", |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 48 | "admin_username": "str", |
| 49 | "admin_password": "str", |
| 50 | "admin_project": "str", |
| 51 | "service_username": "str", |
| 52 | "service_password": "str", |
| 53 | "service_project": "str", |
| 54 | "user_domain_name": "str", |
| 55 | "project_domain_name": "str", |
| 56 | "token_expiration": 10, |
| 57 | "max_file_size": 1, |
| 58 | "site_url": "http://keystone.com", |
| 59 | "ldap_enabled": False, |
| 60 | } |
| 61 | self.harness.update_config(self.config) |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 62 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 63 | def test_config_changed_no_relations( |
| 64 | self, |
| 65 | ) -> NoReturn: |
| 66 | """Test ingress resources without HTTP.""" |
| 67 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 68 | self.harness.charm.on.config_changed.emit() |
| 69 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 70 | # Assertions |
| 71 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 72 | self.assertTrue( |
| 73 | all( |
| 74 | relation in self.harness.charm.unit.status.message |
| 75 | for relation in ["mysql"] |
| 76 | ) |
| 77 | ) |
| 78 | |
| 79 | def test_config_changed_non_leader( |
| 80 | self, |
| 81 | ) -> NoReturn: |
| 82 | """Test ingress resources without HTTP.""" |
| 83 | self.harness.set_leader(is_leader=False) |
| 84 | self.harness.charm.on.config_changed.emit() |
| 85 | |
| 86 | # Assertions |
| 87 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 88 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 89 | def test_with_config(self) -> NoReturn: |
| 90 | "Test with mysql config" |
| 91 | self.initialize_mysql_config() |
| 92 | # Verifying status |
| 93 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 94 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 95 | def test_with_relations( |
| 96 | self, |
| 97 | ) -> NoReturn: |
| 98 | "Test with relations" |
| 99 | self.initialize_mysql_relation() |
| 100 | # Verifying status |
| 101 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 102 | |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 103 | def test_exception_mysql_relation_and_config( |
| 104 | self, |
| 105 | ) -> NoReturn: |
| 106 | "Test with relations and config. Must throw exception" |
| 107 | self.initialize_mysql_config() |
| 108 | self.initialize_mysql_relation() |
| 109 | # Verifying status |
| 110 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 111 | |
| 112 | def initialize_mysql_config(self): |
| David Garcia | 141d935 | 2021-09-08 17:48:40 +0200 | [diff] [blame] | 113 | self.harness.update_config({"mysql_uri": "mysql://root:manopw@mysql:3306/"}) |
| sousaedu | 996a560 | 2021-05-03 00:22:43 +0200 | [diff] [blame] | 114 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 115 | def initialize_mysql_relation(self): |
| 116 | relation_id = self.harness.add_relation("db", "mysql") |
| 117 | self.harness.add_relation_unit(relation_id, "mysql/0") |
| 118 | self.harness.update_relation_data( |
| 119 | relation_id, |
| 120 | "mysql/0", |
| 121 | { |
| 122 | "host": "mysql", |
| 123 | "port": 3306, |
| 124 | "user": "mano", |
| 125 | "password": "manopw", |
| 126 | "root_password": "rootmanopw", |
| 127 | }, |
| 128 | ) |
| 129 | |
| David Garcia | 95ba7e1 | 2021-02-03 11:10:28 +0100 | [diff] [blame] | 130 | |
| 131 | if __name__ == "__main__": |
| 132 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 133 | |
| 134 | |
| 135 | # class TestCharm(unittest.TestCase): |
| 136 | # """Prometheus Charm unit tests.""" |
| 137 | |
| 138 | # def setUp(self) -> NoReturn: |
| 139 | # """Test setup""" |
| 140 | # self.image_info = sys.modules["oci_image"].OCIImageResource().fetch() |
| 141 | # self.harness = Harness(KeystoneCharm) |
| 142 | # self.harness.set_leader(is_leader=True) |
| 143 | # self.harness.begin() |
| 144 | # self.config = { |
| 145 | # "enable_ng_ro": True, |
| 146 | # "database_commonkey": "commonkey", |
| 147 | # "log_level": "INFO", |
| 148 | # "vim_database": "db_name", |
| 149 | # "ro_database": "ro_db_name", |
| 150 | # "openmano_tenant": "mano", |
| 151 | # } |
| 152 | |
| 153 | # def test_config_changed_no_relations( |
| 154 | # self, |
| 155 | # ) -> NoReturn: |
| 156 | # """Test ingress resources without HTTP.""" |
| 157 | |
| 158 | # self.harness.charm.on.config_changed.emit() |
| 159 | |
| 160 | # # Assertions |
| 161 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 162 | # self.assertTrue( |
| 163 | # all( |
| 164 | # relation in self.harness.charm.unit.status.message |
| 165 | # for relation in ["mongodb", "kafka"] |
| 166 | # ) |
| 167 | # ) |
| 168 | |
| 169 | # # Disable ng-ro |
| 170 | # self.harness.update_config({"enable_ng_ro": False}) |
| 171 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 172 | # self.assertTrue( |
| 173 | # all( |
| 174 | # relation in self.harness.charm.unit.status.message |
| 175 | # for relation in ["mysql"] |
| 176 | # ) |
| 177 | # ) |
| 178 | |
| 179 | # def test_config_changed_non_leader( |
| 180 | # self, |
| 181 | # ) -> NoReturn: |
| 182 | # """Test ingress resources without HTTP.""" |
| 183 | # self.harness.set_leader(is_leader=False) |
| 184 | # self.harness.charm.on.config_changed.emit() |
| 185 | |
| 186 | # # Assertions |
| 187 | # self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 188 | |
| 189 | # def test_with_relations_ng( |
| 190 | # self, |
| 191 | # ) -> NoReturn: |
| 192 | # "Test with relations (ng-ro)" |
| 193 | |
| 194 | # # Initializing the kafka relation |
| 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( |
| 198 | # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 199 | # ) |
| 200 | |
| 201 | # # Initializing the mongo relation |
| 202 | # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 203 | # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 204 | # self.harness.update_relation_data( |
| 205 | # mongodb_relation_id, |
| 206 | # "mongodb/0", |
| 207 | # {"connection_string": "mongodb://mongo:27017"}, |
| 208 | # ) |
| 209 | |
| 210 | # self.harness.charm.on.config_changed.emit() |
| 211 | |
| 212 | # # Verifying status |
| 213 | # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 214 | |
| 215 | |
| 216 | # if __name__ == "__main__": |
| 217 | # unittest.main() |