Add integration tests for osm-lcm charm
[osm/devops.git] / installers / charm / osm-lcm / 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 LCM_APP = METADATA["name"]
36 KAFKA_CHARM = "kafka-k8s"
37 KAFKA_APP = "kafka"
38 MONGO_DB_CHARM = "mongodb-k8s"
39 MONGO_DB_APP = "mongodb"
40 RO_CHARM = "osm-ro"
41 RO_APP = "ro"
42 ZOOKEEPER_CHARM = "zookeeper-k8s"
43 ZOOKEEPER_APP = "zookeeper"
44 VCA_CHARM = "osm-vca-integrator"
45 VCA_APP = "vca"
46 APPS = [KAFKA_APP, MONGO_DB_APP, ZOOKEEPER_APP, RO_APP, LCM_APP]
47
48
49 @pytest.mark.abort_on_fail
50 async def test_lcm_is_deployed(ops_test: OpsTest):
51 charm = await ops_test.build_charm(".")
52 resources = {"lcm-image": METADATA["resources"]["lcm-image"]["upstream-source"]}
53
54 await asyncio.gather(
55 ops_test.model.deploy(
56 charm, resources=resources, application_name=LCM_APP, series="focal"
57 ),
58 ops_test.model.deploy(RO_CHARM, application_name=RO_APP, channel="beta"),
59 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
60 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="stable"),
61 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
62 )
63
64 async with ops_test.fast_forward():
65 await ops_test.model.wait_for_idle(
66 apps=APPS,
67 timeout=300,
68 )
69 assert ops_test.model.applications[LCM_APP].status == "blocked"
70 unit = ops_test.model.applications[LCM_APP].units[0]
71 assert unit.workload_status_message == "need kafka, mongodb, ro relations"
72
73 logger.info("Adding relations for other components")
74 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
75 await ops_test.model.add_relation(RO_APP, MONGO_DB_APP)
76 await ops_test.model.add_relation(RO_APP, KAFKA_APP)
77
78 logger.info("Adding relations")
79 await ops_test.model.add_relation(LCM_APP, MONGO_DB_APP)
80 await ops_test.model.add_relation(LCM_APP, KAFKA_APP)
81 await ops_test.model.add_relation(LCM_APP, RO_APP)
82
83 async with ops_test.fast_forward():
84 await ops_test.model.wait_for_idle(
85 apps=APPS,
86 status="active",
87 timeout=300,
88 )
89
90
91 @pytest.mark.abort_on_fail
92 async def test_lcm_scales_up(ops_test: OpsTest):
93 logger.info("Scaling up osm-lcm")
94 expected_units = 3
95 assert len(ops_test.model.applications[LCM_APP].units) == 1
96 await ops_test.model.applications[LCM_APP].scale(expected_units)
97 async with ops_test.fast_forward():
98 await ops_test.model.wait_for_idle(
99 apps=[LCM_APP], status="active", timeout=1000, wait_for_exact_units=expected_units
100 )
101
102
103 @pytest.mark.abort_on_fail
104 @pytest.mark.parametrize("relation_to_remove", [RO_APP, KAFKA_APP, MONGO_DB_APP])
105 async def test_lcm_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
106 logger.info("Removing relation: %s", relation_to_remove)
107 # mongoDB relation is named "database"
108 local_relation = relation_to_remove
109 if relation_to_remove == MONGO_DB_APP:
110 local_relation = "database"
111 await asyncio.gather(
112 ops_test.model.applications[relation_to_remove].remove_relation(local_relation, LCM_APP)
113 )
114 async with ops_test.fast_forward():
115 await ops_test.model.wait_for_idle(apps=[LCM_APP])
116 assert ops_test.model.applications[LCM_APP].status == "blocked"
117 for unit in ops_test.model.applications[LCM_APP].units:
118 assert unit.workload_status_message == f"need {relation_to_remove} relation"
119 await ops_test.model.add_relation(LCM_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 timeout=300,
125 )
126
127
128 @pytest.mark.abort_on_fail
129 async def test_lcm_action_debug_mode_disabled(ops_test: OpsTest):
130 async with ops_test.fast_forward():
131 await ops_test.model.wait_for_idle(
132 apps=APPS,
133 status="active",
134 timeout=300,
135 )
136 logger.info("Running action 'get-debug-mode-information'")
137 action = (
138 await ops_test.model.applications[LCM_APP]
139 .units[0]
140 .run_action("get-debug-mode-information")
141 )
142 async with ops_test.fast_forward():
143 await ops_test.model.wait_for_idle(apps=[LCM_APP])
144 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
145 assert status[action.entity_id] == "failed"
146
147
148 @pytest.mark.abort_on_fail
149 async def test_lcm_action_debug_mode_enabled(ops_test: OpsTest):
150 await ops_test.model.applications[LCM_APP].set_config({"debug-mode": "true"})
151 async with ops_test.fast_forward():
152 await ops_test.model.wait_for_idle(
153 apps=APPS,
154 status="active",
155 timeout=1000,
156 )
157 logger.info("Running action 'get-debug-mode-information'")
158 # list of units is not ordered
159 unit_id = list(
160 filter(
161 lambda x: (x.entity_id == f"{LCM_APP}/0"), ops_test.model.applications[LCM_APP].units
162 )
163 )[0]
164 action = await unit_id.run_action("get-debug-mode-information")
165 async with ops_test.fast_forward():
166 await ops_test.model.wait_for_idle(apps=[LCM_APP])
167 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
168 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
169 assert status[action.entity_id] == "completed"
170 assert "command" in message
171 assert "password" in message
172
173
174 @pytest.mark.abort_on_fail
175 async def test_lcm_integration_vca(ops_test: OpsTest):
176 await asyncio.gather(
177 ops_test.model.deploy(VCA_CHARM, application_name=VCA_APP, channel="beta"),
178 )
179 async with ops_test.fast_forward():
180 await ops_test.model.wait_for_idle(
181 apps=[VCA_APP],
182 timeout=300,
183 )
184 controllers = (Path.home() / ".local/share/juju/controllers.yaml").read_text()
185 accounts = (Path.home() / ".local/share/juju/accounts.yaml").read_text()
186 public_key = (Path.home() / ".local/share/juju/ssh/juju_id_rsa.pub").read_text()
187 await ops_test.model.applications[VCA_APP].set_config(
188 {
189 "controllers": controllers,
190 "accounts": accounts,
191 "public-key": public_key,
192 "k8s-cloud": "microk8s",
193 }
194 )
195 async with ops_test.fast_forward():
196 await ops_test.model.wait_for_idle(
197 apps=APPS + [VCA_APP],
198 status="active",
199 timeout=1000,
200 )
201 await ops_test.model.add_relation(LCM_APP, VCA_APP)
202 async with ops_test.fast_forward():
203 await ops_test.model.wait_for_idle(
204 apps=APPS + [VCA_APP],
205 status="active",
206 timeout=300,
207 )