test_esxdatacenter.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: :email:`Alexandru Bleotu <alexandru.bleotu@morganstanley.com>`
  4. Tests for esxdatacenter proxy
  5. """
  6. # Import Python Libs
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. import salt.exceptions
  9. # Import Salt Libs
  10. import salt.proxy.esxdatacenter as esxdatacenter
  11. from salt.config.schemas.esxdatacenter import EsxdatacenterProxySchema
  12. # Import Salt Testing Libs
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import MagicMock, patch
  15. from tests.support.unit import TestCase, skipIf
  16. # Import external libs
  17. try:
  18. import jsonschema
  19. HAS_JSONSCHEMA = True
  20. except ImportError:
  21. HAS_JSONSCHEMA = False
  22. @skipIf(not HAS_JSONSCHEMA, "jsonschema is required")
  23. class InitTestCase(TestCase, LoaderModuleMockMixin):
  24. """Tests for salt.proxy.esxdatacenter.init"""
  25. def setup_loader_modules(self):
  26. return {
  27. esxdatacenter: {
  28. "__virtual__": MagicMock(return_value="esxdatacenter"),
  29. "DETAILS": {},
  30. "__pillar__": {},
  31. }
  32. }
  33. def setUp(self):
  34. self.opts_userpass = {
  35. "proxy": {
  36. "proxytype": "esxdatacenter",
  37. "vcenter": "fake_vcenter",
  38. "datacenter": "fake_dc",
  39. "mechanism": "userpass",
  40. "username": "fake_username",
  41. "passwords": ["fake_password"],
  42. "protocol": "fake_protocol",
  43. "port": 100,
  44. }
  45. }
  46. self.opts_sspi = {
  47. "proxy": {
  48. "proxytype": "esxdatacenter",
  49. "vcenter": "fake_vcenter",
  50. "datacenter": "fake_dc",
  51. "mechanism": "sspi",
  52. "domain": "fake_domain",
  53. "principal": "fake_principal",
  54. "protocol": "fake_protocol",
  55. "port": 100,
  56. }
  57. }
  58. patches = (
  59. (
  60. "salt.proxy.esxdatacenter.merge",
  61. MagicMock(return_value=self.opts_sspi["proxy"]),
  62. ),
  63. )
  64. for mod, mock in patches:
  65. patcher = patch(mod, mock)
  66. patcher.start()
  67. self.addCleanup(patcher.stop)
  68. def test_merge(self):
  69. mock_pillar_proxy = MagicMock()
  70. mock_opts_proxy = MagicMock()
  71. mock_merge = MagicMock(return_value=self.opts_sspi["proxy"])
  72. with patch.dict(esxdatacenter.__pillar__, {"proxy": mock_pillar_proxy}):
  73. with patch("salt.proxy.esxdatacenter.merge", mock_merge):
  74. esxdatacenter.init(opts={"proxy": mock_opts_proxy})
  75. mock_merge.assert_called_once_with(mock_opts_proxy, mock_pillar_proxy)
  76. def test_esxdatacenter_schema(self):
  77. mock_json_validate = MagicMock()
  78. serialized_schema = EsxdatacenterProxySchema().serialize()
  79. with patch("salt.proxy.esxdatacenter.jsonschema.validate", mock_json_validate):
  80. esxdatacenter.init(self.opts_sspi)
  81. mock_json_validate.assert_called_once_with(
  82. self.opts_sspi["proxy"], serialized_schema
  83. )
  84. def test_invalid_proxy_input_error(self):
  85. with patch(
  86. "salt.proxy.esxdatacenter.jsonschema.validate",
  87. MagicMock(
  88. side_effect=jsonschema.exceptions.ValidationError("Validation Error")
  89. ),
  90. ):
  91. with self.assertRaises(salt.exceptions.InvalidConfigError) as excinfo:
  92. esxdatacenter.init(self.opts_userpass)
  93. self.assertEqual(excinfo.exception.strerror, "Validation Error")
  94. def test_no_username(self):
  95. opts = self.opts_userpass.copy()
  96. del opts["proxy"]["username"]
  97. with patch(
  98. "salt.proxy.esxdatacenter.merge", MagicMock(return_value=opts["proxy"])
  99. ):
  100. with self.assertRaises(salt.exceptions.InvalidConfigError) as excinfo:
  101. esxdatacenter.init(opts)
  102. self.assertEqual(
  103. excinfo.exception.strerror,
  104. "Mechanism is set to 'userpass', but no "
  105. "'username' key found in proxy config.",
  106. )
  107. def test_no_passwords(self):
  108. opts = self.opts_userpass.copy()
  109. del opts["proxy"]["passwords"]
  110. with patch(
  111. "salt.proxy.esxdatacenter.merge", MagicMock(return_value=opts["proxy"])
  112. ):
  113. with self.assertRaises(salt.exceptions.InvalidConfigError) as excinfo:
  114. esxdatacenter.init(opts)
  115. self.assertEqual(
  116. excinfo.exception.strerror,
  117. "Mechanism is set to 'userpass', but no "
  118. "'passwords' key found in proxy config.",
  119. )
  120. def test_no_domain(self):
  121. opts = self.opts_sspi.copy()
  122. del opts["proxy"]["domain"]
  123. with patch(
  124. "salt.proxy.esxdatacenter.merge", MagicMock(return_value=opts["proxy"])
  125. ):
  126. with self.assertRaises(salt.exceptions.InvalidConfigError) as excinfo:
  127. esxdatacenter.init(opts)
  128. self.assertEqual(
  129. excinfo.exception.strerror,
  130. "Mechanism is set to 'sspi', but no " "'domain' key found in proxy config.",
  131. )
  132. def test_no_principal(self):
  133. opts = self.opts_sspi.copy()
  134. del opts["proxy"]["principal"]
  135. with patch(
  136. "salt.proxy.esxdatacenter.merge", MagicMock(return_value=opts["proxy"])
  137. ):
  138. with self.assertRaises(salt.exceptions.InvalidConfigError) as excinfo:
  139. esxdatacenter.init(opts)
  140. self.assertEqual(
  141. excinfo.exception.strerror,
  142. "Mechanism is set to 'sspi', but no "
  143. "'principal' key found in proxy config.",
  144. )
  145. def test_find_credentials(self):
  146. mock_find_credentials = MagicMock(
  147. return_value=("fake_username", "fake_password")
  148. )
  149. with patch(
  150. "salt.proxy.esxdatacenter.merge",
  151. MagicMock(return_value=self.opts_userpass["proxy"]),
  152. ):
  153. with patch(
  154. "salt.proxy.esxdatacenter.find_credentials", mock_find_credentials
  155. ):
  156. esxdatacenter.init(self.opts_userpass)
  157. mock_find_credentials.assert_called_once_with()
  158. def test_details_userpass(self):
  159. mock_find_credentials = MagicMock(
  160. return_value=("fake_username", "fake_password")
  161. )
  162. with patch(
  163. "salt.proxy.esxdatacenter.merge",
  164. MagicMock(return_value=self.opts_userpass["proxy"]),
  165. ):
  166. with patch(
  167. "salt.proxy.esxdatacenter.find_credentials", mock_find_credentials
  168. ):
  169. esxdatacenter.init(self.opts_userpass)
  170. self.assertDictEqual(
  171. esxdatacenter.DETAILS,
  172. {
  173. "vcenter": "fake_vcenter",
  174. "datacenter": "fake_dc",
  175. "mechanism": "userpass",
  176. "username": "fake_username",
  177. "password": "fake_password",
  178. "passwords": ["fake_password"],
  179. "protocol": "fake_protocol",
  180. "port": 100,
  181. },
  182. )
  183. def test_details_sspi(self):
  184. esxdatacenter.init(self.opts_sspi)
  185. self.assertDictEqual(
  186. esxdatacenter.DETAILS,
  187. {
  188. "vcenter": "fake_vcenter",
  189. "datacenter": "fake_dc",
  190. "mechanism": "sspi",
  191. "domain": "fake_domain",
  192. "principal": "fake_principal",
  193. "protocol": "fake_protocol",
  194. "port": 100,
  195. },
  196. )