Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealFalcon committed Sep 21, 2023
1 parent f0ab25d commit feaa88a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ def _new_subp(*args, **kwargs):
handle("", cfg, cloud, [])

assert caplog.records[1].levelname == "INFO"
assert (
caplog.records[1].message == "Snap failed to install package: pkg1"
)
assert caplog.records[1].message == "Failed to 'snap install pkg1'!"

assert caplog.records[2].levelname == "ERROR"
assert caplog.records[2].message.startswith(
Expand Down
70 changes: 70 additions & 0 deletions tests/unittests/distros/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pytest

from cloudinit.distros import LDH_ASCII_CHARS, _get_package_mirror_info
from tests.unittests.distros import _get_distro

# In newer versions of Python, these characters will be omitted instead
# of substituted because of security concerns.
Expand Down Expand Up @@ -246,3 +247,72 @@ def test_valid_substitution(
print(patterns)
print(expected)
assert {"primary": expected} == ret


class TestInstall:
"""Tests for cloudinit.distros.Distro.install_packages."""

@pytest.fixture
def m_apt_install(self, mocker):
return mocker.patch(
"cloudinit.distros.package_management.apt.Apt.install_packages",
return_value=[],
)

@pytest.fixture
def m_snap_install(self, mocker):
return mocker.patch(
"cloudinit.distros.package_management.snap.Snap.install_packages",
return_value=[],
)

def test_invalid_yaml(self, m_apt_install):
"""Test that an invalid YAML raises an exception."""
with pytest.raises(ValueError):
_get_distro("debian").install_packages([["invalid"]])
m_apt_install.assert_not_called()

def test_unknown_package_manager(self, m_apt_install, caplog):
"""Test that an unknown package manager raises an exception."""
_get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"invalid": ["pkg3"]}]
)
assert (
"Cannot install packages under 'invalid' as it is not a supported "
"package manager!" in caplog.text
)
install_args = m_apt_install.call_args_list[0][0][0]
assert "pkg1" in install_args
assert "pkg2" in install_args
assert "pkg3" not in install_args

def test_non_default_package_manager(self, m_apt_install, m_snap_install):
"""Test success from package manager not supported by distro."""
_get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"snap": ["pkg3"]}]
)
apt_install_args = m_apt_install.call_args_list[0][0][0]
assert "pkg1" in apt_install_args
assert "pkg2" in apt_install_args
assert "pkg3" not in apt_install_args

assert "pkg3" in m_snap_install.call_args_list[0][1]["pkglist"]

def test_non_default_package_manager_fail(
self, m_apt_install, mocker, caplog
):
"""Test fail from package manager not supported by distro."""
m_snap_install = mocker.patch(
"cloudinit.distros.package_management.snap.Snap.install_packages",
return_value=["pkg3"],
)
_get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"snap": ["pkg3"]}]
)

assert "pkg3" in m_snap_install.call_args_list[0][1]["pkglist"]
assert (
"Failed to install the following packages: ['pkg3']. "
"See associated package manager logs for more details"
in caplog.text
)

0 comments on commit feaa88a

Please sign in to comment.