mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #27746 from alprs/fix-timedatectl_failure
timezone module: handle timedatectl errors
This commit is contained in:
commit
a158cd50e6
2 changed files with 21 additions and 6 deletions
|
@ -100,8 +100,13 @@ def get_zone():
|
|||
'''
|
||||
cmd = ''
|
||||
if salt.utils.which('timedatectl'):
|
||||
out = __salt__['cmd.run'](['timedatectl'], python_shell=False)
|
||||
for line in (x.strip() for x in out.splitlines()):
|
||||
ret = __salt__['cmd.run_all'](['timedatectl'], python_shell=False)
|
||||
|
||||
if ret['retcode'] > 0:
|
||||
msg = 'timedatectl failed: {0}'.format(ret['stderr'])
|
||||
raise CommandExecutionError(msg)
|
||||
|
||||
for line in (x.strip() for x in ret['stdout'].splitlines()):
|
||||
try:
|
||||
return re.match(r'Time ?zone:\s+(\S+)', line).group(1)
|
||||
except AttributeError:
|
||||
|
|
|
@ -40,11 +40,21 @@ class TimezoneTestCase(TestCase):
|
|||
'''
|
||||
Test to get current timezone (i.e. America/Denver)
|
||||
'''
|
||||
mock = MagicMock(side_effect=['Time zone: A', 'A'])
|
||||
with patch.object(salt.utils, 'which', return_value=True):
|
||||
with patch.dict(timezone.__salt__, {'cmd.run': mock}):
|
||||
self.assertEqual(timezone.get_zone(), 'A')
|
||||
zone = 'MST'
|
||||
|
||||
with patch.object(salt.utils, 'which', return_value=True):
|
||||
mock_cmd = MagicMock(return_value={'stderr': 'error', 'retcode': 1})
|
||||
with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}):
|
||||
self.assertRaises(CommandExecutionError, timezone.get_zone)
|
||||
|
||||
mock_cmd = MagicMock(return_value={'stdout': 'Timezone: {0}'.format(zone),
|
||||
'retcode': 0})
|
||||
with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}):
|
||||
self.assertEqual(timezone.get_zone(), zone)
|
||||
|
||||
mock_cmd = MagicMock(return_value={'stdout': 'ZoneCTL: {0}'.format(zone),
|
||||
'retcode': 0})
|
||||
with patch.dict(timezone.__salt__, {'cmd.run_all': mock_cmd}):
|
||||
self.assertRaises(CommandExecutionError, timezone.get_zone)
|
||||
|
||||
with patch.object(salt.utils, 'which', return_value=False):
|
||||
|
|
Loading…
Add table
Reference in a new issue