Use __utils__, fix unit tests

This commit is contained in:
twangboy 2018-05-23 17:27:58 -06:00
parent 73e033f555
commit 9923176b68
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB
2 changed files with 36 additions and 24 deletions

View file

@ -11,9 +11,6 @@ from datetime import datetime
# Import Salt libs
from salt.exceptions import CommandExecutionError
import salt.utils.path
import salt.utils.platform
import salt.utils.win_reg
log = logging.getLogger(__name__)
@ -186,7 +183,7 @@ def __virtual__():
'''
Only load on windows
'''
if salt.utils.platform.is_windows() and salt.utils.path.which('tzutil'):
if __utils__['platform.is_windows']() and __utils__['path.which']('tzutil'):
return __virtualname__
return (False, "Module win_timezone: tzutil not found or is not on Windows client")
@ -204,7 +201,7 @@ def get_zone():
salt '*' timezone.get_zone
'''
win_zone = salt.utils.win_reg.read_value(
win_zone = __utils__['reg.read_value'](
hive='HKLM',
key='SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation',
vname='TimeZoneKeyName')['vdata']
@ -283,7 +280,11 @@ def set_zone(timezone):
# Set the value
cmd = ['tzutil', '/s', win_zone]
__salt__['cmd.run'](cmd, python_shell=False)
res = __salt__['cmd.run_all'](cmd, python_shell=False)
if res['retcode']:
raise CommandExecutionError('tzutil encountered an error setting '
'timezone: {0}'.format(timezone),
info=res)
return zone_compare(timezone)

View file

@ -2,7 +2,6 @@
'''
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
'''
# Import Python Libs
from __future__ import absolute_import, unicode_literals, print_function
@ -31,12 +30,13 @@ class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it get current timezone (i.e. Asia/Calcutta)
'''
mock_cmd = MagicMock(side_effect=['India Standard Time',
'Indian Standard Time'])
with patch.dict(win_timezone.__salt__, {'cmd.run': mock_cmd}):
mock_read = MagicMock(side_effect=[{'vdata': 'India Standard Time'},
{'vdata': 'Indian Standard Time'}])
with patch.dict(win_timezone.__utils__, {'reg.read_value': mock_read}):
self.assertEqual(win_timezone.get_zone(), 'Asia/Calcutta')
self.assertFalse(win_timezone.get_zone())
self.assertEqual(win_timezone.get_zone(), 'Unknown')
# 'get_offset' function tests: 1
@ -44,15 +44,15 @@ class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it get current numeric timezone offset from UCT (i.e. +0530)
'''
time = ('(UTC+05:30) Chennai, Kolkata, Mumbai, \
New Delhi\nIndia Standard Time')
mock_cmd = MagicMock(side_effect=['India Standard Time', time])
with patch.dict(win_timezone.__salt__, {'cmd.run': mock_cmd}):
self.assertEqual(win_timezone.get_offset(), '+0530')
# time = ('(UTC+05:30) Chennai, Kolkata, Mumbai, \
# New Delhi\nIndia Standard Time')
# mock_cmd = MagicMock(side_effect=['India Standard Time', time])
# with patch.dict(win_timezone.__salt__, {'cmd.run': mock_cmd}):
mock_cmd = MagicMock(return_value='India Standard Time')
with patch.dict(win_timezone.__salt__, {'cmd.run': mock_cmd}):
self.assertFalse(win_timezone.get_offset())
mock_read = MagicMock(return_value={'vdata': 'India Standard Time'})
with patch.dict(win_timezone.__utils__, {'reg.read_value': mock_read}):
self.assertEqual(win_timezone.get_offset(), '+0530')
# 'get_zonecode' function tests: 1
@ -60,7 +60,10 @@ class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it get current timezone (i.e. PST, MDT, etc)
'''
self.assertFalse(win_timezone.get_zonecode())
mock_read = MagicMock(return_value={'vdata': 'India Standard Time'})
with patch.dict(win_timezone.__utils__, {'reg.read_value': mock_read}):
self.assertEqual(win_timezone.get_zonecode(), 'IST')
# 'set_zone' function tests: 1
@ -68,8 +71,15 @@ class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it unlinks, then symlinks /etc/localtime to the set timezone.
'''
mock_cmd = MagicMock(return_value=0)
with patch.dict(win_timezone.__salt__, {'cmd.retcode': mock_cmd}):
mock_cmd = MagicMock(return_value={'pid': 78,
'retcode': 0,
'stderr': '',
'stdout': ''})
mock_read = MagicMock(return_value={'vdata': 'India Standard Time'})
with patch.dict(win_timezone.__salt__, {'cmd.run_all': mock_cmd}), \
patch.dict(win_timezone.__utils__, {'reg.read_value': mock_read}):
self.assertTrue(win_timezone.set_zone('Asia/Calcutta'))
# 'zone_compare' function tests: 1
@ -80,8 +90,9 @@ class WinTimezoneTestCase(TestCase, LoaderModuleMockMixin):
the one set in /etc/localtime. Returns True if they match,
and False if not. Mostly useful for running state checks.
'''
mock_cmd = MagicMock(return_value='India Standard Time')
with patch.dict(win_timezone.__salt__, {'cmd.run': mock_cmd}):
mock_read = MagicMock(return_value={'vdata': 'India Standard Time'})
with patch.dict(win_timezone.__utils__, {'reg.read_value': mock_read}):
self.assertTrue(win_timezone.zone_compare('Asia/Calcutta'))
# 'get_hwclock' function tests: 1