Skip to content

Commit

Permalink
fix(cc_apt_configure): avoid unneeded call to apt-install (#4519)
Browse files Browse the repository at this point in the history
If no apt-sources config is given and no apt depencies are required to
be installed, the avoid unneeded calls to apt-update and apt-install.

The problem was introduced in 015543d.
  • Loading branch information
aciba90 authored Oct 17, 2023
1 parent b2f8683 commit 2ab1f34
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cloudinit/config/cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ def _ensure_dependencies(cfg, aa_repo_match, cloud):
for command in required_cmds:
if not shutil.which(command):
missing_packages.append(PACKAGE_DEPENDENCY_BY_COMMAND[command])
cloud.distro.install_packages(sorted(missing_packages))
if missing_packages:
cloud.distro.install_packages(sorted(missing_packages))


def add_apt_key(ent, cloud, target=None, hardened=False, file_name=None):
Expand Down
69 changes: 69 additions & 0 deletions tests/unittests/config/test_cc_apt_configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@

import pytest

from cloudinit.config import cc_apt_configure
from cloudinit.config.schema import (
SchemaValidationError,
get_schema,
validate_cloudconfig_schema,
)
from tests.unittests.helpers import skipUnlessJsonSchema
from tests.unittests.util import get_cloud


class TestAPTConfigureSchema:
Expand Down Expand Up @@ -197,3 +199,70 @@ def test_schema_validation(self, config, error_msg):
error_msg = error_msg.replace("primary", "security")
with pytest.raises(SchemaValidationError, match=error_msg):
validate_cloudconfig_schema(config, schema, strict=True)


class TestEnsureDependencies:
@pytest.mark.parametrize(
"cfg, already_installed, expected_install",
(
pytest.param({}, [], [], id="empty_cfg_no_pkg_installs"),
pytest.param(
{"sources": {"s1": {"keyid": "haveit"}}},
["gpg"],
[],
id="cfg_needs_gpg_no_installs_when_gpg_present",
),
pytest.param(
{"sources": {"s1": {"keyid": "haveit"}}},
[],
["gnupg"],
id="cfg_needs_gpg_installs_gnupg_when_absent",
),
pytest.param(
{"primary": [{"keyid": "haveit"}]},
[],
["gnupg"],
id="cfg_primary_needs_gpg_installs_gnupg_when_absent",
),
pytest.param(
{"security": [{"keyid": "haveit"}]},
[],
["gnupg"],
id="cfg_security_needs_gpg_installs_gnupg_when_absent",
),
pytest.param(
{"sources": {"s1": {"source": "ppa:yep"}}},
["add-apt-repository"],
[],
id="cfg_needs_sw_prop_common_when_present",
),
pytest.param(
{"sources": {"s1": {"source": "ppa:yep"}}},
[],
["software-properties-common"],
id="cfg_needs_sw_prop_common_when_add_apt_repo_absent",
),
),
)
def test_only_install_needed_packages(
self, cfg, already_installed, expected_install, mocker
):
"""Only invoke install_packages when package installs are necessary"""
mycloud = get_cloud("debian")
install_packages = mocker.patch.object(
mycloud.distro, "install_packages"
)
matcher = re.compile(cc_apt_configure.ADD_APT_REPO_MATCH).search

def fake_which(cmd):
if cmd in already_installed:
return "foundit"
return None

which = mocker.patch.object(cc_apt_configure.shutil, "which")
which.side_effect = fake_which
cc_apt_configure._ensure_dependencies(cfg, matcher, mycloud)
if expected_install:
install_packages.assert_called_once_with(expected_install)
else:
install_packages.assert_not_called()

0 comments on commit 2ab1f34

Please sign in to comment.