1
0

test_esxcluster.py 7.4 KB

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