test_win_dns_client.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import types
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase, skipIf
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch,
  14. Mock,
  15. )
  16. # Import Salt Libs
  17. import salt.modules.win_dns_client as win_dns_client
  18. import salt.utils.stringutils
  19. try:
  20. import wmi
  21. HAS_WMI = True
  22. except ImportError:
  23. HAS_WMI = False
  24. class Mockwmi(object):
  25. '''
  26. Mock wmi class
  27. '''
  28. NetConnectionID = 'Local Area Connection'
  29. Index = 0
  30. DNSServerSearchOrder = ['10.1.1.10']
  31. Description = 'Local Area Connection'
  32. DHCPEnabled = True
  33. def __init__(self):
  34. pass
  35. class Mockwinapi(object):
  36. '''
  37. Mock winapi class
  38. '''
  39. def __init__(self):
  40. pass
  41. class winapi(object):
  42. '''
  43. Mock winapi class
  44. '''
  45. def __init__(self):
  46. pass
  47. @staticmethod
  48. def Com():
  49. '''
  50. Mock Com method
  51. '''
  52. return True
  53. @skipIf(not HAS_WMI, 'WMI only available on Windows')
  54. class WinDnsClientTestCase(TestCase, LoaderModuleMockMixin):
  55. '''
  56. Test cases for salt.modules.win_dns_client
  57. '''
  58. def setup_loader_modules(self):
  59. # wmi and pythoncom modules are platform specific...
  60. mock_pythoncom = types.ModuleType(
  61. salt.utils.stringutils.to_str('pythoncom')
  62. )
  63. sys_modules_patcher = patch.dict('sys.modules',
  64. {'pythoncom': mock_pythoncom})
  65. sys_modules_patcher.start()
  66. self.addCleanup(sys_modules_patcher.stop)
  67. self.WMI = Mock()
  68. self.addCleanup(delattr, self, 'WMI')
  69. return {win_dns_client: {'wmi': wmi}}
  70. # 'get_dns_servers' function tests: 1
  71. def test_get_dns_servers(self):
  72. '''
  73. Test if it return a list of the configured DNS servers
  74. of the specified interface.
  75. '''
  76. with patch('salt.utils', Mockwinapi), \
  77. patch('salt.utils.winapi.Com', MagicMock()), \
  78. patch.object(self.WMI, 'Win32_NetworkAdapter',
  79. return_value=[Mockwmi()]), \
  80. patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
  81. return_value=[Mockwmi()]), \
  82. patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
  83. self.assertListEqual(win_dns_client.get_dns_servers
  84. ('Local Area Connection'),
  85. ['10.1.1.10'])
  86. self.assertFalse(win_dns_client.get_dns_servers('Ethernet'))
  87. # 'rm_dns' function tests: 1
  88. def test_rm_dns(self):
  89. '''
  90. Test if it remove the DNS server from the network interface.
  91. '''
  92. with patch.dict(win_dns_client.__salt__,
  93. {'cmd.retcode': MagicMock(return_value=0)}):
  94. self.assertTrue(win_dns_client.rm_dns('10.1.1.10'))
  95. # 'add_dns' function tests: 1
  96. def test_add_dns(self):
  97. '''
  98. Test if it add the DNS server to the network interface.
  99. '''
  100. with patch('salt.utils.winapi.Com', MagicMock()), \
  101. patch.object(self.WMI, 'Win32_NetworkAdapter',
  102. return_value=[Mockwmi()]), \
  103. patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
  104. return_value=[Mockwmi()]), \
  105. patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
  106. self.assertFalse(win_dns_client.add_dns('10.1.1.10', 'Ethernet'))
  107. self.assertTrue(win_dns_client.add_dns('10.1.1.10', 'Local Area Connection'))
  108. with patch.object(win_dns_client, 'get_dns_servers',
  109. MagicMock(return_value=['10.1.1.10'])), \
  110. patch.dict(win_dns_client.__salt__,
  111. {'cmd.retcode': MagicMock(return_value=0)}), \
  112. patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
  113. self.assertTrue(win_dns_client.add_dns('10.1.1.0', 'Local Area Connection'))
  114. # 'dns_dhcp' function tests: 1
  115. def test_dns_dhcp(self):
  116. '''
  117. Test if it configure the interface to get its
  118. DNS servers from the DHCP server
  119. '''
  120. with patch.dict(win_dns_client.__salt__,
  121. {'cmd.retcode': MagicMock(return_value=0)}):
  122. self.assertTrue(win_dns_client.dns_dhcp())
  123. # 'get_dns_config' function tests: 1
  124. def test_get_dns_config(self):
  125. '''
  126. Test if it get the type of DNS configuration (dhcp / static)
  127. '''
  128. with patch('salt.utils.winapi.Com', MagicMock()), \
  129. patch.object(self.WMI, 'Win32_NetworkAdapter',
  130. return_value=[Mockwmi()]), \
  131. patch.object(self.WMI, 'Win32_NetworkAdapterConfiguration',
  132. return_value=[Mockwmi()]), \
  133. patch.object(wmi, 'WMI', Mock(return_value=self.WMI)):
  134. self.assertTrue(win_dns_client.get_dns_config())