test_win_dns_client.py 5.6 KB

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