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
27 from pathlib
import Path
31 from pytest_operator
.plugin
import OpsTest
33 logger
= logging
.getLogger(__name__
)
35 METADATA
= yaml
.safe_load(Path("./metadata.yaml").read_text())
36 MON_APP
= METADATA
["name"]
37 KAFKA_CHARM
= "kafka-k8s"
39 KEYSTONE_CHARM
= "osm-keystone"
40 KEYSTONE_APP
= "keystone"
41 MARIADB_CHARM
= "charmed-osm-mariadb-k8s"
42 MARIADB_APP
= "mariadb"
43 MONGO_DB_CHARM
= "mongodb-k8s"
44 MONGO_DB_APP
= "mongodb"
45 PROMETHEUS_CHARM
= "osm-prometheus"
46 PROMETHEUS_APP
= "prometheus"
47 ZOOKEEPER_CHARM
= "zookeeper-k8s"
48 ZOOKEEPER_APP
= "zookeeper"
49 VCA_CHARM
= "osm-vca-integrator"
51 APPS
= [KAFKA_APP
, ZOOKEEPER_APP
, KEYSTONE_APP
, MONGO_DB_APP
, MARIADB_APP
, PROMETHEUS_APP
, MON_APP
]
54 @pytest.mark
.abort_on_fail
55 async def test_mon_is_deployed(ops_test
: OpsTest
):
56 charm
= await ops_test
.build_charm(".")
57 resources
= {"mon-image": METADATA
["resources"]["mon-image"]["upstream-source"]}
60 ops_test
.model
.deploy(
61 charm
, resources
=resources
, application_name
=MON_APP
, series
="jammy"
63 ops_test
.model
.deploy(KAFKA_CHARM
, application_name
=KAFKA_APP
, channel
="stable"),
64 ops_test
.model
.deploy(MONGO_DB_CHARM
, application_name
=MONGO_DB_APP
, channel
="5/edge"),
65 ops_test
.model
.deploy(MARIADB_CHARM
, application_name
=MARIADB_APP
, channel
="stable"),
66 ops_test
.model
.deploy(PROMETHEUS_CHARM
, application_name
=PROMETHEUS_APP
, channel
="stable"),
67 ops_test
.model
.deploy(ZOOKEEPER_CHARM
, application_name
=ZOOKEEPER_APP
, channel
="stable"),
69 keystone_image
= "opensourcemano/keystone:testing-daily"
70 cmd
= f
"juju deploy {KEYSTONE_CHARM} {KEYSTONE_APP} --resource keystone-image={keystone_image} --channel=latest/beta --series jammy"
71 await ops_test
.run(*shlex
.split(cmd
), check
=True)
73 async with ops_test
.fast_forward():
74 await ops_test
.model
.wait_for_idle(
77 assert ops_test
.model
.applications
[MON_APP
].status
== "blocked"
78 unit
= ops_test
.model
.applications
[MON_APP
].units
[0]
79 assert unit
.workload_status_message
== "need kafka, mongodb, prometheus, keystone relations"
81 logger
.info("Adding relations for other components")
82 await ops_test
.model
.add_relation(KAFKA_APP
, ZOOKEEPER_APP
)
83 await ops_test
.model
.add_relation(MARIADB_APP
, KEYSTONE_APP
)
85 logger
.info("Adding relations for MON")
86 await ops_test
.model
.add_relation(
87 "{}:mongodb".format(MON_APP
), "{}:database".format(MONGO_DB_APP
)
89 await ops_test
.model
.add_relation(MON_APP
, KAFKA_APP
)
90 await ops_test
.model
.add_relation(MON_APP
, KEYSTONE_APP
)
91 await ops_test
.model
.add_relation(MON_APP
, PROMETHEUS_APP
)
93 async with ops_test
.fast_forward():
94 await ops_test
.model
.wait_for_idle(
100 @pytest.mark
.abort_on_fail
101 async def test_mon_scales_up(ops_test
: OpsTest
):
102 logger
.info("Scaling up osm-mon")
104 assert len(ops_test
.model
.applications
[MON_APP
].units
) == 1
105 await ops_test
.model
.applications
[MON_APP
].scale(expected_units
)
106 async with ops_test
.fast_forward():
107 await ops_test
.model
.wait_for_idle(
108 apps
=[MON_APP
], status
="active", wait_for_exact_units
=expected_units
112 @pytest.mark
.abort_on_fail
113 @pytest.mark
.parametrize(
114 "relation_to_remove", [KAFKA_APP
, MONGO_DB_APP
, PROMETHEUS_APP
, KEYSTONE_APP
]
116 async def test_mon_blocks_without_relation(ops_test
: OpsTest
, relation_to_remove
):
117 logger
.info("Removing relation: %s", relation_to_remove
)
118 # mongoDB relation is named "database"
119 local_relation
= relation_to_remove
120 if relation_to_remove
== MONGO_DB_APP
:
121 local_relation
= "database"
122 await asyncio
.gather(
123 ops_test
.model
.applications
[relation_to_remove
].remove_relation(local_relation
, MON_APP
)
125 async with ops_test
.fast_forward():
126 await ops_test
.model
.wait_for_idle(apps
=[MON_APP
])
127 assert ops_test
.model
.applications
[MON_APP
].status
== "blocked"
128 for unit
in ops_test
.model
.applications
[MON_APP
].units
:
129 assert unit
.workload_status_message
== f
"need {relation_to_remove} relation"
130 await ops_test
.model
.add_relation(MON_APP
, relation_to_remove
)
131 async with ops_test
.fast_forward():
132 await ops_test
.model
.wait_for_idle(
138 @pytest.mark
.abort_on_fail
139 async def test_mon_action_debug_mode_disabled(ops_test
: OpsTest
):
140 async with ops_test
.fast_forward():
141 await ops_test
.model
.wait_for_idle(
145 logger
.info("Running action 'get-debug-mode-information'")
147 await ops_test
.model
.applications
[MON_APP
]
149 .run_action("get-debug-mode-information")
151 async with ops_test
.fast_forward():
152 await ops_test
.model
.wait_for_idle(apps
=[MON_APP
])
153 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
154 assert status
[action
.entity_id
] == "failed"
157 @pytest.mark
.abort_on_fail
158 async def test_mon_action_debug_mode_enabled(ops_test
: OpsTest
):
159 await ops_test
.model
.applications
[MON_APP
].set_config({"debug-mode": "true"})
160 async with ops_test
.fast_forward():
161 await ops_test
.model
.wait_for_idle(
165 logger
.info("Running action 'get-debug-mode-information'")
166 # list of units is not ordered
169 lambda x
: (x
.entity_id
== f
"{MON_APP}/0"), ops_test
.model
.applications
[MON_APP
].units
172 action
= await unit_id
.run_action("get-debug-mode-information")
173 async with ops_test
.fast_forward():
174 await ops_test
.model
.wait_for_idle(apps
=[MON_APP
])
175 status
= await ops_test
.model
.get_action_status(uuid_or_prefix
=action
.entity_id
)
176 message
= await ops_test
.model
.get_action_output(action_uuid
=action
.entity_id
)
177 assert status
[action
.entity_id
] == "completed"
178 assert "command" in message
179 assert "password" in message
182 @pytest.mark
.abort_on_fail
183 async def test_mon_integration_vca(ops_test
: OpsTest
):
184 await asyncio
.gather(
185 ops_test
.model
.deploy(
186 VCA_CHARM
, application_name
=VCA_APP
, channel
="latest/beta", series
="jammy"
189 async with ops_test
.fast_forward():
190 await ops_test
.model
.wait_for_idle(
193 controllers
= (Path
.home() / ".local/share/juju/controllers.yaml").read_text()
194 accounts
= (Path
.home() / ".local/share/juju/accounts.yaml").read_text()
195 public_key
= (Path
.home() / ".local/share/juju/ssh/juju_id_rsa.pub").read_text()
196 await ops_test
.model
.applications
[VCA_APP
].set_config(
198 "controllers": controllers
,
199 "accounts": accounts
,
200 "public-key": public_key
,
201 "k8s-cloud": "microk8s",
204 async with ops_test
.fast_forward():
205 await ops_test
.model
.wait_for_idle(
206 apps
=APPS
+ [VCA_APP
],
209 await ops_test
.model
.add_relation(MON_APP
, VCA_APP
)
210 async with ops_test
.fast_forward():
211 await ops_test
.model
.wait_for_idle(
212 apps
=APPS
+ [VCA_APP
],