Skip to content

Commit

Permalink
fix(snapd): ubuntu do not snap refresh when snap absent
Browse files Browse the repository at this point in the history
No longer call snap refresh when cloud-config user-data
specifies upgade_packages:true and custom Ubuntu images
do not have snapd package installed

LP: #2064300
  • Loading branch information
blackboxsw committed May 3, 2024
1 parent aa357fa commit 51c6569
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cloudinit/distros/ubuntu.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def __init__(self, name, cfg, paths):

def package_command(self, command, args=None, pkgs=None):
super().package_command(command, args, pkgs)
self.snap.upgrade_packages()
if self.snap.available():
self.snap.upgrade_packages()

@property
def preferred_ntp_clients(self):
Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/distros/test_ubuntu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This file is part of cloud-init. See LICENSE file for license information.
import pytest

from cloudinit.distros import fetch


class TestPackageCommand:
@pytest.mark.parametrize("snap_available", (True, False))
def test_package_command_only_refresh_snap_when_available(
self, snap_available, mocker
):
"""Avoid calls to snap refresh when snap command not available."""
m_snap_available = mocker.patch(
"cloudinit.distros.ubuntu.Snap.available",
return_value=snap_available,
)
m_snap_upgrade_packges = mocker.patch(
"cloudinit.distros.ubuntu.Snap.upgrade_packages",
return_value=snap_available,
)
m_apt_run_package_command = mocker.patch(
"cloudinit.distros.package_management.apt.Apt.run_package_command",
)
cls = fetch("ubuntu")
distro = cls("ubuntu", {}, None)
distro.package_command("upgrade")
m_apt_run_package_command.assert_called_once_with("upgrade")
m_snap_available.assert_called_once()
if snap_available:
m_snap_upgrade_packges.assert_called_once()
else:
m_snap_upgrade_packges.assert_not_called()

0 comments on commit 51c6569

Please sign in to comment.