e14e191e7331fadce57e633f51d146d7c87570e3
[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_and_other_charms_are_idle(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="jammy"
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="5/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 keystone_image = "opensourcemano/keystone:testing-daily"
70 cmd = f"juju deploy {KEYSTONE_CHARM} {KEYSTONE_APP} --resource keystone-image={keystone_image} --channel=latest/beta --series jammy"
71 await ops_test.run(*shlex.split(cmd), check=True)
72
73 async with ops_test.fast_forward():
74 await ops_test.model.wait_for_idle(apps=APPS)
75
76
77 @pytest.mark.abort_on_fail
78 async def test_mon_is_blocked_due_to_missing_grafana_config(ops_test: OpsTest):
79 assert ops_test.model.applications[MON_APP].status == "blocked"
80 unit = ops_test.model.applications[MON_APP].units[0]
81 assert (
82 unit.workload_status_message == "need grafana-url, grafana-user, grafana-password config"
83 )
84
85 await ops_test.model.applications[MON_APP].set_config({"grafana-url": "new_value"})
86 async with ops_test.fast_forward():
87 await ops_test.model.wait_for_idle(apps=[MON_APP], status="blocked")
88 assert unit.workload_status_message == "need grafana-user, grafana-password config"
89
90 await ops_test.model.applications[MON_APP].set_config({"grafana-password": "new_value"})
91 async with ops_test.fast_forward():
92 await ops_test.model.wait_for_idle(apps=[MON_APP], status="blocked")
93 assert unit.workload_status_message == "need grafana-user config"
94
95 await ops_test.model.applications[MON_APP].set_config({"grafana-user": "new_value"})
96 async with ops_test.fast_forward():
97 await ops_test.model.wait_for_idle(apps=[MON_APP], status="blocked")
98
99 assert unit.workload_status_message == "Invalid value for grafana-url config: 'new_value'"
100 await ops_test.model.applications[MON_APP].set_config({"grafana-url": "http://valid:92"})
101
102
103 @pytest.mark.abort_on_fail
104 async def test_mon_is_blocked_due_to_missing_relations(ops_test: OpsTest):
105 async with ops_test.fast_forward():
106 await ops_test.model.wait_for_idle(apps=[MON_APP], status="blocked")
107 unit = ops_test.model.applications[MON_APP].units[0]
108 assert unit.workload_status_message == "need kafka, mongodb, prometheus, keystone relations"
109
110 logger.info("Adding relations for other components")
111 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
112 await ops_test.model.add_relation(MARIADB_APP, KEYSTONE_APP)
113
114 logger.info("Adding relations for MON")
115 await ops_test.model.add_relation(
116 "{}:mongodb".format(MON_APP), "{}:database".format(MONGO_DB_APP)
117 )
118 await ops_test.model.add_relation(MON_APP, KAFKA_APP)
119 await ops_test.model.add_relation(MON_APP, KEYSTONE_APP)
120 await ops_test.model.add_relation(MON_APP, PROMETHEUS_APP)
121
122 async with ops_test.fast_forward():
123 await ops_test.model.wait_for_idle(apps=APPS, status="active")
124
125
126 @pytest.mark.abort_on_fail
127 async def test_mon_scales_up(ops_test: OpsTest):
128 logger.info("Scaling up osm-mon")
129 expected_units = 3
130 assert len(ops_test.model.applications[MON_APP].units) == 1
131 await ops_test.model.applications[MON_APP].scale(expected_units)
132 async with ops_test.fast_forward():
133 await ops_test.model.wait_for_idle(
134 apps=[MON_APP], status="active", wait_for_exact_units=expected_units
135 )
136
137
138 @pytest.mark.abort_on_fail
139 @pytest.mark.parametrize(
140 "relation_to_remove", [KAFKA_APP, MONGO_DB_APP, PROMETHEUS_APP, KEYSTONE_APP]
141 )
142 async def test_mon_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
143 logger.info("Removing relation: %s", relation_to_remove)
144 # mongoDB relation is named "database"
145 local_relation = relation_to_remove
146 if relation_to_remove == MONGO_DB_APP:
147 local_relation = "database"
148 await asyncio.gather(
149 ops_test.model.applications[relation_to_remove].remove_relation(local_relation, MON_APP)
150 )
151 async with ops_test.fast_forward():
152 await ops_test.model.wait_for_idle(apps=[MON_APP])
153 assert ops_test.model.applications[MON_APP].status == "blocked"
154 for unit in ops_test.model.applications[MON_APP].units:
155 assert unit.workload_status_message == f"need {relation_to_remove} relation"
156 await ops_test.model.add_relation(MON_APP, relation_to_remove)
157 async with ops_test.fast_forward():
158 await ops_test.model.wait_for_idle(apps=APPS, status="active")
159
160
161 @pytest.mark.abort_on_fail
162 async def test_mon_action_debug_mode_disabled(ops_test: OpsTest):
163 async with ops_test.fast_forward():
164 await ops_test.model.wait_for_idle(apps=APPS, status="active")
165 logger.info("Running action 'get-debug-mode-information'")
166 action = (
167 await ops_test.model.applications[MON_APP]
168 .units[0]
169 .run_action("get-debug-mode-information")
170 )
171 async with ops_test.fast_forward():
172 await ops_test.model.wait_for_idle(apps=[MON_APP])
173 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
174 assert status[action.entity_id] == "failed"
175
176
177 @pytest.mark.abort_on_fail
178 async def test_mon_action_debug_mode_enabled(ops_test: OpsTest):
179 await ops_test.model.applications[MON_APP].set_config({"debug-mode": "true"})
180 async with ops_test.fast_forward():
181 await ops_test.model.wait_for_idle(
182 apps=APPS,
183 status="active",
184 )
185 logger.info("Running action 'get-debug-mode-information'")
186 # list of units is not ordered
187 unit_id = list(
188 filter(
189 lambda x: (x.entity_id == f"{MON_APP}/0"), ops_test.model.applications[MON_APP].units
190 )
191 )[0]
192 action = await unit_id.run_action("get-debug-mode-information")
193 async with ops_test.fast_forward():
194 await ops_test.model.wait_for_idle(apps=[MON_APP])
195 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
196 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
197 assert status[action.entity_id] == "completed"
198 assert "command" in message
199 assert "password" in message
200
201
202 @pytest.mark.abort_on_fail
203 async def test_mon_integration_vca(ops_test: OpsTest):
204 await asyncio.gather(
205 ops_test.model.deploy(
206 VCA_CHARM, application_name=VCA_APP, channel="latest/beta", series="jammy"
207 ),
208 )
209 async with ops_test.fast_forward():
210 await ops_test.model.wait_for_idle(apps=[VCA_APP])
211 controllers = (Path.home() / ".local/share/juju/controllers.yaml").read_text()
212 accounts = (Path.home() / ".local/share/juju/accounts.yaml").read_text()
213 public_key = (Path.home() / ".local/share/juju/ssh/juju_id_rsa.pub").read_text()
214 await ops_test.model.applications[VCA_APP].set_config(
215 {
216 "controllers": controllers,
217 "accounts": accounts,
218 "public-key": public_key,
219 "k8s-cloud": "microk8s",
220 }
221 )
222 async with ops_test.fast_forward():
223 await ops_test.model.wait_for_idle(apps=APPS + [VCA_APP], status="active")
224 await ops_test.model.add_relation(MON_APP, VCA_APP)
225 async with ops_test.fast_forward():
226 await ops_test.model.wait_for_idle(apps=APPS + [VCA_APP], status="active")