Skip to content

Commit

Permalink
fix(model): make Secret.set_content invalidate local cache (#1001)
Browse files Browse the repository at this point in the history
This is so that if get_content() is called after set_content(), it fetches and
returns the expected new content. We could also set self._content to
the new content, but re-fetching it from Juju seems better.
  • Loading branch information
benhoyt authored Aug 30, 2023
1 parent f86a054 commit b6ffaf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions ops/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ def set_content(self, content: Dict[str, str]):
if self._id is None:
self._id = self.get_info().id
self._backend.secret_set(typing.cast(str, self.id), content=content)
self._content = None # invalidate cache so it's refetched next get_content()

def set_info(self, *,
label: Optional[str] = None,
Expand Down
18 changes: 18 additions & 0 deletions test/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3116,6 +3116,24 @@ def test_get_content_copies_dict(self):
self.assertEqual(fake_script_calls(self, clear=True),
[['secret-get', 'secret:z', '--format=json']])

def test_set_content_invalidates_cache(self):
fake_script(self, 'secret-get', """echo '{"foo": "bar"}'""")
fake_script(self, 'secret-set', """exit 0""")

secret = self.make_secret(id='z')
old_content = secret.get_content()
self.assertEqual(old_content, {'foo': 'bar'})
secret.set_content({'new': 'content'})
fake_script(self, 'secret-get', """echo '{"new": "content"}'""")
new_content = secret.get_content()
self.assertEqual(new_content, {'new': 'content'})

self.assertEqual(fake_script_calls(self, clear=True), [
['secret-get', 'secret:z', '--format=json'],
['secret-set', 'secret:z', 'new=content'],
['secret-get', 'secret:z', '--format=json'],
])

def test_peek_content(self):
fake_script(self, 'secret-get', """echo '{"foo": "peeked"}'""")

Expand Down

0 comments on commit b6ffaf0

Please sign in to comment.