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