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