test_rvm.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, unicode_literals, print_function
  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
  8. # Import salt libs
  9. import salt.states.rvm as rvm
  10. # Import 3rd-party libs
  11. from salt.ext import six
  12. class TestRvmState(TestCase, LoaderModuleMockMixin):
  13. def setup_loader_modules(self):
  14. return {
  15. rvm: {
  16. '__opts__': {'test': False},
  17. '__salt__': {
  18. 'cmd.has_exec': MagicMock(return_value=True),
  19. 'config.option': MagicMock(return_value=None)
  20. }
  21. }
  22. }
  23. def test__check_rvm(self):
  24. mock = MagicMock(return_value=True)
  25. with patch.dict(
  26. rvm.__salt__,
  27. {'rvm.is_installed': MagicMock(return_value=False),
  28. 'rvm.install': mock}):
  29. rvm._check_rvm({'changes': {}})
  30. # rvm.install is not run anymore while checking rvm.is_installed
  31. self.assertEqual(mock.call_count, 0)
  32. def test__check_and_install_ruby(self):
  33. mock_check_rvm = MagicMock(
  34. return_value={'changes': {}, 'result': True})
  35. mock_check_ruby = MagicMock(
  36. return_value={'changes': {}, 'result': False})
  37. mock_install_ruby = MagicMock(return_value='')
  38. with patch.object(rvm, '_check_rvm', new=mock_check_rvm):
  39. with patch.object(rvm, '_check_ruby', new=mock_check_ruby):
  40. with patch.dict(rvm.__salt__,
  41. {'rvm.install_ruby': mock_install_ruby}):
  42. rvm._check_and_install_ruby({'changes': {}}, '1.9.3')
  43. mock_install_ruby.assert_called_once_with('1.9.3', runas=None, opts=None, env=None)
  44. def test__check_ruby(self):
  45. mock = MagicMock(return_value=[['ruby', '1.9.3-p125', False],
  46. ['jruby', '1.6.5.1', True]])
  47. with patch.dict(rvm.__salt__, {'rvm.list': mock}):
  48. for ruby, result in six.iteritems({'1.9.3': True,
  49. 'ruby-1.9.3': True,
  50. 'ruby-1.9.3-p125': True,
  51. '1.9.3-p125': True,
  52. '1.9.3-p126': False,
  53. 'rbx': False,
  54. 'jruby': True,
  55. 'jruby-1.6.5.1': True,
  56. 'jruby-1.6': False,
  57. 'jruby-1.9.3': False,
  58. 'jruby-1.9.3-p125': False}):
  59. ret = rvm._check_ruby({'changes': {}, 'result': False}, ruby)
  60. self.assertEqual(result, ret['result'])
  61. def test_gemset_present(self):
  62. with patch.object(rvm, '_check_rvm') as mock_method:
  63. mock_method.return_value = {'result': True, 'changes': {}}
  64. gems = ['global', 'foo', 'bar']
  65. gemset_list = MagicMock(return_value=gems)
  66. gemset_create = MagicMock(return_value=True)
  67. check_ruby = MagicMock(
  68. return_value={'result': False, 'changes': {}})
  69. with patch.object(rvm, '_check_ruby', new=check_ruby):
  70. with patch.dict(rvm.__salt__,
  71. {'rvm.gemset_list': gemset_list,
  72. 'rvm.gemset_create': gemset_create}):
  73. ret = rvm.gemset_present('foo')
  74. self.assertEqual(True, ret['result'])
  75. ret = rvm.gemset_present('quux')
  76. self.assertEqual(True, ret['result'])
  77. gemset_create.assert_called_once_with(
  78. 'default', 'quux', runas=None)
  79. def test_installed(self):
  80. mock = MagicMock()
  81. with patch.object(rvm, '_check_rvm') as mock_method:
  82. mock_method.return_value = {'result': True}
  83. with patch.object(rvm, '_check_and_install_ruby', new=mock):
  84. rvm.installed('1.9.3', default=True)
  85. mock.assert_called_once_with(
  86. {'result': True}, '1.9.3', True, user=None, opts=None, env=None)
  87. def test_installed_with_env(self):
  88. mock = MagicMock()
  89. with patch.object(rvm, '_check_rvm') as mock_method:
  90. mock_method.return_value = {'result': True}
  91. with patch.object(rvm, '_check_and_install_ruby', new=mock):
  92. rvm.installed('1.9.3', default=True, env=[{'RUBY_CONFIGURE_OPTS': '--foobar'}])
  93. mock.assert_called_once_with(
  94. {'result': True}, '1.9.3', True, user=None, opts=None, env=[{'RUBY_CONFIGURE_OPTS': '--foobar'}])
  95. def test_installed_with_opts(self):
  96. mock = MagicMock()
  97. with patch.object(rvm, '_check_rvm') as mock_method:
  98. mock_method.return_value = {'result': True}
  99. with patch.object(rvm, '_check_and_install_ruby', new=mock):
  100. rvm.installed('1.9.3', default=True, opts=[{'-C': '--enable-shared,--with-readline-dir=$HOME/.rvm/usr'}])
  101. mock.assert_called_once_with(
  102. {'result': True}, '1.9.3', True, user=None, opts=[{'-C': '--enable-shared,--with-readline-dir=$HOME/.rvm/usr'}], env=None)