Skip to content

Commit

Permalink
Fix tests __grains__["os_family"] type
Browse files Browse the repository at this point in the history
grains.os_family appears to return str
  • Loading branch information
leeclemens committed Dec 22, 2023
1 parent 201a23e commit a8c4b5d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
18 changes: 15 additions & 3 deletions salt/modules/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,23 @@ def get_zone():

else:
os_family = __grains__["os_family"]
if os_family in ("RedHat", "Suse",):
if os_family in (
"RedHat",
"Suse",
):
return _get_zone_sysconfig()
if os_family in ("Debian", "Gentoo",):
if os_family in (
"Debian",
"Gentoo",
):
return _get_zone_etc_timezone()
if os_family in ("FreeBSD", "OpenBSD", "NetBSD", "NILinuxRT", "Slackware",):
if os_family in (
"FreeBSD",
"OpenBSD",
"NetBSD",
"NILinuxRT",
"Slackware",
):
return _get_zone_etc_localtime()
if os_family in ("Solaris",):
return _get_zone_solaris()
Expand Down
36 changes: 18 additions & 18 deletions tests/pytests/unit/modules/test_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_set_zone_os_family_nilinuxrt(patch_os):
Test zone set on NILinuxRT
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["NILinuxRT"]}), patch.dict(
with patch.dict(timezone.__grains__, {"os_family": "NILinuxRT"}), patch.dict(
timezone.__grains__, {"lsb_distrib_id": "nilrt"}
):
assert timezone.set_zone(TEST_TZ)
Expand All @@ -212,7 +212,7 @@ def test_set_zone_redhat(patch_os):
Test zone set on RH series
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["RedHat"]}):
with patch.dict(timezone.__grains__, {"os_family": "RedHat"}):
assert timezone.set_zone(TEST_TZ)
name, args, kwargs = timezone.__salt__["file.sed"].mock_calls[0]
assert args == ("/etc/sysconfig/clock", "^ZONE=.*", 'ZONE="UTC"')
Expand All @@ -224,7 +224,7 @@ def test_set_zone_suse(patch_os):
Test zone set on SUSE series
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["Suse"]}):
with patch.dict(timezone.__grains__, {"os_family": "Suse"}):
assert timezone.set_zone(TEST_TZ)
name, args, kwargs = timezone.__salt__["file.sed"].mock_calls[0]
assert args == ("/etc/sysconfig/clock", "^TIMEZONE=.*", 'TIMEZONE="UTC"')
Expand All @@ -236,7 +236,7 @@ def test_set_zone_gentoo(patch_os):
Test zone set on Gentoo series
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["Gentoo"]}):
with patch.dict(timezone.__grains__, {"os_family": "Gentoo"}):
with patch("salt.utils.files.fopen", mock_open()) as m_open:
assert timezone.set_zone(TEST_TZ)
fh_ = m_open.filehandles["/etc/timezone"][0]
Expand All @@ -250,7 +250,7 @@ def test_set_zone_debian(patch_os):
Test zone set on Debian series
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["Debian"]}):
with patch.dict(timezone.__grains__, {"os_family": "Debian"}):
with patch("salt.utils.files.fopen", mock_open()) as m_open:
assert timezone.set_zone(TEST_TZ)
fh_ = m_open.filehandles["/etc/timezone"][0]
Expand Down Expand Up @@ -299,7 +299,7 @@ def test_get_hwclock_redhat(patch_os):
Test get hwclock on RedHat
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["RedHat"]}):
with patch.dict(timezone.__grains__, {"os_family": "RedHat"}):
timezone.get_hwclock()
name, args, kwarg = timezone.__salt__["cmd.run"].mock_calls[0]
assert args == (["tail", "-n", "1", "/etc/adjtime"],)
Expand All @@ -313,7 +313,7 @@ def _test_get_hwclock_debian(
Test get hwclock on Debian
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["Debian"]}):
with patch.dict(timezone.__grains__, {"os_family": "Debian"}):
timezone.get_hwclock()
name, args, kwarg = timezone.__salt__["cmd.run"].mock_calls[0]
assert args == (["tail", "-n", "1", "/etc/adjtime"],)
Expand All @@ -327,7 +327,7 @@ def test_get_hwclock_solaris(patch_os):
:return:
"""
# Incomplete
with patch.dict(timezone.__grains__, {"os_family": ["Solaris"]}):
with patch.dict(timezone.__grains__, {"os_family": "Solaris"}):
assert timezone.get_hwclock() == "UTC"
with patch("salt.utils.files.fopen", mock_open()):
assert timezone.get_hwclock() == "localtime"
Expand All @@ -343,7 +343,7 @@ def test_get_hwclock_aix(patch_os):
hwclock = "localtime"
if not os.path.isfile("/etc/environment"):
hwclock = "UTC"
with patch.dict(timezone.__grains__, {"os_family": ["AIX"]}):
with patch.dict(timezone.__grains__, {"os_family": "AIX"}):
assert timezone.get_hwclock() == hwclock


Expand All @@ -353,7 +353,7 @@ def test_get_hwclock_slackware_with_adjtime(patch_os):
Test get hwclock on Slackware with /etc/adjtime present
:return:
"""
with patch.dict(timezone.__grains__, {"os_family": ["Slackware"]}):
with patch.dict(timezone.__grains__, {"os_family": "Slackware"}):
timezone.get_hwclock()
name, args, kwarg = timezone.__salt__["cmd.run"].mock_calls[0]
assert args == (["tail", "-n", "1", "/etc/adjtime"],)
Expand All @@ -370,7 +370,7 @@ def test_get_hwclock_slackware_without_adjtime():
with patch("os.path.exists", MagicMock(return_value=False)):
with patch("os.unlink", MagicMock()):
with patch("os.symlink", MagicMock()):
with patch.dict(timezone.__grains__, {"os_family": ["Slackware"]}):
with patch.dict(timezone.__grains__, {"os_family": "Slackware"}):
with patch(
"salt.utils.files.fopen", mock_open(read_data="UTC")
):
Expand Down Expand Up @@ -422,7 +422,7 @@ def test_set_hwclock_solaris(patch_os):
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(
timezone.__grains__, {"os_family": ["Solaris"], "cpuarch": "x86"}
timezone.__grains__, {"os_family": "Solaris", "cpuarch": "x86"}
):
with pytest.raises(SaltInvocationError):
assert timezone.set_hwclock("forty two")
Expand All @@ -441,7 +441,7 @@ def test_set_hwclock_arch(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["Arch"]}):
with patch.dict(timezone.__grains__, {"os_family": "Arch"}):
assert timezone.set_hwclock("UTC")
name, args, kwargs = timezone.__salt__["cmd.retcode"].mock_calls[0]
assert args == (["timezonectl", "set-local-rtc", "false"],)
Expand All @@ -457,7 +457,7 @@ def test_set_hwclock_redhat(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["RedHat"]}):
with patch.dict(timezone.__grains__, {"os_family": "RedHat"}):
assert timezone.set_hwclock("UTC")
name, args, kwargs = timezone.__salt__["file.sed"].mock_calls[0]
assert args == ("/etc/sysconfig/clock", "^ZONE=.*", 'ZONE="TEST_TIMEZONE"')
Expand All @@ -472,7 +472,7 @@ def test_set_hwclock_suse(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["Suse"]}):
with patch.dict(timezone.__grains__, {"os_family": "Suse"}):
assert timezone.set_hwclock("UTC")
name, args, kwargs = timezone.__salt__["file.sed"].mock_calls[0]
assert args == (
Expand All @@ -491,7 +491,7 @@ def test_set_hwclock_debian(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["Debian"]}):
with patch.dict(timezone.__grains__, {"os_family": "Debianvvvvvvvvvvvvvvvvv"}):
assert timezone.set_hwclock("UTC")
name, args, kwargs = timezone.__salt__["file.sed"].mock_calls[0]
assert args == ("/etc/default/rcS", "^UTC=.*", "UTC=yes")
Expand All @@ -510,7 +510,7 @@ def test_set_hwclock_gentoo(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["Gentoo"]}):
with patch.dict(timezone.__grains__, {"os_family": "Gentoo"}):
with pytest.raises(SaltInvocationError):
timezone.set_hwclock("forty two")

Expand All @@ -532,7 +532,7 @@ def test_set_hwclock_slackware(patch_os):
with patch(
"salt.modules.timezone.get_zone", MagicMock(return_value="TEST_TIMEZONE")
):
with patch.dict(timezone.__grains__, {"os_family": ["Slackware"]}):
with patch.dict(timezone.__grains__, {"os_family": "Slackware"}):
with pytest.raises(SaltInvocationError):
timezone.set_hwclock("forty two")

Expand Down

0 comments on commit a8c4b5d

Please sign in to comment.