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