test_tomcat.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. """
  5. # Import Python Libs
  6. from __future__ import absolute_import
  7. # Import Salt Libs
  8. import salt.states.tomcat as tomcat
  9. from salt.modules import tomcat as tomcatmod
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class TomcatTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Validate the tomcat state
  17. """
  18. def setup_loader_modules(self):
  19. return {tomcat: {"__env__": "base"}}
  20. def test_war_deployed(self):
  21. """
  22. Test to enforce that the WAR will be deployed and
  23. started in the context path it will make use of WAR versions
  24. """
  25. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  26. mock_start = MagicMock(return_value="saltstack")
  27. mock_undeploy = MagicMock(side_effect=["FAIL", "saltstack"])
  28. mock_deploy = MagicMock(return_value="deploy")
  29. mock_ls = MagicMock(
  30. side_effect=[
  31. {"salt": {"version": "jenkins-1.20.4", "mode": "running"}},
  32. {"salt": {"version": "1"}},
  33. {"salt": {"version": "jenkins-1.2.4", "mode": "run"}},
  34. {"salt": {"version": "1"}},
  35. {"salt": {"version": "1"}},
  36. {"salt": {"version": "1"}},
  37. ]
  38. )
  39. with patch.dict(
  40. tomcat.__salt__,
  41. {
  42. "tomcat.ls": mock_ls,
  43. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  44. "tomcat.start": mock_start,
  45. "tomcat.undeploy": mock_undeploy,
  46. "tomcat.deploy_war": mock_deploy,
  47. },
  48. ):
  49. ret.update({"comment": "salt with version 1.20.4" " is already deployed"})
  50. self.assertDictEqual(
  51. tomcat.war_deployed("salt", "salt://jenkins" "-1.20.4.war"), ret
  52. )
  53. with patch.dict(tomcat.__opts__, {"test": True}):
  54. ret.update(
  55. {
  56. "changes": {
  57. "deploy": "will deploy salt" " with version 1.2.4",
  58. "undeploy": "undeployed salt" " with version 1",
  59. },
  60. "result": None,
  61. "comment": "",
  62. }
  63. )
  64. self.assertDictEqual(
  65. tomcat.war_deployed("salt", "salt://jenkins" "-1.2.4.war"), ret
  66. )
  67. with patch.dict(tomcat.__opts__, {"test": False}):
  68. ret.update(
  69. {
  70. "changes": {"start": "starting salt"},
  71. "comment": "saltstack",
  72. "result": False,
  73. }
  74. )
  75. self.assertDictEqual(
  76. tomcat.war_deployed("salt", "salt://jenkins" "-1.2.4.war"), ret
  77. )
  78. ret.update(
  79. {
  80. "changes": {
  81. "deploy": "will deploy salt with" " version 1.2.4",
  82. "undeploy": "undeployed salt with" " version 1",
  83. },
  84. "comment": "FAIL",
  85. }
  86. )
  87. self.assertDictEqual(
  88. tomcat.war_deployed("salt", "salt://jenkins" "-1.2.4.war"), ret
  89. )
  90. ret.update(
  91. {
  92. "changes": {"undeploy": "undeployed salt" " with version 1"},
  93. "comment": "deploy",
  94. }
  95. )
  96. self.assertDictEqual(
  97. tomcat.war_deployed("salt", "salt://jenkins" "-1.2.4.war"), ret
  98. )
  99. def test_war_deployed_no_version(self):
  100. """
  101. Tests that going from versions to no versions and back work, as well
  102. as not overwriting a WAR without version with another without version.
  103. """
  104. ret = {"name": "salt", "changes": {}, "result": None, "comment": ""}
  105. mock_deploy = MagicMock(return_value="deploy")
  106. mock_undeploy = MagicMock(return_value="SUCCESS")
  107. mock_ls_version = MagicMock(
  108. return_value={"salt": {"version": "1.2.4", "mode": "running"}}
  109. )
  110. mock_ls_no_version = MagicMock(
  111. return_value={"salt": {"version": "", "mode": "running"}}
  112. )
  113. # We're just checking what it *would* do.
  114. with patch.dict(tomcat.__opts__, {"test": True}):
  115. with patch.dict(
  116. tomcat.__salt__,
  117. {
  118. "tomcat.ls": mock_ls_version,
  119. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  120. "tomcat.deploy_war": mock_deploy,
  121. "tomcat.undeploy": mock_undeploy,
  122. },
  123. ):
  124. # We deploy from version to no version
  125. ret.update(
  126. {
  127. "changes": {
  128. "deploy": "will deploy salt with no version",
  129. "undeploy": "undeployed salt with version 1.2.4",
  130. },
  131. }
  132. )
  133. self.assertDictEqual(
  134. tomcat.war_deployed("salt", "salt://jenkins.war"), ret
  135. )
  136. with patch.dict(
  137. tomcat.__salt__,
  138. {
  139. "tomcat.ls": mock_ls_no_version,
  140. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  141. "tomcat.deploy_war": mock_deploy,
  142. "tomcat.undeploy": mock_undeploy,
  143. },
  144. ):
  145. # Deploy from none to specified version
  146. ret.update(
  147. {
  148. "changes": {
  149. "deploy": "will deploy salt with version 1.2.4",
  150. "undeploy": "undeployed salt with no version",
  151. },
  152. }
  153. )
  154. self.assertDictEqual(
  155. tomcat.war_deployed("salt", "salt://jenkins.war", version="1.2.4"),
  156. ret,
  157. )
  158. # Deploy from none to extracted version
  159. self.assertDictEqual(
  160. tomcat.war_deployed("salt", "salt://jenkins-1.2.4.war"), ret
  161. )
  162. # Don't deploy from no version to no version
  163. ret.update(
  164. {
  165. "changes": {},
  166. "comment": "salt with no version is already deployed",
  167. "result": True,
  168. }
  169. )
  170. # Don't deploy from blank to blank version
  171. self.assertDictEqual(
  172. tomcat.war_deployed("salt", "salt://jenkins.war"), ret
  173. )
  174. # Don't deploy from blank to false version
  175. self.assertDictEqual(
  176. tomcat.war_deployed(
  177. "salt", "salt://jenkins-1.2.4.war", version=False
  178. ),
  179. ret,
  180. )
  181. def test_wait(self):
  182. """
  183. Test to wait for the tomcat manager to load
  184. """
  185. ret = {
  186. "name": "salt",
  187. "changes": {},
  188. "result": True,
  189. "comment": "tomcat manager is ready",
  190. }
  191. mock = MagicMock(return_value=True)
  192. with patch.dict(
  193. tomcat.__salt__,
  194. {
  195. "tomcat.status": mock,
  196. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  197. },
  198. ):
  199. self.assertDictEqual(tomcat.wait("salt"), ret)
  200. def test_mod_watch(self):
  201. """
  202. Test to the tomcat watcher function.
  203. """
  204. ret = {"name": "salt", "changes": {}, "result": False, "comment": "True"}
  205. mock = MagicMock(return_value="True")
  206. with patch.dict(
  207. tomcat.__salt__,
  208. {
  209. "tomcat.reload": mock,
  210. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  211. },
  212. ):
  213. ret.update({"changes": {"salt": False}})
  214. self.assertDictEqual(tomcat.mod_watch("salt"), ret)
  215. def test_undeployed(self):
  216. """
  217. Test to enforce that the WAR will be un-deployed from the server
  218. """
  219. ret = {"name": "salt", "changes": {}, "result": False, "comment": "True"}
  220. mock = MagicMock(side_effect=[False, True, True, True, True])
  221. mock1 = MagicMock(
  222. side_effect=[
  223. {"salt": {"a": 1}},
  224. {"salt": {"version": 1}},
  225. {"salt": {"version": 1}},
  226. {"salt": {"version": 1}},
  227. ]
  228. )
  229. mock2 = MagicMock(side_effect=["FAIL", "saltstack"])
  230. with patch.dict(
  231. tomcat.__salt__,
  232. {
  233. "tomcat.status": mock,
  234. "tomcat.extract_war_version": tomcatmod.extract_war_version,
  235. "tomcat.ls": mock1,
  236. "tomcat.undeploy": mock2,
  237. },
  238. ):
  239. ret.update({"comment": "Tomcat Manager does not respond"})
  240. self.assertDictEqual(tomcat.undeployed("salt"), ret)
  241. ret.update({"comment": "", "result": True})
  242. self.assertDictEqual(tomcat.undeployed("salt"), ret)
  243. with patch.dict(tomcat.__opts__, {"test": True}):
  244. ret.update({"changes": {"undeploy": 1}, "result": None})
  245. self.assertDictEqual(tomcat.undeployed("salt"), ret)
  246. with patch.dict(tomcat.__opts__, {"test": False}):
  247. ret.update(
  248. {"changes": {"undeploy": 1}, "comment": "FAIL", "result": False}
  249. )
  250. self.assertDictEqual(tomcat.undeployed("salt"), ret)
  251. ret.update({"changes": {"undeploy": 1}, "comment": "", "result": True})
  252. self.assertDictEqual(tomcat.undeployed("salt"), ret)