From 6e14fe9aca62cf7038a6d84e3a412cfd80437de6 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Mon, 22 Jan 2024 11:22:40 +1300 Subject: [PATCH 1/4] As of Juju 3.1.7 (and 3.3.1, and 3.4) secret owners do not automatically refresh to the latest revision of secrets. --- lib/charms/data_platform_libs/v0/data_secrets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/charms/data_platform_libs/v0/data_secrets.py b/lib/charms/data_platform_libs/v0/data_secrets.py index 254b9af3..4b1dc0df 100644 --- a/lib/charms/data_platform_libs/v0/data_secrets.py +++ b/lib/charms/data_platform_libs/v0/data_secrets.py @@ -97,7 +97,7 @@ def get_content(self) -> Dict[str, str]: """Getting cached secret content.""" if not self._secret_content: if self.meta: - self._secret_content = self.meta.get_content() + self._secret_content = self.meta.peek_content() return self._secret_content def set_content(self, content: Dict[str, str]) -> None: From 646a4921db96b0123a822a113c1a888ea7c5c574 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Mon, 22 Jan 2024 15:22:51 +1300 Subject: [PATCH 2/4] The asserts need to check the latest revision. --- tests/unit/test_data_interfaces.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_data_interfaces.py b/tests/unit/test_data_interfaces.py index 5fbdea06..3be5d25f 100644 --- a/tests/unit/test_data_interfaces.py +++ b/tests/unit/test_data_interfaces.py @@ -332,7 +332,7 @@ def test_set_additional_fields_secrets(self): } secret = self.harness.charm.model.get_secret(id=secret_id) - assert secret.get_content() == { + assert secret.peek_content() == { "tls": "True", "tls-ca": "Canonical", } @@ -661,7 +661,7 @@ def test_set_additional_fields_secrets(self): } secret = self.harness.charm.model.get_secret(id=secret_id) - assert secret.get_content() == { + assert secret.peek_content() == { "tls": "True", "tls-ca": "Canonical", } From bbd9a68dde4eba40a948663084318ce1241b19f2 Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Mon, 22 Jan 2024 15:23:09 +1300 Subject: [PATCH 3/4] Add test that setting multiple secrets works correctly. --- tests/unit/test_data_secrets.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/unit/test_data_secrets.py b/tests/unit/test_data_secrets.py index 2596bb14..28e0a4f8 100644 --- a/tests/unit/test_data_secrets.py +++ b/tests/unit/test_data_secrets.py @@ -53,6 +53,36 @@ def test_cached_secret_is_cached(harness, mocker): assert patched_get_content.called_once() +@pytest.mark.usefixtures("only_with_juju_secrets") +def test_add_multiple_secrets(harness): + """Test that more than one secret key:value can be set.""" + cache = SecretCache(harness.charm) + + secret = cache.get("label1") + assert not secret + cache.add("label1", {"best-2020-picture": "Nomadland"}, scope="app") + + secret = cache.get("label1") + assert secret + content = secret.get_content() + content["best-2021-picture"] = "CODA" + secret.set_content(content) + + secret = cache.get("label1") + assert secret + content = secret.get_content() + content["best-2022-picture"] = "Everything Everywhere All at Once" + secret.set_content(content) + + # Verify that it made it to Juju, not just to the cache. + secret = harness.model.get_secret(label="label1") + assert secret.get_content() == { + "best-2020-picture": "Nomadland", + "best-2021-picture": "CODA", + "best-2022-picture": "Everything Everywhere All at Once", + } + + @pytest.mark.usefixtures("only_with_juju_secrets") def test_secret_cache(harness): """Testing the SecretCache class.""" From a0707cb7c4f9e3f3787a0f7e3ea1c3c06f44a2ef Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Mon, 22 Jan 2024 15:24:47 +1300 Subject: [PATCH 4/4] Style fix. --- tests/unit/test_data_secrets.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_data_secrets.py b/tests/unit/test_data_secrets.py index 28e0a4f8..ac219618 100644 --- a/tests/unit/test_data_secrets.py +++ b/tests/unit/test_data_secrets.py @@ -80,7 +80,7 @@ def test_add_multiple_secrets(harness): "best-2020-picture": "Nomadland", "best-2021-picture": "CODA", "best-2022-picture": "Everything Everywhere All at Once", - } + } @pytest.mark.usefixtures("only_with_juju_secrets")