Skip to content

Commit

Permalink
[MISC] Switch test app interface (#362)
Browse files Browse the repository at this point in the history
* Switch test app interface

* Missed changes

* Bump libs

* More missed changes

* More first databases

* Secondary relation ids

* Add a second test app in nodeport test

* Stop continuous writes
  • Loading branch information
dragomirp authored Jul 30, 2024
1 parent cee2b26 commit 2a5b0bf
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 82 deletions.
17 changes: 7 additions & 10 deletions lib/charms/tempo_k8s/v2/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 7
LIBPATCH = 8

PYDEPS = ["pydantic"]

Expand All @@ -116,14 +116,13 @@ def __init__(self, *args):
DEFAULT_RELATION_NAME = "tracing"
RELATION_INTERFACE_NAME = "tracing"

# Supported list rationale https://github.com/canonical/tempo-coordinator-k8s-operator/issues/8
ReceiverProtocol = Literal[
"zipkin",
"kafka",
"opencensus",
"tempo_http",
"tempo_grpc",
"otlp_grpc",
"otlp_http",
"jaeger_grpc",
"jaeger_thrift_http",
]

RawReceiver = Tuple[ReceiverProtocol, str]
Expand All @@ -141,14 +140,12 @@ class TransportProtocolType(str, enum.Enum):
grpc = "grpc"


receiver_protocol_to_transport_protocol = {
receiver_protocol_to_transport_protocol: Dict[ReceiverProtocol, TransportProtocolType] = {
"zipkin": TransportProtocolType.http,
"kafka": TransportProtocolType.http,
"opencensus": TransportProtocolType.http,
"tempo_http": TransportProtocolType.http,
"tempo_grpc": TransportProtocolType.grpc,
"otlp_grpc": TransportProtocolType.grpc,
"otlp_http": TransportProtocolType.http,
"jaeger_thrift_http": TransportProtocolType.http,
"jaeger_grpc": TransportProtocolType.grpc,
}
"""A mapping between telemetry protocols and their corresponding transport protocol.
"""
Expand Down
20 changes: 10 additions & 10 deletions tests/integration/helpers/ha_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ async def count_writes(
ip = service.status.podIP

connection_string = (
f"dbname='{CLIENT_APP_NAME.replace('-', '_')}_first_database' user='operator'"
f"dbname='{CLIENT_APP_NAME.replace('-', '_')}_database' user='operator'"
f" host='{ip}' password='{password}' connect_timeout=10"
)

Expand All @@ -109,7 +109,9 @@ async def count_writes(
return count, maximum


async def start_continuous_writes(ops_test: OpsTest, app: str) -> None:
async def start_continuous_writes(
ops_test: OpsTest, app: str, test_app: str = CLIENT_APP_NAME
) -> None:
"""Start continuous writes to PostgreSQL."""
# Start the process by relating the application to the database or
# by calling the action if the relation already exists.
Expand All @@ -118,33 +120,31 @@ async def start_continuous_writes(ops_test: OpsTest, app: str) -> None:
for relation in ops_test.model.applications[app].relations
if not relation.is_peer
and f"{relation.requires.application_name}:{relation.requires.name}"
== f"{CLIENT_APP_NAME}:first-database"
== f"{test_app}:database"
]
if not relations:
await ops_test.model.relate(app, f"{CLIENT_APP_NAME}:first-database")
await ops_test.model.relate(app, f"{test_app}:database")
await ops_test.model.wait_for_idle(status="active", timeout=1000)
else:
action = (
await ops_test.model.applications[CLIENT_APP_NAME]
await ops_test.model.applications[test_app]
.units[0]
.run_action("start-continuous-writes")
)
await action.wait()
for attempt in Retrying(stop=stop_after_delay(60 * 5), wait=wait_fixed(3), reraise=True):
with attempt:
action = (
await ops_test.model.applications[CLIENT_APP_NAME]
await ops_test.model.applications[test_app]
.units[0]
.run_action("start-continuous-writes")
)
await action.wait()
assert action.results["result"] == "True", "Unable to create continuous_writes table"


async def stop_continuous_writes(ops_test: OpsTest) -> int:
async def stop_continuous_writes(ops_test: OpsTest, test_app: str = CLIENT_APP_NAME) -> int:
"""Stops continuous writes to PostgreSQL and returns the last written value."""
action = await ops_test.model.units.get(f"{CLIENT_APP_NAME}/0").run_action(
"stop-continuous-writes"
)
action = await ops_test.model.units.get(f"{test_app}/0").run_action("stop-continuous-writes")
action = await action.wait()
return int(action.results["writes"])
8 changes: 3 additions & 5 deletions tests/integration/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,11 @@ async def app_name(ops_test: OpsTest, application_name: str = "pgbouncer") -> Op
return None


async def check_tls(ops_test: OpsTest, relation_id: int, enabled: bool) -> bool:
async def check_tls(ops_test: OpsTest, enabled: bool) -> bool:
"""Returns whether TLS is enabled on a related PgBouncer cluster.
Args:
ops_test: The ops test framework instance.
relation_id: The id of the relation.
enabled: check if TLS is enabled/disabled.
Returns:
Expand All @@ -343,9 +342,8 @@ async def check_tls(ops_test: OpsTest, relation_id: int, enabled: bool) -> bool:
cleint_name = await app_name(ops_test, CLIENT_APP_NAME)
unit = ops_test.model.applications[cleint_name].units[0]
params = {
"dbname": f"{CLIENT_APP_NAME.replace('-', '_')}_first_database",
"relation-id": relation_id,
"relation-name": "first-database",
"dbname": f"{CLIENT_APP_NAME.replace('-', '_')}_database",
"relation-name": "database",
"readonly": False,
}
try:
Expand Down
5 changes: 1 addition & 4 deletions tests/integration/relations/pgbouncer_provider/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ async def run_sql_on_application_charm(
unit_name: str,
query: str,
dbname: str,
relation_id,
relation_name,
readonly: bool = False,
timeout=30,
Expand All @@ -116,7 +115,6 @@ async def run_sql_on_application_charm(
params = {
"dbname": dbname,
"query": query,
"relation-id": relation_id,
"relation-name": relation_name,
"readonly": readonly,
}
Expand Down Expand Up @@ -220,7 +218,7 @@ async def build_connection_string(


async def check_new_relation(
ops_test: OpsTest, unit_name, relation_name, relation_id, dbname, table_name="smoke_test"
ops_test: OpsTest, unit_name, relation_name, dbname, table_name="smoke_test"
):
"""Smoke test to check relation is online.
Expand All @@ -240,7 +238,6 @@ async def check_new_relation(
query=query,
dbname=dbname,
relation_name=relation_name,
relation_id=relation_id,
)
assert (
test_data in json.loads(run_query["results"])[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"pgbouncer-image": PGB_METADATA["resources"]["pgbouncer-image"]["upstream-source"]
}
APP_NAMES = [CLIENT_APP_NAME, PG, PGB]
FIRST_DATABASE_RELATION_NAME = "first-database"
FIRST_DATABASE_RELATION_NAME = "database"
SECOND_DATABASE_RELATION_NAME = "second-database"

APPLICATION_FIRST_DBNAME = "postgresql_test_app_first_database"
SECONDARY_APPLICATION_FIRST_DBNAME = "secondary_application_first_database"
APPLICATION_FIRST_DBNAME = "postgresql_test_app_database"
SECONDARY_APPLICATION_FIRST_DBNAME = "secondary_application_database"
SECONDARY_APPLICATION_SECOND_DBNAME = "secondary_application_second_database"


Expand Down Expand Up @@ -92,10 +92,7 @@ async def test_database_relation_with_charm_libraries(ops_test: OpsTest, pgb_cha
)

# Relate the charms and wait for them exchanging some connection data.
global client_relation
client_relation = await ops_test.model.add_relation(
f"{CLIENT_APP_NAME}:{FIRST_DATABASE_RELATION_NAME}", PGB
)
await ops_test.model.add_relation(f"{CLIENT_APP_NAME}:{FIRST_DATABASE_RELATION_NAME}", PGB)

async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active", raise_on_blocked=True)
Expand Down Expand Up @@ -131,7 +128,6 @@ async def test_database_relation_with_charm_libraries(ops_test: OpsTest, pgb_cha
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
Expand All @@ -151,7 +147,6 @@ async def test_database_usage(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=update_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
assert "some data" in json.loads(run_update_query["results"])[0]
Expand All @@ -166,7 +161,6 @@ async def test_database_version(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=version_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
# Get the version of the database and compare with the information that
Expand All @@ -186,7 +180,6 @@ async def test_readonly_reads(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=select_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
readonly=True,
)
Expand All @@ -202,7 +195,6 @@ async def test_cant_write_in_readonly(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=drop_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
readonly=True,
)
Expand All @@ -222,7 +214,6 @@ async def test_database_admin_permissions(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=create_database_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
assert "no results to fetch" in json.loads(run_create_database_query["results"])
Expand All @@ -233,7 +224,6 @@ async def test_database_admin_permissions(ops_test: OpsTest):
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
query=create_user_query,
dbname=APPLICATION_FIRST_DBNAME,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
assert "no results to fetch" in json.loads(run_create_user_query["results"])
Expand All @@ -251,13 +241,19 @@ async def test_no_read_only_endpoint_in_standalone_cluster(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)

relations = [
relation
for relation in ops_test.model.applications[PGB].relations
if not relation.is_peer
and f"{relation.requires.application_name}:{relation.requires.name}"
== f"{CLIENT_APP_NAME}:database"
]
unit = ops_test.model.applications[CLIENT_APP_NAME].units[0]
databag = await get_app_relation_databag(ops_test, unit.name, client_relation.id)
databag = await get_app_relation_databag(ops_test, unit.name, relations[0].id)
assert not databag.get(
"read-only-endpoints", None
), f"read-only-endpoints in pgb databag: {databag}"
Expand All @@ -271,13 +267,19 @@ async def test_read_only_endpoint_in_scaled_up_cluster(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)

relations = [
relation
for relation in ops_test.model.applications[PGB].relations
if not relation.is_peer
and f"{relation.requires.application_name}:{relation.requires.name}"
== f"{CLIENT_APP_NAME}:database"
]
unit = ops_test.model.applications[CLIENT_APP_NAME].units[0]
databag = await get_app_relation_databag(ops_test, unit.name, client_relation.id)
databag = await get_app_relation_databag(ops_test, unit.name, relations[0].id)
read_only_endpoints = databag.get("read-only-endpoints", None)
assert read_only_endpoints, f"read-only-endpoints not in pgb databag: {databag}"

Expand All @@ -298,7 +300,7 @@ async def test_each_relation_has_unique_credentials(ops_test: OpsTest):

# Relate the new application with the database
# and wait for them exchanging some connection data.
secondary_relation = await ops_test.model.add_relation(
await ops_test.model.add_relation(
f"{SECONDARY_CLIENT_APP_NAME}:{FIRST_DATABASE_RELATION_NAME}", PGB
)
wait_for_relation_joined_between(ops_test, PGB, SECONDARY_CLIENT_APP_NAME)
Expand All @@ -308,14 +310,12 @@ async def test_each_relation_has_unique_credentials(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[SECONDARY_CLIENT_APP_NAME].units[0].name,
relation_id=secondary_relation.id,
dbname=SECONDARY_APPLICATION_FIRST_DBNAME,
table_name="check_multiple_apps_connected_to_one_cluster",
relation_name=FIRST_DATABASE_RELATION_NAME,
Expand Down Expand Up @@ -376,7 +376,6 @@ async def test_legacy_relation_compatibility(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
Expand All @@ -400,7 +399,7 @@ async def test_multiple_pgb_can_connect_to_one_backend(ops_test: OpsTest, pgb_ch
async with ops_test.fast_forward():
await ops_test.model.wait_for_idle(apps=APP_NAMES + [pgb_secondary])

secondary_relation = await ops_test.model.add_relation(
await ops_test.model.add_relation(
f"{SECONDARY_CLIENT_APP_NAME}:{SECOND_DATABASE_RELATION_NAME}", pgb_secondary
)
async with ops_test.fast_forward():
Expand All @@ -410,7 +409,6 @@ async def test_multiple_pgb_can_connect_to_one_backend(ops_test: OpsTest, pgb_ch
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[SECONDARY_CLIENT_APP_NAME].units[0].name,
relation_id=secondary_relation.id,
dbname=SECONDARY_APPLICATION_SECOND_DBNAME,
table_name="check_multiple_pgb_connected_to_one_postgres",
relation_name=SECOND_DATABASE_RELATION_NAME,
Expand All @@ -419,7 +417,6 @@ async def test_multiple_pgb_can_connect_to_one_backend(ops_test: OpsTest, pgb_ch
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
relation_name=FIRST_DATABASE_RELATION_NAME,
dbname=APPLICATION_FIRST_DBNAME,
)
Expand All @@ -433,7 +430,6 @@ async def test_scaling(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
Expand All @@ -443,7 +439,6 @@ async def test_scaling(ops_test: OpsTest):
await check_new_relation(
ops_test,
unit_name=ops_test.model.applications[CLIENT_APP_NAME].units[0].name,
relation_id=client_relation.id,
dbname=APPLICATION_FIRST_DBNAME,
relation_name=FIRST_DATABASE_RELATION_NAME,
)
Expand Down Expand Up @@ -474,8 +469,8 @@ async def test_relation_broken(ops_test: OpsTest):
# check relation data was correctly removed from config
pgb_unit_name = ops_test.model.applications[PGB].units[0].name
cfg = await get_cfg(ops_test, pgb_unit_name)
assert "first-database" not in cfg["databases"].keys()
assert "first-database_readonly" not in cfg["databases"].keys()
assert "database" not in cfg["databases"].keys()
assert "database_readonly" not in cfg["databases"].keys()


@pytest.mark.group(1)
Expand Down
Loading

0 comments on commit 2a5b0bf

Please sign in to comment.