diff --git a/cloudinit/distros/freebsd.py b/cloudinit/distros/freebsd.py index 889842edbbe8..fc1c38a424ac 100644 --- a/cloudinit/distros/freebsd.py +++ b/cloudinit/distros/freebsd.py @@ -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 diff --git a/cloudinit/distros/netbsd.py b/cloudinit/distros/netbsd.py index f9df7c42669b..157aba06924b 100644 --- a/cloudinit/distros/netbsd.py +++ b/cloudinit/distros/netbsd.py @@ -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): diff --git a/cloudinit/distros/openbsd.py b/cloudinit/distros/openbsd.py index 3c1f9a01a10a..14cf3be2b8ed 100644 --- a/cloudinit/distros/openbsd.py +++ b/cloudinit/distros/openbsd.py @@ -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): diff --git a/tests/unittests/distros/test_create_users.py b/tests/unittests/distros/test_create_users.py index 8d3c09c60009..23031d78b603 100644 --- a/tests/unittests/distros/test_create_users.py +++ b/tests/unittests/distros/test_create_users.py @@ -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, @@ -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") diff --git a/tests/unittests/distros/test_dragonflybsd.py b/tests/unittests/distros/test_dragonflybsd.py index ef887e532458..5419eeeafd4f 100644 --- a/tests/unittests/distros/test_dragonflybsd.py +++ b/tests/unittests/distros/test_dragonflybsd.py @@ -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 @@ -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( [ @@ -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 ( diff --git a/tests/unittests/distros/test_freebsd.py b/tests/unittests/distros/test_freebsd.py index 9c84feb401b2..50fb8e9ffc0b 100644 --- a/tests/unittests/distros/test_freebsd.py +++ b/tests/unittests/distros/test_freebsd.py @@ -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 @@ -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( [ @@ -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 ( diff --git a/tests/unittests/distros/test_netbsd.py b/tests/unittests/distros/test_netbsd.py index 9c191158b737..c4cb9a551223 100644 --- a/tests/unittests/distros/test_netbsd.py +++ b/tests/unittests/distros/test_netbsd.py @@ -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 @@ -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 [ diff --git a/tests/unittests/distros/test_openbsd.py b/tests/unittests/distros/test_openbsd.py index 37388b6935f0..2bab0d3bd149 100644 --- a/tests/unittests/distros/test_openbsd.py +++ b/tests/unittests/distros/test_openbsd.py @@ -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 @@ -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 (