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