test_rvm.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.rvm as rvm
  6. # Import Salt Testing libs
  7. from tests.support.mixins import LoaderModuleMockMixin
  8. from tests.support.mock import MagicMock, call, patch
  9. from tests.support.unit import TestCase
  10. class TestRvmModule(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {
  13. rvm: {
  14. "__salt__": {
  15. "cmd.has_exec": MagicMock(return_value=True),
  16. "config.option": MagicMock(return_value=None),
  17. }
  18. }
  19. }
  20. def test_rvm(self):
  21. mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
  22. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  23. rvm._rvm(["install", "1.9.3"])
  24. mock.assert_called_once_with(
  25. ["/usr/local/rvm/bin/rvm", "install", "1.9.3"],
  26. runas=None,
  27. cwd=None,
  28. python_shell=False,
  29. env=None,
  30. )
  31. def test_rvm_do(self):
  32. mock = MagicMock(return_value={"retcode": 0, "stdout": "stdout"})
  33. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  34. rvm._rvm_do("1.9.3", ["gemset", "list"])
  35. mock.assert_called_once_with(
  36. ["/usr/local/rvm/bin/rvm", "1.9.3", "do", "gemset", "list"],
  37. runas=None,
  38. cwd=None,
  39. python_shell=False,
  40. env=None,
  41. )
  42. def test_install(self):
  43. mock = MagicMock(return_value={"retcode": 0})
  44. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  45. rvm.install()
  46. curl_cmd = (
  47. "curl -Ls https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer "
  48. "| bash -s stable"
  49. )
  50. mock.assert_called_once_with(curl_cmd, runas=None, python_shell=True)
  51. def test_install_ruby_nonroot(self):
  52. mock = MagicMock(return_value={"retcode": 0, "stdout": "stdout"})
  53. expected = [
  54. call(
  55. ["/usr/local/rvm/bin/rvm", "autolibs", "disable", "2.0.0"],
  56. runas="rvm",
  57. cwd=None,
  58. python_shell=False,
  59. env=None,
  60. ),
  61. call(
  62. ["/usr/local/rvm/bin/rvm", "install", "2.0.0", "--disable-binary"],
  63. runas="rvm",
  64. cwd=None,
  65. python_shell=False,
  66. env=None,
  67. ),
  68. ]
  69. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  70. rvm.install_ruby("2.0.0", runas="rvm")
  71. self.assertEqual(mock.call_args_list, expected)
  72. def test_install_with_env(self):
  73. mock = MagicMock(return_value={"retcode": 0, "stdout": "stdout"})
  74. expected = [
  75. call(
  76. ["/usr/local/rvm/bin/rvm", "install", "2.0.0"],
  77. runas=None,
  78. cwd=None,
  79. python_shell=False,
  80. env=[{"RUBY_CONFIGURE_OPTS": "--foobar"}],
  81. )
  82. ]
  83. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  84. rvm.install_ruby("2.0.0", env=[{"RUBY_CONFIGURE_OPTS": "--foobar"}])
  85. self.assertEqual(mock.call_args_list, expected)
  86. def test_install_with_opts(self):
  87. mock = MagicMock(return_value={"retcode": 0, "stdout": "stdout"})
  88. expected = [
  89. call(
  90. [
  91. "/usr/local/rvm/bin/rvm",
  92. "install",
  93. "2.0.0",
  94. "-C --enable-shared,--with-readline-dir=$HOME/.rvm/usr",
  95. "--patch /path/to/awesome.patch",
  96. ],
  97. runas=None,
  98. cwd=None,
  99. python_shell=False,
  100. env=None,
  101. )
  102. ]
  103. with patch.dict(rvm.__salt__, {"cmd.run_all": mock}):
  104. rvm.install_ruby(
  105. "2.0.0",
  106. opts=[
  107. "-C --enable-shared,--with-readline-dir=$HOME/.rvm/usr",
  108. "--patch /path/to/awesome.patch",
  109. ],
  110. )
  111. self.assertEqual(mock.call_args_list, expected)
  112. def test_list(self):
  113. list_output = """
  114. rvm rubies
  115. jruby-1.6.5.1 [ amd64 ]
  116. ree-1.8.7-2011.03 [ x86_64 ]
  117. ree-1.8.7-2011.12 [ x86_64 ]
  118. =* ree-1.8.7-2012.02 [ x86_64 ]
  119. ruby-1.9.2-p180 [ x86_64 ]
  120. ruby-1.9.3-p125 [ x86_64 ]
  121. ruby-head [ x86_64 ]
  122. # => - current
  123. # =* - current && default
  124. # * - default
  125. """
  126. with patch.object(rvm, "_rvm") as mock_method:
  127. mock_method.return_value = list_output
  128. self.assertEqual(
  129. [
  130. ["jruby", "1.6.5.1", False],
  131. ["ree", "1.8.7-2011.03", False],
  132. ["ree", "1.8.7-2011.12", False],
  133. ["ree", "1.8.7-2012.02", True],
  134. ["ruby", "1.9.2-p180", False],
  135. ["ruby", "1.9.3-p125", False],
  136. ["ruby", "head", False],
  137. ],
  138. rvm.list_(),
  139. )
  140. def test_gemset_list(self):
  141. output = """
  142. gemsets for ree-1.8.7-2012.02 (found in /usr/local/rvm/gems/ree-1.8.7-2012.02)
  143. global
  144. bar
  145. foo
  146. """
  147. with patch.object(rvm, "_rvm_do") as mock_method:
  148. mock_method.return_value = output
  149. self.assertEqual(["global", "bar", "foo"], rvm.gemset_list())
  150. def test_gemset_list_all(self):
  151. output = """
  152. gemsets for ruby-1.9.3-p125 (found in /usr/local/rvm/gems/ruby-1.9.3-p125)
  153. 9bar
  154. 9foo
  155. global
  156. gemsets for ruby-head (found in /usr/local/rvm/gems/ruby-head)
  157. global
  158. headbar
  159. headfoo
  160. gemsets for jruby-1.6.5.1 (found in /usr/local/rvm/gems/jruby-1.6.5.1)
  161. global
  162. jbar
  163. jfoo
  164. gemsets for ruby-1.9.2-p180 (found in /usr/local/rvm/gems/ruby-1.9.2-p180)
  165. global
  166. """
  167. with patch.object(rvm, "_rvm_do") as mock_method:
  168. mock_method.return_value = output
  169. self.assertEqual(
  170. {
  171. "jruby-1.6.5.1": ["global", "jbar", "jfoo"],
  172. "ruby-1.9.2-p180": ["global"],
  173. "ruby-1.9.3-p125": ["9bar", "9foo", "global"],
  174. "ruby-head": ["global", "headbar", "headfoo"],
  175. },
  176. rvm.gemset_list_all(),
  177. )