1
0

test_esxcluster.py 7.1 KB

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