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