Skip to content

Commit

Permalink
Integration tests updated with multi-secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Aug 30, 2023
1 parent 486120b commit 1a81c55
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 21 deletions.
20 changes: 16 additions & 4 deletions tests/integration/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,33 @@ async def build_connection_string(

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

secret_uri = await get_application_relation_data(
ops_test,
application_name,
relation_name,
"secret-endpoints",
relation_id,
relation_alias,
)
secret_data = await get_juju_secret(ops_test, secret_uri)
endpoints = secret_data["endpoints"]

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
)
endpoints = await get_application_relation_data(
ops_test, application_name, relation_name, "endpoints", relation_id, relation_alias
)
host = endpoints.split(",")[0].split(":")[0]

# Build the complete connection string to connect to the database.
Expand Down
10 changes: 0 additions & 10 deletions tests/integration/kafka-charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ def _on_sync_password(self, event: ActionEvent):
logger.info("On sync password")

password = event.params.get("password")
if not password:
secret_uri = event.params.get("secret")
secret_contents = self.charm.model.get_secret(id=secret_uri).get_content()
password = secret_contents.get("password")

self.set_secret("app", "password", password)
logger.info(f"New password: {password}")
# set parameters in the secrets
Expand All @@ -131,11 +126,6 @@ def _on_sync_password(self, event: ActionEvent):
def _on_sync_username(self, event: ActionEvent):
"""Set the username in the data relation databag."""
username = event.params["username"]
if not username:
secret_uri = event.params.get("secret")
secret_contents = self.charm.model.get_secret(id=secret_uri).get_content()
username = secret_contents.get("username")

self.set_secret("app", "username", username)

# set parameters in the secrets
Expand Down
67 changes: 63 additions & 4 deletions tests/integration/test_kafka_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,19 @@ async def test_kafka_relation_with_charm_libraries_secrets(ops_test: OpsTest):
assert "granted" in unit.workload_status_message

secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret"
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-user"
)

secret_content = await get_juju_secret(ops_test, secret_uri)
username = secret_content["username"]
password = secret_content["password"]

boostrap_server = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "endpoints"
secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-endpoints"
)
secret_content = await get_juju_secret(ops_test, secret_uri)
bootstrap_server = secret_content["endpoints"]

consumer_group_prefix = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "consumer-group-prefix"
)
Expand All @@ -114,11 +117,12 @@ async def test_kafka_relation_with_charm_libraries_secrets(ops_test: OpsTest):

assert username == "admin"
assert password == "password"
assert boostrap_server == "host1:port,host2:port"
assert bootstrap_server == "host1:port,host2:port"
assert consumer_group_prefix == "test-prefix"
assert topic == "test-topic"


@pytest.mark.usefixtures("only_without_juju_secrets")
async def test_kafka_bootstrap_server_changed(ops_test: OpsTest):
"""Test that the bootstrap server changed event is correctly triggered."""
app_unit = ops_test.model.applications[APPLICATION_APP_NAME].units[0]
Expand Down Expand Up @@ -157,3 +161,58 @@ async def test_kafka_bootstrap_server_changed(ops_test: OpsTest):
# check the bootstrap_server_changed event is NOT triggered
for unit in ops_test.model.applications[APPLICATION_APP_NAME].units:
assert unit.workload_status_message == ""


@pytest.mark.usefixtures("only_with_juju_secrets")
async def test_kafka_bootstrap_server_changed_secrets(ops_test: OpsTest):
"""Test that the bootstrap server changed event is correctly triggered."""
app_unit = ops_test.model.applications[APPLICATION_APP_NAME].units[0]
kafka_unit = ops_test.model.applications[KAFKA_APP_NAME].units[0]
# set new bootstrap
parameters = {"bootstrap-server": "host1:port,host2:port,host3:port"}
action = await kafka_unit.run_action(action_name="sync-bootstrap-server", **parameters)
result = await action.wait()
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")
assert result.results["bootstrap-server"] == "host1:port,host2:port,host3:port"

# check that the new bootstrap-server is set as a secret
secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-endpoints"
)
secret_content = await get_juju_secret(ops_test, secret_uri)
bootstrap_server = secret_content["endpoints"]
assert bootstrap_server == "host1:port,host2:port,host3:port"

# check that the bootstrap_server_changed event is triggered
for unit in ops_test.model.applications[APPLICATION_APP_NAME].units:
assert unit.workload_status_message == "kafka_bootstrap_server_changed"

# reset unit message
action = await app_unit.run_action(action_name="reset-unit-status")
result = await action.wait()
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")

# check if the message is empty
for unit in ops_test.model.applications[APPLICATION_APP_NAME].units:
assert unit.workload_status_message == ""

# configure the same bootstrap-server
action = await kafka_unit.run_action(action_name="sync-bootstrap-server", **parameters)
result = await action.wait()
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")
assert result.results["bootstrap-server"] == "host1:port,host2:port,host3:port"
bootstrap_server = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "endpoints"
)

# check that the new bootstrap-server secret
secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-endpoints"
)
secret_content = await get_juju_secret(ops_test, secret_uri)
bootstrap_server = secret_content["endpoints"]
assert bootstrap_server == "host1:port,host2:port,host3:port"

# check the bootstrap_server_changed event is NOT triggered
for unit in ops_test.model.applications[APPLICATION_APP_NAME].units:
assert unit.workload_status_message == ""
9 changes: 6 additions & 3 deletions tests/integration/test_opensearch_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,19 @@ async def test_opensearch_relation_with_charm_libraries_secrets(ops_test: OpsTes
assert "granted" in unit.workload_status_message

secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret"
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-user"
)

secret_content = await get_juju_secret(ops_test, secret_uri)
username = secret_content["username"]
password = secret_content["password"]

endpoints = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "endpoints"
secret_uri = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret-endpoints"
)
secret_content = await get_juju_secret(ops_test, secret_uri)
endpoints = secret_content["endpoints"]

index = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "index"
)
Expand Down

0 comments on commit 1a81c55

Please sign in to comment.