2015-04-13 12:36:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
|
|
|
:codeauthor: :email:`Jayesh Kariya <jayeshk@saltstack.com>`
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Import Python Libs
|
|
|
|
from __future__ import absolute_import
|
2015-06-09 20:06:38 +00:00
|
|
|
import types
|
2015-04-13 12:36:45 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-03-22 16:42:17 +00:00
|
|
|
from tests.support.mixins import LoaderModuleMockMixin
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import TestCase, skipIf
|
|
|
|
from tests.support.mock import (
|
2015-04-13 12:36:45 +00:00
|
|
|
MagicMock,
|
|
|
|
patch,
|
2015-04-15 13:12:38 +00:00
|
|
|
Mock,
|
2015-04-13 12:36:45 +00:00
|
|
|
NO_MOCK,
|
|
|
|
NO_MOCK_REASON
|
|
|
|
)
|
|
|
|
|
|
|
|
# Import Salt Libs
|
2017-03-21 17:15:36 +00:00
|
|
|
import salt.modules.win_dns_client as win_dns_client
|
2015-04-13 12:36:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Mockwmi(object):
|
|
|
|
'''
|
|
|
|
Mock wmi class
|
|
|
|
'''
|
|
|
|
NetConnectionID = 'Local Area Connection'
|
|
|
|
Index = 0
|
|
|
|
DNSServerSearchOrder = ['10.1.1.10']
|
|
|
|
Description = 'Local Area Connection'
|
|
|
|
DHCPEnabled = True
|
|
|
|
|
|
|
|
def __init__(self):
|
2015-04-15 13:12:38 +00:00
|
|
|
pass
|
2015-04-13 12:36:45 +00:00
|
|
|
|
|
|
|
|
2015-04-15 13:12:38 +00:00
|
|
|
class Mockwinapi(object):
|
|
|
|
'''
|
|
|
|
Mock winapi class
|
|
|
|
'''
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2015-04-13 12:36:45 +00:00
|
|
|
|
2015-04-15 13:12:38 +00:00
|
|
|
class winapi(object):
|
2015-04-13 12:36:45 +00:00
|
|
|
'''
|
2015-04-15 13:12:38 +00:00
|
|
|
Mock winapi class
|
2015-04-13 12:36:45 +00:00
|
|
|
'''
|
2015-04-15 13:12:38 +00:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
2015-04-13 12:36:45 +00:00
|
|
|
|
2015-04-15 13:12:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def Com():
|
|
|
|
'''
|
|
|
|
Mock Com method
|
|
|
|
'''
|
|
|
|
return True
|
2015-04-13 12:36:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2017-03-22 16:42:17 +00:00
|
|
|
class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
2015-04-13 12:36:45 +00:00
|
|
|
'''
|
|
|
|
Test cases for salt.modules.win_dns_client
|
|
|
|
'''
|
2017-03-22 16:42:17 +00:00
|
|
|
|
|
|
|
def setup_loader_modules(self):
|
|
|
|
# wmi and pythoncom modules are platform specific...
|
|
|
|
wmi = types.ModuleType('wmi')
|
|
|
|
pythoncom = types.ModuleType('pythoncom')
|
|
|
|
sys_modules_patcher = patch.dict('sys.modules', {'wmi': wmi, 'pythoncom': pythoncom})
|
|
|
|
sys_modules_patcher.start()
|
|
|
|
self.addCleanup(sys_modules_patcher.stop)
|
|
|
|
self.WMI = Mock()
|
|
|
|
self.addCleanup(delattr, self, 'WMI')
|
|
|
|
wmi.WMI = Mock(return_value=self.WMI)
|
|
|
|
pythoncom.CoInitialize = Mock()
|
|
|
|
pythoncom.CoUninitialize = Mock()
|
|
|
|
return {win_dns_client: {'wmi': wmi}}
|
|
|
|
|
2015-04-13 12:36:45 +00:00
|
|
|
# 'get_dns_servers' function tests: 1
|
|
|
|
|
2015-04-15 13:12:38 +00:00
|
|
|
@patch('salt.utils', Mockwinapi)
|
2015-04-13 12:36:45 +00:00
|
|
|
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()):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapter',
|
2015-04-15 13:12:38 +00:00
|
|
|
return_value=[Mockwmi()]):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
2015-04-15 13:12:38 +00:00
|
|
|
return_value=[Mockwmi()]):
|
|
|
|
self.assertListEqual(win_dns_client.get_dns_servers
|
|
|
|
('Local Area Connection'),
|
|
|
|
['10.1.1.10'])
|
2015-04-13 12:36:45 +00:00
|
|
|
|
2015-04-15 13:12:38 +00:00
|
|
|
self.assertFalse(win_dns_client.get_dns_servers('Ethernet'))
|
2015-04-13 12:36:45 +00:00
|
|
|
|
|
|
|
# 'rm_dns' function tests: 1
|
|
|
|
|
|
|
|
def test_rm_dns(self):
|
|
|
|
'''
|
|
|
|
Test if it remove the DNS server from the network interface.
|
|
|
|
'''
|
|
|
|
with patch.dict(win_dns_client.__salt__,
|
|
|
|
{'cmd.retcode': MagicMock(return_value=0)}):
|
|
|
|
self.assertTrue(win_dns_client.rm_dns('10.1.1.10'))
|
|
|
|
|
|
|
|
# 'add_dns' function tests: 1
|
|
|
|
|
|
|
|
def test_add_dns(self):
|
|
|
|
'''
|
|
|
|
Test if it add the DNS server to the network interface.
|
|
|
|
'''
|
|
|
|
with patch('salt.utils.winapi.Com', MagicMock()):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapter',
|
2015-04-15 13:12:38 +00:00
|
|
|
return_value=[Mockwmi()]):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
2015-04-15 13:12:38 +00:00
|
|
|
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'))
|
|
|
|
|
|
|
|
with patch.object(win_dns_client, 'get_dns_servers',
|
|
|
|
MagicMock(return_value=['10.1.1.10'])):
|
2015-04-13 12:36:45 +00:00
|
|
|
with patch.dict(win_dns_client.__salt__,
|
|
|
|
{'cmd.retcode': MagicMock(return_value=0)}):
|
|
|
|
self.assertTrue(win_dns_client.add_dns('10.1.1.0',
|
|
|
|
'Local Area Connection'))
|
|
|
|
|
|
|
|
# 'dns_dhcp' function tests: 1
|
|
|
|
|
|
|
|
def test_dns_dhcp(self):
|
|
|
|
'''
|
|
|
|
Test if it configure the interface to get its
|
|
|
|
DNS servers from the DHCP server
|
|
|
|
'''
|
|
|
|
with patch.dict(win_dns_client.__salt__,
|
|
|
|
{'cmd.retcode': MagicMock(return_value=0)}):
|
|
|
|
self.assertTrue(win_dns_client.dns_dhcp())
|
|
|
|
|
|
|
|
# 'get_dns_config' function tests: 1
|
|
|
|
|
|
|
|
def test_get_dns_config(self):
|
|
|
|
'''
|
|
|
|
Test if it get the type of DNS configuration (dhcp / static)
|
|
|
|
'''
|
|
|
|
with patch('salt.utils.winapi.Com', MagicMock()):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapter',
|
2015-04-15 13:12:38 +00:00
|
|
|
return_value=[Mockwmi()]):
|
2017-03-22 16:42:17 +00:00
|
|
|
with patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
2015-04-15 13:12:38 +00:00
|
|
|
return_value=[Mockwmi()]):
|
|
|
|
self.assertTrue(win_dns_client.get_dns_config())
|