test_esxdatacenter.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Alexandru Bleotu <alexandru.bleotu@morganstanley.com>`
  4. Tests for functions in salt.states.esxdatacenter
  5. '''
  6. # Import Python Libs
  7. from __future__ import absolute_import, print_function, unicode_literals
  8. # Import Salt Libs
  9. import salt.states.esxdatacenter as esxdatacenter
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.unit import TestCase
  14. from tests.support.mock import (
  15. MagicMock,
  16. patch,
  17. )
  18. class DatacenterConfiguredTestCase(TestCase, LoaderModuleMockMixin):
  19. '''Tests for salt.modules.esxdatacenter.datacenter_configured'''
  20. def setup_loader_modules(self):
  21. return {
  22. esxdatacenter: {
  23. '__virtual__': MagicMock(return_value='esxdatacenter')}}
  24. def setUp(self):
  25. self.mock_si = MagicMock()
  26. self.mock_dc = MagicMock()
  27. patcher = patch.dict(
  28. esxdatacenter.__salt__,
  29. {'vsphere.get_proxy_type': MagicMock(),
  30. 'vsphere.get_service_instance_via_proxy':
  31. MagicMock(return_value=self.mock_si),
  32. 'vsphere.list_datacenters_via_proxy':
  33. MagicMock(return_value=[self.mock_dc]),
  34. 'vsphere.disconnect': MagicMock()})
  35. patcher.start()
  36. self.addCleanup(patcher.stop)
  37. patcher = patch.dict(esxdatacenter.__opts__, {'test': False})
  38. patcher.start()
  39. self.addCleanup(patcher.stop)
  40. def tearDown(self):
  41. for attrname in ('mock_si',):
  42. try:
  43. delattr(self, attrname)
  44. except AttributeError:
  45. continue
  46. def test_dc_name_different_proxy(self):
  47. with patch.dict(esxdatacenter.__salt__,
  48. {'vsphere.get_proxy_type':
  49. MagicMock(return_value='different_proxy')}):
  50. res = esxdatacenter.datacenter_configured('fake_dc')
  51. self.assertDictEqual(res, {'name': 'fake_dc',
  52. 'changes': {},
  53. 'result': True,
  54. 'comment': 'Datacenter \'fake_dc\' already '
  55. 'exists. Nothing to be done.'})
  56. def test_dc_name_esxdatacenter_proxy(self):
  57. with patch.dict(esxdatacenter.__salt__,
  58. {'vsphere.get_proxy_type':
  59. MagicMock(return_value='esxdatacenter'),
  60. 'esxdatacenter.get_details':
  61. MagicMock(return_value={'datacenter': 'proxy_dc'})}):
  62. res = esxdatacenter.datacenter_configured('fake_dc')
  63. self.assertDictEqual(res, {'name': 'fake_dc',
  64. 'changes': {},
  65. 'result': True,
  66. 'comment': 'Datacenter \'proxy_dc\' '
  67. 'already exists. Nothing to be done.'})
  68. def test_get_service_instance(self):
  69. mock_get_service_instance = MagicMock()
  70. with patch.dict(esxdatacenter.__salt__,
  71. {'vsphere.get_service_instance_via_proxy':
  72. mock_get_service_instance}):
  73. esxdatacenter.datacenter_configured('fake_dc')
  74. mock_get_service_instance.assert_called_once_with()
  75. def test_list_datacenters(self):
  76. mock_list_datacenters = MagicMock()
  77. with patch.dict(esxdatacenter.__salt__,
  78. {'vsphere.list_datacenters_via_proxy':
  79. mock_list_datacenters}):
  80. esxdatacenter.datacenter_configured('fake_dc')
  81. mock_list_datacenters.assert_called_once_with(
  82. datacenter_names=['fake_dc'], service_instance=self.mock_si)
  83. def test_create_datacenter(self):
  84. mock_create_datacenter = MagicMock()
  85. with patch.dict(esxdatacenter.__salt__,
  86. {'vsphere.list_datacenters_via_proxy':
  87. MagicMock(return_value=[]),
  88. 'vsphere.create_datacenter':
  89. mock_create_datacenter}):
  90. res = esxdatacenter.datacenter_configured('fake_dc')
  91. mock_create_datacenter.assert_called_once_with('fake_dc', self.mock_si)
  92. self.assertDictEqual(res,
  93. {'name': 'fake_dc',
  94. 'changes': {'new': {'name': 'fake_dc'}},
  95. 'result': True,
  96. 'comment': 'Created datacenter \'fake_dc\'.'})
  97. def test_create_datacenter_test_mode(self):
  98. with patch.dict(esxdatacenter.__opts__, {'test': True}):
  99. with patch.dict(esxdatacenter.__salt__,
  100. {'vsphere.list_datacenters_via_proxy':
  101. MagicMock(return_value=[])}):
  102. res = esxdatacenter.datacenter_configured('fake_dc')
  103. self.assertDictEqual(res,
  104. {'name': 'fake_dc',
  105. 'changes': {'new': {'name': 'fake_dc'}},
  106. 'result': None,
  107. 'comment': 'State will create '
  108. 'datacenter \'fake_dc\'.'})
  109. def test_nothing_to_be_done_test_mode(self):
  110. with patch.dict(esxdatacenter.__opts__, {'test': True}):
  111. with patch.dict(esxdatacenter.__salt__,
  112. {'vsphere.get_proxy_type':
  113. MagicMock(return_value='different_proxy')}):
  114. res = esxdatacenter.datacenter_configured('fake_dc')
  115. self.assertDictEqual(res, {'name': 'fake_dc',
  116. 'changes': {},
  117. 'result': True,
  118. 'comment': 'Datacenter \'fake_dc\' already '
  119. 'exists. Nothing to be done.'})
  120. def test_state_get_service_instance_raise_command_execution_error(self):
  121. mock_disconnect = MagicMock()
  122. with patch.dict(esxdatacenter.__salt__,
  123. {'vsphere.disconnect': mock_disconnect,
  124. 'vsphere.get_service_instance_via_proxy':
  125. MagicMock(
  126. side_effect=CommandExecutionError('Error'))}):
  127. res = esxdatacenter.datacenter_configured('fake_dc')
  128. self.assertEqual(mock_disconnect.call_count, 0)
  129. self.assertDictEqual(res, {'name': 'fake_dc',
  130. 'changes': {},
  131. 'result': False,
  132. 'comment': 'Error'})
  133. def test_state_raise_command_execution_error_after_si(self):
  134. mock_disconnect = MagicMock()
  135. with patch.dict(esxdatacenter.__salt__,
  136. {'vsphere.disconnect': mock_disconnect,
  137. 'vsphere.list_datacenters_via_proxy':
  138. MagicMock(
  139. side_effect=CommandExecutionError('Error'))}):
  140. res = esxdatacenter.datacenter_configured('fake_dc')
  141. mock_disconnect.assert_called_once_with(self.mock_si)
  142. self.assertDictEqual(res, {'name': 'fake_dc',
  143. 'changes': {},
  144. 'result': False,
  145. 'comment': 'Error'})
  146. def test_state_raise_command_execution_error_test_mode(self):
  147. with patch.dict(esxdatacenter.__opts__, {'test': True}):
  148. with patch.dict(esxdatacenter.__salt__,
  149. {'vsphere.list_datacenters_via_proxy':
  150. MagicMock(
  151. side_effect=CommandExecutionError('Error'))}):
  152. res = esxdatacenter.datacenter_configured('fake_dc')
  153. self.assertDictEqual(res, {'name': 'fake_dc',
  154. 'changes': {},
  155. 'result': None,
  156. 'comment': 'Error'})