test_esxdatacenter.py 7.9 KB

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