test_rvm.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.states.rvm as rvm
  6. # Import 3rd-party libs
  7. from salt.ext import six
  8. # Import Salt Testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.unit import TestCase
  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), "rvm.install": mock},
  28. ):
  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(return_value={"changes": {}, "result": True})
  34. mock_check_ruby = MagicMock(return_value={"changes": {}, "result": False})
  35. mock_install_ruby = MagicMock(return_value="")
  36. with patch.object(rvm, "_check_rvm", new=mock_check_rvm):
  37. with patch.object(rvm, "_check_ruby", new=mock_check_ruby):
  38. with patch.dict(rvm.__salt__, {"rvm.install_ruby": mock_install_ruby}):
  39. rvm._check_and_install_ruby({"changes": {}}, "1.9.3")
  40. mock_install_ruby.assert_called_once_with(
  41. "1.9.3", runas=None, opts=None, env=None
  42. )
  43. def test__check_ruby(self):
  44. mock = MagicMock(
  45. return_value=[["ruby", "1.9.3-p125", False], ["jruby", "1.6.5.1", True]]
  46. )
  47. with patch.dict(rvm.__salt__, {"rvm.list": mock}):
  48. for ruby, result in six.iteritems(
  49. {
  50. "1.9.3": True,
  51. "ruby-1.9.3": True,
  52. "ruby-1.9.3-p125": True,
  53. "1.9.3-p125": True,
  54. "1.9.3-p126": False,
  55. "rbx": False,
  56. "jruby": True,
  57. "jruby-1.6.5.1": True,
  58. "jruby-1.6": False,
  59. "jruby-1.9.3": False,
  60. "jruby-1.9.3-p125": False,
  61. }
  62. ):
  63. ret = rvm._check_ruby({"changes": {}, "result": False}, ruby)
  64. self.assertEqual(result, ret["result"])
  65. def test_gemset_present(self):
  66. with patch.object(rvm, "_check_rvm") as mock_method:
  67. mock_method.return_value = {"result": True, "changes": {}}
  68. gems = ["global", "foo", "bar"]
  69. gemset_list = MagicMock(return_value=gems)
  70. gemset_create = MagicMock(return_value=True)
  71. check_ruby = MagicMock(return_value={"result": False, "changes": {}})
  72. with patch.object(rvm, "_check_ruby", new=check_ruby):
  73. with patch.dict(
  74. rvm.__salt__,
  75. {
  76. "rvm.gemset_list": gemset_list,
  77. "rvm.gemset_create": gemset_create,
  78. },
  79. ):
  80. ret = rvm.gemset_present("foo")
  81. self.assertEqual(True, ret["result"])
  82. ret = rvm.gemset_present("quux")
  83. self.assertEqual(True, ret["result"])
  84. gemset_create.assert_called_once_with("default", "quux", runas=None)
  85. def test_installed(self):
  86. mock = MagicMock()
  87. with patch.object(rvm, "_check_rvm") as mock_method:
  88. mock_method.return_value = {"result": True}
  89. with patch.object(rvm, "_check_and_install_ruby", new=mock):
  90. rvm.installed("1.9.3", default=True)
  91. mock.assert_called_once_with(
  92. {"result": True}, "1.9.3", True, user=None, opts=None, env=None
  93. )
  94. def test_installed_with_env(self):
  95. mock = MagicMock()
  96. with patch.object(rvm, "_check_rvm") as mock_method:
  97. mock_method.return_value = {"result": True}
  98. with patch.object(rvm, "_check_and_install_ruby", new=mock):
  99. rvm.installed(
  100. "1.9.3", default=True, env=[{"RUBY_CONFIGURE_OPTS": "--foobar"}]
  101. )
  102. mock.assert_called_once_with(
  103. {"result": True},
  104. "1.9.3",
  105. True,
  106. user=None,
  107. opts=None,
  108. env=[{"RUBY_CONFIGURE_OPTS": "--foobar"}],
  109. )
  110. def test_installed_with_opts(self):
  111. mock = MagicMock()
  112. with patch.object(rvm, "_check_rvm") as mock_method:
  113. mock_method.return_value = {"result": True}
  114. with patch.object(rvm, "_check_and_install_ruby", new=mock):
  115. rvm.installed(
  116. "1.9.3",
  117. default=True,
  118. opts=[{"-C": "--enable-shared,--with-readline-dir=$HOME/.rvm/usr"}],
  119. )
  120. mock.assert_called_once_with(
  121. {"result": True},
  122. "1.9.3",
  123. True,
  124. user=None,
  125. opts=[{"-C": "--enable-shared,--with-readline-dir=$HOME/.rvm/usr"}],
  126. env=None,
  127. )