adding win_dns_client test case and used @patch for mocking.

This commit is contained in:
Jayesh Kariya 2015-04-15 18:42:38 +05:30
parent 34526d536d
commit f56c24f909

View File

@ -5,12 +5,15 @@
# Import Python Libs
from __future__ import absolute_import
import new
import sys
# Import Salt Testing Libs
from salttesting import TestCase, skipIf
from salttesting.mock import (
MagicMock,
patch,
Mock,
NO_MOCK,
NO_MOCK_REASON
)
@ -19,6 +22,19 @@ from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# wmi and pythoncom modules are platform specific...
wmi = new.module('wmi')
sys.modules['wmi'] = wmi
pythoncom = new.module('pythoncom')
sys.modules['pythoncom'] = pythoncom
if NO_MOCK is False:
WMI = Mock()
wmi.WMI = Mock(return_value=WMI)
pythoncom.CoInitialize = Mock()
pythoncom.CoUninitialize = Mock()
# Import Salt Libs
from salt.modules import win_dns_client
@ -38,33 +54,29 @@ class Mockwmi(object):
DHCPEnabled = True
def __init__(self):
self.netenabled = None
self.index = None
self.ipenabled = None
pass
@staticmethod
def WMI():
'''
Mock WMI method
'''
return Mockwmi()
def Win32_NetworkAdapter(self, NetEnabled=True):
'''
Mock Win32_NetworkAdapter method
'''
self.netenabled = NetEnabled
return [Mockwmi()]
class Mockwinapi(object):
'''
Mock winapi class
'''
def __init__(self):
pass
def Win32_NetworkAdapterConfiguration(self, Index=0, IPEnabled=1):
class winapi(object):
'''
Mock Win32_NetworkAdapterConfiguration method
Mock winapi class
'''
self.index = Index
self.ipenabled = IPEnabled
return [Mockwmi()]
def __init__(self):
pass
win_dns_client.wmi = Mockwmi
@staticmethod
def Com():
'''
Mock Com method
'''
return True
@skipIf(NO_MOCK, NO_MOCK_REASON)
@ -74,16 +86,22 @@ class WinDnsClientTestCase(TestCase):
'''
# 'get_dns_servers' function tests: 1
@patch('salt.utils', Mockwinapi)
def test_get_dns_servers(self):
'''
Test if it return a list of the configured DNS servers
of the specified interface.
'''
with patch('salt.utils.winapi.Com', MagicMock()):
self.assertListEqual(win_dns_client.get_dns_servers
('Local Area Connection'), ['10.1.1.10'])
with patch.object(WMI, 'Win32_NetworkAdapter',
return_value=[Mockwmi()]):
with patch.object(WMI, 'Win32_NetworkAdapterConfiguration',
return_value=[Mockwmi()]):
self.assertListEqual(win_dns_client.get_dns_servers
('Local Area Connection'),
['10.1.1.10'])
self.assertFalse(win_dns_client.get_dns_servers('Ethernet'))
self.assertFalse(win_dns_client.get_dns_servers('Ethernet'))
# 'rm_dns' function tests: 1
@ -102,11 +120,18 @@ class WinDnsClientTestCase(TestCase):
Test if it add the DNS server to the network interface.
'''
with patch('salt.utils.winapi.Com', MagicMock()):
self.assertFalse(win_dns_client.add_dns('10.1.1.10', 'Ethernet'))
with patch.object(WMI, 'Win32_NetworkAdapter',
return_value=[Mockwmi()]):
with patch.object(WMI, 'Win32_NetworkAdapterConfiguration',
return_value=[Mockwmi()]):
self.assertFalse(win_dns_client.add_dns('10.1.1.10',
'Ethernet'))
self.assertTrue(win_dns_client.add_dns('10.1.1.10',
'Local Area Connection'))
self.assertTrue(win_dns_client.add_dns
('10.1.1.10', 'Local Area Connection'))
with patch.object(win_dns_client, 'get_dns_servers',
MagicMock(return_value=['10.1.1.10'])):
with patch.dict(win_dns_client.__salt__,
{'cmd.retcode': MagicMock(return_value=0)}):
self.assertTrue(win_dns_client.add_dns('10.1.1.0',
@ -130,7 +155,11 @@ class WinDnsClientTestCase(TestCase):
Test if it get the type of DNS configuration (dhcp / static)
'''
with patch('salt.utils.winapi.Com', MagicMock()):
self.assertTrue(win_dns_client.get_dns_config())
with patch.object(WMI, 'Win32_NetworkAdapter',
return_value=[Mockwmi()]):
with patch.object(WMI, 'Win32_NetworkAdapterConfiguration',
return_value=[Mockwmi()]):
self.assertTrue(win_dns_client.get_dns_config())
if __name__ == '__main__':