| sousaedu | abe7321 | 2020-11-04 15:13:35 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # Copyright 2020 Canonical Ltd. |
| 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 | |
| 23 | from typing import NoReturn |
| 24 | import unittest |
| 25 | from ops.model import BlockedStatus |
| 26 | |
| 27 | from ops.testing import Harness |
| 28 | |
| 29 | from charm import LcmCharm |
| 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| 33 | """LCM Charm unit tests.""" |
| 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| 37 | self.harness = Harness(LcmCharm) |
| 38 | self.harness.set_leader(is_leader=True) |
| 39 | self.harness.begin() |
| 40 | |
| 41 | def test_on_start_without_relations(self) -> NoReturn: |
| 42 | """Test installation without any relation.""" |
| 43 | self.harness.charm.on.start.emit() |
| 44 | |
| 45 | # Verifying status |
| 46 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 47 | |
| 48 | # Verifying status message |
| 49 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 50 | self.assertTrue( |
| 51 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 52 | ) |
| 53 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 54 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 55 | self.assertIn("ro", self.harness.charm.unit.status.message) |
| 56 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 57 | |
| 58 | def test_on_start_with_relations(self) -> NoReturn: |
| 59 | """Test deployment without keystone.""" |
| 60 | expected_result = { |
| 61 | "version": 3, |
| 62 | "containers": [ |
| 63 | { |
| 64 | "name": "lcm", |
| 65 | "imageDetails": self.harness.charm.image.fetch(), |
| 66 | "imagePullPolicy": "Always", |
| 67 | "ports": [ |
| 68 | { |
| 69 | "name": "lcm", |
| 70 | "containerPort": 9999, |
| 71 | "protocol": "TCP", |
| 72 | } |
| 73 | ], |
| 74 | "envConfig": { |
| 75 | "ALLOW_ANONYMOUS_LOGIN": "yes", |
| 76 | "OSMLCM_GLOBAL_LOGLEVEL": "INFO", |
| 77 | "OSMLCM_RO_HOST": "ro", |
| 78 | "OSMLCM_RO_PORT": 9090, |
| 79 | "OSMLCM_RO_TENANT": "osm", |
| 80 | "OSMLCM_MESSAGE_DRIVER": "kafka", |
| 81 | "OSMLCM_MESSAGE_HOST": "kafka", |
| 82 | "OSMLCM_MESSAGE_PORT": 9092, |
| 83 | "OSMLCM_DATABASE_DRIVER": "mongo", |
| 84 | "OSMLCM_DATABASE_URI": "mongodb://mongo:27017", |
| 85 | "OSMLCM_DATABASE_COMMONKEY": "osm", |
| 86 | "OSMLCM_STORAGE_DRIVER": "mongo", |
| 87 | "OSMLCM_STORAGE_PATH": "/app/storage", |
| 88 | "OSMLCM_STORAGE_COLLECTION": "files", |
| 89 | "OSMLCM_STORAGE_URI": "mongodb://mongo:27017", |
| 90 | "OSMLCM_VCA_HOST": "admin", |
| 91 | "OSMLCM_VCA_PORT": 17070, |
| 92 | "OSMLCM_VCA_USER": "admin", |
| 93 | "OSMLCM_VCA_PUBKEY": "secret", |
| 94 | "OSMLCM_VCA_SECRET": "secret", |
| 95 | "OSMLCM_VCA_CACERT": "", |
| 96 | "OSMLCM_VCA_CLOUD": "localhost", |
| 97 | "OSMLCM_VCA_K8S_CLOUD": "k8scloud", |
| 98 | }, |
| 99 | } |
| 100 | ], |
| 101 | "kubernetesResources": {"ingressResources": []}, |
| 102 | } |
| 103 | |
| 104 | self.harness.charm.on.start.emit() |
| 105 | |
| 106 | # Check if kafka datastore is initialized |
| 107 | self.assertIsNone(self.harness.charm.state.message_host) |
| 108 | self.assertIsNone(self.harness.charm.state.message_port) |
| 109 | |
| 110 | # Check if mongodb datastore is initialized |
| 111 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 112 | |
| 113 | # Check if RO datastore is initialized |
| 114 | self.assertIsNone(self.harness.charm.state.ro_host) |
| 115 | self.assertIsNone(self.harness.charm.state.ro_port) |
| 116 | |
| 117 | # Initializing the kafka relation |
| 118 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 119 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 120 | self.harness.update_relation_data( |
| 121 | kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 122 | ) |
| 123 | |
| 124 | # Initializing the mongo relation |
| 125 | mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 126 | self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 127 | self.harness.update_relation_data( |
| 128 | mongodb_relation_id, |
| 129 | "mongodb/0", |
| 130 | {"connection_string": "mongodb://mongo:27017"}, |
| 131 | ) |
| 132 | |
| 133 | # Initializing the RO relation |
| 134 | ro_relation_id = self.harness.add_relation("ro", "ro") |
| 135 | self.harness.add_relation_unit(ro_relation_id, "ro/0") |
| 136 | self.harness.update_relation_data( |
| 137 | ro_relation_id, "ro", {"host": "ro", "port": 9090} |
| 138 | ) |
| 139 | |
| 140 | # Checking if kafka data is stored |
| 141 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 142 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 143 | |
| 144 | # Checking if mongodb data is stored |
| 145 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 146 | |
| 147 | # Checking if RO data is stored |
| 148 | self.assertEqual(self.harness.charm.state.ro_host, "ro") |
| 149 | self.assertEqual(self.harness.charm.state.ro_port, 9090) |
| 150 | |
| 151 | # Verifying status |
| 152 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 153 | |
| 154 | pod_spec, _ = self.harness.get_pod_spec() |
| 155 | |
| 156 | self.assertDictEqual(expected_result, pod_spec) |
| 157 | |
| 158 | def test_on_kafka_relation_app_changed(self) -> NoReturn: |
| 159 | """Test to see if kafka relation is updated.""" |
| 160 | self.harness.charm.on.start.emit() |
| 161 | |
| 162 | self.assertIsNone(self.harness.charm.state.message_host) |
| 163 | self.assertIsNone(self.harness.charm.state.message_port) |
| 164 | |
| 165 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 166 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 167 | self.harness.update_relation_data( |
| 168 | relation_id, "kafka", {"host": "kafka", "port": 9092} |
| 169 | ) |
| 170 | |
| 171 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 172 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 173 | |
| 174 | # Verifying status |
| 175 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 176 | |
| 177 | # Verifying status message |
| 178 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 179 | self.assertTrue( |
| 180 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 181 | ) |
| 182 | self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 183 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 184 | self.assertIn("ro", self.harness.charm.unit.status.message) |
| 185 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 186 | |
| 187 | def test_on_kafka_relation_unit_changed(self) -> NoReturn: |
| 188 | """Test to see if kafka relation is updated.""" |
| 189 | self.harness.charm.on.start.emit() |
| 190 | |
| 191 | self.assertIsNone(self.harness.charm.state.message_host) |
| 192 | self.assertIsNone(self.harness.charm.state.message_port) |
| 193 | |
| 194 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 195 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 196 | self.harness.update_relation_data( |
| 197 | relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 198 | ) |
| 199 | |
| 200 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 201 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 202 | |
| 203 | # Verifying status |
| 204 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 205 | |
| 206 | # Verifying status message |
| 207 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 208 | self.assertTrue( |
| 209 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 210 | ) |
| 211 | self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 212 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 213 | self.assertIn("ro", self.harness.charm.unit.status.message) |
| 214 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 215 | |
| 216 | def test_on_mongodb_app_relation_changed(self) -> NoReturn: |
| 217 | """Test to see if mongodb relation is updated.""" |
| 218 | self.harness.charm.on.start.emit() |
| 219 | |
| 220 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 221 | |
| 222 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 223 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 224 | self.harness.update_relation_data( |
| 225 | relation_id, "mongodb", {"connection_string": "mongodb://mongo:27017"} |
| 226 | ) |
| 227 | |
| 228 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 229 | |
| 230 | # Verifying status |
| 231 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 232 | |
| 233 | # Verifying status message |
| 234 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 235 | self.assertTrue( |
| 236 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 237 | ) |
| 238 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 239 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 240 | self.assertIn("ro", self.harness.charm.unit.status.message) |
| 241 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 242 | |
| 243 | def test_on_mongodb_unit_relation_changed(self) -> NoReturn: |
| 244 | """Test to see if mongodb relation is updated.""" |
| 245 | self.harness.charm.on.start.emit() |
| 246 | |
| 247 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 248 | |
| 249 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 250 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 251 | self.harness.update_relation_data( |
| 252 | relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"} |
| 253 | ) |
| 254 | |
| 255 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 256 | |
| 257 | # Verifying status |
| 258 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 259 | |
| 260 | # Verifying status message |
| 261 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 262 | self.assertTrue( |
| 263 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 264 | ) |
| 265 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 266 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 267 | self.assertIn("ro", self.harness.charm.unit.status.message) |
| 268 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 269 | |
| 270 | def test_on_ro_app_relation_changed(self) -> NoReturn: |
| 271 | """Test to see if RO relation is updated.""" |
| 272 | self.harness.charm.on.start.emit() |
| 273 | |
| 274 | self.assertIsNone(self.harness.charm.state.ro_host) |
| 275 | self.assertIsNone(self.harness.charm.state.ro_port) |
| 276 | |
| 277 | relation_id = self.harness.add_relation("ro", "ro") |
| 278 | self.harness.add_relation_unit(relation_id, "ro/0") |
| 279 | self.harness.update_relation_data( |
| 280 | relation_id, "ro", {"host": "ro", "port": 9090} |
| 281 | ) |
| 282 | |
| 283 | self.assertEqual(self.harness.charm.state.ro_host, "ro") |
| 284 | self.assertEqual(self.harness.charm.state.ro_port, 9090) |
| 285 | |
| 286 | # Verifying status |
| 287 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 288 | |
| 289 | # Verifying status message |
| 290 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 291 | self.assertTrue( |
| 292 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 293 | ) |
| 294 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 295 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 296 | self.assertNotIn("ro", self.harness.charm.unit.status.message) |
| 297 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 298 | |
| 299 | def test_on_ro_unit_relation_changed(self) -> NoReturn: |
| 300 | """Test to see if RO relation is updated.""" |
| 301 | self.harness.charm.on.start.emit() |
| 302 | |
| 303 | self.assertIsNone(self.harness.charm.state.ro_host) |
| 304 | self.assertIsNone(self.harness.charm.state.ro_port) |
| 305 | |
| 306 | relation_id = self.harness.add_relation("ro", "ro") |
| 307 | self.harness.add_relation_unit(relation_id, "ro/0") |
| 308 | self.harness.update_relation_data( |
| 309 | relation_id, "ro/0", {"host": "ro", "port": 9090} |
| 310 | ) |
| 311 | |
| 312 | self.assertEqual(self.harness.charm.state.ro_host, "ro") |
| 313 | self.assertEqual(self.harness.charm.state.ro_port, 9090) |
| 314 | |
| 315 | # Verifying status |
| 316 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 317 | |
| 318 | # Verifying status message |
| 319 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 320 | self.assertTrue( |
| 321 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 322 | ) |
| 323 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 324 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 325 | self.assertNotIn("ro", self.harness.charm.unit.status.message) |
| 326 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 327 | |
| 328 | |
| 329 | if __name__ == "__main__": |
| 330 | unittest.main() |