mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Merge pull request #44457 from twangboy/win_remove_wmi_monkeypatching
Remove wmi monkeypatching
This commit is contained in:
commit
ebbe5949ea
4 changed files with 51 additions and 50 deletions
|
@ -6,8 +6,7 @@ or for problem solving if your minion is having problems.
|
|||
|
||||
.. versionadded:: 0.12.0
|
||||
|
||||
:depends: - pythoncom
|
||||
- wmi
|
||||
:depends: - wmi
|
||||
'''
|
||||
|
||||
# Import Python Libs
|
||||
|
|
|
@ -21,6 +21,12 @@ from tests.support.mock import (
|
|||
# Import Salt Libs
|
||||
import salt.modules.win_dns_client as win_dns_client
|
||||
|
||||
try:
|
||||
import wmi
|
||||
HAS_WMI = True
|
||||
except ImportError:
|
||||
HAS_WMI = False
|
||||
|
||||
|
||||
class Mockwmi(object):
|
||||
'''
|
||||
|
@ -59,6 +65,7 @@ class Mockwinapi(object):
|
|||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@skipIf(not HAS_WMI, 'WMI only available on Windows')
|
||||
class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
||||
'''
|
||||
Test cases for salt.modules.win_dns_client
|
||||
|
@ -66,16 +73,13 @@ class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
|||
|
||||
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})
|
||||
mock_pythoncom = types.ModuleType('pythoncom')
|
||||
sys_modules_patcher = patch.dict('sys.modules',
|
||||
{'pythoncom': mock_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}}
|
||||
|
||||
# 'get_dns_servers' function tests: 1
|
||||
|
@ -90,7 +94,8 @@ class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
|||
patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
||||
return_value=[Mockwmi()]):
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
|
||||
self.assertListEqual(win_dns_client.get_dns_servers
|
||||
('Local Area Connection'),
|
||||
['10.1.1.10'])
|
||||
|
@ -113,23 +118,22 @@ class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
|||
'''
|
||||
Test if it add the DNS server to the network interface.
|
||||
'''
|
||||
with patch('salt.utils.winapi.Com', MagicMock()):
|
||||
with patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]):
|
||||
with patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
||||
return_value=[Mockwmi()]):
|
||||
self.assertFalse(win_dns_client.add_dns('10.1.1.10',
|
||||
'Ethernet'))
|
||||
with patch('salt.utils.winapi.Com', MagicMock()), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
|
||||
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',
|
||||
'Local Area Connection'))
|
||||
MagicMock(return_value=['10.1.1.10'])), \
|
||||
patch.dict(win_dns_client.__salt__,
|
||||
{'cmd.retcode': MagicMock(return_value=0)}), \
|
||||
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
|
||||
self.assertTrue(win_dns_client.add_dns('10.1.1.0', 'Local Area Connection'))
|
||||
|
||||
# 'dns_dhcp' function tests: 1
|
||||
|
||||
|
@ -148,9 +152,10 @@ class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
|
|||
'''
|
||||
Test if it get the type of DNS configuration (dhcp / static)
|
||||
'''
|
||||
with patch('salt.utils.winapi.Com', MagicMock()):
|
||||
with patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]):
|
||||
with patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
||||
return_value=[Mockwmi()]):
|
||||
self.assertTrue(win_dns_client.get_dns_config())
|
||||
with patch('salt.utils.winapi.Com', MagicMock()), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
|
||||
self.assertTrue(win_dns_client.get_dns_config())
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
# Import Python Libs
|
||||
from __future__ import absolute_import
|
||||
import types
|
||||
|
||||
# Import Salt Testing Libs
|
||||
from tests.support.mixins import LoaderModuleMockMixin
|
||||
|
@ -22,6 +21,12 @@ from tests.support.mock import (
|
|||
import salt.utils
|
||||
import salt.modules.win_network as win_network
|
||||
|
||||
try:
|
||||
import wmi
|
||||
HAS_WMI = True
|
||||
except ImportError:
|
||||
HAS_WMI = False
|
||||
|
||||
|
||||
class Mockwmi(object):
|
||||
'''
|
||||
|
@ -64,12 +69,9 @@ class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
|
|||
Test cases for salt.modules.win_network
|
||||
'''
|
||||
def setup_loader_modules(self):
|
||||
# wmi modules are platform specific...
|
||||
wmi = types.ModuleType('wmi')
|
||||
self.WMI = Mock()
|
||||
self.addCleanup(delattr, self, 'WMI')
|
||||
wmi.WMI = Mock(return_value=self.WMI)
|
||||
return {win_network: {'wmi': wmi}}
|
||||
return {win_network: {}}
|
||||
|
||||
# 'ping' function tests: 1
|
||||
|
||||
|
@ -156,6 +158,7 @@ class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
|
|||
|
||||
# 'interfaces_names' function tests: 1
|
||||
|
||||
@skipIf(not HAS_WMI, "WMI only available on Windows")
|
||||
def test_interfaces_names(self):
|
||||
'''
|
||||
Test if it return a list of all the interfaces names
|
||||
|
@ -164,7 +167,8 @@ class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
|
|||
with patch('salt.utils.winapi.Com', MagicMock()), \
|
||||
patch.object(self.WMI, 'Win32_NetworkAdapter',
|
||||
return_value=[Mockwmi()]), \
|
||||
patch('salt.utils', Mockwinapi):
|
||||
patch('salt.utils', Mockwinapi), \
|
||||
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
|
||||
self.assertListEqual(win_network.interfaces_names(),
|
||||
['Ethernet'])
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
# Import python libs
|
||||
from __future__ import absolute_import
|
||||
import sys
|
||||
import types
|
||||
|
||||
# Import Salt libs
|
||||
import salt.ext.six as six
|
||||
|
@ -12,25 +11,16 @@ import salt.ext.six as six
|
|||
from tests.support.unit import skipIf, TestCase
|
||||
from tests.support.mock import NO_MOCK, NO_MOCK_REASON, Mock, patch, ANY
|
||||
|
||||
# wmi and pythoncom modules are platform specific...
|
||||
wmi = types.ModuleType('wmi')
|
||||
sys.modules['wmi'] = wmi
|
||||
|
||||
pythoncom = types.ModuleType('pythoncom')
|
||||
sys.modules['pythoncom'] = pythoncom
|
||||
|
||||
if NO_MOCK is False:
|
||||
WMI = Mock()
|
||||
wmi.WMI = Mock(return_value=WMI)
|
||||
pythoncom.CoInitialize = Mock()
|
||||
pythoncom.CoUninitialize = Mock()
|
||||
try:
|
||||
import wmi
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# This is imported late so mock can do its job
|
||||
import salt.modules.win_status as status
|
||||
|
||||
|
||||
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
||||
@skipIf(sys.stdin.encoding != 'UTF-8', 'UTF-8 encoding required for this test is not supported')
|
||||
@skipIf(status.HAS_WMI is False, 'This test requires Windows')
|
||||
class TestProcsBase(TestCase):
|
||||
def __init__(self, *args, **kwargs):
|
||||
|
@ -55,8 +45,10 @@ class TestProcsBase(TestCase):
|
|||
self.__processes.append(process)
|
||||
|
||||
def call_procs(self):
|
||||
WMI = Mock()
|
||||
WMI.win32_process = Mock(return_value=self.__processes)
|
||||
self.result = status.procs()
|
||||
with patch.object(wmi, 'WMI', Mock(return_value=WMI)):
|
||||
self.result = status.procs()
|
||||
|
||||
|
||||
class TestProcsCount(TestProcsBase):
|
||||
|
@ -101,6 +93,7 @@ class TestProcsAttributes(TestProcsBase):
|
|||
self.assertEqual(self.proc['user_domain'], self._expected_domain)
|
||||
|
||||
|
||||
@skipIf(sys.stdin.encoding != 'UTF-8', 'UTF-8 encoding required for this test is not supported')
|
||||
class TestProcsUnicodeAttributes(TestProcsBase):
|
||||
def setUp(self):
|
||||
unicode_str = u'\xc1'
|
||||
|
|
Loading…
Add table
Reference in a new issue