Build jammy charms for osm
[osm/devops.git] / installers / charm / osm-pol / 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 from pathlib import Path
27
28 import pytest
29 import yaml
30 from pytest_operator.plugin import OpsTest
31
32 logger = logging.getLogger(__name__)
33
34 METADATA = yaml.safe_load(Path("./metadata.yaml").read_text())
35 POL_APP = METADATA["name"]
36 KAFKA_CHARM = "kafka-k8s"
37 KAFKA_APP = "kafka"
38 MONGO_DB_CHARM = "mongodb-k8s"
39 MONGO_DB_APP = "mongodb"
40 MARIADB_CHARM = "charmed-osm-mariadb-k8s"
41 MARIADB_APP = "mariadb"
42 ZOOKEEPER_CHARM = "zookeeper-k8s"
43 ZOOKEEPER_APP = "zookeeper"
44 APPS = [KAFKA_APP, ZOOKEEPER_APP, MONGO_DB_APP, MARIADB_APP, POL_APP]
45
46
47 @pytest.mark.abort_on_fail
48 async def test_pol_is_deployed(ops_test: OpsTest):
49 charm = await ops_test.build_charm(".")
50 resources = {"pol-image": METADATA["resources"]["pol-image"]["upstream-source"]}
51
52 await asyncio.gather(
53 ops_test.model.deploy(
54 charm, resources=resources, application_name=POL_APP, series="jammy"
55 ),
56 ops_test.model.deploy(KAFKA_CHARM, application_name=KAFKA_APP, channel="stable"),
57 ops_test.model.deploy(MONGO_DB_CHARM, application_name=MONGO_DB_APP, channel="5/edge"),
58 ops_test.model.deploy(MARIADB_CHARM, application_name=MARIADB_APP, channel="stable"),
59 ops_test.model.deploy(ZOOKEEPER_CHARM, application_name=ZOOKEEPER_APP, channel="stable"),
60 )
61
62 async with ops_test.fast_forward():
63 await ops_test.model.wait_for_idle(
64 apps=APPS,
65 )
66 assert ops_test.model.applications[POL_APP].status == "blocked"
67 unit = ops_test.model.applications[POL_APP].units[0]
68 assert unit.workload_status_message == "need kafka, mongodb, mysql relations"
69
70 logger.info("Adding relations for other components")
71 await ops_test.model.add_relation(KAFKA_APP, ZOOKEEPER_APP)
72
73 logger.info("Adding relations for POL")
74 await ops_test.model.add_relation(POL_APP, KAFKA_APP)
75 await ops_test.model.add_relation(
76 "{}:mongodb".format(POL_APP), "{}:database".format(MONGO_DB_APP)
77 )
78 await ops_test.model.add_relation(POL_APP, MARIADB_APP)
79
80 async with ops_test.fast_forward():
81 await ops_test.model.wait_for_idle(
82 apps=APPS,
83 status="active",
84 )
85
86
87 @pytest.mark.abort_on_fail
88 async def test_pol_scales_up(ops_test: OpsTest):
89 logger.info("Scaling up osm-pol")
90 expected_units = 3
91 assert len(ops_test.model.applications[POL_APP].units) == 1
92 await ops_test.model.applications[POL_APP].scale(expected_units)
93 async with ops_test.fast_forward():
94 await ops_test.model.wait_for_idle(
95 apps=[POL_APP], status="active", wait_for_exact_units=expected_units
96 )
97
98
99 @pytest.mark.abort_on_fail
100 @pytest.mark.parametrize("relation_to_remove", [KAFKA_APP, MONGO_DB_APP, MARIADB_APP])
101 async def test_pol_blocks_without_relation(ops_test: OpsTest, relation_to_remove):
102 logger.info("Removing relation: %s", relation_to_remove)
103 # mongoDB relation is named "database"
104 local_relation = relation_to_remove
105 if relation_to_remove == MONGO_DB_APP:
106 local_relation = "database"
107 # mariaDB relation is named "mysql"
108 if relation_to_remove == MARIADB_APP:
109 local_relation = "mysql"
110 await asyncio.gather(
111 ops_test.model.applications[relation_to_remove].remove_relation(local_relation, POL_APP)
112 )
113 async with ops_test.fast_forward():
114 await ops_test.model.wait_for_idle(apps=[POL_APP])
115 assert ops_test.model.applications[POL_APP].status == "blocked"
116 for unit in ops_test.model.applications[POL_APP].units:
117 assert (
118 unit.workload_status_message
119 == f"need {'mysql' if relation_to_remove == MARIADB_APP else relation_to_remove} relation"
120 )
121 await ops_test.model.add_relation(POL_APP, relation_to_remove)
122 async with ops_test.fast_forward():
123 await ops_test.model.wait_for_idle(
124 apps=APPS,
125 status="active",
126 )
127
128
129 @pytest.mark.abort_on_fail
130 async def test_pol_action_debug_mode_disabled(ops_test: OpsTest):
131 async with ops_test.fast_forward():
132 await ops_test.model.wait_for_idle(
133 apps=APPS,
134 status="active",
135 )
136 logger.info("Running action 'get-debug-mode-information'")
137 action = (
138 await ops_test.model.applications[POL_APP]
139 .units[0]
140 .run_action("get-debug-mode-information")
141 )
142 async with ops_test.fast_forward():
143 await ops_test.model.wait_for_idle(apps=[POL_APP])
144 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
145 assert status[action.entity_id] == "failed"
146
147
148 @pytest.mark.abort_on_fail
149 async def test_pol_action_debug_mode_enabled(ops_test: OpsTest):
150 await ops_test.model.applications[POL_APP].set_config({"debug-mode": "true"})
151 async with ops_test.fast_forward():
152 await ops_test.model.wait_for_idle(
153 apps=APPS,
154 status="active",
155 )
156 logger.info("Running action 'get-debug-mode-information'")
157 # list of units is not ordered
158 unit_id = list(
159 filter(
160 lambda x: (x.entity_id == f"{POL_APP}/0"), ops_test.model.applications[POL_APP].units
161 )
162 )[0]
163 action = await unit_id.run_action("get-debug-mode-information")
164 async with ops_test.fast_forward():
165 await ops_test.model.wait_for_idle(apps=[POL_APP])
166 status = await ops_test.model.get_action_status(uuid_or_prefix=action.entity_id)
167 message = await ops_test.model.get_action_output(action_uuid=action.entity_id)
168 assert status[action.entity_id] == "completed"
169 assert "command" in message
170 assert "password" in message