1
0

test_esxcluster.py 8.1 KB

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