Skip to content

Commit

Permalink
Integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Aug 27, 2023
1 parent a03af23 commit 11a9374
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#!/usr/bin/env python3
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.
from typing import Optional
import json
from typing import Dict, Optional

import yaml
from ops import JujuVersion
from pytest_operator.plugin import OpsTest


async def get_juju_secret(ops_test: OpsTest, secret_uri: str) -> Dict[str, str]:
"""Retrieve juju secret."""
secret_unique_id = secret_uri.split("/")[-1]
complete_command = f"show-secret {secret_uri} --reveal --format=json"
_, stdout, _ = await ops_test.juju(*complete_command.split())
return json.loads(stdout)[secret_unique_id]["content"]["Data"]


async def build_connection_string(
ops_test: OpsTest,
application_name: str,
Expand All @@ -30,12 +40,21 @@ async def build_connection_string(
"""
# Get the connection data exposed to the application through the relation.
database = f'{application_name.replace("-", "_")}_{relation_name.replace("-", "_")}'
username = await get_application_relation_data(
ops_test, application_name, relation_name, "username", relation_id, relation_alias
)
password = await get_application_relation_data(
ops_test, application_name, relation_name, "password", relation_id, relation_alias
)

if JujuVersion.from_environ().has_secrets:
secret_uri = await get_application_relation_data(
ops_test, application_name, relation_name, "secret", relation_id, relation_alias
)
secret_data = await get_juju_secret(ops_test, secret_uri)
username = secret_data["username"]
password = secret_data["password"]
else:
username = await get_application_relation_data(
ops_test, application_name, relation_name, "username", relation_id, relation_alias
)
password = await get_application_relation_data(
ops_test, application_name, relation_name, "password", relation_id, relation_alias
)
endpoints = await get_application_relation_data(
ops_test, application_name, relation_name, "endpoints", relation_id, relation_alias
)
Expand Down

0 comments on commit 11a9374

Please sign in to comment.