test_win_network.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.win_network as win_network
  16. class WinNetworkTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the nftables state
  19. '''
  20. def setup_loader_modules(self):
  21. return {win_network: {}}
  22. def test_managed_missing_parameters(self):
  23. '''
  24. Test to ensure that the named interface is configured properly.
  25. '''
  26. ret = {'name': 'salt',
  27. 'changes': {},
  28. 'result': False,
  29. 'comment': 'dns_proto must be one of the following: static, dhcp\n'
  30. 'ip_proto must be one of the following: static, dhcp'}
  31. self.assertDictEqual(win_network.managed('salt'), ret)
  32. def test_managed_static_enabled_false(self):
  33. ret = {'name': 'salt',
  34. 'changes': {},
  35. 'result': True,
  36. 'comment': 'Interface \'salt\' is up to date (already disabled)'}
  37. mock_false = MagicMock(return_value=False)
  38. with patch.dict(win_network.__salt__, {"ip.is_enabled": mock_false}):
  39. self.assertDictEqual(
  40. win_network.managed(
  41. 'salt', dns_proto='static', ip_proto='static', enabled=False),
  42. ret)
  43. def test_managed_test_true(self):
  44. ret = {'name': 'salt',
  45. 'changes': {},
  46. 'result': False,
  47. 'comment': 'Failed to enable interface \'salt\' to make changes'}
  48. mock_false = MagicMock(return_value=False)
  49. with patch.dict(win_network.__salt__, {"ip.is_enabled": mock_false,
  50. "ip.enable": mock_false}), \
  51. patch.dict(win_network.__opts__, {"test": False}):
  52. self.assertDictEqual(
  53. win_network.managed(
  54. 'salt', dns_proto='static', ip_proto='static'),
  55. ret)
  56. def test_managed_validate_errors(self):
  57. ret = {'name': 'salt',
  58. 'changes': {},
  59. 'result': False,
  60. 'comment': 'The following SLS configuration errors were '
  61. 'detected:\n'
  62. '- First Error\n'
  63. '- Second Error'}
  64. mock_true = MagicMock(return_value=True)
  65. mock_validate = MagicMock(return_value=['First Error', 'Second Error'])
  66. with patch.dict(win_network.__salt__, {"ip.is_enabled": mock_true}),\
  67. patch.object(win_network, '_validate', mock_validate):
  68. self.assertDictEqual(
  69. win_network.managed(
  70. 'salt', dns_proto='static', ip_proto='static'),
  71. ret)
  72. def test_managed_get_current_config_failed(self):
  73. ret = {'name': 'salt',
  74. 'changes': {},
  75. 'result': False,
  76. 'comment': 'Unable to get current configuration for interface '
  77. '\'salt\''}
  78. mock_true = MagicMock(return_value=True)
  79. mock_false = MagicMock(return_value=False)
  80. mock_validate = MagicMock(return_value=[])
  81. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  82. 'ip.get_interface': mock_false}), \
  83. patch.object(win_network, '_validate', mock_validate):
  84. self.assertDictEqual(
  85. win_network.managed('salt', dns_proto='dhcp', ip_proto='dhcp'),
  86. ret)
  87. def test_managed_test_true_no_changes(self):
  88. ret = {'name': 'salt',
  89. 'changes': {},
  90. 'result': True,
  91. 'comment': 'Interface \'salt\' is up to date'}
  92. mock_true = MagicMock(return_value=True)
  93. mock_validate = MagicMock(return_value=[])
  94. mock_get_int = MagicMock(return_value={
  95. 'DHCP enabled': 'yes',
  96. 'DNS servers configured through DHCP': '192.168.0.10'})
  97. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  98. 'ip.get_interface': mock_get_int}), \
  99. patch.dict(win_network.__opts__, {"test": True}), \
  100. patch.object(win_network, '_validate', mock_validate):
  101. self.assertDictEqual(
  102. win_network.managed('salt', dns_proto='dhcp', ip_proto='dhcp'),
  103. ret)
  104. def test_managed_test_true_changes(self):
  105. ret = {'name': 'salt',
  106. 'changes': {},
  107. 'result': None,
  108. 'comment': 'The following changes will be made to interface '
  109. '\'salt\':\n'
  110. '- DNS protocol will be changed to: dhcp'}
  111. mock_true = MagicMock(return_value=True)
  112. mock_validate = MagicMock(return_value=[])
  113. mock_get_int = MagicMock(return_value={
  114. 'DHCP enabled': 'no',
  115. 'Statically Configured DNS Servers': '192.168.0.10'})
  116. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  117. 'ip.get_interface': mock_get_int}), \
  118. patch.dict(win_network.__opts__, {"test": True}), \
  119. patch.object(win_network, '_validate', mock_validate):
  120. self.assertDictEqual(
  121. win_network.managed('salt', dns_proto='dhcp', ip_proto='dhcp'),
  122. ret)
  123. def test_managed_failed(self):
  124. ret = {'name': 'salt',
  125. 'changes': {},
  126. 'result': False,
  127. 'comment': 'Failed to set desired configuration settings for '
  128. 'interface \'salt\''}
  129. mock_true = MagicMock(return_value=True)
  130. mock_validate = MagicMock(return_value=[])
  131. mock_get_int = MagicMock(return_value={
  132. 'DHCP enabled': 'no',
  133. 'Statically Configured DNS Servers': '192.168.0.10'})
  134. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  135. 'ip.get_interface': mock_get_int,
  136. 'ip.set_dhcp_dns': mock_true,
  137. 'ip.set_dhcp_ip': mock_true}), \
  138. patch.dict(win_network.__opts__, {"test": False}), \
  139. patch.object(win_network, '_validate', mock_validate):
  140. self.assertDictEqual(
  141. win_network.managed('salt', dns_proto='dhcp', ip_proto='dhcp'),
  142. ret)
  143. def test_managed(self):
  144. ret = {'name': 'salt',
  145. 'changes': {
  146. 'DHCP enabled': {
  147. 'new': 'yes',
  148. 'old': 'no'},
  149. 'DNS servers configured through DHCP': {
  150. 'new': '192.168.0.10',
  151. 'old': ''},
  152. 'Statically Configured DNS Servers': {
  153. 'new': '',
  154. 'old': '192.168.0.10'
  155. }
  156. },
  157. 'result': True,
  158. 'comment': 'Successfully updated configuration for interface '
  159. '\'salt\''}
  160. mock_true = MagicMock(return_value=True)
  161. mock_validate = MagicMock(return_value=[])
  162. mock_get_int = MagicMock(side_effect=[
  163. {'DHCP enabled': 'no', 'Statically Configured DNS Servers': '192.168.0.10'},
  164. {'DHCP enabled': 'yes', 'DNS servers configured through DHCP': '192.168.0.10'},
  165. ])
  166. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  167. 'ip.get_interface': mock_get_int,
  168. 'ip.set_dhcp_dns': mock_true,
  169. 'ip.set_dhcp_ip': mock_true}), \
  170. patch.dict(win_network.__opts__, {"test": False}), \
  171. patch.object(win_network, '_validate', mock_validate):
  172. self.assertDictEqual(
  173. win_network.managed('salt', dns_proto='dhcp', ip_proto='dhcp'),
  174. ret)
  175. def test_managed_static_dns_clear(self):
  176. expected = {'name': 'salt',
  177. 'changes': {
  178. 'Statically Configured DNS Servers': {
  179. 'new': 'None',
  180. 'old': '192.168.0.10'
  181. }
  182. },
  183. 'result': True,
  184. 'comment': 'Successfully updated configuration for '
  185. 'interface \'salt\''}
  186. mock_true = MagicMock(return_value=True)
  187. mock_validate = MagicMock(return_value=[])
  188. mock_get_int = MagicMock(side_effect=[
  189. {'DHCP enabled': 'no', 'Statically Configured DNS Servers': '192.168.0.10'},
  190. {'DHCP enabled': 'no', 'Statically Configured DNS Servers': 'None'},
  191. ])
  192. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  193. 'ip.get_interface': mock_get_int,
  194. 'ip.set_static_dns': mock_true}), \
  195. patch.dict(win_network.__opts__, {"test": False}), \
  196. patch.object(win_network, '_validate', mock_validate):
  197. ret = win_network.managed(
  198. 'salt', dns_proto='static', dns_servers=[], ip_proto='dhcp')
  199. self.assertDictEqual(ret, expected)
  200. def test_managed_static_dns(self):
  201. expected = {'name': 'salt',
  202. 'changes': {
  203. 'Statically Configured DNS Servers': {
  204. 'new': '192.168.0.10',
  205. 'old': 'None'
  206. }
  207. },
  208. 'result': True,
  209. 'comment': 'Successfully updated configuration for '
  210. 'interface \'salt\''}
  211. mock_true = MagicMock(return_value=True)
  212. mock_validate = MagicMock(return_value=[])
  213. mock_get_int = MagicMock(side_effect=[
  214. {'DHCP enabled': 'no', 'Statically Configured DNS Servers': 'None'},
  215. {'DHCP enabled': 'no', 'Statically Configured DNS Servers': '192.168.0.10'},
  216. ])
  217. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  218. 'ip.get_interface': mock_get_int,
  219. 'ip.set_static_dns': mock_true}), \
  220. patch.dict(win_network.__opts__, {"test": False}), \
  221. patch.object(win_network, '_validate', mock_validate):
  222. ret = win_network.managed(
  223. 'salt', dns_proto='static', dns_servers=['192.168.0.10'], ip_proto='dhcp')
  224. self.assertDictEqual(ret, expected)
  225. def test_managed_static_dns_no_action(self):
  226. expected = {'name': 'salt',
  227. 'changes': {},
  228. 'result': True,
  229. 'comment': 'Interface \'salt\' is up to date'}
  230. mock_true = MagicMock(return_value=True)
  231. mock_validate = MagicMock(return_value=[])
  232. mock_get_int = MagicMock(return_value={
  233. 'DHCP enabled': 'no',
  234. 'Statically Configured DNS Servers': '192.168.0.10'
  235. })
  236. with patch.dict(win_network.__salt__, {'ip.is_enabled': mock_true,
  237. 'ip.get_interface': mock_get_int,
  238. 'ip.set_static_dns': mock_true}), \
  239. patch.dict(win_network.__opts__, {"test": False}), \
  240. patch.object(win_network, '_validate', mock_validate):
  241. # Don't pass dns_servers
  242. ret = win_network.managed('salt', dns_proto='static', ip_proto='dhcp')
  243. self.assertDictEqual(ret, expected)
  244. # Pass dns_servers=None
  245. ret = win_network.managed(
  246. 'salt', dns_proto='static', dns_servers=None, ip_proto='dhcp')
  247. self.assertDictEqual(ret, expected)