test_heat.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 as heat
  7. import salt.modules.win_file as win_file
  8. # Import Salt Libs
  9. import salt.utils.platform
  10. import salt.utils.win_dacl as dacl
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.runtests import RUNTIME_VARS
  15. from tests.support.unit import TestCase
  16. class MockStacks(object):
  17. """
  18. Mock stacks.StackManager
  19. """
  20. def validate(self, **kwargs):
  21. """
  22. Mock of stacks.StackManager.validate method
  23. """
  24. self.mock_val_ret = MagicMock()
  25. self.mock_val_ret.json.return_value = {"result": "mocked response"}
  26. self.mock_validate = MagicMock()
  27. self.mock_validate.post.return_value = self.mock_val_ret
  28. return self.mock_validate
  29. def create(self, **kwargs):
  30. self.mock_create_ret = MagicMock()
  31. self.mock_create_ret.json.return_value = {
  32. "result": "mocked create",
  33. "fields": kwargs,
  34. }
  35. self.mock_create = MagicMock()
  36. self.mock_create.post.return_value = self.mock_create_ret
  37. return self.mock_create
  38. def update(self, name, **kwargs):
  39. self.mock_update_ret = MagicMock()
  40. self.mock_update_ret.json.return_value = {
  41. "result": "mocked update",
  42. "fields": kwargs,
  43. "name": name,
  44. }
  45. self.mock_update = MagicMock()
  46. self.mock_update.post.return_value = self.mock_update_ret
  47. return self.mock_update
  48. class MockClient(object):
  49. """
  50. Mock of Client class
  51. """
  52. def __init__(self, profile=None, **conn_args):
  53. self.stacks = MockStacks()
  54. class HeatTestCase(TestCase, LoaderModuleMockMixin):
  55. """
  56. Test cases for salt.modules.heat
  57. """
  58. def setup_loader_modules(self):
  59. return {
  60. heat: {"_auth": MockClient},
  61. file_: {
  62. "__opts__": {
  63. "hash_type": "sha256",
  64. "cachedir": os.path.join(RUNTIME_VARS.TMP, "rootdir", "cache"),
  65. "test": False,
  66. },
  67. "__salt__": {
  68. "config.option": MagicMock(
  69. return_value={"obfuscate_templates": False}
  70. ),
  71. "config.backup_mode": MagicMock(return_value=False),
  72. },
  73. },
  74. win_file: {
  75. "__utils__": {"dacl.check_perms": salt.utils.win_dacl.check_perms}
  76. },
  77. dacl: {"__opts__": {"test": False}},
  78. }
  79. def setUp(self):
  80. self.patch_check = patch("salt.modules.file.check_perms", file_.check_perms)
  81. if salt.utils.platform.is_windows():
  82. self.patch_check = patch(
  83. "salt.modules.file.check_perms", win_file.check_perms
  84. )
  85. def test_heat_create_stack(self):
  86. """
  87. Test salt.modules.heat.create_stack method
  88. """
  89. patch_file = patch.dict(
  90. heat.__salt__,
  91. {
  92. "file.get_managed": file_.get_managed,
  93. "file.manage_file": file_.manage_file,
  94. },
  95. )
  96. with patch_file, self.patch_check:
  97. ret = heat.create_stack(
  98. name="mystack",
  99. profile="openstack1",
  100. template_file=os.path.join(
  101. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  102. ),
  103. )
  104. assert ret == {"result": True, "comment": "Created stack 'mystack'."}
  105. def test_heat_create_stack_environment(self):
  106. """
  107. Test salt.modules.heat.create_stack method with environment set
  108. """
  109. patch_file = patch.dict(
  110. "salt.modules.heat.__salt__",
  111. {
  112. "file.get_managed": file_.get_managed,
  113. "file.manage_file": file_.manage_file,
  114. },
  115. )
  116. with patch_file, self.patch_check:
  117. ret = heat.create_stack(
  118. name="mystack",
  119. profile="openstack1",
  120. environment=os.path.join(
  121. RUNTIME_VARS.BASE_FILES, "templates", "heat-env.yml"
  122. ),
  123. template_file=os.path.join(
  124. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  125. ),
  126. )
  127. assert ret == {"result": True, "comment": "Created stack 'mystack'."}
  128. def test_heat_create_stack_environment_err(self):
  129. """
  130. Test salt.modules.heat.create_stack method with environment set
  131. and there is an error reading the environment file
  132. """
  133. patch_file = patch.dict(
  134. "salt.modules.heat.__salt__",
  135. {
  136. "file.get_managed": file_.get_managed,
  137. "file.manage_file": MagicMock(
  138. side_effect=[{"result": True}, {"result": False}]
  139. ),
  140. },
  141. )
  142. patch_template = patch(
  143. "salt.modules.heat._parse_template", MagicMock(return_value=True)
  144. )
  145. env_file = os.path.join(RUNTIME_VARS.BASE_FILES, "templates", "heat-env.yml")
  146. with patch_file, patch_template, self.patch_check:
  147. ret = heat.create_stack(
  148. name="mystack",
  149. profile="openstack1",
  150. environment=env_file,
  151. template_file=os.path.join(
  152. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  153. ),
  154. )
  155. assert ret == {
  156. "result": False,
  157. "comment": "Can not open environment: {0}, ".format(env_file),
  158. }
  159. def test_heat_update_stack(self):
  160. """
  161. Test salt.modules.heat.update_method method
  162. """
  163. patch_file = patch.dict(
  164. heat.__salt__,
  165. {
  166. "file.get_managed": file_.get_managed,
  167. "file.manage_file": file_.manage_file,
  168. },
  169. )
  170. with patch_file, self.patch_check:
  171. ret = heat.update_stack(
  172. name="mystack",
  173. profile="openstack1",
  174. template_file=os.path.join(
  175. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  176. ),
  177. )
  178. assert ret == {"result": True, "comment": ("Updated stack 'mystack'.",)}
  179. def test_heat_update_stack_env(self):
  180. """
  181. Test salt.modules.heat.update_method method
  182. with environment set
  183. """
  184. patch_file = patch.dict(
  185. heat.__salt__,
  186. {
  187. "file.get_managed": file_.get_managed,
  188. "file.manage_file": file_.manage_file,
  189. },
  190. )
  191. with patch_file, self.patch_check:
  192. ret = heat.update_stack(
  193. name="mystack",
  194. profile="openstack1",
  195. template_file=os.path.join(
  196. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  197. ),
  198. environment=os.path.join(
  199. RUNTIME_VARS.BASE_FILES, "templates", "heat-env.yml"
  200. ),
  201. )
  202. assert ret == {"result": True, "comment": ("Updated stack 'mystack'.",)}
  203. def test_heat_update_stack_env_err(self):
  204. """
  205. Test salt.modules.heat.update_method method
  206. with environment set and there is an error
  207. reading the environment file
  208. """
  209. patch_file = patch.dict(
  210. heat.__salt__,
  211. {
  212. "file.get_managed": file_.get_managed,
  213. "file.manage_file": MagicMock(
  214. side_effect=[{"result": True}, {"result": False}]
  215. ),
  216. },
  217. )
  218. with patch_file, self.patch_check:
  219. ret = heat.update_stack(
  220. name="mystack",
  221. profile="openstack1",
  222. template_file=os.path.join(
  223. RUNTIME_VARS.BASE_FILES, "templates", "heat-template.yml"
  224. ),
  225. environment=os.path.join(
  226. RUNTIME_VARS.BASE_FILES, "templates", "heat-env.yml"
  227. ),
  228. )
  229. assert ret == {
  230. "result": False,
  231. "comment": "Error parsing template Template format version not found.",
  232. }