1
0

test_heat.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # -*- coding: utf-8 -*-
  2. # Import Python Libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  4. import os
  5. # Import Salt Testing Libs
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.unit import TestCase
  8. from tests.support.runtests import RUNTIME_VARS
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch,
  12. )
  13. # Import Salt Libs
  14. import tests.unit.modules.test_heat
  15. import salt.utils.platform
  16. import salt.states.heat as heat
  17. import salt.modules.file as file_
  18. import salt.modules.heat
  19. import salt.modules.win_file as win_file
  20. import salt.utils.win_dacl as dacl
  21. class HeatTestCase(TestCase, LoaderModuleMockMixin):
  22. '''
  23. Test cases for salt.states.heat
  24. '''
  25. def setup_loader_modules(self):
  26. return {
  27. heat: {
  28. '_auth': tests.unit.modules.test_heat.MockClient,
  29. '__opts__': {'test': False},
  30. },
  31. salt.modules.heat: {'_auth':
  32. tests.unit.modules.test_heat.MockClient, },
  33. file_: {
  34. '__opts__': {'hash_type': 'sha256',
  35. 'cachedir': os.path.join(RUNTIME_VARS.TMP,
  36. 'rootdir', 'cache'),
  37. 'test': False},
  38. '__salt__': {'config.option':
  39. MagicMock(return_value={'obfuscate_templates':
  40. False}),
  41. 'config.backup_mode':
  42. MagicMock(return_value=False)}
  43. },
  44. win_file: {
  45. '__utils__': {'dacl.check_perms': salt.utils.win_dacl.check_perms}
  46. },
  47. dacl: {'__opts__': {'test': False}},
  48. }
  49. def setUp(self):
  50. self.patch_check = patch('salt.modules.file.check_perms', file_.check_perms)
  51. if salt.utils.platform.is_windows():
  52. self.patch_check = patch('salt.modules.file.check_perms', win_file.check_perms)
  53. def test_heat_deployed(self):
  54. '''
  55. Test salt.states.heat.deployed method
  56. '''
  57. exp_ret = {'name': ('mystack',),
  58. 'comment': "Created stack 'mystack'.",
  59. 'changes': {'stack_name': 'mystack',
  60. 'comment': 'Create stack'},
  61. 'result': True}
  62. patch_heat = patch.dict(heat.__salt__,
  63. {'heat.show_stack': MagicMock(return_value={'result': False}),
  64. 'heat.create_stack': salt.modules.heat.create_stack})
  65. patch_file = patch.dict('salt.modules.heat.__salt__',
  66. {'file.get_managed': file_.get_managed,
  67. 'file.manage_file': file_.manage_file, },)
  68. patch_create = patch('salt.modules.heat.create_stack',
  69. MagicMock(return_value={'result': True, 'comment':
  70. "Created stack 'mystack'."}))
  71. with patch_heat, patch_file, patch_create, self.patch_check:
  72. ret = heat.deployed(name='mystack',
  73. profile='openstack1',
  74. template=os.path.join(RUNTIME_VARS.BASE_FILES,
  75. 'templates', 'heat-template.yml'),
  76. poll=0)
  77. assert ret == exp_ret
  78. def test_heat_deployed_environment(self):
  79. '''
  80. Test salt.states.heat.deployed method
  81. with environment set
  82. '''
  83. exp_ret = {'name': ('mystack',),
  84. 'comment': "Created stack 'mystack'.",
  85. 'changes': {'stack_name': 'mystack',
  86. 'comment': 'Create stack'},
  87. 'result': True}
  88. patch_heat = patch.dict(heat.__salt__,
  89. {'heat.show_stack': MagicMock(return_value={'result': False}),
  90. 'heat.create_stack': salt.modules.heat.create_stack})
  91. patch_file = patch.dict('salt.modules.heat.__salt__',
  92. {'file.get_managed': file_.get_managed,
  93. 'file.manage_file': file_.manage_file, },)
  94. patch_create = patch('salt.modules.heat.create_stack',
  95. MagicMock(return_value={'result': True, 'comment':
  96. "Created stack 'mystack'."}))
  97. with patch_heat, patch_file, patch_create, self.patch_check:
  98. ret = heat.deployed(name='mystack',
  99. profile='openstack1',
  100. template=os.path.join(RUNTIME_VARS.BASE_FILES,
  101. 'templates', 'heat-template.yml'),
  102. poll=0,
  103. environment=os.path.join(RUNTIME_VARS.BASE_FILES,
  104. 'templates', 'heat-env.yml'))
  105. assert ret == exp_ret
  106. def test_heat_deployed_environment_error(self):
  107. '''
  108. Test salt.states.heat.deployed method
  109. with environment set and there is an error
  110. reading the environment file.
  111. '''
  112. exp_ret = {'name': ('mystack',),
  113. 'comment': 'Error parsing template Template format version not found.',
  114. 'changes': {'stack_name': 'mystack',
  115. 'comment': 'Create stack'},
  116. 'result': False}
  117. patch_heat = patch.dict(heat.__salt__,
  118. {'heat.show_stack': MagicMock(return_value={'result': False}),
  119. 'heat.create_stack': salt.modules.heat.create_stack})
  120. patch_file = patch.dict('salt.modules.heat.__salt__',
  121. {'file.get_managed': file_.get_managed,
  122. 'file.manage_file': MagicMock(side_effect=[{'result': True},
  123. {'result': False}])})
  124. patch_create = patch('salt.modules.heat.create_stack',
  125. MagicMock(return_value={'result': True, 'comment':
  126. "Created stack 'mystack'."}))
  127. with patch_heat, patch_file, patch_create, self.patch_check:
  128. ret = heat.deployed(name='mystack',
  129. profile='openstack1',
  130. template=os.path.join(RUNTIME_VARS.BASE_FILES,
  131. 'templates', 'heat-template.yml'),
  132. poll=0,
  133. environment=os.path.join(RUNTIME_VARS.BASE_FILES,
  134. 'templates', 'heat-env.yml'))
  135. assert ret == exp_ret