Skip to content

Commit

Permalink
feat(Harness): add some support for user secrets (#1167)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jameinel authored Mar 28, 2024
1 parent 8bae12d commit 335f4bd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ops/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down
20 changes: 20 additions & 0 deletions test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='''
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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'})

Expand Down

0 comments on commit 335f4bd

Please sign in to comment.