test_gem.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. # Import python libs
  3. from __future__ import absolute_import, print_function, unicode_literals
  4. # Late import so mock can do its job
  5. import salt.states.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 TestGemState(TestCase, LoaderModuleMockMixin):
  11. def setup_loader_modules(self):
  12. return {gem: {"__opts__": {"test": False}}}
  13. def test_installed(self):
  14. gems = {"foo": ["1.0"], "bar": ["2.0"]}
  15. gem_list = MagicMock(return_value=gems)
  16. gem_install_succeeds = MagicMock(return_value=True)
  17. gem_install_fails = MagicMock(return_value=False)
  18. with patch.dict(gem.__salt__, {"gem.list": gem_list}):
  19. with patch.dict(gem.__salt__, {"gem.install": gem_install_succeeds}):
  20. ret = gem.installed("foo")
  21. self.assertEqual(True, ret["result"])
  22. ret = gem.installed("quux")
  23. self.assertEqual(True, ret["result"])
  24. gem_install_succeeds.assert_called_once_with(
  25. "quux",
  26. pre_releases=False,
  27. ruby=None,
  28. runas=None,
  29. version=None,
  30. proxy=None,
  31. rdoc=False,
  32. source=None,
  33. ri=False,
  34. gem_bin=None,
  35. )
  36. with patch.dict(gem.__salt__, {"gem.install": gem_install_fails}):
  37. ret = gem.installed("quux")
  38. self.assertEqual(False, ret["result"])
  39. gem_install_fails.assert_called_once_with(
  40. "quux",
  41. pre_releases=False,
  42. ruby=None,
  43. runas=None,
  44. version=None,
  45. proxy=None,
  46. rdoc=False,
  47. source=None,
  48. ri=False,
  49. gem_bin=None,
  50. )
  51. def test_installed_version(self):
  52. gems = {"foo": ["1.0"], "bar": ["2.0"]}
  53. gem_list = MagicMock(return_value=gems)
  54. gem_install_succeeds = MagicMock(return_value=True)
  55. with patch.dict(gem.__salt__, {"gem.list": gem_list}):
  56. with patch.dict(gem.__salt__, {"gem.install": gem_install_succeeds}):
  57. ret = gem.installed("foo", version=">= 1.0")
  58. self.assertEqual(True, ret["result"])
  59. self.assertEqual(
  60. "Installed Gem meets version requirements.", ret["comment"]
  61. )
  62. def test_removed(self):
  63. gems = ["foo", "bar"]
  64. gem_list = MagicMock(return_value=gems)
  65. gem_uninstall_succeeds = MagicMock(return_value=True)
  66. gem_uninstall_fails = MagicMock(return_value=False)
  67. with patch.dict(gem.__salt__, {"gem.list": gem_list}):
  68. with patch.dict(gem.__salt__, {"gem.uninstall": gem_uninstall_succeeds}):
  69. ret = gem.removed("quux")
  70. self.assertEqual(True, ret["result"])
  71. ret = gem.removed("foo")
  72. self.assertEqual(True, ret["result"])
  73. gem_uninstall_succeeds.assert_called_once_with(
  74. "foo", None, runas=None, gem_bin=None
  75. )
  76. with patch.dict(gem.__salt__, {"gem.uninstall": gem_uninstall_fails}):
  77. ret = gem.removed("bar")
  78. self.assertEqual(False, ret["result"])
  79. gem_uninstall_fails.assert_called_once_with(
  80. "bar", None, runas=None, gem_bin=None
  81. )
  82. def test_sources_add(self):
  83. gem_sources = ["http://foo", "http://bar"]
  84. gem_sources_list = MagicMock(return_value=gem_sources)
  85. gem_sources_add_succeeds = MagicMock(return_value=True)
  86. gem_sources_add_fails = MagicMock(return_value=False)
  87. with patch.dict(gem.__salt__, {"gem.sources_list": gem_sources_list}):
  88. with patch.dict(
  89. gem.__salt__, {"gem.sources_add": gem_sources_add_succeeds}
  90. ):
  91. ret = gem.sources_add("http://foo")
  92. self.assertEqual(True, ret["result"])
  93. ret = gem.sources_add("http://fui")
  94. self.assertEqual(True, ret["result"])
  95. gem_sources_add_succeeds.assert_called_once_with(
  96. source_uri="http://fui", ruby=None, runas=None
  97. )
  98. with patch.dict(gem.__salt__, {"gem.sources_add": gem_sources_add_fails}):
  99. ret = gem.sources_add("http://fui")
  100. self.assertEqual(False, ret["result"])
  101. gem_sources_add_fails.assert_called_once_with(
  102. source_uri="http://fui", ruby=None, runas=None
  103. )
  104. def test_sources_remove(self):
  105. gem_sources = ["http://foo", "http://bar"]
  106. gem_sources_list = MagicMock(return_value=gem_sources)
  107. gem_sources_remove_succeeds = MagicMock(return_value=True)
  108. gem_sources_remove_fails = MagicMock(return_value=False)
  109. with patch.dict(gem.__salt__, {"gem.sources_list": gem_sources_list}):
  110. with patch.dict(
  111. gem.__salt__, {"gem.sources_remove": gem_sources_remove_succeeds}
  112. ):
  113. ret = gem.sources_remove("http://fui")
  114. self.assertEqual(True, ret["result"])
  115. ret = gem.sources_remove("http://foo")
  116. self.assertEqual(True, ret["result"])
  117. gem_sources_remove_succeeds.assert_called_once_with(
  118. source_uri="http://foo", ruby=None, runas=None
  119. )
  120. with patch.dict(
  121. gem.__salt__, {"gem.sources_remove": gem_sources_remove_fails}
  122. ):
  123. ret = gem.sources_remove("http://bar")
  124. self.assertEqual(False, ret["result"])
  125. gem_sources_remove_fails.assert_called_once_with(
  126. source_uri="http://bar", ruby=None, runas=None
  127. )