mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
Merge pull request #28550 from jfindlay/ctl_err
check timedatectl errno and return stdout on failure
This commit is contained in:
commit
bd0b291b63
@ -27,6 +27,19 @@ def __virtual__():
|
||||
return True
|
||||
|
||||
|
||||
def _timedatectl():
|
||||
'''
|
||||
get the output of timedatectl
|
||||
'''
|
||||
ret = __salt__['cmd.run_all'](['timedatectl'], python_shell=False)
|
||||
|
||||
if ret['retcode'] != 0:
|
||||
msg = 'timedatectl failed: {0}'.format(ret['stderr'])
|
||||
raise CommandExecutionError(msg)
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
def _get_zone_solaris():
|
||||
tzfile = '/etc/TIMEZONE'
|
||||
with salt.utils.fopen(tzfile, 'r') as fp_:
|
||||
@ -100,20 +113,18 @@ def get_zone():
|
||||
'''
|
||||
cmd = ''
|
||||
if salt.utils.which('timedatectl'):
|
||||
ret = __salt__['cmd.run_all'](['timedatectl'], python_shell=False)
|
||||
|
||||
if ret['retcode'] > 0:
|
||||
msg = 'timedatectl failed: {0}'.format(ret['stderr'])
|
||||
raise CommandExecutionError(msg)
|
||||
ret = _timedatectl()
|
||||
|
||||
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:
|
||||
pass
|
||||
raise CommandExecutionError(
|
||||
'Failed to parse timedatectl output, this is likely a bug'
|
||||
)
|
||||
|
||||
msg = ('Failed to parse timedatectl output: {0}\n'
|
||||
'Please file an issue with SaltStack').format(ret['stdout'])
|
||||
raise CommandExecutionError(msg)
|
||||
|
||||
else:
|
||||
if __grains__['os'].lower() == 'centos':
|
||||
return _get_zone_etc_localtime()
|
||||
@ -264,8 +275,8 @@ def get_hwclock():
|
||||
'''
|
||||
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 = _timedatectl()
|
||||
for line in (x.strip() for x in ret['stdout'].splitlines()):
|
||||
if 'rtc in local tz' in line.lower():
|
||||
try:
|
||||
if line.split(':')[-1].strip().lower() == 'yes':
|
||||
@ -274,9 +285,11 @@ def get_hwclock():
|
||||
return 'UTC'
|
||||
except IndexError:
|
||||
pass
|
||||
raise CommandExecutionError(
|
||||
'Failed to parse timedatectl output, this is likely a bug'
|
||||
)
|
||||
|
||||
msg = ('Failed to parse timedatectl output: {0}\n'
|
||||
'Please file an issue with SaltStack').format(ret['stdout'])
|
||||
raise CommandExecutionError(msg)
|
||||
|
||||
else:
|
||||
os_family = __grains__['os_family']
|
||||
for family in ('RedHat', 'Suse'):
|
||||
|
@ -107,8 +107,8 @@ class TimezoneTestCase(TestCase):
|
||||
'''
|
||||
def zone_checking_and_unlinking():
|
||||
ret = ('Zone does not exist: /usr/share/lib/zoneinfo/timezone')
|
||||
mock = MagicMock(side_effect=[False, True, True])
|
||||
with patch.object(os.path, 'exists', mock):
|
||||
mock_exists = MagicMock(side_effect=[False, True, True])
|
||||
with patch.object(os.path, 'exists', mock_exists):
|
||||
self.assertEqual(timezone.set_zone('timezone'), ret)
|
||||
|
||||
with patch.object(os, 'unlink', return_value=None):
|
||||
@ -164,20 +164,20 @@ class TimezoneTestCase(TestCase):
|
||||
'''
|
||||
Test to get current hardware clock setting (UTC or localtime)
|
||||
'''
|
||||
mock_t = MagicMock(return_value=True)
|
||||
mock_f = MagicMock(return_value=False)
|
||||
|
||||
with patch.object(salt.utils, 'which', return_value=True):
|
||||
with patch.dict(timezone.__salt__,
|
||||
{'cmd.run':
|
||||
MagicMock(return_value='rtc in local tz:yes\n')}):
|
||||
with patch.object(timezone, '_timedatectl',
|
||||
MagicMock(return_value={'stdout': 'rtc in local tz:yes\n'})):
|
||||
self.assertEqual(timezone.get_hwclock(), 'localtime')
|
||||
|
||||
with patch.dict(timezone.__salt__,
|
||||
{'cmd.run':
|
||||
MagicMock(return_value='rtc in local tz:No\n')}):
|
||||
with patch.object(timezone, '_timedatectl',
|
||||
MagicMock(return_value={'stdout': 'rtc in local tz:No\n'})):
|
||||
self.assertEqual(timezone.get_hwclock(), 'UTC')
|
||||
|
||||
with patch.dict(timezone.__salt__,
|
||||
{'cmd.run':
|
||||
MagicMock(return_value='rtc')}):
|
||||
with patch.object(timezone, '_timedatectl',
|
||||
MagicMock(return_value={'stdout': 'rtc'})):
|
||||
self.assertRaises(CommandExecutionError, timezone.get_hwclock)
|
||||
|
||||
with patch.object(salt.utils, 'which', return_value=False):
|
||||
@ -213,8 +213,7 @@ class TimezoneTestCase(TestCase):
|
||||
mfile.return_value.__iter__.return_value = [fl_data]
|
||||
self.assertEqual(timezone.get_hwclock(), 'UTC')
|
||||
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.object(os.path, 'isfile', mock):
|
||||
with patch.object(os.path, 'isfile', mock_t):
|
||||
fl_data = 'zone_info=GMT'
|
||||
with patch('salt.utils.fopen',
|
||||
mock_open(read_data=fl_data),
|
||||
@ -225,8 +224,7 @@ class TimezoneTestCase(TestCase):
|
||||
{'os_family': 'Solaris'}):
|
||||
self.assertEqual(timezone.get_hwclock(), 'UTC')
|
||||
|
||||
mock = MagicMock(return_value=True)
|
||||
with patch.object(os.path, 'isfile', mock):
|
||||
with patch.object(os.path, 'isfile', mock_t):
|
||||
fl_data = 'A=GMT'
|
||||
with patch('salt.utils.fopen',
|
||||
mock_open(read_data=fl_data),
|
||||
@ -239,8 +237,7 @@ class TimezoneTestCase(TestCase):
|
||||
|
||||
with patch.object(salt.utils, 'which', return_value=False):
|
||||
with patch.dict(timezone.__grains__, {'os_family': 'Solaris'}):
|
||||
mock = MagicMock(return_value=False)
|
||||
with patch.object(os.path, 'isfile', mock):
|
||||
with patch.object(os.path, 'isfile', mock_f):
|
||||
self.assertEqual(timezone.get_hwclock(), 'UTC')
|
||||
|
||||
def test_set_hwclock(self):
|
||||
@ -248,8 +245,8 @@ class TimezoneTestCase(TestCase):
|
||||
Test to sets the hardware clock to be either UTC or localtime
|
||||
'''
|
||||
zone = 'America/Denver'
|
||||
with patch.object(timezone, 'get_zone', return_value=zone):
|
||||
|
||||
with patch.object(timezone, 'get_zone', return_value=zone):
|
||||
with patch.dict(timezone.__grains__, {'os_family': 'Solaris',
|
||||
'cpuarch': 'sparc'}):
|
||||
self.assertRaises(
|
||||
|
Loading…
Reference in New Issue
Block a user