|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | +import pytest |
| 5 | +from pytest_operator.plugin import OpsTest |
| 6 | + |
| 7 | +from ..helpers import METADATA, wait_for_mongodb_units_blocked |
| 8 | + |
| 9 | +MONGODB_K8S_CHARM = "mongodb-k8s" |
| 10 | +SHARD_REL_NAME = "sharding" |
| 11 | +CONFIG_SERVER_REL_NAME = "config-server" |
| 12 | + |
| 13 | +LOCAL_SHARD_APP_NAME = "local-shard" |
| 14 | +REMOTE_SHARD_APP_NAME = "remote-shard" |
| 15 | +LOCAL_CONFIG_SERVER_APP_NAME = "local-config-server" |
| 16 | +REMOTE_CONFIG_SERVER_APP_NAME = "remote-config-server" |
| 17 | + |
| 18 | +CLUSTER_COMPONENTS = [ |
| 19 | + LOCAL_SHARD_APP_NAME, |
| 20 | + REMOTE_SHARD_APP_NAME, |
| 21 | + LOCAL_CONFIG_SERVER_APP_NAME, |
| 22 | + REMOTE_CONFIG_SERVER_APP_NAME, |
| 23 | +] |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.group(1) |
| 27 | +@pytest.mark.abort_on_fail |
| 28 | +async def test_build_and_deploy(ops_test: OpsTest) -> None: |
| 29 | + my_charm = await ops_test.build_charm(".") |
| 30 | + resources = {"mongodb-image": METADATA["resources"]["mongodb-image"]["upstream-source"]} |
| 31 | + |
| 32 | + await ops_test.model.deploy( |
| 33 | + MONGODB_K8S_CHARM, |
| 34 | + application_name=REMOTE_SHARD_APP_NAME, |
| 35 | + config={"role": "shard"}, |
| 36 | + channel="edge", |
| 37 | + ) |
| 38 | + |
| 39 | + await ops_test.model.deploy( |
| 40 | + MONGODB_K8S_CHARM, |
| 41 | + application_name=REMOTE_CONFIG_SERVER_APP_NAME, |
| 42 | + config={"role": "config-server"}, |
| 43 | + channel="edge", |
| 44 | + ) |
| 45 | + await ops_test.model.deploy( |
| 46 | + my_charm, |
| 47 | + resources=resources, |
| 48 | + config={"role": "config-server"}, |
| 49 | + application_name=LOCAL_CONFIG_SERVER_APP_NAME, |
| 50 | + ) |
| 51 | + await ops_test.model.deploy( |
| 52 | + my_charm, |
| 53 | + resources=resources, |
| 54 | + config={"role": "shard"}, |
| 55 | + application_name=LOCAL_SHARD_APP_NAME, |
| 56 | + ) |
| 57 | + |
| 58 | + await ops_test.model.wait_for_idle(apps=CLUSTER_COMPONENTS, idle_period=20) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.group(1) |
| 62 | +@pytest.mark.abort_on_fail |
| 63 | +async def test_local_config_server_reports_remote_shard(ops_test: OpsTest) -> None: |
| 64 | + """Tests that the local config server reports remote shard.""" |
| 65 | + await ops_test.model.integrate( |
| 66 | + f"{REMOTE_SHARD_APP_NAME}:{SHARD_REL_NAME}", |
| 67 | + f"{LOCAL_CONFIG_SERVER_APP_NAME}:{CONFIG_SERVER_REL_NAME}", |
| 68 | + ) |
| 69 | + |
| 70 | + await ops_test.model.wait_for_idle( |
| 71 | + apps=[LOCAL_CONFIG_SERVER_APP_NAME], |
| 72 | + status="waiting", |
| 73 | + raise_on_blocked=False, |
| 74 | + idle_period=20, |
| 75 | + ) |
| 76 | + |
| 77 | + config_server_unit = ops_test.model.applications[LOCAL_CONFIG_SERVER_APP_NAME].units[0] |
| 78 | + |
| 79 | + assert ( |
| 80 | + "Waiting for shards to upgrade/downgrade to revision" |
| 81 | + in config_server_unit.workload_status_message |
| 82 | + ), "Config server does not correctly report mismatch in revision" |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.group(1) |
| 86 | +@pytest.mark.abort_on_fail |
| 87 | +async def test_local_shard_reports_remote_config_server(ops_test: OpsTest) -> None: |
| 88 | + """Tests that the local shard reports remote config-server.""" |
| 89 | + await ops_test.model.integrate( |
| 90 | + f"{LOCAL_SHARD_APP_NAME}:{SHARD_REL_NAME}", |
| 91 | + f"{REMOTE_CONFIG_SERVER_APP_NAME}:{CONFIG_SERVER_REL_NAME}", |
| 92 | + ) |
| 93 | + |
| 94 | + await wait_for_mongodb_units_blocked( |
| 95 | + ops_test, |
| 96 | + LOCAL_SHARD_APP_NAME, |
| 97 | + timeout=300, |
| 98 | + ) |
| 99 | + |
| 100 | + shard_unit = ops_test.model.applications[LOCAL_SHARD_APP_NAME].units[0] |
| 101 | + assert ( |
| 102 | + "is not up-to date with config-server." in shard_unit.workload_status_message |
| 103 | + ), "Shard does not correctly report mismatch in revision" |
0 commit comments