Skip to content

test: drop FilesystemMockingTestCase in netconfig #6290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions tests/unittests/distros/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1 @@
# This file is part of cloud-init. See LICENSE file for license information.
import copy

from cloudinit import distros, helpers, settings


def _get_distro(dtype, system_info=None):
"""Return a Distro class of distro 'dtype'.

cfg is format of CFG_BUILTIN['system_info'].

example: _get_distro("debian")
"""
if system_info is None:
system_info = copy.deepcopy(settings.CFG_BUILTIN["system_info"])
system_info["distro"] = dtype
paths = helpers.Paths(system_info["paths"])
distro_cls = distros.fetch(dtype)
return distro_cls(dtype, system_info, paths)
5 changes: 2 additions & 3 deletions tests/unittests/distros/test_aosc.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# This file is part of cloud-init. See LICENSE file for license information.

from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase
from tests.unittests.helpers import CiTestCase, get_distro


class TestAOSC(CiTestCase):
def test_get_distro(self):
distro = _get_distro("aosc")
distro = get_distro("aosc")
self.assertEqual(distro.osfamily, "aosc")
5 changes: 2 additions & 3 deletions tests/unittests/distros/test_arch.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import util
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase
from tests.unittests.helpers import CiTestCase, get_distro


class TestArch(CiTestCase):
def test_get_distro(self):
distro = _get_distro("arch")
distro = get_distro("arch")
hostname = "myhostname"
hostfile = self.tmp_path("hostfile")
distro._write_hostname(hostname, hostfile)
Expand Down
4 changes: 2 additions & 2 deletions tests/unittests/distros/test_azurelinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from tests.unittests.helpers import CiTestCase

from . import _get_distro
from ..helpers import get_distro

SYSTEM_INFO = {
"paths": {
Expand All @@ -15,7 +15,7 @@

class TestAzurelinux(CiTestCase):
with_logs = True
distro = _get_distro("azurelinux", SYSTEM_INFO)
distro = get_distro("azurelinux", SYSTEM_INFO)
expected_log_line = "Rely on Azure Linux default network config"

def test_network_renderer(self):
Expand Down
5 changes: 2 additions & 3 deletions tests/unittests/distros/test_create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import pytest

from cloudinit import distros, features, lifecycle, ssh_util
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import mock
from tests.unittests.helpers import get_distro, mock
from tests.unittests.util import abstract_to_concrete

USER = "foo_user"
Expand Down Expand Up @@ -368,7 +367,7 @@ def test_avoid_unlock_preexisting_user_empty_password(
mocker,
tmpdir,
):
dist = _get_distro(distro_name)
dist = get_distro(distro_name)
dist.shadow_fn = tmpdir.join(dist.shadow_fn).strpath
dist.shadow_extrausers_fn = tmpdir.join(
dist.shadow_extrausers_fn
Expand Down
7 changes: 3 additions & 4 deletions tests/unittests/distros/test_dragonflybsd.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# This file is part of cloud-init. See LICENSE file for license information.

import cloudinit.util
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import mock
from tests.unittests.helpers import get_distro, mock

M_PATH = "cloudinit.distros."


class TestDragonFlyBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp):
distro = _get_distro("dragonflybsd")
distro = get_distro("dragonflybsd")
assert True is distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
Expand All @@ -29,7 +28,7 @@ def test_add_user(self, m_subp):
] == m_subp.call_args_list

def test_unlock_passwd(self, caplog):
distro = _get_distro("dragonflybsd")
distro = get_distro("dragonflybsd")
distro.unlock_passwd("me2")
assert (
"Dragonfly BSD/FreeBSD password lock is not reversible, "
Expand Down
7 changes: 3 additions & 4 deletions tests/unittests/distros/test_freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import os

from cloudinit.util import find_freebsd_part, get_path_dev_freebsd
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase, mock
from tests.unittests.helpers import CiTestCase, get_distro, mock

M_PATH = "cloudinit.distros.freebsd."


class TestFreeBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp):
distro = _get_distro("freebsd")
distro = get_distro("freebsd")
assert True is distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
Expand All @@ -38,7 +37,7 @@ def test_add_user(self, m_subp):
] == m_subp.call_args_list

