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
="focal"
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
="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")
74 await ops_test
.model
.add_relation(POL_APP
, KAFKA_APP
)
75 await ops_test
.model
.add_relation(POL_APP
, MONGO_DB_APP
)
76 await ops_test
.model
.add_relation(POL_APP
, MARIADB_APP
)
78 async with ops_test
.fast_forward():
79 await ops_test
.model
.wait_for_idle(
85 @pytest.mark
.abort_on_fail
86 async def test_pol_scales_up(ops_test
: OpsTest
):
87 logger
.info("Scaling up osm-pol")
89 assert len(ops_test
.model
.applications
[POL_APP
].units
) == 1
90 await ops_test
.model
.applications
[POL_APP
].scale(expected_units
)
91 async with ops_test
.fast_forward():
92 await ops_test
.model
.wait_for_idle(
93 apps
=[POL_APP
], status
="active", wait_for_exact_units
=expected_units
97 @pytest.mark
.abort_on_fail
98 @pytest.mark
.parametrize("relation_to_remove", [KAFKA_APP
, MONGO_DB_APP
, MARIADB_APP
])
99 async def test_pol_blocks_without_relation(ops_test
: OpsTest
, relation_to_remove
):
100 logger
.info("Removing relation: %s", relation_to_remove
)
101 # mongoDB relation is named "database"
102 local_relation
= relation_to_remove
103 if relation_to_remove
== MONGO_DB_APP
:
104 local_relation
= "database"
105 # mariaDB relation is named "mysql"
106 if relation_to_remove
== MARIADB_APP
:
107 local_relation
= "mysql"
108 await asyncio
.gather(
109 ops_test
.model
.applications
[relation_to_remove
].remove_relation(local_relation
, POL_APP
)
111 async with ops_test
.fast_forward():
112 await ops_test
.model
.wait_for_idle(apps
=[POL_APP
])
113 assert ops_test
.model
.applications
[POL_APP
].status
== "blocked"
114 for unit
in ops_test
.model
.applications
[POL_APP
].units
:
116 unit
.workload_status_message
117 == f
"need {'mysql' if relation_to_remove == MARIADB_APP else relation_to_remove} relation"
119 await ops_test
.model
.add_relation(POL_APP
, relation_to_remove
)
120 async with ops_test
.fast_forward():
121 await ops_test
.model
.wait_for_idle(
127 @pytest.mark
.abort_on_fail
128 async def test_pol_action_debug_mode_disabled(ops_test
: OpsTest
):
129 async with ops_test
.fast_forward():
130 await ops_test
.model
.wait_for_idle(
134 logger
.info("Running action 'get-debug-mode-information'")
136 await ops_test
.model
.applications
[POL_APP
]
138 .run_action("get-debug-mode-information")
140 async with ops_test
.fast_forward():
141 await ops_test
.model
.wait_for_idle(apps
=[POL_APP
])
142 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
143 assert status
[action
.entity_id
] == "failed"
146 @pytest.mark
.abort_on_fail
147 async def test_pol_action_debug_mode_enabled(ops_test
: OpsTest
):
148 await ops_test
.model
.applications
[POL_APP
].set_config({"debug-mode": "true"})
149 async with ops_test
.fast_forward():
150 await ops_test
.model
.wait_for_idle(
154 logger
.info("Running action 'get-debug-mode-information'")
155 # list of units is not ordered
158 lambda x
: (x
.entity_id
== f
"{POL_APP}/0"), ops_test
.model
.applications
[POL_APP
].units
161 action
= await unit_id
.run_action("get-debug-mode-information")
162 async with ops_test
.fast_forward():
163 await ops_test
.model
.wait_for_idle(apps
=[POL_APP
])
164 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
165 message
= await ops_test
.model
.get_action_output(action_uuid
=action
.entity_id
)
166 assert status
[action
.entity_id
] == "completed"
167 assert "command" in message
168 assert "password" in message