test_esxdatacenter.py 7.1 KB

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