Skip to content

Commit

Permalink
proper regex for Distro.shadow_empty_locked_passwd_patterns
Browse files Browse the repository at this point in the history
drop unnneeded test mocks of BSDNetworking
  • Loading branch information
blackboxsw committed Aug 28, 2024
1 parent 15a5610 commit 625d667
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 43 deletions.
6 changes: 3 additions & 3 deletions cloudinit/distros/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class Distro(cloudinit.distros.bsd.BSD):
# field value of either "*" or "*LOCKED*" indicate differing forms of
# "locked" but with no password defined.
shadow_empty_locked_passwd_patterns = [
"^{username}::",
"^{username}:\*:",
"^{username}:\*LOCKED\*:",
r"^{username}::",
r"^{username}:\*:",
r"^{username}:\*LOCKED\*:",
]

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/distros/netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class NetBSD(cloudinit.distros.bsd.BSD):
# password, and a password field of "*LOCKED*" followed by 13 "*"
# indicates a locked and blank password.
shadow_empty_locked_passwd_patterns = [
"^{username}::",
"^{username}:\*\*\*\*\*\*\*\*\*\*\*\*\*:",
"^{username}:\*LOCKED\*\*\*\*\*\*\*\*\*\*\*\*\*\*:",
r"^{username}::",
r"^{username}:\*\*\*\*\*\*\*\*\*\*\*\*\*:",
r"^{username}:\*LOCKED\*\*\*\*\*\*\*\*\*\*\*\*\*\*:",
]

def __init__(self, name, cfg, paths):
Expand Down
6 changes: 3 additions & 3 deletions cloudinit/distros/openbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class Distro(cloudinit.distros.netbsd.NetBSD):
# "*" or "*************" (13 "*") indicate differing forms of "locked"
# but with no password defined.
shadow_empty_locked_passwd_patterns = [
"^{username}::",
"^{username}:\*:",
"^{username}:\*\*\*\*\*\*\*\*\*\*\*\*\*:",
r"^{username}::",
r"^{username}:\*:",
r"^{username}:\*\*\*\*\*\*\*\*\*\*\*\*\*:",
]

def _read_hostname(self, filename, default=None):
Expand Down
6 changes: 4 additions & 2 deletions tests/unittests/distros/test_create_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def test_create_options(
),
pytest.param(
{
"/etc/master.passwd": f"dnsmasq::\n{USER}:*LOCKED**************:"
"/etc/master.passwd":
f"dnsmasq::\n{USER}:*LOCKED**************:"
},
"netbsd",
False,
Expand Down Expand Up @@ -280,7 +281,8 @@ def test_avoid_unlock_preexisting_user_empty_password(
shadow_file.parent.mkdir(parents=True, exist_ok=True)
else:
raise AssertionError(
f"Shadow file path {filename} not defined for distro {dist.name}"
f"Shadow file path {filename} not defined for distro"
f" {dist.name}"
)
shadow_file.write_text(content)
unlock_passwd = mocker.patch.object(dist, "unlock_passwd")
Expand Down
11 changes: 3 additions & 8 deletions tests/unittests/distros/test_dragonflybsd.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# This file is part of cloud-init. See LICENSE file for license information.

import cloudinit.util
from cloudinit.distros.dragonflybsd import Distro
from cloudinit.distros.freebsd import FreeBSDNetworking
from tests.unittests.distros import _get_distro
from tests.unittests.helpers import mock

Expand All @@ -11,10 +9,9 @@

class TestDragonFlyBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
def test_add_user(self, m_subp):
distro = _get_distro("dragonflybsd")
user_created = distro.add_user("me2", uid=1234, default=False)
assert True is distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
[
Expand All @@ -30,10 +27,8 @@ 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)
def test_unlock_passwd(self, caplog):
distro = _get_distro("dragonflybsd")
distro.unlock_passwd("me2")
assert (
Expand Down
10 changes: 3 additions & 7 deletions tests/unittests/distros/test_freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import os

from cloudinit.distros.freebsd import Distro, FreeBSDNetworking
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
Expand All @@ -12,10 +11,9 @@

class TestFreeBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=FreeBSDNetworking)
def test_add_user(self, m_subp):
distro = _get_distro("freebsd")
user_created = distro.add_user("me2", uid=1234, default=False)
assert True is distro.add_user("me2", uid=1234, default=False)
assert [
mock.call(
[
Expand All @@ -38,10 +36,8 @@ 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)
def test_unlock_passwd(self, caplog):
distro = _get_distro("freebsd")
distro.unlock_passwd("me2")
assert (
Expand Down
12 changes: 3 additions & 9 deletions tests/unittests/distros/test_netbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

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
Expand All @@ -20,20 +17,17 @@

class TestNetBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
def test_add_user(self, m_subp):
distro = _get_distro("netbsd")
user_created = distro.add_user("me2", uid=1234, default=False)
assert True is 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)
def test_unlock_passwd(self, m_subp, caplog):
distro = _get_distro("netbsd")
distro.unlock_passwd("me2")
assert [
Expand Down
11 changes: 3 additions & 8 deletions tests/unittests/distros/test_openbsd.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# This file is part of cloud-init. See LICENSE file for license information.

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

Expand All @@ -10,19 +8,16 @@

class TestOpenBSD:
@mock.patch(M_PATH + "subp.subp")
def test_add_user(self, m_subp, mocker):
mocker.patch.object(Distro, "networking_cls", spec=BSDNetworking)
def test_add_user(self, m_subp):
distro = _get_distro("openbsd")
user_created = distro.add_user("me2", uid=1234, default=False)
assert True is 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)
def test_unlock_passwd(self, caplog):
distro = _get_distro("openbsd")
distro.unlock_passwd("me2")
assert (
Expand Down

0 comments on commit 625d667

Please sign in to comment.