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"
41 ZOOKEEPER_CHARM
= "zookeeper-k8s"
42 ZOOKEEPER_APP
= "zookeeper"
43 VCA_CHARM
= "osm-vca-integrator"
45 APPS
= [KAFKA_APP
, MONGO_DB_APP
, ZOOKEEPER_APP
, RO_APP
, LCM_APP
]
48 @pytest.mark
.abort_on_fail
49 async def test_lcm_is_deployed(ops_test
: OpsTest
):
50 charm
= await ops_test
.build_charm(".")
51 resources
= {"lcm-image": METADATA
["resources"]["lcm-image"]["upstream-source"]}
52 ro_charm
= await ops_test
.build_charm("../osm-ro/")
53 ro_resources
= {"ro-image": "opensourcemano/ro"}
56 ops_test
.model
.deploy(
57 charm
, resources
=resources
, application_name
=LCM_APP
, series
="focal"
59 ops_test
.model
.deploy(
60 ro_charm
, resources
=ro_resources
, application_name
=RO_APP
, series
="focal"
62 ops_test
.model
.deploy(KAFKA_CHARM
, application_name
=KAFKA_APP
, channel
="stable"),
63 ops_test
.model
.deploy(MONGO_DB_CHARM
, application_name
=MONGO_DB_APP
, channel
="edge"),
64 ops_test
.model
.deploy(ZOOKEEPER_CHARM
, application_name
=ZOOKEEPER_APP
, channel
="stable"),
67 async with ops_test
.fast_forward():
68 await ops_test
.model
.wait_for_idle(
72 assert ops_test
.model
.applications
[LCM_APP
].status
== "blocked"
73 unit
= ops_test
.model
.applications
[LCM_APP
].units
[0]
74 assert unit
.workload_status_message
== "need kafka, mongodb, ro relations"
76 logger
.info("Adding relations for other components")
77 await ops_test
.model
.add_relation(KAFKA_APP
, ZOOKEEPER_APP
)
78 await ops_test
.model
.add_relation(RO_APP
, MONGO_DB_APP
)
79 await ops_test
.model
.add_relation(RO_APP
, KAFKA_APP
)
81 logger
.info("Adding relations")
82 await ops_test
.model
.add_relation(LCM_APP
, MONGO_DB_APP
)
83 await ops_test
.model
.add_relation(LCM_APP
, KAFKA_APP
)
84 await ops_test
.model
.add_relation(LCM_APP
, RO_APP
)
86 async with ops_test
.fast_forward():
87 await ops_test
.model
.wait_for_idle(
94 @pytest.mark
.abort_on_fail
95 async def test_lcm_scales_up(ops_test
: OpsTest
):
96 logger
.info("Scaling up osm-lcm")
98 assert len(ops_test
.model
.applications
[LCM_APP
].units
) == 1
99 await ops_test
.model
.applications
[LCM_APP
].scale(expected_units
)
100 async with ops_test
.fast_forward():
101 await ops_test
.model
.wait_for_idle(
102 apps
=[LCM_APP
], status
="active", timeout
=1000, wait_for_exact_units
=expected_units
106 @pytest.mark
.abort_on_fail
107 @pytest.mark
.parametrize("relation_to_remove", [RO_APP
, KAFKA_APP
, MONGO_DB_APP
])
108 async def test_lcm_blocks_without_relation(ops_test
: OpsTest
, relation_to_remove
):
109 logger
.info("Removing relation: %s", relation_to_remove
)
110 # mongoDB relation is named "database"
111 local_relation
= relation_to_remove
112 if relation_to_remove
== MONGO_DB_APP
:
113 local_relation
= "database"
114 await asyncio
.gather(
115 ops_test
.model
.applications
[relation_to_remove
].remove_relation(local_relation
, LCM_APP
)
117 async with ops_test
.fast_forward():
118 await ops_test
.model
.wait_for_idle(apps
=[LCM_APP
])
119 assert ops_test
.model
.applications
[LCM_APP
].status
== "blocked"
120 for unit
in ops_test
.model
.applications
[LCM_APP
].units
:
121 assert unit
.workload_status_message
== f
"need {relation_to_remove} relation"
122 await ops_test
.model
.add_relation(LCM_APP
, relation_to_remove
)
123 async with ops_test
.fast_forward():
124 await ops_test
.model
.wait_for_idle(
131 @pytest.mark
.abort_on_fail
132 async def test_lcm_action_debug_mode_disabled(ops_test
: OpsTest
):
133 async with ops_test
.fast_forward():
134 await ops_test
.model
.wait_for_idle(
139 logger
.info("Running action 'get-debug-mode-information'")
141 await ops_test
.model
.applications
[LCM_APP
]
143 .run_action("get-debug-mode-information")
145 async with ops_test
.fast_forward():
146 await ops_test
.model
.wait_for_idle(apps
=[LCM_APP
])
147 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
148 assert status
[action
.entity_id
] == "failed"
151 @pytest.mark
.abort_on_fail
152 async def test_lcm_action_debug_mode_enabled(ops_test
: OpsTest
):
153 await ops_test
.model
.applications
[LCM_APP
].set_config({"debug-mode": "true"})
154 async with ops_test
.fast_forward():
155 await ops_test
.model
.wait_for_idle(
160 logger
.info("Running action 'get-debug-mode-information'")
161 # list of units is not ordered
164 lambda x
: (x
.entity_id
== f
"{LCM_APP}/0"), ops_test
.model
.applications
[LCM_APP
].units
167 action
= await unit_id
.run_action("get-debug-mode-information")
168 async with ops_test
.fast_forward():
169 await ops_test
.model
.wait_for_idle(apps
=[LCM_APP
])
170 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
171 message
= await ops_test
.model
.get_action_output(action_uuid
=action
.entity_id
)
172 assert status
[action
.entity_id
] == "completed"
173 assert "command" in message
174 assert "password" in message
177 @pytest.mark
.abort_on_fail
178 async def test_lcm_integration_vca(ops_test
: OpsTest
):
179 await asyncio
.gather(
180 ops_test
.model
.deploy(VCA_CHARM
, application_name
=VCA_APP
, channel
="beta"),
182 async with ops_test
.fast_forward():
183 await ops_test
.model
.wait_for_idle(
187 controllers
= (Path
.home() / ".local/share/juju/controllers.yaml").read_text()
188 accounts
= (Path
.home() / ".local/share/juju/accounts.yaml").read_text()
189 public_key
= (Path
.home() / ".local/share/juju/ssh/juju_id_rsa.pub").read_text()
190 await ops_test
.model
.applications
[VCA_APP
].set_config(
192 "controllers": controllers
,
193 "accounts": accounts
,
194 "public-key": public_key
,
195 "k8s-cloud": "microk8s",
198 async with ops_test
.fast_forward():
199 await ops_test
.model
.wait_for_idle(
200 apps
=APPS
+ [VCA_APP
],
204 await ops_test
.model
.add_relation(LCM_APP
, VCA_APP
)
205 async with ops_test
.fast_forward():
206 await ops_test
.model
.wait_for_idle(
207 apps
=APPS
+ [VCA_APP
],