Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealFalcon committed Mar 19, 2024
1 parent 1f7b3c0 commit 6cd3978
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
14 changes: 7 additions & 7 deletions cloudinit/distros/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,23 @@ def install_packages(self, pkglist: PackageList):

# First install packages using package manager(s)
# supported by the distro
uninstalled = set()
total_failed = set()
for manager in self.package_managers:

manager_packages = packages_by_manager.get(
manager.__class__, set()
)

to_try = manager_packages | generic_packages
uninstalled.difference_update(to_try)
total_failed.difference_update(to_try)
if not manager.available():
LOG.debug("Package manager '%s' not available", manager.name)
uninstalled.update(to_try)
total_failed.update(to_try)
continue
if not to_try:
continue
failed = manager.install_packages(to_try)
uninstalled.update(failed)
total_failed.update(failed)
unexpected_failed = {
pkg for pkg in failed if pkg not in generic_packages
}
Expand All @@ -271,14 +271,14 @@ def install_packages(self, pkglist: PackageList):
if manager_type.name in [p.name for p in self.package_managers]:
# We already installed/attempted these; don't try again
continue
uninstalled.update(
total_failed.update(
manager_type.from_config(
self._runner, self._cfg
).install_packages(pkglist=packages)
)

if uninstalled:
raise PackageInstallerError(error_message % uninstalled)
if total_failed:
raise PackageInstallerError(error_message % total_failed)

@property
def dhcp_client(self) -> dhcp.DhcpClient:
Expand Down
3 changes: 2 additions & 1 deletion cloudinit/distros/package_management/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def get_unavailable_packages(self, pkglist: Iterable[str]):
return [
pkg
for pkg in pkglist
if re.split("/|=|-", pkg)[0] not in self.get_all_packages()
if re.split("/|=", pkg)[0].rstrip("-")
not in self.get_all_packages()
]

def install_packages(self, pkglist: Iterable) -> UninstalledPackages:
Expand Down
7 changes: 4 additions & 3 deletions tests/unittests/distros/package_management/test_apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,15 @@ def test_search_stem(self, m_subp, m_which, mocker):
mocker.patch(f"{M_PATH}update_package_sources")
mocker.patch(
f"{M_PATH}get_all_packages",
return_value=["pkg1", "pkg2", "pkg3", "pkg4"],
return_value=["cloud-init", "pkg2", "pkg3", "pkg4"],
)
m_install = mocker.patch(f"{M_PATH}run_package_command")

apt = Apt(runner=mock.Mock())
apt.install_packages(
["pkg1", "pkg2-", "pkg3/jammy-updates", "pkg4=1.2"]
["cloud-init", "pkg2-", "pkg3/jammy-updates", "pkg4=1.2"]
)
m_install.assert_called_with(
"install", pkgs=["pkg1", "pkg2-", "pkg3/jammy-updates", "pkg4=1.2"]
"install",
pkgs=["cloud-init", "pkg2-", "pkg3/jammy-updates", "pkg4=1.2"],
)
14 changes: 7 additions & 7 deletions tests/unittests/distros/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def test_no_attempt_if_no_package_manager(
assert "Package manager 'snap' not available" in caplog.text

@pytest.mark.parametrize(
"distro,pkg_list,apt_available,apt_failed,snap_failed,uninstalled",
"distro,pkg_list,apt_available,apt_failed,snap_failed,total_failed",
[
pytest.param(
"debian",
Expand Down Expand Up @@ -428,22 +428,22 @@ def test_no_attempt_if_no_package_manager(
),
],
)
def test_uninstalled(
def test_failed(
self,
distro,
pkg_list,
apt_available,
apt_failed,
snap_failed,
uninstalled,
total_failed,
mocker,
m_apt_install,
m_snap_install,
):
"""Test that uninstalled packages are properly tracked.
"""Test that failed packages are properly tracked.
We need to ensure that the uninstalled packages are properly tracked:
1. When package install fails
We need to ensure that the failed packages are properly tracked:
1. When package install fails normally
2. When package manager is not available
3. When package manager is not explicitly supported by the distro
Expand All @@ -465,5 +465,5 @@ def test_uninstalled(
_get_distro(distro).install_packages(pkg_list)
message = exc.value.args[0]
assert "Failed to install the following packages" in message
for pkg in uninstalled:
for pkg in total_failed:
assert pkg in message

0 comments on commit 6cd3978

Please sign in to comment.