diff --git a/ops/model.py b/ops/model.py index 906b46374..304af8fe9 100644 --- a/ops/model.py +++ b/ops/model.py @@ -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, diff --git a/test/test_model.py b/test/test_model.py index d74dd1725..8b6434702 100755 --- a/test/test_model.py +++ b/test/test_model.py @@ -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"}'""")