Skip to content

Commit

Permalink
Minor testcases additions and fixups:
Browse files Browse the repository at this point in the history
- add DragonflyBSD testcases to test_create_users.py
- check return code of create_users function in test_*bsd.py files
- make testcase id names more uniform
- add some user tests to test_netbsd.py
  • Loading branch information
dermotbradley authored and blackboxsw committed Aug 28, 2024
1 parent d9cfa17 commit 35ec122
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 22 deletions.
63 changes: 44 additions & 19 deletions tests/unittests/distros/test_create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,6 @@ def common_mocks(mocker):
mocker.patch("cloudinit.distros.util.system_is_snappy", return_value=False)


def _existing_shadow_grep(name: str):
"""Return a mock of grep of /etc/shadow call based on username."""
return mock.call(
["grep", "-q", "-e", f"^{name}::", "-e", f"^{name}:!:", "/etc/shadow"]
)


def _chpasswdmock(name: str, password: str, hashed: bool = False):
"""Return a mock of chpasswd call based on args"""
cmd = ["chpasswd", "-e"] if hashed else ["chpasswd"]
Expand Down Expand Up @@ -135,7 +128,7 @@ def dist(self, tmpdir):
)
@mock.patch("cloudinit.distros.util.is_user", return_value=False)
def test_create_options(
self, _is_user, m_subp, dist, create_kwargs, expected
self, m_is_user, m_subp, dist, create_kwargs, expected
):
dist.create_user(name=USER, **create_kwargs)
assert m_subp.call_args_list == expected
Expand All @@ -148,7 +141,11 @@ def test_create_options(
"ubuntu",
False,
[
"Not unlocking blank password for existing user foo_user. 'lock_passwd: false' present in user-data but no existing password set and no 'plain_text_passwd'/'hashed_passwd' provided in user-data"
"Not unlocking blank password for existing user "
"foo_user. 'lock_passwd: false' present in user-data "
"but no existing password set and no "
"'plain_text_passwd'/'hashed_passwd' provided in "
"user-data"
],
id="no_unlock_on_locked_empty_user_passwd",
),
Expand All @@ -157,7 +154,7 @@ def test_create_options(
"ubuntu",
True,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_in_snappy_on_empty_locked_user_passwd_in_extrausers",
id="no_unlock_in_snappy_on_locked_empty_user_passwd_in_extrausers",
),
pytest.param(
{"/etc/shadow": f"dnsmasq::\n{USER}::"},
Expand All @@ -166,6 +163,27 @@ def test_create_options(
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_user_passwd_alpine",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}::"},
"dragonflybsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_user_passwd_dragonflybsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*:"},
"dragonflybsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_locked_format1_empty_user_passwd_dragonflybsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*LOCKED*:"},
"dragonflybsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_locked_format2_empty_user_passwd_dragonflybsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}::"},
"freebsd",
Expand All @@ -178,28 +196,28 @@ def test_create_options(
"freebsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_user_passwd_freebsd",
id="no_unlock_on_locked_format1_empty_user_passwd_freebsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*LOCKED*:"},
"freebsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_user_passwd_freebsd",
id="no_unlock_on_locked_format2_empty_user_passwd_freebsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*:"},
"freebsd",
{"/etc/master.passwd": f"dnsmasq::\n{USER}::"},
"netbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_format2_user_passwd_freebsd",
id="no_unlock_on_empty_format1_user_passwd_netbsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*************:"},
"netbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_user_passwd_netbsd",
id="no_unlock_on_empty_format2_user_passwd_netbsd",
),
pytest.param(
{
Expand All @@ -208,21 +226,28 @@ def test_create_options(
"netbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_user_passwd_netbsd",
id="no_unlock_on_locked_empty_user_passwd_netbsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}::"},
"openbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_user_passwd_openbsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*:"},
"openbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_user_passwd_openbsd",
id="no_unlock_on_locked_format1_empty_user_passwd_openbsd",
),
pytest.param(
{"/etc/master.passwd": f"dnsmasq::\n{USER}:*************:"},
"openbsd",
False,
["Not unlocking blank password for existing user foo_user."],
id="no_unlock_on_empty_locked_format2_user_passwd_openbsd",
id="no_unlock_on_locked_format2_empty_user_passwd_openbsd",
),
),
)
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/distros/test_dragonflybsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestDragonFlyBSD:
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
distro = _get_distro("dragonflybsd")
distro.add_user("me2", uid=1234, default=False)
user_created = distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
[
Expand All @@ -30,6 +30,7 @@ def test_add_user(self, m_subp, mocker):
logstring=["pw", "useradd", "-n", "me2", "-d/home/me2", "-m"],
)
] == m_subp.call_args_list
assert user_created == True

def test_unlock_passwd(self, mocker, caplog):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/distros/test_freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class TestFreeBSD:
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
distro = _get_distro("freebsd")
distro.add_user("me2", uid=1234, default=False)
user_created = distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
[
Expand All @@ -38,6 +38,7 @@ def test_add_user(self, m_subp, mocker):
],
)
] == m_subp.call_args_list
assert user_created == True

def test_unlock_passwd(self, mocker, caplog):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
Expand Down
30 changes: 30 additions & 0 deletions tests/unittests/distros/test_netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import pytest

from cloudinit.distros.bsd import BSDNetworking
from cloudinit.distros.netbsd import Distro
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import mock

try:
# Blowfish not available in < 3.7, so this has never worked. Ignore failure
# to import with AttributeError. We need this module imported prior to
Expand All @@ -10,6 +15,31 @@
except AttributeError:
pass

M_PATH = "cloudinit.distros.netbsd."


class TestNetBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
distro = _get_distro("netbsd")
user_created = distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
["useradd", "-m", "me2"], logstring=["useradd", "-m", "me2"]
)
] == m_subp.call_args_list
assert user_created == True

@mock.patch(M_PATH + "subp.subp")
def test_unlock_passwd(self, m_subp, mocker, caplog):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
distro = _get_distro("netbsd")
distro.unlock_passwd("me2")
assert [
mock.call(["usermod", "-C", "no", "me2"])
] == m_subp.call_args_list


@pytest.mark.parametrize("with_pkgin", (True, False))
@mock.patch("cloudinit.distros.netbsd.os")
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/distros/test_openbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class TestOpenBSD:
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
distro = _get_distro("openbsd")
distro.add_user("me2", uid=1234, default=False)
user_created = distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
["useradd", "-m", "me2"], logstring=["useradd", "-m", "me2"]
)
] == m_subp.call_args_list
assert user_created == True

def test_unlock_passwd(self, mocker, caplog):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
Expand Down

0 comments on commit 35ec122

Please sign in to comment.