Add integration tests for osm-ro charm
[osm/devops.git] / installers / charm / osm-ro / tests / integration / test_charm.py
1 #!/usr/bin/env python3
2 # Copyright 2022 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 # Learn more about testing at: https://juju.is/docs/sdk/testing
23
24 import asyncio
25 import logging
26 from pathlib import Path
27
28 import pytest
29 import yaml
30 from pytest_operator.plugin import OpsTest
31
32 logger = logging.getLogger(__name__)
33
34 METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
35 RO_APP = METADATA["name"]
36 KAFKA_CHARM = "kafka-k8s"
37 KAFKA_APP = "kafka"
38 MONGO_DB_CHARM = "mongodb-k8s"
39 MONGO_DB_APP = "mongodb"
40 ZOOKEEPER_CHARM = "zookeeper-k8s"
41 ZOOKEEPER_APP = "zookeeper"
42 APPS = [KAFKA_APP, MONGO_DB_APP, ZOOKEEPER_APP, RO_APP]
43
44
45 @pytest.mark.abort_on_fail
46 async def test_ro_is_deployed(ops_test: OpsTest):
47 charm = await ops_test.build_charm(".")
48 resources = {"ro-image": METADATA["resources"]["ro-image"]["upstream-source"]}
49
50 await asyncio.gather(
51 ops_test.model.deploy(charm, resources=resources, application_name=RO_APP),
52 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
53 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
54 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="stable"),
55 )
56
57 async with ops_test.fast_forward():
58 await ops_test.model.wait_for_idle(
59 apps=APPS,
60 timeout=300,
61 )
62 assert ops_test.model.applications[RO_APP].status == "blocked"
63 unit = ops_test.model.applications[RO_APP].units[0]
64 assert unit.workload_status_message == "need kafka, mongodb relations"
65
66 logger.info("Adding relations")
67 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
68 await ops_test.model.add_relation(RO_APP, MONGO_DB_APP)
69 await ops_test.model.add_relation(RO_APP, KAFKA_APP)
70
71 async with ops_test.fast_forward():
72 await ops_test.model.wait_for_idle(
73 apps=APPS,
74 status="active",
75 timeout=300,
76 )
77
78
79 @pytest.mark.abort_on_fail
80 async def test_ro_scales(ops_test: OpsTest):
81 logger.info("Scaling osm-ro")
82 expected_units = 3
83 assert len(ops_test.model.applications[RO_APP].units) == 1
84 await ops_test.model.applications[RO_APP].scale(expected_units)
85 async with ops_test.fast_forward():
86 await ops_test.model.wait_for_idle(
87 apps=[RO_APP], status="active", timeout=1000, wait_for_exact_units=expected_units
88 )
89
90
91 @pytest.mark.abort_on_fail
92 async def test_ro_blocks_without_kafka(ops_test: OpsTest):
93 await asyncio.gather(ops_test.model.applications[KAFKA_APP].remove())
94 async with ops_test.fast_forward():
95 await ops_test.model.wait_for_idle(apps=[RO_APP])
96 assert ops_test.model.applications[RO_APP].status == "blocked"
97 for unit in ops_test.model.applications[RO_APP].units:
98 assert unit.workload_status_message == "need kafka relation"