1
0

test_esxdatacenter.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Alexandru Bleotu <alexandru.bleotu@morganstanley.com>`
  4. Tests for esxdatacenter 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.esxdatacenter as esxdatacenter
  16. import salt.exceptions
  17. from salt.config.schemas.esxdatacenter import EsxdatacenterProxySchema
  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.esxdatacenter.init'''
  31. def setup_loader_modules(self):
  32. return {esxdatacenter: {'__virtual__':
  33. MagicMock(return_value='esxdatacenter'),
  34. 'DETAILS': {}, '__pillar__': {}}}
  35. def setUp(self):
  36. self.opts_userpass = {'proxy': {'proxytype': 'esxdatacenter',
  37. 'vcenter': 'fake_vcenter',
  38. 'datacenter': 'fake_dc',
  39. 'mechanism': 'userpass',
  40. 'username': 'fake_username',
  41. 'passwords': ['fake_password'],
  42. 'protocol': 'fake_protocol',
  43. 'port': 100}}
  44. self.opts_sspi = {'proxy': {'proxytype': 'esxdatacenter',
  45. 'vcenter': 'fake_vcenter',
  46. 'datacenter': 'fake_dc',
  47. 'mechanism': 'sspi',
  48. 'domain': 'fake_domain',
  49. 'principal': 'fake_principal',
  50. 'protocol': 'fake_protocol',
  51. 'port': 100}}
  52. patches = (('salt.proxy.esxdatacenter.merge',
  53. MagicMock(return_value=self.opts_sspi['proxy'])),)
  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(esxdatacenter.__pillar__,
  63. {'proxy': mock_pillar_proxy}):
  64. with patch('salt.proxy.esxdatacenter.merge', mock_merge):
  65. esxdatacenter.init(opts={'proxy': mock_opts_proxy})
  66. mock_merge.assert_called_once_with(mock_opts_proxy, mock_pillar_proxy)
  67. def test_esxdatacenter_schema(self):
  68. mock_json_validate = MagicMock()
  69. serialized_schema = EsxdatacenterProxySchema().serialize()
  70. with patch('salt.proxy.esxdatacenter.jsonschema.validate',
  71. mock_json_validate):
  72. esxdatacenter.init(self.opts_sspi)
  73. mock_json_validate.assert_called_once_with(
  74. self.opts_sspi['proxy'], serialized_schema)
  75. def test_invalid_proxy_input_error(self):
  76. with patch('salt.proxy.esxdatacenter.jsonschema.validate',
  77. MagicMock(side_effect=jsonschema.exceptions.ValidationError(
  78. 'Validation Error'))):
  79. with self.assertRaises(salt.exceptions.InvalidConfigError) as \
  80. excinfo:
  81. esxdatacenter.init(self.opts_userpass)
  82. self.assertEqual(excinfo.exception.strerror,
  83. 'Validation Error')
  84. def test_no_username(self):
  85. opts = self.opts_userpass.copy()
  86. del opts['proxy']['username']
  87. with patch('salt.proxy.esxdatacenter.merge',
  88. MagicMock(return_value=opts['proxy'])):
  89. with self.assertRaises(salt.exceptions.InvalidConfigError) as \
  90. excinfo:
  91. esxdatacenter.init(opts)
  92. self.assertEqual(excinfo.exception.strerror,
  93. 'Mechanism is set to \'userpass\', but no '
  94. '\'username\' key found in proxy config.')
  95. def test_no_passwords(self):
  96. opts = self.opts_userpass.copy()
  97. del opts['proxy']['passwords']
  98. with patch('salt.proxy.esxdatacenter.merge',
  99. MagicMock(return_value=opts['proxy'])):
  100. with self.assertRaises(salt.exceptions.InvalidConfigError) as \
  101. excinfo:
  102. esxdatacenter.init(opts)
  103. self.assertEqual(excinfo.exception.strerror,
  104. 'Mechanism is set to \'userpass\', but no '
  105. '\'passwords\' key found in proxy config.')
  106. def test_no_domain(self):
  107. opts = self.opts_sspi.copy()
  108. del opts['proxy']['domain']
  109. with patch('salt.proxy.esxdatacenter.merge',
  110. MagicMock(return_value=opts['proxy'])):
  111. with self.assertRaises(salt.exceptions.InvalidConfigError) as \
  112. excinfo:
  113. esxdatacenter.init(opts)
  114. self.assertEqual(excinfo.exception.strerror,
  115. 'Mechanism is set to \'sspi\', but no '
  116. '\'domain\' key found in proxy config.')
  117. def test_no_principal(self):
  118. opts = self.opts_sspi.copy()
  119. del opts['proxy']['principal']
  120. with patch('salt.proxy.esxdatacenter.merge',
  121. MagicMock(return_value=opts['proxy'])):
  122. with self.assertRaises(salt.exceptions.InvalidConfigError) as \
  123. excinfo:
  124. esxdatacenter.init(opts)
  125. self.assertEqual(excinfo.exception.strerror,
  126. 'Mechanism is set to \'sspi\', but no '
  127. '\'principal\' key found in proxy config.')
  128. def test_find_credentials(self):
  129. mock_find_credentials = MagicMock(return_value=('fake_username',
  130. 'fake_password'))
  131. with patch('salt.proxy.esxdatacenter.merge',
  132. MagicMock(return_value=self.opts_userpass['proxy'])):
  133. with patch('salt.proxy.esxdatacenter.find_credentials',
  134. mock_find_credentials):
  135. esxdatacenter.init(self.opts_userpass)
  136. mock_find_credentials.assert_called_once_with()
  137. def test_details_userpass(self):
  138. mock_find_credentials = MagicMock(return_value=('fake_username',
  139. 'fake_password'))
  140. with patch('salt.proxy.esxdatacenter.merge',
  141. MagicMock(return_value=self.opts_userpass['proxy'])):
  142. with patch('salt.proxy.esxdatacenter.find_credentials',
  143. mock_find_credentials):
  144. esxdatacenter.init(self.opts_userpass)
  145. self.assertDictEqual(esxdatacenter.DETAILS,
  146. {'vcenter': 'fake_vcenter',
  147. 'datacenter': 'fake_dc',
  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. esxdatacenter.init(self.opts_sspi)
  156. self.assertDictEqual(esxdatacenter.DETAILS,
  157. {'vcenter': 'fake_vcenter',
  158. 'datacenter': 'fake_dc',
  159. 'mechanism': 'sspi',
  160. 'domain': 'fake_domain',
  161. 'principal': 'fake_principal',
  162. 'protocol': 'fake_protocol',
  163. 'port': 100})