Update from master
[osm/devops.git] / installers / charm / osm-nbi / 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 NBI_APP = METADATA["name"]
37 KAFKA_CHARM = "kafka-k8s"
38 KAFKA_APP = "kafka"
39 MARIADB_CHARM = "charmed-osm-mariadb-k8s"
40 MARIADB_APP = "mariadb"
41 MONGO_DB_CHARM = "mongodb-k8s"
42 MONGO_DB_APP = "mongodb"
43 KEYSTONE_CHARM = "osm-keystone"
44 KEYSTONE_APP = "keystone"
45 TEMPORAL_CHARM = "osm-temporal"
46 TEMPORAL_APP = "temporal"
47 PROMETHEUS_CHARM = "osm-prometheus"
48 PROMETHEUS_APP = "prometheus"
49 ZOOKEEPER_CHARM = "zookeeper-k8s"
50 ZOOKEEPER_APP = "zookeeper"
51 INGRESS_CHARM = "nginx-ingress-integrator"
52 INGRESS_APP = "ingress"
53 APPS = [
54 KAFKA_APP,
55 MONGO_DB_APP,
56 MARIADB_APP,
57 ZOOKEEPER_APP,
58 KEYSTONE_APP,
59 TEMPORAL_APP,
60 PROMETHEUS_APP,
61 NBI_APP,
62 ]
63
64
65 @pytest.mark.abort_on_fail
66 async def test_nbi_is_deployed(ops_test: OpsTest):
67 charm = await ops_test.build_charm(".")
68 resources = {"nbi-image": METADATA["resources"]["nbi-image"]["upstream-source"]}
69
70 await asyncio.gather(
71 ops_test.model.deploy(
72 charm, resources=resources, application_name=NBI_APP, series="focal"
73 ),
74 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
75 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="edge"),
76 ops_test.model.deploy(MARIADB_CHARM, application_name=MARIADB_APP, channel="stable"),
77 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
78 ops_test.model.deploy(PROMETHEUS_CHARM, application_name=PROMETHEUS_APP, channel="stable"),
79 )
80 # Keystone charm has to be deployed differently since
81 # bug https://github.com/juju/python-libjuju/issues/766
82 # prevents setting correctly the resources
83 cmd = f"juju deploy {KEYSTONE_CHARM} {KEYSTONE_APP} --resource keystone-image=opensourcemano/keystone:12"
84 await ops_test.run(*shlex.split(cmd), check=True)
85 cmd = f"juju deploy {TEMPORAL_CHARM} {TEMPORAL_APP} --resource temporal-server-image=temporalio/auto-setup:1.20 --series focal --channel=latest/edge"
86 await ops_test.run(*shlex.split(cmd), check=True)
87
88 async with ops_test.fast_forward():
89 await ops_test.model.wait_for_idle(
90 apps=APPS,
91 )
92 assert ops_test.model.applications[NBI_APP].status == "blocked"
93 unit = ops_test.model.applications[NBI_APP].units[0]
94 assert (
95 unit.workload_status_message
96 == "need kafka, mongodb, prometheus, keystone, temporal relations"
97 )
98
99 logger.info("Adding relations for other components")
100 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
101 await ops_test.model.add_relation(MARIADB_APP, KEYSTONE_APP)
102 await ops_test.model.add_relation(MARIADB_APP, f"{TEMPORAL_APP}:db")
103
104 logger.info("Adding relations")
105 await ops_test.model.add_relation(NBI_APP, MONGO_DB_APP)
106 await ops_test.model.add_relation(NBI_APP, KAFKA_APP)
107 await ops_test.model.add_relation(NBI_APP, PROMETHEUS_APP)
108 await ops_test.model.add_relation(NBI_APP, KEYSTONE_APP)
109 await ops_test.model.add_relation(NBI_APP, TEMPORAL_APP)
110
111 async with ops_test.fast_forward():
112 await ops_test.model.wait_for_idle(
113 apps=APPS,
114 status="active",
115 )
116
117
118 @pytest.mark.abort_on_fail
119 async def test_nbi_scales_up(ops_test: OpsTest):
120 logger.info("Scaling up osm-nbi")
121 expected_units = 3
122 assert len(ops_test.model.applications[NBI_APP].units) == 1
123 await ops_test.model.applications[NBI_APP].scale(expected_units)
124 async with ops_test.fast_forward():
125 await ops_test.model.wait_for_idle(
126 apps=[NBI_APP], status="active", wait_for_exact_units=expected_units
127 )
128
129
130 @pytest.mark.abort_on_fail
131 @pytest.mark.parametrize(
132 "relation_to_remove", [KAFKA_APP, MONGO_DB_APP, PROMETHEUS_APP, KEYSTONE_APP, TEMPORAL_APP]
133 )
134 async def test_nbi_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
135 logger.info("Removing relation: %s", relation_to_remove)
136 # mongoDB relation is named "database"
137 local_relation = relation_to_remove
138 if local_relation == MONGO_DB_APP:
139 local_relation = "database"
140 await asyncio.gather(
141 ops_test.model.applications[relation_to_remove].remove_relation(local_relation, NBI_APP)
142 )
143 async with ops_test.fast_forward():
144 await ops_test.model.wait_for_idle(apps=[NBI_APP])
145 assert ops_test.model.applications[NBI_APP].status == "blocked"
146 for unit in ops_test.model.applications[NBI_APP].units:
147 assert unit.workload_status_message == f"need {relation_to_remove} relation"
148 await ops_test.model.add_relation(NBI_APP, relation_to_remove)
149 async with ops_test.fast_forward():
150 await ops_test.model.wait_for_idle(
151 apps=APPS,
152 status="active",
153 )
154
155
156 @pytest.mark.abort_on_fail
157 async def test_nbi_action_debug_mode_disabled(ops_test: OpsTest):
158 async with ops_test.fast_forward():
159 await ops_test.model.wait_for_idle(
160 apps=APPS,
161 status="active",
162 )
163 logger.info("Running action 'get-debug-mode-information'")
164 action = (
165 await ops_test.model.applications[NBI_APP]
166 .units[0]
167 .run_action("get-debug-mode-information")
168 )
169 async with ops_test.fast_forward():
170 await ops_test.model.wait_for_idle(apps=[NBI_APP])
171 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
172 assert status[action.entity_id] == "failed"
173
174
175 @pytest.mark.abort_on_fail
176 async def test_nbi_action_debug_mode_enabled(ops_test: OpsTest):
177 await ops_test.model.applications[NBI_APP].set_config({"debug-mode": "true"})
178 async with ops_test.fast_forward():
179 await ops_test.model.wait_for_idle(
180 apps=APPS,
181 status="active",
182 )
183 logger.info("Running action 'get-debug-mode-information'")
184 # list of units is not ordered
185 unit_id = list(
186 filter(
187 lambda x: (x.entity_id == f"{NBI_APP}/0"), ops_test.model.applications[NBI_APP].units
188 )
189 )[0]
190 action = await unit_id.run_action("get-debug-mode-information")
191 async with ops_test.fast_forward():
192 await ops_test.model.wait_for_idle(apps=[NBI_APP])
193 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
194 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
195 assert status[action.entity_id] == "completed"
196 assert "command" in message
197 assert "password" in message
198
199
200 @pytest.mark.abort_on_fail
201 async def test_nbi_integration_ingress(ops_test: OpsTest):
202 await asyncio.gather(
203 ops_test.model.deploy(INGRESS_CHARM, application_name=INGRESS_APP, channel="beta"),
204 )
205 async with ops_test.fast_forward():
206 await ops_test.model.wait_for_idle(
207 apps=APPS + [INGRESS_APP],
208 )
209
210 await ops_test.model.add_relation(NBI_APP, INGRESS_APP)
211 async with ops_test.fast_forward():
212 await ops_test.model.wait_for_idle(
213 apps=APPS + [INGRESS_APP],
214 status="active",
215 )