test_heat.py 6.5 KB

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