Add/fix tests

Use named parameters where possible
This commit is contained in:
twangboy 2018-09-25 16:48:16 -06:00
parent 5005a4df2f
commit af1ed1c4e3
No known key found for this signature in database
GPG Key ID: 93FF3BDEB278C9EB
2 changed files with 90 additions and 19 deletions

View File

@ -178,7 +178,9 @@ def enable(iface):
'''
if is_enabled(iface):
return True
cmd = ['netsh', 'interface', 'set', 'interface', iface, 'admin=ENABLED']
cmd = ['netsh', 'interface', 'set', 'interface',
'name={0}'.format(iface),
'admin=ENABLED']
__salt__['cmd.run'](cmd, python_shell=False)
return is_enabled(iface)
@ -195,7 +197,9 @@ def disable(iface):
'''
if is_disabled(iface):
return True
cmd = ['netsh', 'interface', 'set', 'interface', iface, 'admin=DISABLED']
cmd = ['netsh', 'interface', 'set', 'interface',
'name={0}'.format(iface),
'admin=DISABLED']
__salt__['cmd.run'](cmd, python_shell=False)
return is_disabled(iface)
@ -343,7 +347,7 @@ def set_static_dns(iface, *addrs):
# Clear the list of DNS servers if [] is passed
if str(addrs[0]).lower() == '[]':
log.debug('Clearing list of DNS servers')
cmd = ['netsh', 'int', 'ip', 'set', 'dns',
cmd = ['netsh', 'interface', 'ip', 'set', 'dns',
'name={0}'.format(iface),
'source=static',
'address=none']
@ -352,14 +356,17 @@ def set_static_dns(iface, *addrs):
addr_index = 1
for addr in addrs:
if addr_index == 1:
cmd = ['netsh', 'int', 'ip', 'set', 'dns',
iface, 'static', addrs[0], 'primary']
cmd = ['netsh', 'interface', 'ip', 'set', 'dns',
'name={0}'.format(iface),
'source=static',
'address={0}'.format(addr),
'register=primary']
__salt__['cmd.run'](cmd, python_shell=False)
addr_index = addr_index + 1
else:
cmd = ['netsh', 'interface', 'ip', 'add', 'dns',
'name={0}'.format(iface),
'addr={0}'.format(addr),
'address={0}'.format(addr),
'index={0}'.format(addr_index)]
__salt__['cmd.run'](cmd, python_shell=False)
addr_index = addr_index + 1

View File

@ -13,7 +13,8 @@ from tests.support.mock import (
MagicMock,
patch,
NO_MOCK,
NO_MOCK_REASON
NO_MOCK_REASON,
call
)
# Import Salt Libs
@ -124,13 +125,21 @@ class WinShadowTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it enable an interface.
'''
mock_cmd = MagicMock(return_value=ETHERNET_ENABLE)
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
# Test with enabled interface
with patch.object(win_ip, 'is_enabled', return_value=True):
self.assertTrue(win_ip.enable('Ethernet'))
mock_cmd = MagicMock(return_value='Connect state: Disconnected')
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertFalse(win_ip.enable('Ethernet'))
mock_cmd = MagicMock()
with patch.object(win_ip, 'is_enabled', side_effect=[False, True]), \
patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertTrue(win_ip.enable('Ethernet'))
mock_cmd.called_once_with(
['netsh', 'interface', 'set', 'interface',
'name=Ethernet',
'admin=ENABLED'],
python_shell=False
)
# 'disable' function tests: 1
@ -138,14 +147,21 @@ class WinShadowTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it disable an interface.
'''
mock_cmd = MagicMock(return_value=ETHERNET_ENABLE)
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertFalse(win_ip.disable('Ethernet'))
mock_cmd = MagicMock(return_value='Connect state: Disconnected')
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
with patch.object(win_ip, 'is_disabled', return_value=True):
self.assertTrue(win_ip.disable('Ethernet'))
mock_cmd = MagicMock()
with patch.object(win_ip, 'is_disabled', side_effect=[False, True]),\
patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertTrue(win_ip.disable('Ethernet'))
mock_cmd.called_once_with(
['netsh', 'interface', 'set', 'interface',
'name=Ethernet',
'admin=DISABLED'],
python_shell=False
)
# 'get_subnet_length' function tests: 1
def test_get_subnet_length(self):
@ -203,7 +219,7 @@ class WinShadowTestCase(TestCase, LoaderModuleMockMixin):
'''
Test if it set static DNS configuration on a Windows NIC.
'''
mock_cmd = MagicMock(return_value=ETHERNET_CONFIG)
mock_cmd = MagicMock()
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertDictEqual(win_ip.set_static_dns('Ethernet',
'192.168.1.252',
@ -211,6 +227,54 @@ class WinShadowTestCase(TestCase, LoaderModuleMockMixin):
{'DNS Server': ('192.168.1.252',
'192.168.1.253'),
'Interface': 'Ethernet'})
mock_cmd.assert_has_calls([
call(['netsh', 'interface', 'ip', 'set', 'dns',
'name=Ethernet',
'source=static',
'address=192.168.1.252',
'register=primary'],
python_shell=False),
call(['netsh', 'interface', 'ip', 'add', 'dns',
'name=Ethernet',
'address=192.168.1.253',
'index=2'],
python_shell=False)]
)
def test_set_static_dns_clear(self):
'''
Test if it set static DNS configuration on a Windows NIC.
'''
mock_cmd = MagicMock()
with patch.dict(win_ip.__salt__, {'cmd.run': mock_cmd}):
self.assertDictEqual(win_ip.set_static_dns('Ethernet', []),
{'DNS Server': [],
'Interface': 'Ethernet'})
mock_cmd.assert_called_once_with(
['netsh', 'interface', 'ip', 'set', 'dns',
'name=Ethernet',
'source=static',
'address=none'],
python_shell=False
)
def test_set_static_dns_no_action(self):
'''
Test if it set static DNS configuration on a Windows NIC.
'''
# Test passing nothing
self.assertDictEqual(win_ip.set_static_dns('Ethernet'),
{'DNS Server': 'No Changes',
'Interface': 'Ethernet'})
# Test passing None
self.assertDictEqual(win_ip.set_static_dns('Ethernet', None),
{'DNS Server': 'No Changes',
'Interface': 'Ethernet'})
# Test passing string None
self.assertDictEqual(win_ip.set_static_dns('Ethernet', 'None'),
{'DNS Server': 'No Changes',
'Interface': 'Ethernet'})
# 'set_dhcp_dns' function tests: 1