Skip to content

Commit

Permalink
cc_wireguard: make tests pass on FreeBSD
Browse files Browse the repository at this point in the history
These tests relied on the SUT to provide sensible return values for
util.kernel_version(). On FreeBSD, those values are 14.0-CURRENT for my
development machine, which doesn't work well with when split at '.' and
compared against a pair of integers (14, '0-CURRENT') < (5, 4) doesn't
make much sense.

This patch adds `@mock`s for util.kernel_version wherever it is used.
That way, test_cc_wireguard.py tests passes on a FreeBSD system.

Sponsored by: The FreeBSD Foundation
  • Loading branch information
igalic committed Aug 14, 2023
1 parent b1f4a27 commit 8c21b3d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions tests/unittests/config/test_cc_wireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def __init__(self, distro):


class TestWireGuard(CiTestCase):

with_logs = True
allowed_subp = [CiTestCase.SUBP_SHELL_TRUE]

Expand Down Expand Up @@ -137,10 +136,14 @@ def test_maybe_install_wg_packages_noop_when_wg_tools_present(
cc_wireguard.maybe_install_wireguard_packages(cloud=FakeCloud(distro))

@mock.patch("%s.subp.which" % MPATH)
def test_maybe_install_wf_tools_raises_update_errors(self, m_which):
@mock.patch("%s.util.kernel_version" % MPATH)
def test_maybe_install_wf_tools_raises_update_errors(
self, m_kernel_version, m_which
):
"""maybe_install_wireguard_packages logs and raises
apt update errors."""
m_which.return_value = None
m_kernel_version.return_value = (4, 42)
distro = mock.MagicMock()
distro.update_package_sources.side_effect = RuntimeError(
"Some apt error"
Expand All @@ -153,10 +156,14 @@ def test_maybe_install_wf_tools_raises_update_errors(self, m_which):
self.assertIn("Package update failed\nTraceback", self.logs.getvalue())

@mock.patch("%s.subp.which" % MPATH)
def test_maybe_install_wg_raises_install_errors(self, m_which):
@mock.patch("%s.util.kernel_version" % MPATH)
def test_maybe_install_wg_raises_install_errors(
self, m_kernel_version, m_which
):
"""maybe_install_wireguard_packages logs and raises package
install errors."""
m_which.return_value = None
m_kernel_version.return_value = (5, 12)
distro = mock.MagicMock()
distro.update_package_sources.return_value = None
distro.install_packages.side_effect = RuntimeError(
Expand Down Expand Up @@ -192,10 +199,14 @@ def test_load_wg_module_failed(self, m_subp):
)

@mock.patch("%s.subp.which" % MPATH)
def test_maybe_install_wg_packages_happy_path(self, m_which):
@mock.patch("%s.util.kernel_version" % MPATH)
def test_maybe_install_wg_packages_happy_path(
self, m_kernel_version, m_which
):
"""maybe_install_wireguard_packages installs wireguard-tools."""
packages = ["wireguard-tools"]

m_kernel_version.return_value = (5, 2)
if util.kernel_version() < MIN_KERNEL_VERSION:
packages.append("wireguard")

Expand Down Expand Up @@ -259,6 +270,3 @@ def test_schema_validation(self, config, error_msg):
validate_cloudconfig_schema(config, get_schema(), strict=True)
else:
validate_cloudconfig_schema(config, get_schema(), strict=True)


# vi: ts=4 expandtab

0 comments on commit 8c21b3d

Please sign in to comment.