Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix win_dns_client when used with scheduler #56339

Merged
merged 3 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions salt/modules/win_dns_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

try:
import wmi
import salt.utils.winapi
HAS_LIBS = True
except ImportError:
pass
HAS_LIBS = False

log = logging.getLogger(__name__)

Expand All @@ -22,15 +24,25 @@ def __virtual__():
'''
Only works on Windows systems
'''
if salt.utils.platform.is_windows():
return 'win_dns_client'
return (False, "Module win_dns_client: module only works on Windows systems")
if not salt.utils.platform.is_windows():
return False, 'Module win_dns_client: module only works on Windows ' \
'systems'
if not HAS_LIBS:
return False, 'Module win_dns_client: missing required libraries'
return 'win_dns_client'
twangboy marked this conversation as resolved.
Show resolved Hide resolved


def get_dns_servers(interface='Local Area Connection'):
'''
Return a list of the configured DNS servers of the specified interface

Args:
interface (str): The name of the network interface. This is the name as
it appears in the Control Panel under Network Connections

Returns:
list: A list of dns servers

CLI Example:

.. code-block:: bash
Expand Down Expand Up @@ -121,7 +133,14 @@ def dns_dhcp(interface='Local Area Connection'):

def get_dns_config(interface='Local Area Connection'):
'''
Get the type of DNS configuration (dhcp / static)
Get the type of DNS configuration (dhcp / static).

Args:
interface (str): The name of the network interface. This is the
Description in the Network Connection Details for the device

Returns:
bool: ``True`` if DNS is configured, otherwise ``False``

CLI Example:

Expand Down
29 changes: 21 additions & 8 deletions tests/unit/modules/test_win_dns_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
# Import Salt Testing Libs
from tests.support.mixins import LoaderModuleMockMixin
from tests.support.unit import TestCase, skipIf
from tests.support.mock import (
MagicMock,
patch,
Mock,
)
from tests.support.mock import MagicMock, patch, Mock

# Import Salt Libs
import salt.modules.win_dns_client as win_dns_client
Expand Down Expand Up @@ -68,7 +64,6 @@ class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
'''
Test cases for salt.modules.win_dns_client
'''

def setup_loader_modules(self):
# wmi and pythoncom modules are platform specific...
mock_pythoncom = types.ModuleType(
Expand All @@ -89,8 +84,7 @@ 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', Mockwinapi), \
patch('salt.utils.winapi.Com', MagicMock()), \
with patch('salt.utils.winapi.Com', MagicMock()), \
patch.object(self.WMI, 'Win32_NetworkAdapter',
return_value=[Mockwmi()]), \
patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
Expand Down Expand Up @@ -159,3 +153,22 @@ def test_get_dns_config(self):
return_value=[Mockwmi()]), \
patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
self.assertTrue(win_dns_client.get_dns_config())

@patch('salt.utils.platform.is_windows')
def test___virtual__non_windows(self, mock):
mock.return_value = False
result = win_dns_client.__virtual__()
expected = (False, 'Module win_dns_client: module only works on '
'Windows systems')
self.assertEqual(result, expected)

@patch.object(win_dns_client, 'HAS_LIBS', False)
def test___virtual__missing_libs(self):
result = win_dns_client.__virtual__()
expected = (False, 'Module win_dns_client: missing required libraries')
self.assertEqual(result, expected)

def test___virtual__(self):
result = win_dns_client.__virtual__()
expected = 'win_dns_client'
self.assertEqual(result, expected)