Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent refresh by revision when the snap is already on that revision #121

Merged
merged 6 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/charms/operator_libs_linux/v2/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,13 +584,16 @@ def ensure(
"Installing snap %s, revision %s, tracking %s", self._name, revision, channel
)
self._install(channel, cohort, revision)
else:
logger.info("The snap installation completed successfully")
elif (revision is None) or (revision != self._revision):
addyess marked this conversation as resolved.
Show resolved Hide resolved
# The snap is installed, but we are changing it (e.g., switching channels).
logger.info(
"Refreshing snap %s, revision %s, tracking %s", self._name, revision, channel
)
self._refresh(channel=channel, cohort=cohort, revision=revision, devmode=devmode)
logger.info("The snap installation completed successfully")
logger.info("The snap refresh completed successfully")
else:
logger.info("Snap refresh was unnecessary %s", self._name)
addyess marked this conversation as resolved.
Show resolved Hide resolved

self._update_snap_apps()
self._state = state
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,27 @@ def test_cohort(self, mock_subprocess):
universal_newlines=True,
)

@patch("charms.operator_libs_linux.v2.snap.subprocess")
def test_revision_doesnt_refresh(self, mock_subprocess):
mock_subprocess.check_output = MagicMock()
addyess marked this conversation as resolved.
Show resolved Hide resolved

snap.add("curl", revision="233", cohort="+")
mock_subprocess.check_output.assert_called_with(
[
"snap",
"install",
"curl",
'--revision="233"',
'--cohort="+"',
],
universal_newlines=True,
)

mock_subprocess.reset_mock()
# ensure that calling refresh with the same revision doesn't subprocess out
benhoyt marked this conversation as resolved.
Show resolved Hide resolved
snap.ensure("curl", "latest", classic=True, revision="233", cohort="+")
mock_subprocess.check_output.assert_not_called()

@patch("charms.operator_libs_linux.v2.snap.subprocess.check_output")
def test_can_ensure_states(self, mock_subprocess):
mock_subprocess.return_value = 0
Expand Down
Loading