Add OSM-POL integration tests
[osm/devops.git] / installers / charm / osm-pol / 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 POL_APP = METADATA["name"]
36 KAFKA_CHARM = "kafka-k8s"
37 KAFKA_APP = "kafka"
38 MONGO_DB_CHARM = "mongodb-k8s"
39 MONGO_DB_APP = "mongodb"
40 MARIADB_CHARM = "charmed-osm-mariadb-k8s"
41 MARIADB_APP = "mariadb"
42 ZOOKEEPER_CHARM = "zookeeper-k8s"
43 ZOOKEEPER_APP = "zookeeper"
44 APPS = [KAFKA_APP, ZOOKEEPER_APP, MONGO_DB_APP, MARIADB_APP, POL_APP]
45
46
47 @pytest.mark.abort_on_fail
48 async def test_pol_is_deployed(ops_test: OpsTest):
49 charm = await ops_test.build_charm(".")
50 resources = {"pol-image": METADATA["resources"]["pol-image"]["upstream-source"]}
51
52 await asyncio.gather(
53 ops_test.model.deploy(
54 charm, resources=resources, application_name=POL_APP, series="focal"
55 ),
56 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
57 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="stable"),
58 ops_test.model.deploy(MARIADB_CHARM, application_name=MARIADB_APP, channel="stable"),
59 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
60 )
61
62 async with ops_test.fast_forward():
63 await ops_test.model.wait_for_idle(
64 apps=APPS,
65 )
66 assert ops_test.model.applications[POL_APP].status == "blocked"
67 unit = ops_test.model.applications[POL_APP].units[0]
68 assert unit.workload_status_message == "need kafka, mongodb, mysql relations"
69
70 logger.info("Adding relations for other components")
71 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
72
73 logger.info("Adding relations")
74 await ops_test.model.add_relation(POL_APP, KAFKA_APP)
75 await ops_test.model.add_relation(POL_APP, MONGO_DB_APP)
76 await ops_test.model.add_relation(POL_APP, MARIADB_APP)
77
78 async with ops_test.fast_forward():
79 await ops_test.model.wait_for_idle(
80 apps=APPS,
81 status="active",
82 )
83
84
85 @pytest.mark.abort_on_fail
86 async def test_pol_scales_up(ops_test: OpsTest):
87 logger.info("Scaling up osm-pol")
88 expected_units = 3
89 assert len(ops_test.model.applications[POL_APP].units) == 1
90 await ops_test.model.applications[POL_APP].scale(expected_units)
91 async with ops_test.fast_forward():
92 await ops_test.model.wait_for_idle(
93 apps=[POL_APP], status="active", wait_for_exact_units=expected_units
94 )
95
96
97 @pytest.mark.abort_on_fail
98 @pytest.mark.parametrize("relation_to_remove", [KAFKA_APP, MONGO_DB_APP, MARIADB_APP])
99 async def test_pol_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
100 logger.info("Removing relation: %s", relation_to_remove)
101 # mongoDB relation is named "database"
102 local_relation = relation_to_remove
103 if relation_to_remove == MONGO_DB_APP:
104 local_relation = "database"
105 # mariaDB relation is named "mysql"
106 if relation_to_remove == MARIADB_APP:
107 local_relation = "mysql"
108 await asyncio.gather(
109 ops_test.model.applications[relation_to_remove].remove_relation(local_relation, POL_APP)
110 )
111 async with ops_test.fast_forward():
112 await ops_test.model.wait_for_idle(apps=[POL_APP])
113 assert ops_test.model.applications[POL_APP].status == "blocked"
114 for unit in ops_test.model.applications[POL_APP].units:
115 assert (
116 unit.workload_status_message
117 == f"need {'mysql' if relation_to_remove == MARIADB_APP else relation_to_remove} relation"
118 )
119 await ops_test.model.add_relation(POL_APP, relation_to_remove)
120 async with ops_test.fast_forward():
121 await ops_test.model.wait_for_idle(
122 apps=APPS,
123 status="active",
124 )
125
126
127 @pytest.mark.abort_on_fail
128 async def test_pol_action_debug_mode_disabled(ops_test: OpsTest):
129 async with ops_test.fast_forward():
130 await ops_test.model.wait_for_idle(
131 apps=APPS,
132 status="active",
133 )
134 logger.info("Running action 'get-debug-mode-information'")
135 action = (
136 await ops_test.model.applications[POL_APP]
137 .units[0]
138 .run_action("get-debug-mode-information")
139 )
140 async with ops_test.fast_forward():
141 await ops_test.model.wait_for_idle(apps=[POL_APP])
142 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
143 assert status[action.entity_id] == "failed"
144
145
146 @pytest.mark.abort_on_fail
147 async def test_pol_action_debug_mode_enabled(ops_test: OpsTest):
148 await ops_test.model.applications[POL_APP].set_config({"debug-mode": "true"})
149 async with ops_test.fast_forward():
150 await ops_test.model.wait_for_idle(
151 apps=APPS,
152 status="active",
153 )
154 logger.info("Running action 'get-debug-mode-information'")
155 # list of units is not ordered
156 unit_id = list(
157 filter(
158 lambda x: (x.entity_id == f"{POL_APP}/0"), ops_test.model.applications[POL_APP].units
159 )
160 )[0]
161 action = await unit_id.run_action("get-debug-mode-information")
162 async with ops_test.fast_forward():
163 await ops_test.model.wait_for_idle(apps=[POL_APP])
164 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
165 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
166 assert status[action.entity_id] == "completed"
167 assert "command" in message
168 assert "password" in message