test_composer.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rupesh Tare <rupesht@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.modules.composer as composer
  9. from salt.exceptions import (
  10. CommandExecutionError,
  11. CommandNotFoundError,
  12. SaltInvocationError,
  13. )
  14. # Import Salt Testing Libs
  15. from tests.support.mixins import LoaderModuleMockMixin
  16. from tests.support.mock import MagicMock, patch
  17. from tests.support.unit import TestCase
  18. class ComposerTestCase(TestCase, LoaderModuleMockMixin):
  19. """
  20. Test cases for salt.modules.composer
  21. """
  22. def setup_loader_modules(self):
  23. return {composer: {}}
  24. def test_install(self):
  25. """
  26. Test for Install composer dependencies for a directory.
  27. """
  28. # Test _valid_composer=False throws exception
  29. mock = MagicMock(return_value=False)
  30. with patch.object(composer, "_valid_composer", mock):
  31. self.assertRaises(CommandNotFoundError, composer.install, "d")
  32. # Test no directory specified throws exception
  33. mock = MagicMock(return_value=True)
  34. with patch.object(composer, "_valid_composer", mock):
  35. self.assertRaises(SaltInvocationError, composer.install, None)
  36. # Test `composer install` exit status != 0 throws exception
  37. mock = MagicMock(return_value=True)
  38. with patch.object(composer, "_valid_composer", mock):
  39. mock = MagicMock(return_value={"retcode": 1, "stderr": "A"})
  40. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  41. self.assertRaises(CommandExecutionError, composer.install, "d")
  42. # Test success with quiet=True returns True
  43. mock = MagicMock(return_value=True)
  44. with patch.object(composer, "_valid_composer", mock):
  45. mock = MagicMock(return_value={"retcode": 0, "stderr": "A"})
  46. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  47. self.assertTrue(
  48. composer.install(
  49. "dir",
  50. None,
  51. None,
  52. None,
  53. None,
  54. None,
  55. None,
  56. None,
  57. None,
  58. None,
  59. True,
  60. )
  61. )
  62. # Test success with quiet=False returns object
  63. mock = MagicMock(return_value=True)
  64. with patch.object(composer, "_valid_composer", mock):
  65. rval = {"retcode": 0, "stderr": "A", "stdout": "B"}
  66. mock = MagicMock(return_value=rval)
  67. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  68. self.assertEqual(composer.install("dir"), rval)
  69. def test_update(self):
  70. """
  71. Test for Update composer dependencies for a directory.
  72. """
  73. # Test _valid_composer=False throws exception
  74. mock = MagicMock(return_value=False)
  75. with patch.object(composer, "_valid_composer", mock):
  76. self.assertRaises(CommandNotFoundError, composer.update, "d")
  77. # Test no directory specified throws exception
  78. mock = MagicMock(return_value=True)
  79. with patch.object(composer, "_valid_composer", mock):
  80. mock = MagicMock(return_value=True)
  81. with patch.object(composer, "did_composer_install", mock):
  82. self.assertRaises(SaltInvocationError, composer.update, None)
  83. # Test update with error exit status throws exception
  84. mock = MagicMock(return_value=True)
  85. with patch.object(composer, "_valid_composer", mock):
  86. mock = MagicMock(return_value=True)
  87. with patch.object(composer, "did_composer_install", mock):
  88. mock = MagicMock(return_value={"retcode": 1, "stderr": "A"})
  89. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  90. self.assertRaises(CommandExecutionError, composer.update, "d")
  91. # Test update with existing vendor directory and quiet=True
  92. mock = MagicMock(return_value=True)
  93. with patch.object(composer, "_valid_composer", mock):
  94. mock = MagicMock(return_value=True)
  95. with patch.object(composer, "did_composer_install", mock):
  96. mock = MagicMock(return_value={"retcode": 0, "stderr": "A"})
  97. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  98. self.assertTrue(
  99. composer.update(
  100. "dir",
  101. None,
  102. None,
  103. None,
  104. None,
  105. None,
  106. None,
  107. None,
  108. None,
  109. None,
  110. True,
  111. )
  112. )
  113. # Test update with no vendor directory and quiet=True
  114. mock = MagicMock(return_value=True)
  115. with patch.object(composer, "_valid_composer", mock):
  116. mock = MagicMock(return_value=False)
  117. with patch.object(composer, "did_composer_install", mock):
  118. mock = MagicMock(return_value={"retcode": 0, "stderr": "A"})
  119. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  120. self.assertTrue(
  121. composer.update(
  122. "dir",
  123. None,
  124. None,
  125. None,
  126. None,
  127. None,
  128. None,
  129. None,
  130. None,
  131. None,
  132. True,
  133. )
  134. )
  135. # Test update with existing vendor directory
  136. mock = MagicMock(return_value=True)
  137. with patch.object(composer, "_valid_composer", mock):
  138. mock = MagicMock(return_value=True)
  139. with patch.object(composer, "did_composer_install", mock):
  140. rval = {"retcode": 0, "stderr": "A", "stdout": "B"}
  141. mock = MagicMock(return_value=rval)
  142. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  143. self.assertEqual(composer.update("dir"), rval)
  144. # Test update with no vendor directory
  145. mock = MagicMock(return_value=True)
  146. with patch.object(composer, "_valid_composer", mock):
  147. mock = MagicMock(return_value=False)
  148. with patch.object(composer, "did_composer_install", mock):
  149. rval = {"retcode": 0, "stderr": "A", "stdout": "B"}
  150. mock = MagicMock(return_value=rval)
  151. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  152. self.assertEqual(composer.update("dir"), rval)
  153. def test_selfupdate(self):
  154. """
  155. Test for Composer selfupdate
  156. """
  157. mock = MagicMock(return_value=False)
  158. with patch.object(composer, "_valid_composer", mock):
  159. self.assertRaises(CommandNotFoundError, composer.selfupdate)
  160. mock = MagicMock(return_value=True)
  161. with patch.object(composer, "_valid_composer", mock):
  162. mock = MagicMock(return_value={"retcode": 1, "stderr": "A"})
  163. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  164. self.assertRaises(CommandExecutionError, composer.selfupdate)
  165. mock = MagicMock(return_value=True)
  166. with patch.object(composer, "_valid_composer", mock):
  167. mock = MagicMock(return_value={"retcode": 0, "stderr": "A"})
  168. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  169. self.assertTrue(composer.selfupdate(quiet=True))
  170. mock = MagicMock(return_value=True)
  171. with patch.object(composer, "_valid_composer", mock):
  172. rval = {"retcode": 0, "stderr": "A", "stdout": "B"}
  173. mock = MagicMock(return_value=rval)
  174. with patch.dict(composer.__salt__, {"cmd.run_all": mock}):
  175. self.assertEqual(composer.selfupdate(), rval)