def test_unlock_passwd(self, caplog):
distro = _get_distro("freebsd")
distro = get_distro("freebsd")
distro.unlock_passwd("me2")
assert (
"Dragonfly BSD/FreeBSD password lock is not reversible, "
Expand Down
7 changes: 3 additions & 4 deletions tests/unittests/distros/test_gentoo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# This file is part of cloud-init. See LICENSE file for license information.

from cloudinit import atomic_helper, util
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase, mock
from tests.unittests.helpers import CiTestCase, get_distro, mock


class TestGentoo(CiTestCase):
def test_write_hostname(self, whatever=False):
distro = _get_distro("gentoo")
distro = get_distro("gentoo")
hostname = "myhostname"
hostfile = self.tmp_path("hostfile")
distro._write_hostname(hostname, hostfile)
Expand All @@ -19,7 +18,7 @@ def test_write_hostname(self, whatever=False):
)

def test_write_existing_hostname_with_comments(self, whatever=False):
distro = _get_distro("gentoo")
distro = get_distro("gentoo")
hostname = "myhostname"
contents = '#This is the hostname\nhostname="localhost"'
hostfile = self.tmp_path("hostfile")
Expand Down
22 changes: 11 additions & 11 deletions tests/unittests/distros/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
PackageInstallerError,
_get_package_mirror_info,
)
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import get_distro

# In newer versions of Python, these characters will be omitted instead
# of substituted because of security concerns.
Expand Down Expand Up @@ -293,7 +293,7 @@ def test_log_errors_with_updating_package_source(
M_PATH + "snap.Snap.update_package_sources",
side_effect=snap_error,
)
_get_distro("ubuntu").update_package_sources()
get_distro("ubuntu").update_package_sources()
for log in expected_logs:
assert log in caplog.text

