| sousaedu | 1dd4c0d | 2020-11-04 17:43:47 +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 MonCharm |
| 30 | |
| 31 | |
| 32 | class TestCharm(unittest.TestCase): |
| 33 | """MON Charm unit tests.""" |
| 34 | |
| 35 | def setUp(self) -> NoReturn: |
| 36 | """Test setup""" |
| 37 | self.harness = Harness(MonCharm) |
| 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("prometheus", 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": "mon", |
| 65 | "imageDetails": self.harness.charm.image.fetch(), |
| 66 | "imagePullPolicy": "Always", |
| 67 | "ports": [ |
| 68 | { |
| 69 | "name": "mon", |
| 70 | "containerPort": 8000, |
| 71 | "protocol": "TCP", |
| 72 | } |
| 73 | ], |
| 74 | "envConfig": { |
| 75 | "ALLOW_ANONYMOUS_LOGIN": "yes", |
| 76 | "OSMMON_OPENSTACK_DEFAULT_GRANULARITY": 300, |
| 77 | "OSMMON_GLOBAL_REQUEST_TIMEOUT": 10, |
| 78 | "OSMMON_GLOBAL_LOGLEVEL": "INFO", |
| 79 | "OSMMON_COLLECTOR_INTERVAL": 30, |
| 80 | "OSMMON_EVALUATOR_INTERVAL": 30, |
| 81 | "OSMMON_MESSAGE_DRIVER": "kafka", |
| 82 | "OSMMON_MESSAGE_HOST": "kafka", |
| 83 | "OSMMON_MESSAGE_PORT": 9092, |
| 84 | "OSMMON_DATABASE_DRIVER": "mongo", |
| 85 | "OSMMON_DATABASE_URI": "mongodb://mongo:27017", |
| 86 | "OSMMON_DATABASE_COMMONKEY": "osm", |
| 87 | "OSMMON_PROMETHEUS_URL": "http://prometheus:9090", |
| 88 | "OSMMON_VCA_HOST": "admin", |
| 89 | "OSMMON_VCA_USER": "admin", |
| 90 | "OSMMON_VCA_SECRET": "secret", |
| 91 | "OSMMON_VCA_CACERT": "", |
| 92 | }, |
| 93 | } |
| 94 | ], |
| 95 | "kubernetesResources": {"ingressResources": []}, |
| 96 | } |
| 97 | |
| 98 | self.harness.charm.on.start.emit() |
| 99 | |
| 100 | # Check if kafka datastore is initialized |
| 101 | self.assertIsNone(self.harness.charm.state.message_host) |
| 102 | self.assertIsNone(self.harness.charm.state.message_port) |
| 103 | |
| 104 | # Check if mongodb datastore is initialized |
| 105 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 106 | |
| 107 | # Check if prometheus datastore is initialized |
| 108 | self.assertIsNone(self.harness.charm.state.prometheus_host) |
| 109 | self.assertIsNone(self.harness.charm.state.prometheus_port) |
| 110 | |
| 111 | # Initializing the kafka relation |
| 112 | kafka_relation_id = self.harness.add_relation("kafka", "kafka") |
| 113 | self.harness.add_relation_unit(kafka_relation_id, "kafka/0") |
| 114 | self.harness.update_relation_data( |
| 115 | kafka_relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 116 | ) |
| 117 | |
| 118 | # Initializing the mongo relation |
| 119 | mongodb_relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 120 | self.harness.add_relation_unit(mongodb_relation_id, "mongodb/0") |
| 121 | self.harness.update_relation_data( |
| 122 | mongodb_relation_id, |
| 123 | "mongodb/0", |
| 124 | {"connection_string": "mongodb://mongo:27017"}, |
| 125 | ) |
| 126 | |
| 127 | # Initializing the prometheus relation |
| 128 | prometheus_relation_id = self.harness.add_relation("prometheus", "prometheus") |
| 129 | self.harness.add_relation_unit(prometheus_relation_id, "prometheus/0") |
| 130 | self.harness.update_relation_data( |
| 131 | prometheus_relation_id, |
| 132 | "prometheus/0", |
| 133 | {"hostname": "prometheus", "port": 9090}, |
| 134 | ) |
| 135 | |
| 136 | # Checking if kafka data is stored |
| 137 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 138 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 139 | |
| 140 | # Checking if mongodb data is stored |
| 141 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 142 | |
| 143 | # Checking if prometheus data is stored |
| 144 | self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus") |
| 145 | self.assertEqual(self.harness.charm.state.prometheus_port, 9090) |
| 146 | |
| 147 | # Verifying status |
| 148 | self.assertNotIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 149 | |
| 150 | pod_spec, _ = self.harness.get_pod_spec() |
| 151 | |
| 152 | self.assertDictEqual(expected_result, pod_spec) |
| 153 | |
| 154 | def test_on_kafka_app_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", {"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.assertIn("prometheus", self.harness.charm.unit.status.message) |
| 181 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 182 | |
| 183 | def test_on_kafka_unit_relation_changed(self) -> NoReturn: |
| 184 | """Test to see if kafka relation is updated.""" |
| 185 | self.harness.charm.on.start.emit() |
| 186 | |
| 187 | self.assertIsNone(self.harness.charm.state.message_host) |
| 188 | self.assertIsNone(self.harness.charm.state.message_port) |
| 189 | |
| 190 | relation_id = self.harness.add_relation("kafka", "kafka") |
| 191 | self.harness.add_relation_unit(relation_id, "kafka/0") |
| 192 | self.harness.update_relation_data( |
| 193 | relation_id, "kafka/0", {"host": "kafka", "port": 9092} |
| 194 | ) |
| 195 | |
| 196 | self.assertEqual(self.harness.charm.state.message_host, "kafka") |
| 197 | self.assertEqual(self.harness.charm.state.message_port, 9092) |
| 198 | |
| 199 | # Verifying status |
| 200 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 201 | |
| 202 | # Verifying status message |
| 203 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 204 | self.assertTrue( |
| 205 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 206 | ) |
| 207 | self.assertNotIn("kafka", self.harness.charm.unit.status.message) |
| 208 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 209 | self.assertIn("prometheus", self.harness.charm.unit.status.message) |
| 210 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 211 | |
| 212 | def test_on_mongodb_app_relation_changed(self) -> NoReturn: |
| 213 | """Test to see if mongodb relation is updated.""" |
| 214 | self.harness.charm.on.start.emit() |
| 215 | |
| 216 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 217 | |
| 218 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 219 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 220 | self.harness.update_relation_data( |
| 221 | relation_id, "mongodb", {"connection_string": "mongodb://mongo:27017"} |
| 222 | ) |
| 223 | |
| 224 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 225 | |
| 226 | # Verifying status |
| 227 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 228 | |
| 229 | # Verifying status message |
| 230 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 231 | self.assertTrue( |
| 232 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 233 | ) |
| 234 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 235 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 236 | self.assertIn("prometheus", self.harness.charm.unit.status.message) |
| 237 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 238 | |
| 239 | def test_on_mongodb_unit_relation_changed(self) -> NoReturn: |
| 240 | """Test to see if mongodb relation is updated.""" |
| 241 | self.harness.charm.on.start.emit() |
| 242 | |
| 243 | self.assertIsNone(self.harness.charm.state.database_uri) |
| 244 | |
| 245 | relation_id = self.harness.add_relation("mongodb", "mongodb") |
| 246 | self.harness.add_relation_unit(relation_id, "mongodb/0") |
| 247 | self.harness.update_relation_data( |
| 248 | relation_id, "mongodb/0", {"connection_string": "mongodb://mongo:27017"} |
| 249 | ) |
| 250 | |
| 251 | self.assertEqual(self.harness.charm.state.database_uri, "mongodb://mongo:27017") |
| 252 | |
| 253 | # Verifying status |
| 254 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 255 | |
| 256 | # Verifying status message |
| 257 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 258 | self.assertTrue( |
| 259 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 260 | ) |
| 261 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 262 | self.assertNotIn("mongodb", self.harness.charm.unit.status.message) |
| 263 | self.assertIn("prometheus", self.harness.charm.unit.status.message) |
| 264 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 265 | |
| 266 | def test_on_prometheus_app_relation_changed(self) -> NoReturn: |
| 267 | """Test to see if prometheus relation is updated.""" |
| 268 | self.harness.charm.on.start.emit() |
| 269 | |
| 270 | self.assertIsNone(self.harness.charm.state.prometheus_host) |
| 271 | self.assertIsNone(self.harness.charm.state.prometheus_port) |
| 272 | |
| 273 | relation_id = self.harness.add_relation("prometheus", "prometheus") |
| 274 | self.harness.add_relation_unit(relation_id, "prometheus/0") |
| 275 | self.harness.update_relation_data( |
| 276 | relation_id, "prometheus", {"hostname": "prometheus", "port": 9090} |
| 277 | ) |
| 278 | |
| 279 | self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus") |
| 280 | self.assertEqual(self.harness.charm.state.prometheus_port, 9090) |
| 281 | |
| 282 | # Verifying status |
| 283 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 284 | |
| 285 | # Verifying status message |
| 286 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 287 | self.assertTrue( |
| 288 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 289 | ) |
| 290 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 291 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 292 | self.assertNotIn("prometheus", self.harness.charm.unit.status.message) |
| 293 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 294 | |
| 295 | def test_on_prometheus_unit_relation_changed(self) -> NoReturn: |
| 296 | """Test to see if prometheus relation is updated.""" |
| 297 | self.harness.charm.on.start.emit() |
| 298 | |
| 299 | self.assertIsNone(self.harness.charm.state.prometheus_host) |
| 300 | self.assertIsNone(self.harness.charm.state.prometheus_port) |
| 301 | |
| 302 | relation_id = self.harness.add_relation("prometheus", "prometheus") |
| 303 | self.harness.add_relation_unit(relation_id, "prometheus/0") |
| 304 | self.harness.update_relation_data( |
| 305 | relation_id, "prometheus/0", {"hostname": "prometheus", "port": 9090} |
| 306 | ) |
| 307 | |
| 308 | self.assertEqual(self.harness.charm.state.prometheus_host, "prometheus") |
| 309 | self.assertEqual(self.harness.charm.state.prometheus_port, 9090) |
| 310 | |
| 311 | # Verifying status |
| 312 | self.assertIsInstance(self.harness.charm.unit.status, BlockedStatus) |
| 313 | |
| 314 | # Verifying status message |
| 315 | self.assertGreater(len(self.harness.charm.unit.status.message), 0) |
| 316 | self.assertTrue( |
| 317 | self.harness.charm.unit.status.message.startswith("Waiting for ") |
| 318 | ) |
| 319 | self.assertIn("kafka", self.harness.charm.unit.status.message) |
| 320 | self.assertIn("mongodb", self.harness.charm.unit.status.message) |
| 321 | self.assertNotIn("prometheus", self.harness.charm.unit.status.message) |
| 322 | self.assertTrue(self.harness.charm.unit.status.message.endswith(" relations")) |
| 323 | |
| 324 | |
| 325 | if __name__ == "__main__": |
| 326 | unittest.main() |