Fix regression in win_timezone.get_zone, move unit test to pytest

This commit is contained in:
Lukas Raska 2021-09-02 09:38:54 +02:00 committed by Megan Wilhite
parent cc0abfe717
commit 439438848c
4 changed files with 209 additions and 170 deletions

1
changelog/60829.fixed Normal file
View file

@ -0,0 +1 @@
Fix regression in win_timezone.get_zone which failed to resolve specific timezones that begin or end with d/s/t/o/f/_ characters

View file

@ -218,7 +218,10 @@ def get_zone():
raise CommandExecutionError(
"tzutil encountered an error getting timezone", info=res
)
tz = res["stdout"].lower().strip("_dstoff")
tz = res["stdout"].lower()
if tz.endswith("_dstoff"):
tz = tz[:-7]
return mapper.get_unix(tz, "Unknown")

View file

@ -0,0 +1,204 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""
import pytest
import salt.modules.win_timezone as win_timezone
from salt.exceptions import CommandExecutionError
from tests.support.mock import MagicMock, patch
pytestmark = [
pytest.mark.skipif(not win_timezone.HAS_PYTZ, reason="This test requires pytz"),
]
@pytest.fixture
def configure_loader_modules():
return {
win_timezone: {
"__opts__": {},
"__salt__": {},
"__utils__": {},
},
}
def test_get_zone_normal():
"""
Test if it get current timezone (i.e. Asia/Calcutta)
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
assert win_timezone.get_zone() == "Asia/Calcutta"
def test_get_zone_normal_dstoff():
"""
Test if it gets current timezone with dst off (i.e. America/Denver)
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "Mountain Standard Time_dstoff",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
assert win_timezone.get_zone() == "America/Denver"
def test_get_zone_normal_dstoff_issue():
"""
Test regression with dstoff fix stripping unwanted characters
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "FLE Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
assert win_timezone.get_zone() == "Europe/Kiev"
@pytest.mark.parametrize("timezone", win_timezone.mapper.list_win())
def test_get_zone_all(timezone):
"""
Test all Win zones are properly resolved and none returns Unknown
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": timezone,
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
assert win_timezone.get_zone() != "Unknown"
def test_get_zone_unknown():
"""
Test get_zone with unknown timezone (i.e. Indian Standard Time)
"""
mock_read_error = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "Indian Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_error}):
assert win_timezone.get_zone() == "Unknown"
def test_get_zone_error():
"""
Test get_zone when it encounters an error
"""
mock_read_fatal = MagicMock(
return_value={"pid": 78, "retcode": 1, "stderr": "", "stdout": ""}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_fatal}):
with pytest.raises(CommandExecutionError):
win_timezone.get_zone()
def test_get_offset():
"""
Test if it get current numeric timezone offset from UCT (i.e. +0530)
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
assert win_timezone.get_offset() == "+0530"
def test_get_zonecode():
"""
Test if it get current timezone (i.e. PST, MDT, etc)
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
assert win_timezone.get_zonecode() == "IST"
def test_set_zone():
"""
Test if it unlinks, then symlinks /etc/localtime to the set timezone.
"""
mock_write = MagicMock(
return_value={"pid": 78, "retcode": 0, "stderr": "", "stdout": ""}
)
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_write}), patch.dict(
win_timezone.__salt__, {"cmd.run_all": mock_read}
):
assert win_timezone.set_zone("Asia/Calcutta")
def test_zone_compare():
"""
Test if it checks the md5sum between the given timezone, and
the one set in /etc/localtime. Returns True if they match,
and False if not. Mostly useful for running state checks.
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
assert win_timezone.zone_compare("Asia/Calcutta")
def test_get_hwclock():
"""
Test if it get current hardware clock setting (UTC or localtime)
"""
assert win_timezone.get_hwclock() == "localtime"
def test_set_hwclock():
"""
Test if it sets the hardware clock to be either UTC or localtime
"""
assert not win_timezone.set_hwclock("UTC")

View file

@ -1,169 +0,0 @@
"""
:codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
"""
import salt.modules.win_timezone as win_timezone
from salt.exceptions import CommandExecutionError
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.mock import MagicMock, patch
from tests.support.unit import TestCase, skipIf
@skipIf(not win_timezone.HAS_PYTZ, "This test requires pytz")
class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
"""
Test cases for salt.modules.win_timezone
"""
def setup_loader_modules(self):
return {win_timezone: {}}
def test_get_zone_normal(self):
"""
Test if it get current timezone (i.e. Asia/Calcutta)
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
self.assertEqual(win_timezone.get_zone(), "Asia/Calcutta")
def test_get_zone_normal_dstoff(self):
"""
Test if it gets current timezone with dst off (i.e. America/Denver)
"""
mock_read_ok = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "Mountain Standard Time_dstoff",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_ok}):
self.assertEqual(win_timezone.get_zone(), "America/Denver")
def test_get_zone_unknown(self):
"""
Test get_zone with unknown timezone (i.e. Indian Standard Time)
"""
mock_read_error = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "Indian Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_error}):
self.assertEqual(win_timezone.get_zone(), "Unknown")
def test_get_zone_error(self):
"""
Test get_zone when it encounters an error
"""
mock_read_fatal = MagicMock(
return_value={"pid": 78, "retcode": 1, "stderr": "", "stdout": ""}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read_fatal}):
self.assertRaises(CommandExecutionError, win_timezone.get_zone)
# 'get_offset' function tests: 1
def test_get_offset(self):
"""
Test if it get current numeric timezone offset from UCT (i.e. +0530)
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
self.assertEqual(win_timezone.get_offset(), "+0530")
# 'get_zonecode' function tests: 1
def test_get_zonecode(self):
"""
Test if it get current timezone (i.e. PST, MDT, etc)
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
self.assertEqual(win_timezone.get_zonecode(), "IST")
# 'set_zone' function tests: 1
def test_set_zone(self):
"""
Test if it unlinks, then symlinks /etc/localtime to the set timezone.
"""
mock_write = MagicMock(
return_value={"pid": 78, "retcode": 0, "stderr": "", "stdout": ""}
)
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_write}), patch.dict(
win_timezone.__salt__, {"cmd.run_all": mock_read}
):
self.assertTrue(win_timezone.set_zone("Asia/Calcutta"))
# 'zone_compare' function tests: 1
def test_zone_compare(self):
"""
Test if it checks the md5sum between the given timezone, and
the one set in /etc/localtime. Returns True if they match,
and False if not. Mostly useful for running state checks.
"""
mock_read = MagicMock(
return_value={
"pid": 78,
"retcode": 0,
"stderr": "",
"stdout": "India Standard Time",
}
)
with patch.dict(win_timezone.__salt__, {"cmd.run_all": mock_read}):
self.assertTrue(win_timezone.zone_compare("Asia/Calcutta"))
# 'get_hwclock' function tests: 1
def test_get_hwclock(self):
"""
Test if it get current hardware clock setting (UTC or localtime)
"""
self.assertEqual(win_timezone.get_hwclock(), "localtime")
# 'set_hwclock' function tests: 1
def test_set_hwclock(self):
"""
Test if it sets the hardware clock to be either UTC or localtime
"""
self.assertFalse(win_timezone.set_hwclock("UTC"))