1
0

test_esxcluster.py 8.0 KB

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