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