From 1a81c55068f5306fa7c07758d5907d9741d93422 Mon Sep 17 00:00:00 2001 From: Judit Novak Date: Tue, 29 Aug 2023 03:54:59 +0200 Subject: [PATCH] Integration tests updated with multi-secrets --- tests/integration/helpers.py | 20 +++++-- tests/integration/kafka-charm/src/charm.py | 10 ---- tests/integration/test_kafka_charm.py | 67 ++++++++++++++++++++-- tests/integration/test_opensearch_charm.py | 9 ++- 4 files changed, 85 insertions(+), 21 deletions(-) diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index d256b13d..4fa93d6b 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -43,11 +43,23 @@ 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 @@ -55,9 +67,9 @@ async def build_connection_string( 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. diff --git a/tests/integration/kafka-charm/src/charm.py b/tests/integration/kafka-charm/src/charm.py index 0a85a991..5a880e2e 100755 --- a/tests/integration/kafka-charm/src/charm.py +++ b/tests/integration/kafka-charm/src/charm.py @@ -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 @@ -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 diff --git a/tests/integration/test_kafka_charm.py b/tests/integration/test_kafka_charm.py index e8f0bfbc..69bbb900 100644 --- a/tests/integration/test_kafka_charm.py +++ b/tests/integration/test_kafka_charm.py @@ -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" ) @@ -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] @@ -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 == "" diff --git a/tests/integration/test_opensearch_charm.py b/tests/integration/test_opensearch_charm.py index 276bcfd4..cd98450c 100644 --- a/tests/integration/test_opensearch_charm.py +++ b/tests/integration/test_opensearch_charm.py @@ -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" )