2 # Copyright 2022 Canonical Ltd.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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
16 # For those usages not covered by the Apache License, Version 2.0 please
17 # contact: legal@canonical.com
19 # To get in touch with the maintainers, please contact:
20 # osm-charmers@lists.launchpad.net
22 # Learn more about testing at: https://juju.is/docs/sdk/testing
26 from pathlib
import Path
30 from pytest_operator
.plugin
import OpsTest
32 logger
= logging
.getLogger(__name__
)
34 METADATA
= yaml
.safe_load(Path("./metadata.yaml").read_text())
35 POL_APP
= METADATA
["name"]
36 KAFKA_CHARM
= "kafka-k8s"
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
]
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"]}
53 ops_test
.model
.deploy(
54 charm
, resources
=resources
, application_name
=POL_APP
, series
="jammy"
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"),
62 async with ops_test
.fast_forward():
63 await ops_test
.model
.wait_for_idle(
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"
70 logger
.info("Adding relations for other components")
71 await ops_test
.model
.add_relation(KAFKA_APP
, ZOOKEEPER_APP
)
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
)
78 await ops_test
.model
.add_relation(POL_APP
, MARIADB_APP
)
80 async with ops_test
.fast_forward():
81 await ops_test
.model
.wait_for_idle(
87 @pytest.mark
.abort_on_fail
88 async def test_pol_scales_up(ops_test
: OpsTest
):
89 logger
.info("Scaling up osm-pol")
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
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
)
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
:
118 unit
.workload_status_message
119 == f
"need {'mysql' if relation_to_remove == MARIADB_APP else relation_to_remove} relation"
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(
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(
136 logger
.info("Running action 'get-debug-mode-information'")
138 await ops_test
.model
.applications
[POL_APP
]
140 .run_action("get-debug-mode-information")
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"
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(
156 logger
.info("Running action 'get-debug-mode-information'")
157 # list of units is not ordered
160 lambda x
: (x
.entity_id
== f
"{POL_APP}/0"), ops_test
.model
.applications
[POL_APP
].units
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