test_gem.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import salt libs
  5. import salt.modules.gem as gem
  6. # Import Salt Testing libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, patch
  9. from tests.support.unit import TestCase
  10. class TestGemModule(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {gem: {}}
  13. def test_gem(self):
  14. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  15. with patch.dict(
  16. gem.__salt__,
  17. {
  18. "rvm.is_installed": MagicMock(return_value=False),
  19. "rbenv.is_installed": MagicMock(return_value=False),
  20. "cmd.run_all": mock,
  21. },
  22. ):
  23. gem._gem(["install", "rails"])
  24. mock.assert_called_once_with(
  25. ["gem", "install", "rails"], runas=None, python_shell=False
  26. )
  27. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  28. rvm_mock = MagicMock()
  29. with patch.dict(
  30. gem.__salt__,
  31. {
  32. "rvm.is_installed": rvm_mock,
  33. "rbenv.is_installed": rvm_mock,
  34. "cmd.run_all": mock,
  35. },
  36. ):
  37. gem._gem(["install", "rails"], gem_bin="/usr/local/bin/gem")
  38. self.assertEqual(
  39. False,
  40. rvm_mock.called,
  41. "Should never call rvm.is_installed if gem_bin provided",
  42. )
  43. mock.assert_called_once_with(
  44. ["/usr/local/bin/gem", "install", "rails"],
  45. runas=None,
  46. python_shell=False,
  47. )
  48. mock = MagicMock(return_value=None)
  49. with patch.dict(
  50. gem.__salt__,
  51. {
  52. "rvm.is_installed": MagicMock(return_value=True),
  53. "rbenv.is_installed": MagicMock(return_value=False),
  54. "rvm.do": mock,
  55. },
  56. ):
  57. gem._gem(["install", "rails"], ruby="1.9.3")
  58. mock.assert_called_once_with(
  59. "1.9.3", ["gem", "install", "rails"], runas=None
  60. )
  61. mock = MagicMock(return_value=None)
  62. with patch.dict(
  63. gem.__salt__,
  64. {
  65. "rvm.is_installed": MagicMock(return_value=False),
  66. "rbenv.is_installed": MagicMock(return_value=True),
  67. "rbenv.do": mock,
  68. },
  69. ), patch("salt.utils.platform.is_windows", return_value=False):
  70. gem._gem(["install", "rails"])
  71. mock.assert_called_once_with(["gem", "install", "rails"], runas=None)
  72. def test_install_pre_rubygems_3(self):
  73. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  74. with patch.dict(
  75. gem.__salt__,
  76. {
  77. "rvm.is_installed": MagicMock(return_value=False),
  78. "rbenv.is_installed": MagicMock(return_value=False),
  79. "cmd.run_all": mock,
  80. },
  81. ), patch.object(gem, "_has_rubygems_3", MagicMock(return_value=True)):
  82. gem.install("rails", pre_releases=True)
  83. mock.assert_called_once_with(
  84. ["gem", "install", "rails", "--no-document", "--prerelease"],
  85. runas=None,
  86. python_shell=False,
  87. )
  88. def test_install_pre(self):
  89. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  90. with patch.dict(
  91. gem.__salt__,
  92. {
  93. "rvm.is_installed": MagicMock(return_value=False),
  94. "rbenv.is_installed": MagicMock(return_value=False),
  95. "cmd.run_all": mock,
  96. },
  97. ), patch.object(gem, "_has_rubygems_3", MagicMock(return_value=False)):
  98. gem.install("rails", pre_releases=True)
  99. mock.assert_called_once_with(
  100. ["gem", "install", "rails", "--no-rdoc", "--no-ri", "--pre"],
  101. runas=None,
  102. python_shell=False,
  103. )
  104. def test_list(self):
  105. output = """
  106. actionmailer (2.3.14)
  107. actionpack (2.3.14)
  108. activerecord (2.3.14)
  109. activeresource (2.3.14)
  110. activesupport (3.0.5, 2.3.14)
  111. rake (0.9.2, 0.8.7)
  112. responds_to_parent (1.0.20091013)
  113. sass (3.1.15, 3.1.7)
  114. """
  115. mock = MagicMock(return_value=output)
  116. with patch.object(gem, "_gem", new=mock):
  117. self.assertEqual(
  118. {
  119. "actionmailer": ["2.3.14"],
  120. "actionpack": ["2.3.14"],
  121. "activerecord": ["2.3.14"],
  122. "activeresource": ["2.3.14"],
  123. "activesupport": ["3.0.5", "2.3.14"],
  124. "rake": ["0.9.2", "0.8.7"],
  125. "responds_to_parent": ["1.0.20091013"],
  126. "sass": ["3.1.15", "3.1.7"],
  127. },
  128. gem.list_(),
  129. )
  130. def test_list_upgrades(self):
  131. output = """
  132. arel (5.0.1.20140414130214 < 6.0.0)
  133. rails (4.1.9 < 4.2.0)
  134. rake (10.3.2 < 10.4.2)
  135. """
  136. mock = MagicMock(return_value=output)
  137. with patch.object(gem, "_gem", new=mock):
  138. self.assertEqual(
  139. {"arel": "6.0.0", "rails": "4.2.0", "rake": "10.4.2"},
  140. gem.list_upgrades(),
  141. )
  142. def test_sources_list(self):
  143. output = """*** CURRENT SOURCES ***
  144. http://rubygems.org/
  145. """
  146. mock = MagicMock(return_value=output)
  147. with patch.object(gem, "_gem", new=mock):
  148. self.assertEqual(["http://rubygems.org/"], gem.sources_list())