|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2026 Canonical Ltd. |
| 4 | +# See LICENSE file for licensing details. |
| 5 | + |
| 6 | +"""Tests for the config/user hash + tls plain-databag accessors on peer state. |
| 7 | +
|
| 8 | +These accessors scaffold the config-subsystem port: PostgreSQLPeer.config_hash |
| 9 | +and .user_hash persist the last-applied config/user hashes, PostgreSQLPeer.tls |
| 10 | +persists the last-rendered TLS flag, and PostgreSQLApplication.user_hash |
| 11 | +persists the app-wide user hash. All are plain (non-secret) databag values, |
| 12 | +matching the charm's unit_peer_data / app_peer_data storage. |
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +def _get_unit_db(harness, key): |
| 17 | + rel_id = harness.model.get_relation("database-peers").id |
| 18 | + return harness.get_relation_data(rel_id, harness.charm.unit.name).get(key) |
| 19 | + |
| 20 | + |
| 21 | +def _get_app_db(harness, key): |
| 22 | + rel_id = harness.model.get_relation("database-peers").id |
| 23 | + return harness.get_relation_data(rel_id, harness.charm.app.name).get(key) |
| 24 | + |
| 25 | + |
| 26 | +# -- PostgreSQLPeer.config_hash ---------------------------------------------- |
| 27 | + |
| 28 | + |
| 29 | +def test_config_hash_unset_is_none(harness): |
| 30 | + assert harness.charm.state.peer.config_hash is None |
| 31 | + |
| 32 | + |
| 33 | +def test_config_hash_roundtrip(harness): |
| 34 | + peer = harness.charm.state.peer |
| 35 | + peer.config_hash = "abc123" |
| 36 | + assert peer.config_hash == "abc123" |
| 37 | + |
| 38 | + |
| 39 | +def test_config_hash_writes_exact_databag_key(harness): |
| 40 | + harness.charm.state.peer.config_hash = "abc123" |
| 41 | + assert _get_unit_db(harness, "config_hash") == "abc123" |
| 42 | + |
| 43 | + |
| 44 | +# -- PostgreSQLPeer.user_hash ------------------------------------------------- |
| 45 | + |
| 46 | + |
| 47 | +def test_peer_user_hash_unset_is_none(harness): |
| 48 | + assert harness.charm.state.peer.user_hash is None |
| 49 | + |
| 50 | + |
| 51 | +def test_peer_user_hash_roundtrip(harness): |
| 52 | + peer = harness.charm.state.peer |
| 53 | + peer.user_hash = "def456" |
| 54 | + assert peer.user_hash == "def456" |
| 55 | + |
| 56 | + |
| 57 | +def test_peer_user_hash_writes_exact_databag_key(harness): |
| 58 | + harness.charm.state.peer.user_hash = "def456" |
| 59 | + assert _get_unit_db(harness, "user_hash") == "def456" |
| 60 | + |
| 61 | + |
| 62 | +# -- PostgreSQLPeer.tls -------------------------------------------------------- |
| 63 | + |
| 64 | + |
| 65 | +def test_tls_unset_is_false(harness): |
| 66 | + assert harness.charm.state.peer.tls is False |
| 67 | + |
| 68 | + |
| 69 | +def test_tls_set_true_roundtrip(harness): |
| 70 | + peer = harness.charm.state.peer |
| 71 | + peer.tls = True |
| 72 | + assert peer.tls is True |
| 73 | + |
| 74 | + |
| 75 | +def test_tls_set_true_writes_enabled(harness): |
| 76 | + harness.charm.state.peer.tls = True |
| 77 | + assert _get_unit_db(harness, "tls") == "enabled" |
| 78 | + |
| 79 | + |
| 80 | +def test_tls_set_false_roundtrip(harness): |
| 81 | + peer = harness.charm.state.peer |
| 82 | + peer.tls = True |
| 83 | + peer.tls = False |
| 84 | + assert peer.tls is False |
| 85 | + |
| 86 | + |
| 87 | +def test_tls_set_false_clears_enabled_key(harness): |
| 88 | + """tls=False writes "", which ops relation data drops on write — observed as key absence.""" |
| 89 | + peer = harness.charm.state.peer |
| 90 | + peer.tls = True |
| 91 | + peer.tls = False |
| 92 | + assert _get_unit_db(harness, "tls") is None |
| 93 | + |
| 94 | + |
| 95 | +# -- PostgreSQLApplication.user_hash ------------------------------------------ |
| 96 | + |
| 97 | + |
| 98 | +def test_app_user_hash_unset_is_none(harness): |
| 99 | + assert harness.charm.state.application.user_hash is None |
| 100 | + |
| 101 | + |
| 102 | +def test_app_user_hash_roundtrip(harness): |
| 103 | + application = harness.charm.state.application |
| 104 | + application.user_hash = "ghi789" |
| 105 | + assert application.user_hash == "ghi789" |
| 106 | + |
| 107 | + |
| 108 | +def test_app_user_hash_writes_exact_databag_key(harness): |
| 109 | + harness.charm.state.application.user_hash = "ghi789" |
| 110 | + assert _get_app_db(harness, "user_hash") == "ghi789" |
0 commit comments