Skip to content

Commit

Permalink
Changes on the Kafka tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juditnovak committed Aug 27, 2023
1 parent 24ee485 commit 30c02e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
14 changes: 13 additions & 1 deletion tests/integration/kafka-charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,13 @@ def _on_topic_requested(self, event: TopicRequestedEvent):
def _on_sync_password(self, event: ActionEvent):
"""Set the password in the data relation databag."""
logger.info("On sync password")
password = event.params["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 @@ -125,7 +131,13 @@ 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
# update relation data if the relation is present
if len(self.kafka_provider.relations) > 0:
Expand Down
44 changes: 43 additions & 1 deletion tests/integration/test_kafka_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from pytest_operator.plugin import OpsTest

from .helpers import get_application_relation_data
from .helpers import get_application_relation_data, get_juju_secret

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -39,6 +39,7 @@ async def test_deploy_charms(ops_test: OpsTest, application_charm, kafka_charm):


@pytest.mark.abort_on_fail
@pytest.mark.usefixtures("only_without_juju_secrets")
async def test_kafka_relation_with_charm_libraries(ops_test: OpsTest):
"""Test basic functionality of kafka relation interface."""
# Relate the charms and wait for them exchanging some connection data.
Expand Down Expand Up @@ -77,6 +78,47 @@ async def test_kafka_relation_with_charm_libraries(ops_test: OpsTest):
assert topic == "test-topic"


@pytest.mark.abort_on_fail
@pytest.mark.usefixtures("only_with_juju_secrets")
async def test_kafka_relation_with_charm_libraries_secrets(ops_test: OpsTest):
"""Test basic functionality of kafka relation interface."""
# Relate the charms and wait for them exchanging some connection data.
await ops_test.model.add_relation(KAFKA_APP_NAME, APPLICATION_APP_NAME)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active")

# check unit messagge to check if the topic_created_event is triggered
for unit in ops_test.model.applications[APPLICATION_APP_NAME].units:
assert unit.workload_status_message == "kafka_topic_created"
# check if the topic was granted
for unit in ops_test.model.applications[KAFKA_APP_NAME].units:
assert "granted" in unit.workload_status_message

secret_id = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "secret"
)

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

boostrap_server = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "endpoints"
)
consumer_group_prefix = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "consumer-group-prefix"
)

topic = await get_application_relation_data(
ops_test, APPLICATION_APP_NAME, RELATION_NAME, "topic"
)

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


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

0 comments on commit 30c02e7

Please sign in to comment.