| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [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 | cab58cb | 2020-11-04 18:48:17 +0000 | [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 | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 24 | from typing import NoReturn |
| 25 | import unittest |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 26 | |
| 27 | from charm import PolCharm |
| David Garcia | c753dc5 | 2021-03-17 15:28:47 +0100 | [diff] [blame] | 28 | from ops.model import ActiveStatus, BlockedStatus |
| 29 | from ops.testing import Harness |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 33 | """Pol Charm unit tests.""" |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [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 | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 38 | self.harness = Harness(PolCharm) |
| 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 | "log_level": "INFO", |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 43 | } |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 44 | self.harness.update_config(self.config) |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 45 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 46 | def test_config_changed_no_relations( |
| 47 | self, |
| 48 | ) -> NoReturn: |
| 49 | """Test ingress resources without HTTP.""" |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 50 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 51 | self.harness.charm.on.config_changed.emit() |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 52 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 53 | # Assertions |
| 54 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 55 | self.assertTrue( |
| 56 | all( |
| 57 | relation in self.harness.charm.unit.status.message |
| 58 | for relation in ["mongodb", "kafka"] |
| 59 | ) |
| 60 | ) |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 61 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 62 | def test_config_changed_non_leader( |
| 63 | self, |
| 64 | ) -> NoReturn: |
| 65 | """Test ingress resources without HTTP.""" |
| 66 | self.harness.set_leader(is_leader=False) |
| 67 | self.harness.charm.on.config_changed.emit() |
| 68 | |
| 69 | # Assertions |
| 70 | self.assertIsInstance(self.harness.charm.unit.status, ActiveStatus) |
| 71 | |
| 72 | def test_with_relations( |
| 73 | self, |
| 74 | ) -> NoReturn: |
| 75 | "Test with relations (internal)" |
| 76 | self.initialize_kafka_relation() |
| 77 | self.initialize_mongo_relation() |
| 78 | # Verifying status |
| 79 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 80 | |
| 81 | def initialize_kafka_relation(self): |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 82 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 83 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 84 | self.harness.update_relation_data( |
| 85 | kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 86 | ) |
| 87 | |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 88 | def initialize_mongo_relation(self): |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 89 | mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 90 | self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 91 | self.harness.update_relation_data( |
| 92 | mongodb_relation_id, |
| 93 | "mongodb/0", |
| 94 | {"connection_string": "mongodb://mongo:27017"}, |
| 95 | ) |
| 96 | |
| sousaedu | cab58cb | 2020-11-04 18:48:17 +0000 | [diff] [blame] | 97 | |
| 98 | if __name__ == "__main__": |
| 99 | unittest.main() |
| David Garcia | 49379ce | 2021-02-24 13:48:22 +0100 | [diff] [blame] | 100 | |
| 101 | |
| 102 | # class TestCharm(unittest.TestCase): |
| 103 | # """POL Charm unit tests.""" |
| 104 | |
| 105 | # def setUp(self) -> NoReturn: |
| 106 | # """Test setup""" |
| 107 | # self.harness = Harness(PolCharm) |
| 108 | # self.harness.set_leader(is_leader=True) |
| 109 | # self.harness.begin() |
| 110 | |
| 111 | # def test_on_start_without_relations(self) -> NoReturn: |
| 112 | # """Test installation without any relation.""" |
| 113 | # self.harness.charm.on.start.emit() |
| 114 | |
| 115 | # # Verifying status |
| 116 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 117 | |
| 118 | # # Verifying status message |
| 119 | # self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 120 | # self.assertTrue( |
| 121 | # self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 122 | # ) |
| 123 | # self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 124 | # self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 125 | # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 126 | |
| 127 | # def test_on_start_with_relations(self) -> NoReturn: |
| 128 | # """Test deployment without keystone.""" |
| 129 | # expected_result = { |
| 130 | # "version": 3, |
| 131 | # "containers": [ |
| 132 | # { |
| 133 | # "name": "pol", |
| 134 | # "imageDetails": self.harness.charm.image.fetch(), |
| 135 | # "imagePullPolicy": "Always", |
| 136 | # "ports": [ |
| 137 | # { |
| 138 | # "name": "pol", |
| 139 | # "containerPort": 80, |
| 140 | # "protocol": "TCP", |
| 141 | # } |
| 142 | # ], |
| 143 | # "envConfig": { |
| 144 | # "ALLOW_ANONYMOUS_LOGIN": "yes", |
| 145 | # "OSMPOL_GLOBAL_LOGLEVEL": "INFO", |
| 146 | # "OSMPOL_MESSAGE_HOST": "kafka", |
| 147 | # "OSMPOL_MESSAGE_DRIVER": "kafka", |
| 148 | # "OSMPOL_MESSAGE_PORT": 9092, |
| 149 | # "OSMPOL_DATABASE_DRIVER": "mongo", |
| 150 | # "OSMPOL_DATABASE_URI": "mongodb://mongo:27017", |
| 151 | # }, |
| 152 | # } |
| 153 | # ], |
| 154 | # "kubernetesResources": {"ingressResources": []}, |
| 155 | # } |
| 156 | |
| 157 | # self.harness.charm.on.start.emit() |
| 158 | |
| 159 | # # Check if kafka datastore is initialized |
| 160 | # self.assertIsNone(self.harness.charm.state.message_host) |
| 161 | # self.assertIsNone(self.harness.charm.state.message_port) |
| 162 | |
| 163 | # # Check if mongodb datastore is initialized |
| 164 | # self.assertIsNone(self.harness.charm.state.database_uri) |
| 165 | |
| 166 | # # Initializing the kafka relation |
| 167 | # kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 168 | # self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 169 | # self.harness.update_relation_data( |
| 170 | # kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 171 | # ) |
| 172 | |
| 173 | # # Initializing the mongo relation |
| 174 | # mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 175 | # self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 176 | # self.harness.update_relation_data( |
| 177 | # mongodb_relation_id, |
| 178 | # "mongodb/0", |
| 179 | # {"connection_string": "mongodb://mongo:27017"}, |
| 180 | # ) |
| 181 | |
| 182 | # # Checking if kafka data is stored |
| 183 | # self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 184 | # self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 185 | |
| 186 | # # Checking if mongodb data is stored |
| 187 | # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 188 | |
| 189 | # # Verifying status |
| 190 | # self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 191 | |
| 192 | # pod_spec, _ = self.harness.get_pod_spec() |
| 193 | |
| 194 | # self.assertDictEqual(expected_result, pod_spec) |
| 195 | |
| 196 | # def test_on_kafka_unit_relation_changed(self) -> NoReturn: |
| 197 | # """Test to see if kafka relation is updated.""" |
| 198 | # self.harness.charm.on.start.emit() |
| 199 | |
| 200 | # self.assertIsNone(self.harness.charm.state.message_host) |
| 201 | # self.assertIsNone(self.harness.charm.state.message_port) |
| 202 | |
| 203 | # relation_id = self.harness.add_relation("kafka", "kafka") |
| 204 | # self.harness.add_relation_unit(relation_id, "kafka/0") |
| 205 | # self.harness.update_relation_data( |
| 206 | # relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 207 | # ) |
| 208 | |
| 209 | # self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 210 | # self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 211 | |
| 212 | # # Verifying status |
| 213 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 214 | |
| 215 | # # Verifying status message |
| 216 | # self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 217 | # self.assertTrue( |
| 218 | # self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 219 | # ) |
| 220 | # self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 221 | # self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 222 | # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 223 | |
| 224 | # def test_on_mongodb_unit_relation_changed(self) -> NoReturn: |
| 225 | # """Test to see if mongodb relation is updated.""" |
| 226 | # self.harness.charm.on.start.emit() |
| 227 | |
| 228 | # self.assertIsNone(self.harness.charm.state.database_uri) |
| 229 | |
| 230 | # relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 231 | # self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 232 | # self.harness.update_relation_data( |
| 233 | # relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"} |
| 234 | # ) |
| 235 | |
| 236 | # self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 237 | |
| 238 | # # Verifying status |
| 239 | # self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 240 | |
| 241 | # # Verifying status message |
| 242 | # self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 243 | # self.assertTrue( |
| 244 | # self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 245 | # ) |
| 246 | # self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 247 | # self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 248 | # self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 249 | |
| 250 | |
| 251 | # if __name__ == "__main__": |
| 252 | # unittest.main() |