|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2024 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | +import asyncio |
| 5 | +import logging |
| 6 | + |
| 7 | +import psycopg2 |
| 8 | +import pytest |
| 9 | +from pytest_operator.plugin import OpsTest |
| 10 | +from tenacity import Retrying, stop_after_delay, wait_fixed |
| 11 | + |
| 12 | +from ..helpers import CHARM_SERIES, METADATA |
| 13 | +from ..new_relations.test_new_relations import APPLICATION_APP_NAME, build_connection_string |
| 14 | +from ..relations.helpers import get_legacy_db_connection_str |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +APP_NAME = METADATA["name"] |
| 19 | +# MAILMAN3_CORE_APP_NAME = "mailman3-core" |
| 20 | +DB_RELATION = "db" |
| 21 | +DATABASE_RELATION = "database" |
| 22 | +FIRST_DATABASE_RELATION = "first-database" |
| 23 | +DATABASE_APP_NAME = "database-app" |
| 24 | +DB_APP_NAME = "db-app" |
| 25 | +APP_NAMES = [APP_NAME, DATABASE_APP_NAME, DB_APP_NAME] |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.group(1) |
| 29 | +@pytest.mark.abort_on_fail |
| 30 | +async def test_deploy_charms(ops_test: OpsTest, charm): |
| 31 | + """Deploy both charms (application and database) to use in the tests.""" |
| 32 | + # Deploy both charms (multiple units for each application to test that later they correctly |
| 33 | + # set data in the relation application databag using only the leader unit). |
| 34 | + async with ops_test.fast_forward(): |
| 35 | + await asyncio.gather( |
| 36 | + ops_test.model.deploy( |
| 37 | + APPLICATION_APP_NAME, |
| 38 | + application_name=DATABASE_APP_NAME, |
| 39 | + num_units=1, |
| 40 | + series=CHARM_SERIES, |
| 41 | + channel="edge", |
| 42 | + ), |
| 43 | + ops_test.model.deploy( |
| 44 | + charm, |
| 45 | + application_name=APP_NAME, |
| 46 | + num_units=1, |
| 47 | + series=CHARM_SERIES, |
| 48 | + config={ |
| 49 | + "profile": "testing", |
| 50 | + "plugin_unaccent_enable": "True", |
| 51 | + "plugin_pg_trgm_enable": "True", |
| 52 | + }, |
| 53 | + ), |
| 54 | + ops_test.model.deploy( |
| 55 | + APPLICATION_APP_NAME, |
| 56 | + application_name=DB_APP_NAME, |
| 57 | + num_units=1, |
| 58 | + series=CHARM_SERIES, |
| 59 | + channel="edge", |
| 60 | + ), |
| 61 | + ) |
| 62 | + |
| 63 | + await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active", timeout=3000) |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.group(1) |
| 67 | +async def test_legacy_endpoint_with_multiple_related_endpoints(ops_test: OpsTest): |
| 68 | + await ops_test.model.relate(f"{DB_APP_NAME}:{DB_RELATION}", f"{APP_NAME}:{DB_RELATION}") |
| 69 | + await ops_test.model.relate(APP_NAME, f"{DATABASE_APP_NAME}:{FIRST_DATABASE_RELATION}") |
| 70 | + |
| 71 | + app = ops_test.model.applications[APP_NAME] |
| 72 | + await ops_test.model.block_until( |
| 73 | + lambda: "blocked" in {unit.workload_status for unit in app.units}, |
| 74 | + timeout=1500, |
| 75 | + ) |
| 76 | + |
| 77 | + logger.info(" remove relation with modern endpoints") |
| 78 | + await ops_test.model.applications[APP_NAME].remove_relation( |
| 79 | + f"{APP_NAME}:{DATABASE_RELATION}", f"{DATABASE_APP_NAME}:{FIRST_DATABASE_RELATION}" |
| 80 | + ) |
| 81 | + async with ops_test.fast_forward(): |
| 82 | + await ops_test.model.wait_for_idle( |
| 83 | + status="active", |
| 84 | + timeout=1500, |
| 85 | + raise_on_error=False, |
| 86 | + ) |
| 87 | + |
| 88 | + legacy_interface_connect = await get_legacy_db_connection_str( |
| 89 | + ops_test, DB_APP_NAME, DB_RELATION, remote_unit_name=f"{APP_NAME}/0" |
| 90 | + ) |
| 91 | + logger.info(f" check connect to = {legacy_interface_connect}") |
| 92 | + for attempt in Retrying(stop=stop_after_delay(60 * 3), wait=wait_fixed(10)): |
| 93 | + with attempt: |
| 94 | + with psycopg2.connect(legacy_interface_connect) as connection: |
| 95 | + assert connection.status == psycopg2.extensions.STATUS_READY |
| 96 | + |
| 97 | + logger.info(f" remove relation {DB_APP_NAME}:{DB_RELATION}") |
| 98 | + async with ops_test.fast_forward(): |
| 99 | + await ops_test.model.applications[APP_NAME].remove_relation( |
| 100 | + f"{APP_NAME}:{DB_RELATION}", f"{DB_APP_NAME}:{DB_RELATION}" |
| 101 | + ) |
| 102 | + await ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=1000) |
| 103 | + for attempt in Retrying(stop=stop_after_delay(60 * 5), wait=wait_fixed(10)): |
| 104 | + with attempt: |
| 105 | + with pytest.raises(psycopg2.OperationalError): |
| 106 | + psycopg2.connect(legacy_interface_connect) |
| 107 | + |
| 108 | + |
| 109 | +@pytest.mark.group(1) |
| 110 | +async def test_modern_endpoint_with_multiple_related_endpoints(ops_test: OpsTest): |
| 111 | + await ops_test.model.relate(f"{DB_APP_NAME}:{DB_RELATION}", f"{APP_NAME}:{DB_RELATION}") |
| 112 | + await ops_test.model.relate(APP_NAME, f"{DATABASE_APP_NAME}:{FIRST_DATABASE_RELATION}") |
| 113 | + |
| 114 | + app = ops_test.model.applications[APP_NAME] |
| 115 | + await ops_test.model.block_until( |
| 116 | + lambda: "blocked" in {unit.workload_status for unit in app.units}, |
| 117 | + timeout=1500, |
| 118 | + ) |
| 119 | + |
| 120 | + logger.info(" remove relation with legacy endpoints") |
| 121 | + await ops_test.model.applications[APP_NAME].remove_relation( |
| 122 | + f"{DB_APP_NAME}:{DB_RELATION}", f"{APP_NAME}:{DB_RELATION}" |
| 123 | + ) |
| 124 | + async with ops_test.fast_forward(): |
| 125 | + await ops_test.model.wait_for_idle(status="active", timeout=3000, raise_on_error=False) |
| 126 | + |
| 127 | + modern_interface_connect = await build_connection_string( |
| 128 | + ops_test, DATABASE_APP_NAME, FIRST_DATABASE_RELATION |
| 129 | + ) |
| 130 | + logger.info(f"check connect to = {modern_interface_connect}") |
| 131 | + for attempt in Retrying(stop=stop_after_delay(60 * 3), wait=wait_fixed(10)): |
| 132 | + with attempt: |
| 133 | + with psycopg2.connect(modern_interface_connect) as connection: |
| 134 | + assert connection.status == psycopg2.extensions.STATUS_READY |
| 135 | + |
| 136 | + logger.info(f"remove relation {DATABASE_APP_NAME}:{FIRST_DATABASE_RELATION}") |
| 137 | + async with ops_test.fast_forward(): |
| 138 | + await ops_test.model.applications[APP_NAME].remove_relation( |
| 139 | + f"{APP_NAME}:{DATABASE_RELATION}", f"{DATABASE_APP_NAME}:{FIRST_DATABASE_RELATION}" |
| 140 | + ) |
| 141 | + await ops_test.model.wait_for_idle(apps=[APP_NAME], status="active", timeout=1000) |
| 142 | + for attempt in Retrying(stop=stop_after_delay(60 * 5), wait=wait_fixed(10)): |
| 143 | + with attempt: |
| 144 | + with pytest.raises(psycopg2.OperationalError): |
| 145 | + psycopg2.connect(modern_interface_connect) |
0 commit comments