test_proxy.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. # Import Salt Testing Libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import (
  8. MagicMock,
  9. patch,
  10. call
  11. )
  12. # Import Salt Libs
  13. import salt.states.proxy as proxy
  14. class ProxyTestCase(TestCase, LoaderModuleMockMixin):
  15. '''
  16. Validate the proxy state
  17. '''
  18. def setup_loader_modules(self):
  19. return {proxy: {}}
  20. def test_set_proxy_macos(self):
  21. '''
  22. Test to make sure we can set the proxy settings on macOS
  23. '''
  24. with patch.dict(proxy.__grains__, {'os': 'Darwin'}):
  25. expected = {'changes': {
  26. 'new': [
  27. {'port': '3128',
  28. 'server': '192.168.0.1',
  29. 'service': 'http',
  30. 'user': 'frank'},
  31. {'port': '3128',
  32. 'server': '192.168.0.1',
  33. 'service': 'https',
  34. 'user': 'frank'},
  35. {'port': '3128',
  36. 'server': '192.168.0.1',
  37. 'service': 'ftp',
  38. 'user': 'frank'},
  39. {'bypass_domains': ['salt.com', 'test.com']}]
  40. },
  41. 'comment': 'http proxy settings updated correctly\nhttps proxy settings updated correctly\nftp proxy '
  42. 'settings updated correctly\nProxy bypass domains updated correctly\n',
  43. 'name': '192.168.0.1',
  44. 'result': True}
  45. set_proxy_mock = MagicMock(return_value=True)
  46. patches = {
  47. 'proxy.get_http_proxy': MagicMock(return_value={}),
  48. 'proxy.get_https_proxy': MagicMock(return_value={}),
  49. 'proxy.get_ftp_proxy': MagicMock(return_value={}),
  50. 'proxy.get_proxy_bypass': MagicMock(return_value=[]),
  51. 'proxy.set_http_proxy': set_proxy_mock,
  52. 'proxy.set_https_proxy': set_proxy_mock,
  53. 'proxy.set_ftp_proxy': set_proxy_mock,
  54. 'proxy.set_proxy_bypass': set_proxy_mock,
  55. }
  56. with patch.dict(proxy.__salt__, patches):
  57. out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd',
  58. bypass_domains=['salt.com', 'test.com'])
  59. out['changes']['new'][-1]['bypass_domains'] = sorted(out['changes']['new'][-1]['bypass_domains'])
  60. calls = [
  61. call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'),
  62. call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'),
  63. call('192.168.0.1', '3128', 'frank', 'passw0rd', 'Ethernet'),
  64. call(['salt.com', 'test.com'], 'Ethernet')
  65. ]
  66. set_proxy_mock.assert_has_calls(calls)
  67. self.assertEqual(out, expected)
  68. def test_set_proxy_macos_same(self):
  69. '''
  70. Test to make sure we can set the proxy settings on macOS
  71. '''
  72. with patch.dict(proxy.__grains__, {'os': 'Darwin'}):
  73. expected = {'changes': {
  74. },
  75. 'comment': 'http proxy settings already set.\nhttps proxy settings already set.\nftp proxy settings'
  76. ' already set.\nProxy bypass domains are already set correctly.\n',
  77. 'name': '192.168.0.1',
  78. 'result': True}
  79. proxy_val = {
  80. 'enabled': True,
  81. 'server': '192.168.0.1',
  82. 'port': '3128'
  83. }
  84. set_proxy_mock = MagicMock()
  85. patches = {
  86. 'proxy.get_http_proxy': MagicMock(return_value=proxy_val),
  87. 'proxy.get_https_proxy': MagicMock(return_value=proxy_val),
  88. 'proxy.get_ftp_proxy': MagicMock(return_value=proxy_val),
  89. 'proxy.get_proxy_bypass': MagicMock(return_value=['test.com', 'salt.com']),
  90. 'proxy.set_http_proxy': set_proxy_mock,
  91. 'proxy.set_https_proxy': set_proxy_mock,
  92. 'proxy.set_ftp_proxy': set_proxy_mock,
  93. 'proxy.set_proxy_bypass': set_proxy_mock,
  94. }
  95. with patch.dict(proxy.__salt__, patches):
  96. out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd',
  97. bypass_domains=['salt.com', 'test.com'])
  98. assert not set_proxy_mock.called
  99. self.assertEqual(out, expected)
  100. def test_set_proxy_windows(self):
  101. '''
  102. Test to make sure we can set the proxy settings on Windows
  103. '''
  104. with patch.dict(proxy.__grains__, {'os': 'Windows'}):
  105. expected = {
  106. 'changes': {},
  107. 'comment': 'Proxy settings updated correctly',
  108. 'name': '192.168.0.1',
  109. 'result': True
  110. }
  111. set_proxy_mock = MagicMock(return_value=True)
  112. patches = {
  113. 'proxy.get_proxy_win': MagicMock(return_value={}),
  114. 'proxy.get_proxy_bypass': MagicMock(return_value=[]),
  115. 'proxy.set_proxy_win': set_proxy_mock,
  116. }
  117. with patch.dict(proxy.__salt__, patches):
  118. out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd',
  119. bypass_domains=['salt.com', 'test.com'])
  120. set_proxy_mock.assert_called_once_with('192.168.0.1', '3128', ['http', 'https', 'ftp'],
  121. ['salt.com', 'test.com'])
  122. self.assertEqual(out, expected)
  123. def test_set_proxy_windows_same(self):
  124. '''
  125. Test to make sure we can set the proxy settings on Windows
  126. '''
  127. with patch.dict(proxy.__grains__, {'os': 'Windows'}):
  128. expected = {
  129. 'changes': {},
  130. 'comment': 'Proxy settings already correct.',
  131. 'name': '192.168.0.1',
  132. 'result': True
  133. }
  134. proxy_val = {
  135. 'enabled': True,
  136. 'http': {
  137. 'enabled': True,
  138. 'server': '192.168.0.1',
  139. 'port': '3128'
  140. },
  141. 'https': {
  142. 'enabled': True,
  143. 'server': '192.168.0.1',
  144. 'port': '3128'
  145. },
  146. 'ftp': {
  147. 'enabled': True,
  148. 'server': '192.168.0.1',
  149. 'port': '3128'
  150. }
  151. }
  152. set_proxy_mock = MagicMock(return_value=True)
  153. patches = {
  154. 'proxy.get_proxy_win': MagicMock(return_value=proxy_val),
  155. 'proxy.get_proxy_bypass': MagicMock(return_value=['salt.com', 'test.com']),
  156. 'proxy.set_proxy_win': set_proxy_mock,
  157. }
  158. with patch.dict(proxy.__salt__, patches):
  159. out = proxy.managed('192.168.0.1', '3128', user='frank', password='passw0rd',
  160. bypass_domains=['salt.com', 'test.com'])
  161. assert not set_proxy_mock.called
  162. self.assertEqual(out, expected)