From 335f4bd95e2e5a78b3f60804e04877b8e9d4393c Mon Sep 17 00:00:00 2001 From: John Arbash Meinel Date: Thu, 28 Mar 2024 00:25:53 -0400 Subject: [PATCH] feat(Harness): add some support for user secrets (#1167) At least we should support if a config entry has a 'secret' stanza, which is supported in juju 3.4. It doesn't do all the things yet, but a rough cut. --- ops/testing.py | 3 ++- test/test_testing.py | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ops/testing.py b/ops/testing.py index 47c79eb84..b47a1e465 100644 --- a/ops/testing.py +++ b/ops/testing.py @@ -2011,7 +2011,8 @@ class _TestingConfig(Dict[str, Union[str, int, float, bool]]): 'string': str, 'boolean': bool, 'int': int, - 'float': float + 'float': float, + 'secret': str, # There is some special structure, but they are strings. } def __init__(self, config: '_RawConfig'): diff --git a/test/test_testing.py b/test/test_testing.py index 883f1ebd6..cc0364cac 100644 --- a/test/test_testing.py +++ b/test/test_testing.py @@ -1093,6 +1093,22 @@ def test_bad_config_option_type(self): default: False ''') + def test_config_secret_option(self): + harness = ops.testing.Harness(RecordingCharm, config=''' + options: + a: + description: a config option + type: secret + default: "" + ''') + self.addCleanup(harness.cleanup) + harness.begin() + # [jam] I don't think this is right, as user-secrets aren't owned by the app + secret_id = harness.add_model_secret('mycharm', {'key': 'value'}) + harness.update_config(key_values={'a': secret_id}) + self.assertEqual(harness.charm.changes, + [{'name': 'config-changed', 'data': {'a': secret_id}}]) + def test_no_config_option_type(self): with self.assertRaises(RuntimeError): ops.testing.Harness(RecordingCharm, config=''' @@ -3095,6 +3111,7 @@ def __init__(self, framework: ops.Framework): self.framework.observe(self.on.start, self._on_start) self.framework.observe(self.on.stop, self._on_stop) self.framework.observe(self.on.remove, self._on_remove) + self.framework.observe(self.on.secret_changed, self._on_secret_changed) self.framework.observe(self.on.upgrade_charm, self._on_upgrade_charm) self.framework.observe(self.on.update_status, self._on_update_status) @@ -3127,6 +3144,9 @@ def _on_leader_elected(self, _: ops.LeaderElectedEvent): def _on_leader_settings_changed(self, _: ops.LeaderSettingsChangedEvent): self.changes.append({'name': 'leader-settings-changed'}) + def _on_secret_changed(self, _: ops.SecretChangedEvent): + self.changes.append({'name': 'secret-changed'}) + def _on_upgrade_charm(self, _: ops.UpgradeCharmEvent): self.changes.append({'name': 'upgrade-charm'})