1
0

test_rvm.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # -*- coding: utf-8 -*-
  2. # Import Python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Import Salt Testing libs
  5. from tests.support.mixins import LoaderModuleMockMixin
  6. from tests.support.unit import TestCase
  7. from tests.support.mock import MagicMock, patch, call
  8. # Import salt libs
  9. import salt.modules.rvm as rvm
  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 = 'curl -Ls https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer ' \
  47. '| bash -s stable'
  48. mock.assert_called_once_with(curl_cmd, runas=None, python_shell=True)
  49. def test_install_ruby_nonroot(self):
  50. mock = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout'})
  51. expected = [
  52. call(
  53. ['/usr/local/rvm/bin/rvm', 'autolibs', 'disable', '2.0.0'],
  54. runas='rvm',
  55. cwd=None,
  56. python_shell=False,
  57. env=None
  58. ),
  59. call(
  60. ['/usr/local/rvm/bin/rvm', 'install', '2.0.0',
  61. '--disable-binary'],
  62. runas='rvm',
  63. cwd=None,
  64. python_shell=False,
  65. env=None
  66. )
  67. ]
  68. with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
  69. rvm.install_ruby('2.0.0', runas='rvm')
  70. self.assertEqual(mock.call_args_list, expected)
  71. def test_install_with_env(self):
  72. mock = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout'})
  73. expected = [
  74. call(
  75. ['/usr/local/rvm/bin/rvm', 'install', '2.0.0'],
  76. runas=None,
  77. cwd=None,
  78. python_shell=False,
  79. env=[{'RUBY_CONFIGURE_OPTS': '--foobar'}]
  80. )
  81. ]
  82. with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
  83. rvm.install_ruby('2.0.0', env=[{'RUBY_CONFIGURE_OPTS': '--foobar'}])
  84. self.assertEqual(mock.call_args_list, expected)
  85. def test_install_with_opts(self):
  86. mock = MagicMock(return_value={'retcode': 0, 'stdout': 'stdout'})
  87. expected = [
  88. call(
  89. ['/usr/local/rvm/bin/rvm', 'install', '2.0.0',
  90. '-C --enable-shared,--with-readline-dir=$HOME/.rvm/usr',
  91. '--patch /path/to/awesome.patch'],
  92. runas=None,
  93. cwd=None,
  94. python_shell=False,
  95. env=None
  96. )
  97. ]
  98. with patch.dict(rvm.__salt__, {'cmd.run_all': mock}):
  99. rvm.install_ruby('2.0.0', opts=[
  100. '-C --enable-shared,--with-readline-dir=$HOME/.rvm/usr',
  101. '--patch /path/to/awesome.patch'
  102. ])
  103. self.assertEqual(mock.call_args_list, expected)
  104. def test_list(self):
  105. list_output = '''
  106. rvm rubies
  107. jruby-1.6.5.1 [ amd64 ]
  108. ree-1.8.7-2011.03 [ x86_64 ]
  109. ree-1.8.7-2011.12 [ x86_64 ]
  110. =* ree-1.8.7-2012.02 [ x86_64 ]
  111. ruby-1.9.2-p180 [ x86_64 ]
  112. ruby-1.9.3-p125 [ x86_64 ]
  113. ruby-head [ x86_64 ]
  114. # => - current
  115. # =* - current && default
  116. # * - default
  117. '''
  118. with patch.object(rvm, '_rvm') as mock_method:
  119. mock_method.return_value = list_output
  120. self.assertEqual(
  121. [['jruby', '1.6.5.1', False],
  122. ['ree', '1.8.7-2011.03', False],
  123. ['ree', '1.8.7-2011.12', False],
  124. ['ree', '1.8.7-2012.02', True],
  125. ['ruby', '1.9.2-p180', False],
  126. ['ruby', '1.9.3-p125', False],
  127. ['ruby', 'head', False]],
  128. rvm.list_())
  129. def test_gemset_list(self):
  130. output = '''
  131. gemsets for ree-1.8.7-2012.02 (found in /usr/local/rvm/gems/ree-1.8.7-2012.02)
  132. global
  133. bar
  134. foo
  135. '''
  136. with patch.object(rvm, '_rvm_do') as mock_method:
  137. mock_method.return_value = output
  138. self.assertEqual(
  139. ['global', 'bar', 'foo'],
  140. rvm.gemset_list())
  141. def test_gemset_list_all(self):
  142. output = '''
  143. gemsets for ruby-1.9.3-p125 (found in /usr/local/rvm/gems/ruby-1.9.3-p125)
  144. 9bar
  145. 9foo
  146. global
  147. gemsets for ruby-head (found in /usr/local/rvm/gems/ruby-head)
  148. global
  149. headbar
  150. headfoo
  151. gemsets for jruby-1.6.5.1 (found in /usr/local/rvm/gems/jruby-1.6.5.1)
  152. global
  153. jbar
  154. jfoo
  155. gemsets for ruby-1.9.2-p180 (found in /usr/local/rvm/gems/ruby-1.9.2-p180)
  156. global
  157. '''
  158. with patch.object(rvm, '_rvm_do') as mock_method:
  159. mock_method.return_value = output
  160. self.assertEqual(
  161. {'jruby-1.6.5.1': ['global', 'jbar', 'jfoo'],
  162. 'ruby-1.9.2-p180': ['global'],
  163. 'ruby-1.9.3-p125': ['9bar', '9foo', 'global'],
  164. 'ruby-head': ['global', 'headbar', 'headfoo']},
  165. rvm.gemset_list_all())