1528869ab90d73a97067e3d35237b78745b4fd99
[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 ZOOKEEPER_CHARM = "zookeeper-k8s"
46 ZOOKEEPER_APP = "zookeeper"
47 INGRESS_CHARM = "nginx-ingress-integrator"
48 INGRESS_APP = "ingress"
49 APPS = [KAFKA_APP, MONGO_DB_APP, MARIADB_APP, ZOOKEEPER_APP, KEYSTONE_APP, NBI_APP]
50
51
52 @pytest.mark.abort_on_fail
53 async def test_nbi_and_other_charms_are_idle(ops_test: OpsTest):
54 charm = await ops_test.build_charm(".")
55 resources = {"nbi-image": METADATA["resources"]["nbi-image"]["upstream-source"]}
56
57 await asyncio.gather(
58 ops_test.model.deploy(
59 charm, resources=resources, application_name=NBI_APP, series="jammy"
60 ),
61 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
62 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="5/edge"),
63 ops_test.model.deploy(MARIADB_CHARM, application_name=MARIADB_APP, channel="stable"),
64 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
65 )
66 # Keystone charm has to be deployed differently since
67 # bug https://github.com/juju/python-libjuju/issues/766
68 # prevents setting correctly the resources
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_nbi_is_blocked_when_prometheus_config_is_invalid(ops_test: OpsTest):
79 assert ops_test.model.applications[NBI_APP].status == "blocked"
80 unit = ops_test.model.applications[NBI_APP].units[0]
81 assert unit.workload_status_message == "need prometheus-url config"
82
83 await ops_test.model.applications[NBI_APP].set_config({"prometheus-url": "some_url"})
84 async with ops_test.fast_forward():
85 await ops_test.model.wait_for_idle(apps=[NBI_APP], status="blocked")
86 assert unit.workload_status_message == "Invalid value for prometheus-url config: 'some_url'"
87
88 await ops_test.model.applications[NBI_APP].set_config(
89 {"prometheus-url": "http://prometheus:0231"}
90 )
91
92
93 @pytest.mark.abort_on_fail
94 async def test_nbi_is_blocked_when_relations_are_missing(ops_test: OpsTest):
95 async with ops_test.fast_forward():
96 await ops_test.model.wait_for_idle(apps=[NBI_APP], status="blocked")
97 unit = ops_test.model.applications[NBI_APP].units[0]
98 assert unit.workload_status_message == "need kafka, mongodb, keystone relations"
99
100 logger.info("Adding relations for other components")
101 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
102 await ops_test.model.add_relation(MARIADB_APP, KEYSTONE_APP)
103
104 logger.info("Adding relations for NBI")
105 await ops_test.model.add_relation(
106 "{}:mongodb".format(NBI_APP), "{}:database".format(MONGO_DB_APP)
107 )
108 await ops_test.model.add_relation(NBI_APP, KAFKA_APP)
109 await ops_test.model.add_relation(NBI_APP, KEYSTONE_APP)
110
111 async with ops_test.fast_forward():
112 await ops_test.model.wait_for_idle(apps=APPS, status="active")
113
114
115 @pytest.mark.abort_on_fail
116 async def test_nbi_scales_up(ops_test: OpsTest):
117 logger.info("Scaling up osm-nbi")
118 expected_units = 3
119 assert len(ops_test.model.applications[NBI_APP].units) == 1
120 await ops_test.model.applications[NBI_APP].scale(expected_units)
121 async with ops_test.fast_forward():
122 await ops_test.model.wait_for_idle(
123 apps=[NBI_APP], status="active", wait_for_exact_units=expected_units
124 )
125
126
127 @pytest.mark.abort_on_fail
128 @pytest.mark.parametrize("relation_to_remove", [KAFKA_APP, MONGO_DB_APP, KEYSTONE_APP])
129 async def test_nbi_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
130 logger.info("Removing relation: %s", relation_to_remove)
131 # mongoDB relation is named "database"
132 local_relation = relation_to_remove
133 if local_relation == MONGO_DB_APP:
134 local_relation = "database"
135 await ops_test.model.applications[relation_to_remove].remove_relation(local_relation, NBI_APP)
136 async with ops_test.fast_forward():
137 await ops_test.model.wait_for_idle(apps=[NBI_APP])
138 assert ops_test.model.applications[NBI_APP].status == "blocked"
139 for unit in ops_test.model.applications[NBI_APP].units:
140 assert unit.workload_status_message == f"need {relation_to_remove} relation"
141 await ops_test.model.add_relation(NBI_APP, relation_to_remove)
142 async with ops_test.fast_forward():
143 await ops_test.model.wait_for_idle(apps=APPS, status="active")
144
145
146 @pytest.mark.abort_on_fail
147 async def test_nbi_action_debug_mode_disabled(ops_test: OpsTest):
148 async with ops_test.fast_forward():
149 await ops_test.model.wait_for_idle(
150 apps=APPS,
151 status="active",
152 )
153 logger.info("Running action 'get-debug-mode-information'")
154 action = (
155 await ops_test.model.applications[NBI_APP]
156 .units[0]
157 .run_action("get-debug-mode-information")
158 )
159 async with ops_test.fast_forward():
160 await ops_test.model.wait_for_idle(apps=[NBI_APP])
161 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
162 assert status[action.entity_id] == "failed"
163
164
165 @pytest.mark.abort_on_fail
166 async def test_nbi_action_debug_mode_enabled(ops_test: OpsTest):
167 await ops_test.model.applications[NBI_APP].set_config({"debug-mode": "true"})
168 async with ops_test.fast_forward():
169 await ops_test.model.wait_for_idle(apps=APPS, status="active")
170 logger.info("Running action 'get-debug-mode-information'")
171 # list of units is not ordered
172 unit_id = list(
173 filter(
174 lambda x: (x.entity_id == f"{NBI_APP}/0"), ops_test.model.applications[NBI_APP].units
175 )
176 )[0]
177 action = await unit_id.run_action("get-debug-mode-information")
178 async with ops_test.fast_forward():
179 await ops_test.model.wait_for_idle(apps=[NBI_APP])
180 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
181 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
182 assert status[action.entity_id] == "completed"
183 assert "command" in message
184 assert "password" in message
185
186
187 @pytest.mark.abort_on_fail
188 async def test_nbi_integration_ingress(ops_test: OpsTest):
189 # Temporal workaround due to python-libjuju 2.9.42.2 bug fixed in
190 # https://github.com/juju/python-libjuju/pull/854
191 # To be replaced when juju version 2.9.43 is used.
192 cmd = f"juju deploy {INGRESS_CHARM} {INGRESS_APP} --channel stable"
193 await ops_test.run(*shlex.split(cmd), check=True)
194
195 async with ops_test.fast_forward():
196 await ops_test.model.wait_for_idle(apps=APPS + [INGRESS_APP])
197
198 await ops_test.model.add_relation(NBI_APP, INGRESS_APP)
199 async with ops_test.fast_forward():
200 await ops_test.model.wait_for_idle(apps=APPS + [INGRESS_APP], status="active")