| sousaedu | cab58cb | 2020-11-04 18:48:17 +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 PolCharm |
| 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| 33 | """POL Charm unit tests.""" |
| 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| 37 | self.harness = Harness(PolCharm) |
| 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.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 56 | |
| 57 | def test_on_start_with_relations(self) -> NoReturn: |
| 58 | """Test deployment without keystone.""" |
| 59 | expected_result = { |
| 60 | "version": 3, |
| 61 | "containers": [ |
| 62 | { |
| 63 | "name": "pol", |
| 64 | "imageDetails": self.harness.charm.image.fetch(), |
| 65 | "imagePullPolicy": "Always", |
| 66 | "ports": [ |
| 67 | { |
| 68 | "name": "pol", |
| 69 | "containerPort": 80, |
| 70 | "protocol": "TCP", |
| 71 | } |
| 72 | ], |
| 73 | "envConfig": { |
| 74 | "ALLOW_ANONYMOUS_LOGIN": "yes", |
| 75 | "OSMPOL_GLOBAL_LOGLEVEL": "INFO", |
| 76 | "OSMPOL_MESSAGE_HOST": "kafka", |
| 77 | "OSMPOL_MESSAGE_DRIVER": "kafka", |
| 78 | "OSMPOL_MESSAGE_PORT": 9092, |
| 79 | "OSMPOL_DATABASE_DRIVER": "mongo", |
| 80 | "OSMPOL_DATABASE_URI": "mongodb://mongo:27017", |
| 81 | }, |
| 82 | } |
| 83 | ], |
| 84 | "kubernetesResources": {"ingressResources": []}, |
| 85 | } |
| 86 | |
| 87 | self.harness.charm.on.start.emit() |
| 88 | |
| 89 | # Check if kafka datastore is initialized |
| 90 | self.assertIsNone(self.harness.charm.state.message_host) |
| 91 | self.assertIsNone(self.harness.charm.state.message_port) |
| 92 | |
| 93 | # Check if mongodb datastore is initialized |
| 94 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 95 | |
| 96 | # Initializing the kafka relation |
| 97 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 98 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 99 | self.harness.update_relation_data( |
| 100 | kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 101 | ) |
| 102 | |
| 103 | # Initializing the mongo relation |
| 104 | mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 105 | self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 106 | self.harness.update_relation_data( |
| 107 | mongodb_relation_id, |
| 108 | "mongodb/0", |
| 109 | {"connection_string": "mongodb://mongo:27017"}, |
| 110 | ) |
| 111 | |
| 112 | # Checking if kafka data is stored |
| 113 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 114 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 115 | |
| 116 | # Checking if mongodb data is stored |
| 117 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 118 | |
| 119 | # Verifying status |
| 120 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 121 | |
| 122 | pod_spec, _ = self.harness.get_pod_spec() |
| 123 | |
| 124 | self.assertDictEqual(expected_result, pod_spec) |
| 125 | |
| 126 | def test_on_kafka_app_relation_changed(self) -> NoReturn: |
| 127 | """Test to see if kafka relation is updated.""" |
| 128 | self.harness.charm.on.start.emit() |
| 129 | |
| 130 | self.assertIsNone(self.harness.charm.state.message_host) |
| 131 | self.assertIsNone(self.harness.charm.state.message_port) |
| 132 | |
| 133 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 134 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 135 | self.harness.update_relation_data( |
| 136 | relation_id, "kafka", {"host": "kafka", "port": 9092} |
| 137 | ) |
| 138 | |
| 139 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 140 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 141 | |
| 142 | # Verifying status |
| 143 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 144 | |
| 145 | # Verifying status message |
| 146 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 147 | self.assertTrue( |
| 148 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 149 | ) |
| 150 | self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 151 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 152 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 153 | |
| 154 | def test_on_kafka_unit_relation_changed(self) -> NoReturn: |
| 155 | """Test to see if kafka relation is updated.""" |
| 156 | self.harness.charm.on.start.emit() |
| 157 | |
| 158 | self.assertIsNone(self.harness.charm.state.message_host) |
| 159 | self.assertIsNone(self.harness.charm.state.message_port) |
| 160 | |
| 161 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 162 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 163 | self.harness.update_relation_data( |
| 164 | relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 165 | ) |
| 166 | |
| 167 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 168 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 169 | |
| 170 | # Verifying status |
| 171 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 172 | |
| 173 | # Verifying status message |
| 174 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 175 | self.assertTrue( |
| 176 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 177 | ) |
| 178 | self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 179 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 180 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 181 | |
| 182 | def test_on_mongodb_app_relation_changed(self) -> NoReturn: |
| 183 | """Test to see if mongodb relation is updated.""" |
| 184 | self.harness.charm.on.start.emit() |
| 185 | |
| 186 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 187 | |
| 188 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 189 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 190 | self.harness.update_relation_data( |
| 191 | relation_id, "mongodb", {"connection_string": "mongodb://mongo:27017"} |
| 192 | ) |
| 193 | |
| 194 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 195 | |
| 196 | # Verifying status |
| 197 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 198 | |
| 199 | # Verifying status message |
| 200 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 201 | self.assertTrue( |
| 202 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 203 | ) |
| 204 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 205 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 206 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 207 | |
| 208 | def test_on_mongodb_unit_relation_changed(self) -> NoReturn: |
| 209 | """Test to see if mongodb relation is updated.""" |
| 210 | self.harness.charm.on.start.emit() |
| 211 | |
| 212 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 213 | |
| 214 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 215 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 216 | self.harness.update_relation_data( |
| 217 | relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"} |
| 218 | ) |
| 219 | |
| 220 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 221 | |
| 222 | # Verifying status |
| 223 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 224 | |
| 225 | # Verifying status message |
| 226 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 227 | self.assertTrue( |
| 228 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 229 | ) |
| 230 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 231 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 232 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relation")) |
| 233 | |
| 234 | |
| 235 | if __name__ == "__main__": |
| 236 | unittest.main() |