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 LCM_APP
= METADATA
["name"]
36 KAFKA_CHARM
= "kafka-k8s"
38 MONGO_DB_CHARM
= "mongodb-k8s"
39 MONGO_DB_APP
= "mongodb"
42 ZOOKEEPER_CHARM
= "zookeeper-k8s"
43 ZOOKEEPER_APP
= "zookeeper"
44 VCA_CHARM
= "osm-vca-integrator"
46 APPS
= [KAFKA_APP
, MONGO_DB_APP
, ZOOKEEPER_APP
, RO_APP
, LCM_APP
]
49 @pytest.mark
.abort_on_fail
50 async def test_lcm_is_deployed(ops_test
: OpsTest
):
51 charm
= await ops_test
.build_charm(".")
52 resources
= {"lcm-image": METADATA
["resources"]["lcm-image"]["upstream-source"]}
55 ops_test
.model
.deploy(
56 charm
, resources
=resources
, application_name
=LCM_APP
, series
="focal"
58 ops_test
.model
.deploy(RO_CHARM
, application_name
=RO_APP
, channel
="beta"),
59 ops_test
.model
.deploy(KAFKA_CHARM
, application_name
=KAFKA_APP
, channel
="stable"),
60 ops_test
.model
.deploy(MONGO_DB_CHARM
, application_name
=MONGO_DB_APP
, channel
="stable"),
61 ops_test
.model
.deploy(ZOOKEEPER_CHARM
, application_name
=ZOOKEEPER_APP
, channel
="stable"),
64 async with ops_test
.fast_forward():
65 await ops_test
.model
.wait_for_idle(
69 assert ops_test
.model
.applications
[LCM_APP
].status
== "blocked"
70 unit
= ops_test
.model
.applications
[LCM_APP
].units
[0]
71 assert unit
.workload_status_message
== "need kafka, mongodb, ro relations"
73 logger
.info("Adding relations for other components")
74 await ops_test
.model
.add_relation(KAFKA_APP
, ZOOKEEPER_APP
)
75 await ops_test
.model
.add_relation(RO_APP
, MONGO_DB_APP
)
76 await ops_test
.model
.add_relation(RO_APP
, KAFKA_APP
)
78 logger
.info("Adding relations")
79 await ops_test
.model
.add_relation(LCM_APP
, MONGO_DB_APP
)
80 await ops_test
.model
.add_relation(LCM_APP
, KAFKA_APP
)
81 await ops_test
.model
.add_relation(LCM_APP
, RO_APP
)
83 async with ops_test
.fast_forward():
84 await ops_test
.model
.wait_for_idle(
91 @pytest.mark
.abort_on_fail
92 async def test_lcm_scales_up(ops_test
: OpsTest
):
93 logger
.info("Scaling up osm-lcm")
95 assert len(ops_test
.model
.applications
[LCM_APP
].units
) == 1
96 await ops_test
.model
.applications
[LCM_APP
].scale(expected_units
)
97 async with ops_test
.fast_forward():
98 await ops_test
.model
.wait_for_idle(
99 apps
=[LCM_APP
], status
="active", timeout
=1000, wait_for_exact_units
=expected_units
103 @pytest.mark
.abort_on_fail
104 @pytest.mark
.parametrize("relation_to_remove", [RO_APP
, KAFKA_APP
, MONGO_DB_APP
])
105 async def test_lcm_blocks_without_relation(ops_test
: OpsTest
, relation_to_remove
):
106 logger
.info("Removing relation: %s", relation_to_remove
)
107 # mongoDB relation is named "database"
108 local_relation
= relation_to_remove
109 if relation_to_remove
== MONGO_DB_APP
:
110 local_relation
= "database"
111 await asyncio
.gather(
112 ops_test
.model
.applications
[relation_to_remove
].remove_relation(local_relation
, LCM_APP
)
114 async with ops_test
.fast_forward():
115 await ops_test
.model
.wait_for_idle(apps
=[LCM_APP
])
116 assert ops_test
.model
.applications
[LCM_APP
].status
== "blocked"
117 for unit
in ops_test
.model
.applications
[LCM_APP
].units
:
118 assert unit
.workload_status_message
== f
"need {relation_to_remove} relation"
119 await ops_test
.model
.add_relation(LCM_APP
, relation_to_remove
)
120 async with ops_test
.fast_forward():
121 await ops_test
.model
.wait_for_idle(
128 @pytest.mark
.abort_on_fail
129 async def test_lcm_action_debug_mode_disabled(ops_test
: OpsTest
):
130 async with ops_test
.fast_forward():
131 await ops_test
.model
.wait_for_idle(
136 logger
.info("Running action 'get-debug-mode-information'")
138 await ops_test
.model
.applications
[LCM_APP
]
140 .run_action("get-debug-mode-information")
142 async with ops_test
.fast_forward():
143 await ops_test
.model
.wait_for_idle(apps
=[LCM_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_lcm_action_debug_mode_enabled(ops_test
: OpsTest
):
150 await ops_test
.model
.applications
[LCM_APP
].set_config({"debug-mode": "true"})
151 async with ops_test
.fast_forward():
152 await ops_test
.model
.wait_for_idle(
157 logger
.info("Running action 'get-debug-mode-information'")
158 # list of units is not ordered
161 lambda x
: (x
.entity_id
== f
"{LCM_APP}/0"), ops_test
.model
.applications
[LCM_APP
].units
164 action
= await unit_id
.run_action("get-debug-mode-information")
165 async with ops_test
.fast_forward():
166 await ops_test
.model
.wait_for_idle(apps
=[LCM_APP
])
167 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
168 message
= await ops_test
.model
.get_action_output(action_uuid
=action
.entity_id
)
169 assert status
[action
.entity_id
] == "completed"
170 assert "command" in message
171 assert "password" in message
174 @pytest.mark
.abort_on_fail
175 async def test_lcm_integration_vca(ops_test
: OpsTest
):
176 await asyncio
.gather(
177 ops_test
.model
.deploy(VCA_CHARM
, application_name
=VCA_APP
, channel
="beta"),
179 async with ops_test
.fast_forward():
180 await ops_test
.model
.wait_for_idle(
184 controllers
= (Path
.home() / ".local/share/juju/controllers.yaml").read_text()
185 accounts
= (Path
.home() / ".local/share/juju/accounts.yaml").read_text()
186 public_key
= (Path
.home() / ".local/share/juju/ssh/juju_id_rsa.pub").read_text()
187 await ops_test
.model
.applications
[VCA_APP
].set_config(
189 "controllers": controllers
,
190 "accounts": accounts
,
191 "public-key": public_key
,
192 "k8s-cloud": "microk8s",
195 async with ops_test
.fast_forward():
196 await ops_test
.model
.wait_for_idle(
197 apps
=APPS
+ [VCA_APP
],
201 await ops_test
.model
.add_relation(LCM_APP
, VCA_APP
)
202 async with ops_test
.fast_forward():
203 await ops_test
.model
.wait_for_idle(
204 apps
=APPS
+ [VCA_APP
],