Expand Down Expand Up @@ -337,7 +337,7 @@ def test_run_available_package_managers(
m_snap_update = mocker.patch(
M_PATH + "snap.Snap.update_package_sources"
)
_get_distro("ubuntu").update_package_sources()
get_distro("ubuntu").update_package_sources()
if not snap_available:
m_snap_update.assert_not_called()
else:
Expand Down Expand Up @@ -381,12 +381,12 @@ def m_subp(self, mocker):
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"]])
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(
get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"invalid": ["pkg3"]}]
)
assert (
Expand All @@ -400,7 +400,7 @@ def test_unknown_package_manager(self, m_apt_install, caplog):

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(
get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"snap": ["pkg3"]}]
)
apt_install_args = m_apt_install.call_args_list[0][0][0]
Expand All @@ -422,7 +422,7 @@ def test_non_default_package_manager_fail(
PackageInstallerError,
match="Failed to install the following packages: {'pkg3'}",
):
_get_distro("debian").install_packages(
get_distro("debian").install_packages(
[{"apt": ["pkg1"]}, "pkg2", {"snap": ["pkg3"]}]
)

Expand All @@ -432,7 +432,7 @@ def test_default_and_specific_package_manager(
self, m_apt_install, m_snap_install
):
"""Test success from package manager not supported by distro."""
_get_distro("ubuntu").install_packages(
get_distro("ubuntu").install_packages(
["pkg1", ["pkg3", "ver3"], {"apt": [["pkg2", "ver2"]]}]
)
apt_install_args = m_apt_install.call_args_list[0][0][0]
Expand All @@ -451,7 +451,7 @@ def test_specific_package_manager_fail_doesnt_retry(
return_value=["pkg1"],
)
with pytest.raises(PackageInstallerError):
_get_distro("ubuntu").install_packages([{"apt": ["pkg1"]}])
get_distro("ubuntu").install_packages([{"apt": ["pkg1"]}])
apt_install_args = m_apt_install.call_args_list[0][0][0]
assert "pkg1" in apt_install_args
m_snap_install.assert_not_called()
Expand All @@ -463,7 +463,7 @@ def test_no_attempt_if_no_package_manager(
mocker.patch(M_PATH + "apt.Apt.available", return_value=False)
mocker.patch(M_PATH + "snap.Snap.available", return_value=False)
with pytest.raises(PackageInstallerError):
_get_distro("ubuntu").install_packages(
get_distro("ubuntu").install_packages(
["pkg1", "pkg2", {"other": "pkg3"}]
)
m_apt_install.assert_not_called()
Expand Down Expand Up @@ -544,7 +544,7 @@ def test_failed(
return_value=snap_failed,
)
with pytest.raises(PackageInstallerError) as exc:
_get_distro(distro).install_packages(pkg_list)
get_distro(distro).install_packages(pkg_list)
message = exc.value.args[0]
assert "Failed to install the following packages" in message
for pkg in total_failed:
Expand Down
11 changes: 5 additions & 6 deletions tests/unittests/distros/test_manage_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This file is part of cloud-init. See LICENSE file for license information.

from tests.unittests.distros import _get_distro
from tests.unittests.helpers import CiTestCase, mock
from tests.unittests.helpers import CiTestCase, get_distro, mock
from tests.unittests.util import MockDistro


Expand Down Expand Up @@ -34,7 +33,7 @@ def test_manage_service_service_initcmd(self, m_subp, m_sysd):
@mock.patch.object(MockDistro, "uses_systemd", return_value=False)
@mock.patch("cloudinit.distros.subp.subp")
def test_manage_service_rcservice_initcmd(self, m_subp, m_sysd):
dist = _get_distro("alpine")
dist = get_distro("alpine")
dist.init_cmd = ["rc-service", "--nocolor"]
dist.manage_service("start", "myssh")
m_subp.assert_called_with(
Expand All @@ -45,7 +44,7 @@ def test_manage_service_rcservice_initcmd(self, m_subp, m_sysd):

@mock.patch("cloudinit.distros.subp.subp")
def test_manage_service_alpine_rcupdate_cmd(self, m_subp):
dist = _get_distro("alpine")
dist = get_distro("alpine")
dist.update_cmd = ["rc-update", "--nocolor"]
dist.manage_service("enable", "myssh")
m_subp.assert_called_with(
Expand All @@ -54,7 +53,7 @@ def test_manage_service_alpine_rcupdate_cmd(self, m_subp):

@mock.patch("cloudinit.distros.subp.subp")
def test_manage_service_rcctl_initcmd(self, m_subp):
dist = _get_distro("openbsd")
dist = get_distro("openbsd")
dist.init_cmd = ["rcctl"]
dist.manage_service("start", "myssh")
m_subp.assert_called_with(
Expand All @@ -63,7 +62,7 @@ def test_manage_service_rcctl_initcmd(self, m_subp):

@mock.patch("cloudinit.distros.subp.subp")
def test_manage_service_fbsd_service_initcmd(self, m_subp):
dist = _get_distro("freebsd")
dist = get_distro("freebsd")
dist.init_cmd = ["service"]
dist.manage_service("enable", "myssh")
m_subp.assert_called_with(
Expand Down
4 changes: 2 additions & 2 deletions tests/unittests/distros/test_mariner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from tests.unittests.helpers import CiTestCase

from . import _get_distro
from ..helpers import get_distro

SYSTEM_INFO = {
"paths": {
Expand All @@ -15,7 +15,7 @@

class TestMariner(CiTestCase):
with_logs = True
distro = _get_distro("mariner", SYSTEM_INFO)
distro = get_distro("mariner", SYSTEM_INFO)
expected_log_line = "Rely on MarinerOS default network config"

def test_network_renderer(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/unittests/distros/test_netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from tests.unittests.distros import _get_distro
from tests.unittests.helpers import get_distro

try:
# Blowfish not available in < 3.7, so this has never worked. Ignore failure
Expand All @@ -18,7 +18,7 @@
class TestNetBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp):
distro = _get_distro("netbsd")
distro = get_distro("netbsd")
assert True is distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
Expand All @@ -28,7 +28,7 @@ def test_add_user(self, m_subp):

@mock.patch(M_PATH + "subp.subp")
def test_unlock_passwd(self, m_subp, caplog):
distro = _get_distro("netbsd")
distro = get_distro("netbsd")
distro.unlock_passwd("me2")
assert [
mock.call(["usermod", "-C", "no", "me2"])
Expand Down
Loading