|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2025 Canonical Ltd. |
| 3 | +# See LICENSE file for licensing details. |
| 4 | + |
| 5 | +import logging |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +import jubilant |
| 9 | +from helpers import connect_cql, get_address, get_secrets_by_label |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +def test_deploy_bad_custom_secret( |
| 15 | + juju: jubilant.Juju, cassandra_charm: Path, app_name: str |
| 16 | +) -> None: |
| 17 | + custom_secret = juju.add_secret("custom_secret", {"foo": "bar"}) |
| 18 | + |
| 19 | + juju.deploy( |
| 20 | + cassandra_charm, |
| 21 | + app=app_name, |
| 22 | + config={"profile": "testing", "system_users": custom_secret}, |
| 23 | + ) |
| 24 | + |
| 25 | + juju.cli("grant-secret", "custom_secret", app_name) |
| 26 | + |
| 27 | + juju.wait(jubilant.all_active) |
| 28 | + |
| 29 | + |
| 30 | +def test_update_custom_secret(juju: jubilant.Juju, app_name: str) -> None: |
| 31 | + secrets = get_secrets_by_label(juju, f"cassandra-peers.{app_name}.app", app_name) |
| 32 | + assert len(secrets) == 1 and secrets[0].get("cassandra-password") != "custom_password" |
| 33 | + with connect_cql( |
| 34 | + juju=juju, app_name=app_name, hosts=[get_address(juju, app_name, 0)] |
| 35 | + ) as session: |
| 36 | + session.execute( |
| 37 | + "CREATE KEYSPACE test " |
| 38 | + "WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}" |
| 39 | + ) |
| 40 | + session.set_keyspace("test") |
| 41 | + session.execute("CREATE TABLE test(message TEXT PRIMARY KEY)") |
| 42 | + |
| 43 | + juju.cli("update-secret", "custom_secret", "cassandra-password=custom_password") |
| 44 | + |
| 45 | + juju.wait( |
| 46 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 47 | + delay=10, |
| 48 | + timeout=300, |
| 49 | + ) |
| 50 | + |
| 51 | + secrets = get_secrets_by_label(juju, f"cassandra-peers.{app_name}.app", app_name) |
| 52 | + assert len(secrets) == 1 and secrets[0].get("cassandra-password") == "custom_password" |
| 53 | + with connect_cql( |
| 54 | + juju=juju, app_name=app_name, hosts=[get_address(juju, app_name, 0)], keyspace="test" |
| 55 | + ) as session: |
| 56 | + session.execute("INSERT INTO test(message) VALUES ('hello')") |
| 57 | + |
| 58 | + |
| 59 | +def test_change_custom_secret(juju: jubilant.Juju, app_name: str) -> None: |
| 60 | + custom_secret_second = juju.add_secret( |
| 61 | + "custom_secret_second", {"cassandra-password": "custom_password_second"} |
| 62 | + ) |
| 63 | + juju.cli("grant-secret", "custom_secret_second", app_name) |
| 64 | + |
| 65 | + juju.config(app=app_name, values={"profile": "testing", "system_users": custom_secret_second}) |
| 66 | + juju.wait( |
| 67 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 68 | + delay=10, |
| 69 | + timeout=300, |
| 70 | + ) |
| 71 | + |
| 72 | + secrets = get_secrets_by_label(juju, f"cassandra-peers.{app_name}.app", app_name) |
| 73 | + assert len(secrets) == 1 and secrets[0].get("cassandra-password") == "custom_password_second" |
| 74 | + with connect_cql( |
| 75 | + juju=juju, app_name=app_name, hosts=[get_address(juju, app_name, 0)], keyspace="test" |
| 76 | + ) as session: |
| 77 | + session.execute("INSERT INTO test(message) VALUES ('world')") |
| 78 | + |
| 79 | + |
| 80 | +def test_remove_custom_secret(juju: jubilant.Juju, app_name: str) -> None: |
| 81 | + juju.config(app=app_name, values={"profile": "testing", "system_users": ""}) |
| 82 | + juju.wait( |
| 83 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 84 | + delay=10, |
| 85 | + timeout=300, |
| 86 | + ) |
| 87 | + |
| 88 | + juju.cli("update-secret", "custom_secret_second", "cassandra-password=custom_password_third") |
| 89 | + juju.wait( |
| 90 | + ready=lambda status: jubilant.all_agents_idle(status) and jubilant.all_active(status), |
| 91 | + delay=10, |
| 92 | + timeout=300, |
| 93 | + ) |
| 94 | + |
| 95 | + secrets = get_secrets_by_label(juju, f"cassandra-peers.{app_name}.app", app_name) |
| 96 | + assert len(secrets) == 1 and secrets[0].get("cassandra-password") == "custom_password_second" |
| 97 | + with connect_cql( |
| 98 | + juju=juju, app_name=app_name, hosts=[get_address(juju, app_name, 0)], keyspace="test" |
| 99 | + ) as session: |
| 100 | + session.execute("INSERT INTO test(message) VALUES ('!')") |
0 commit comments