| sousaedu | ccfacbb | 2020-11-04 21:44:01 +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 RoCharm |
| 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| 33 | """RO Charm unit tests.""" |
| 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| 37 | self.harness = Harness(RoCharm) |
| 38 | self.harness.set_leader(is_leader=True) |
| 39 | self.harness.begin() |
| 40 | |
| 41 | def test_on_start_without_relations_ng_ro(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.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 56 | |
| 57 | def test_on_start_without_relations_no_ng_ro(self) -> NoReturn: |
| 58 | """Test installation without any relation.""" |
| 59 | self.harness.update_config({"enable_ng_ro": False}) |
| 60 | |
| 61 | self.harness.charm.on.start.emit() |
| 62 | |
| 63 | # Verifying status |
| 64 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 65 | |
| 66 | # Verifying status message |
| 67 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 68 | self.assertTrue( |
| 69 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 70 | ) |
| 71 | self.assertIn("mysql", self.harness.charm.unit.status.message) |
| 72 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 73 | |
| 74 | def test_on_start_with_relations_ng_ro(self) -> NoReturn: |
| 75 | """Test deployment with NG-RO.""" |
| 76 | expected_result = { |
| 77 | "version": 3, |
| 78 | "containers": [ |
| 79 | { |
| 80 | "name": "ro", |
| 81 | "imageDetails": self.harness.charm.image.fetch(), |
| 82 | "imagePullPolicy": "Always", |
| 83 | "ports": [ |
| 84 | { |
| 85 | "name": "ro", |
| 86 | "containerPort": 9090, |
| 87 | "protocol": "TCP", |
| 88 | } |
| 89 | ], |
| 90 | "envConfig": { |
| 91 | "OSMRO_LOG_LEVEL": "INFO", |
| 92 | "OSMRO_MESSAGE_DRIVER": "kafka", |
| 93 | "OSMRO_MESSAGE_HOST": "kafka", |
| sousaedu | 0bb922b | 2021-01-18 17:53:28 +0000 | [diff] [blame] | 94 | "OSMRO_MESSAGE_PORT": "9090", |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 95 | "OSMRO_DATABASE_DRIVER": "mongo", |
| 96 | "OSMRO_DATABASE_URI": "mongodb://mongo", |
| 97 | "OSMRO_DATABASE_COMMONKEY": "osm", |
| 98 | }, |
| 99 | "kubernetes": { |
| 100 | "startupProbe": { |
| 101 | "exec": {"command": ["/usr/bin/pgrep", "python3"]}, |
| 102 | "initialDelaySeconds": 60, |
| 103 | "timeoutSeconds": 5, |
| 104 | }, |
| 105 | "readinessProbe": { |
| 106 | "httpGet": { |
| 107 | "path": "/openmano/tenants", |
| 108 | "port": 9090, |
| 109 | }, |
| 110 | "periodSeconds": 10, |
| 111 | "timeoutSeconds": 5, |
| 112 | "successThreshold": 1, |
| 113 | "failureThreshold": 3, |
| 114 | }, |
| 115 | "livenessProbe": { |
| 116 | "httpGet": { |
| 117 | "path": "/openmano/tenants", |
| 118 | "port": 9090, |
| 119 | }, |
| 120 | "initialDelaySeconds": 600, |
| 121 | "periodSeconds": 10, |
| 122 | "timeoutSeconds": 5, |
| 123 | "successThreshold": 1, |
| 124 | "failureThreshold": 3, |
| 125 | }, |
| 126 | }, |
| 127 | } |
| 128 | ], |
| 129 | "kubernetesResources": {"ingressResources": []}, |
| 130 | } |
| 131 | |
| 132 | self.harness.charm.on.start.emit() |
| 133 | |
| 134 | # Initializing the kafka relation |
| 135 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 136 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 137 | self.harness.update_relation_data( |
| 138 | relation_id, |
| 139 | "kafka/0", |
| 140 | { |
| 141 | "host": "kafka", |
| sousaedu | 0bb922b | 2021-01-18 17:53:28 +0000 | [diff] [blame] | 142 | "port": "9090", |
| sousaedu | ccfacbb | 2020-11-04 21:44:01 +0000 | [diff] [blame] | 143 | }, |
| 144 | ) |
| 145 | |
| 146 | # Initializing the mongodb relation |
| 147 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 148 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 149 | self.harness.update_relation_data( |
| 150 | relation_id, |
| 151 | "mongodb/0", |
| 152 | { |
| 153 | "connection_string": "mongodb://mongo", |
| 154 | }, |
| 155 | ) |
| 156 | |
| 157 | # Verifying status |
| 158 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 159 | |
| 160 | pod_spec, _ = self.harness.get_pod_spec() |
| 161 | |
| 162 | self.assertDictEqual(expected_result, pod_spec) |
| 163 | |
| 164 | def test_on_start_with_relations_no_ng_ro(self) -> NoReturn: |
| 165 | """Test deployment with old RO.""" |
| 166 | self.harness.update_config({"enable_ng_ro": False}) |
| 167 | |
| 168 | expected_result = { |
| 169 | "version": 3, |
| 170 | "containers": [ |
| 171 | { |
| 172 | "name": "ro", |
| 173 | "imageDetails": self.harness.charm.image.fetch(), |
| 174 | "imagePullPolicy": "Always", |
| 175 | "ports": [ |
| 176 | { |
| 177 | "name": "ro", |
| 178 | "containerPort": 9090, |
| 179 | "protocol": "TCP", |
| 180 | } |
| 181 | ], |
| 182 | "envConfig": { |
| 183 | "OSMRO_LOG_LEVEL": "INFO", |
| 184 | "RO_DB_HOST": "mysql", |
| 185 | "RO_DB_OVIM_HOST": "mysql", |
| 186 | "RO_DB_PORT": 3306, |
| 187 | "RO_DB_OVIM_PORT": 3306, |
| 188 | "RO_DB_USER": "mano", |
| 189 | "RO_DB_OVIM_USER": "mano", |
| 190 | "RO_DB_PASSWORD": "manopw", |
| 191 | "RO_DB_OVIM_PASSWORD": "manopw", |
| 192 | "RO_DB_ROOT_PASSWORD": "rootmanopw", |
| 193 | "RO_DB_OVIM_ROOT_PASSWORD": "rootmanopw", |
| 194 | "RO_DB_NAME": "mano_db", |
| 195 | "RO_DB_OVIM_NAME": "mano_vim_db", |
| 196 | "OPENMANO_TENANT": "osm", |
| 197 | }, |
| 198 | "kubernetes": { |
| 199 | "startupProbe": { |
| 200 | "exec": {"command": ["/usr/bin/pgrep", "python3"]}, |
| 201 | "initialDelaySeconds": 60, |
| 202 | "timeoutSeconds": 5, |
| 203 | }, |
| 204 | "readinessProbe": { |
| 205 | "httpGet": { |
| 206 | "path": "/openmano/tenants", |
| 207 | "port": 9090, |
| 208 | }, |
| 209 | "periodSeconds": 10, |
| 210 | "timeoutSeconds": 5, |
| 211 | "successThreshold": 1, |
| 212 | "failureThreshold": 3, |
| 213 | }, |
| 214 | "livenessProbe": { |
| 215 | "httpGet": { |
| 216 | "path": "/openmano/tenants", |
| 217 | "port": 9090, |
| 218 | }, |
| 219 | "initialDelaySeconds": 600, |
| 220 | "periodSeconds": 10, |
| 221 | "timeoutSeconds": 5, |
| 222 | "successThreshold": 1, |
| 223 | "failureThreshold": 3, |
| 224 | }, |
| 225 | }, |
| 226 | } |
| 227 | ], |
| 228 | "kubernetesResources": {"ingressResources": []}, |
| 229 | } |
| 230 | |
| 231 | self.harness.charm.on.start.emit() |
| 232 | |
| 233 | # Initializing the mysql relation |
| 234 | relation_id = self.harness.add_relation("mysql", "mysql") |
| 235 | self.harness.add_relation_unit(relation_id, "mysql/0") |
| 236 | self.harness.update_relation_data( |
| 237 | relation_id, |
| 238 | "mysql/0", |
| 239 | { |
| 240 | "host": "mysql", |
| 241 | "port": 3306, |
| 242 | "user": "mano", |
| 243 | "password": "manopw", |
| 244 | "root_password": "rootmanopw", |
| 245 | }, |
| 246 | ) |
| 247 | |
| 248 | # Verifying status |
| 249 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 250 | |
| 251 | pod_spec, _ = self.harness.get_pod_spec() |
| 252 | |
| 253 | self.assertDictEqual(expected_result, pod_spec) |
| 254 | |
| 255 | def test_on_kafka_unit_relation_changed(self) -> NoReturn: |
| 256 | """Test to see if kafka relation is updated.""" |
| 257 | self.harness.charm.on.start.emit() |
| 258 | |
| 259 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 260 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 261 | self.harness.update_relation_data( |
| 262 | relation_id, |
| 263 | "kafka/0", |
| 264 | { |
| 265 | "host": "kafka", |
| 266 | "port": 9090, |
| 267 | }, |
| 268 | ) |
| 269 | |
| 270 | # Verifying status |
| 271 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 272 | |
| 273 | # Verifying status message |
| 274 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 275 | self.assertTrue( |
| 276 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 277 | ) |
| 278 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 279 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 280 | |
| 281 | def test_on_mongodb_unit_relation_changed(self) -> NoReturn: |
| 282 | """Test to see if mongodb relation is updated.""" |
| 283 | self.harness.charm.on.start.emit() |
| 284 | |
| 285 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 286 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 287 | self.harness.update_relation_data( |
| 288 | relation_id, |
| 289 | "mongodb/0", |
| 290 | { |
| 291 | "connection_string": "mongodb://mongo", |
| 292 | }, |
| 293 | ) |
| 294 | |
| 295 | # Verifying status |
| 296 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 297 | |
| 298 | # Verifying status message |
| 299 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 300 | self.assertTrue( |
| 301 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 302 | ) |
| 303 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 304 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 305 | |
| 306 | def test_on_mysql_unit_relation_changed(self) -> NoReturn: |
| 307 | """Test to see if mysql relation is updated.""" |
| 308 | self.harness.charm.on.start.emit() |
| 309 | |
| 310 | relation_id = self.harness.add_relation("mysql", "mysql") |
| 311 | self.harness.add_relation_unit(relation_id, "mysql/0") |
| 312 | self.harness.update_relation_data( |
| 313 | relation_id, |
| 314 | "mysql/0", |
| 315 | { |
| 316 | "host": "mysql", |
| 317 | "port": 3306, |
| 318 | "user": "mano", |
| 319 | "password": "manopw", |
| 320 | "root_password": "rootmanopw", |
| 321 | }, |
| 322 | ) |
| 323 | |
| 324 | # Verifying status |
| 325 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 326 | |
| 327 | # Verifying status message |
| 328 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 329 | self.assertTrue( |
| 330 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 331 | ) |
| 332 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 333 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 334 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 335 | |
| 336 | def test_publish_ro_info(self) -> NoReturn: |
| 337 | """Test to see if ro relation is updated.""" |
| 338 | expected_result = { |
| 339 | "host": "ro", |
| 340 | "port": "9090", |
| 341 | } |
| 342 | |
| 343 | self.harness.charm.on.start.emit() |
| 344 | |
| 345 | relation_id = self.harness.add_relation("ro", "lcm") |
| 346 | self.harness.add_relation_unit(relation_id, "lcm/0") |
| 347 | relation_data = self.harness.get_relation_data(relation_id, "ro") |
| 348 | |
| 349 | self.assertDictEqual(expected_result, relation_data) |
| 350 | |
| 351 | |
| 352 | if __name__ == "__main__": |
| 353 | unittest.main() |