Skip to content

Commit

Permalink
Check the exact value of the ro endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Feb 28, 2025
1 parent c2cb14e commit 5b47189
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 57 deletions.
42 changes: 0 additions & 42 deletions tests/integration/new_relations/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from lightkube import AsyncClient
from lightkube.resources.core_v1 import Service
from pytest_operator.plugin import OpsTest
from tenacity import RetryError, Retrying, stop_after_attempt, wait_exponential


async def get_juju_secret(ops_test: OpsTest, secret_uri: str) -> dict[str, str]:
Expand Down Expand Up @@ -86,47 +85,6 @@ async def build_connection_string(
return f"dbname='{database}' user='{username}' host='{ip}' password='{password}' connect_timeout=10"


async def check_relation_data_existence(
ops_test: OpsTest,
application_name: str,
relation_name: str,
key: str,
exists: bool = True,
) -> bool:
"""Checks for the existence of a key in the relation data.
Args:
ops_test: The ops test framework instance
application_name: The name of the application
relation_name: Name of the relation to get relation data from
key: Key of data to be checked
exists: Whether to check for the existence or non-existence
Returns:
whether the key exists in the relation data
"""
try:
# Retry mechanism used to wait for some events to be triggered,
# like the relation departed event.
for attempt in Retrying(
stop=stop_after_attempt(10), wait=wait_exponential(multiplier=1, min=2, max=30)
):
with attempt:
data = await get_application_relation_data(
ops_test,
application_name,
relation_name,
key,
)
if exists:
assert data is not None
else:
assert data is None
return True
except RetryError:
return False


async def get_alias_from_relation_data(
ops_test: OpsTest, unit_name: str, related_unit_name: str
) -> str | None:
Expand Down
39 changes: 24 additions & 15 deletions tests/integration/new_relations/test_new_relations_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
)
from .helpers import (
build_connection_string,
check_relation_data_existence,
get_application_relation_data,
)

Expand Down Expand Up @@ -335,13 +334,18 @@ async def test_primary_read_only_endpoint_in_standalone_cluster(ops_test: OpsTes

# Try to get the connection string of the database using the read-only endpoint.
# It should not be available anymore.
assert await check_relation_data_existence(
ops_test,
APPLICATION_APP_NAME,
FIRST_DATABASE_RELATION_NAME,
"read-only-endpoints",
exists=True,
)
for attempt in Retrying(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True):
with attempt:
data = await get_application_relation_data(
ops_test,
APPLICATION_APP_NAME,
FIRST_DATABASE_RELATION_NAME,
"read-only-endpoints",
)
assert (
data
== f"{DATABASE_APP_NAME}-primary.{ops_test.model.name}.svc.cluster.local:5432"
)


async def test_read_only_endpoint_in_scaled_up_cluster(ops_test: OpsTest):
Expand All @@ -352,13 +356,18 @@ async def test_read_only_endpoint_in_scaled_up_cluster(ops_test: OpsTest):

# Try to get the connection string of the database using the read-only endpoint.
# It should be available again.
assert await check_relation_data_existence(
ops_test,
APPLICATION_APP_NAME,
FIRST_DATABASE_RELATION_NAME,
"read-only-endpoints",
exists=True,
)
for attempt in Retrying(stop=stop_after_attempt(3), wait=wait_fixed(3), reraise=True):
with attempt:
data = await get_application_relation_data(
ops_test,
APPLICATION_APP_NAME,
FIRST_DATABASE_RELATION_NAME,
"read-only-endpoints",
)
assert (
data
== f"{DATABASE_APP_NAME}-replicas.{ops_test.model.name}.svc.cluster.local:5432"
)


async def test_relation_broken(ops_test: OpsTest):
Expand Down

0 comments on commit 5b47189

Please sign in to comment.