1
0

test_alternatives.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, print_function, unicode_literals
  3. import pytest
  4. import salt.modules.alternatives as alternatives
  5. from tests.support.helpers import TstSuiteLoggingHandler
  6. from tests.support.mock import MagicMock, patch
  7. @pytest.fixture(autouse=True)
  8. def setup_loader(request):
  9. setup_loader_modules = {alternatives: {}}
  10. with pytest.helpers.loader_mock(request, setup_loader_modules) as loader_mock:
  11. yield loader_mock
  12. def test_display():
  13. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  14. mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
  15. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  16. solution = alternatives.display("better-world")
  17. assert "salt" == solution
  18. mock.assert_called_once_with(
  19. ["alternatives", "--display", "better-world"],
  20. python_shell=False,
  21. ignore_retcode=True,
  22. )
  23. with patch.dict(alternatives.__grains__, {"os_family": "Suse"}):
  24. mock = MagicMock(return_value={"retcode": 0, "stdout": "undoubtedly-salt"})
  25. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  26. solution = alternatives.display("better-world")
  27. assert "undoubtedly-salt" == solution
  28. mock.assert_called_once_with(
  29. ["update-alternatives", "--display", "better-world"],
  30. python_shell=False,
  31. ignore_retcode=True,
  32. )
  33. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  34. mock = MagicMock(
  35. return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
  36. )
  37. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  38. solution = alternatives.display("better-world")
  39. assert "salt-err" == solution
  40. mock.assert_called_once_with(
  41. ["alternatives", "--display", "better-world"],
  42. python_shell=False,
  43. ignore_retcode=True,
  44. )
  45. def test_show_current():
  46. mock = MagicMock(return_value="/etc/alternatives/salt")
  47. with patch("salt.utils.path.readlink", mock):
  48. ret = alternatives.show_current("better-world")
  49. assert "/etc/alternatives/salt" == ret
  50. mock.assert_called_once_with("/etc/alternatives/better-world")
  51. with TstSuiteLoggingHandler() as handler:
  52. mock.side_effect = OSError("Hell was not found!!!")
  53. assert not alternatives.show_current("hell")
  54. mock.assert_called_with("/etc/alternatives/hell")
  55. assert "ERROR:alternative: hell does not exist" in handler.messages
  56. def test_check_installed():
  57. mock = MagicMock(return_value="/etc/alternatives/salt")
  58. with patch("salt.utils.path.readlink", mock):
  59. assert alternatives.check_installed("better-world", "/etc/alternatives/salt")
  60. mock.return_value = False
  61. assert not alternatives.check_installed("help", "/etc/alternatives/salt")
  62. def test_install():
  63. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  64. mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
  65. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  66. solution = alternatives.install(
  67. "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
  68. )
  69. assert "salt" == solution
  70. mock.assert_called_once_with(
  71. [
  72. "alternatives",
  73. "--install",
  74. "/usr/bin/better-world",
  75. "better-world",
  76. "/usr/bin/salt",
  77. "100",
  78. ],
  79. python_shell=False,
  80. )
  81. with patch.dict(alternatives.__grains__, {"os_family": "Debian"}):
  82. mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
  83. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  84. solution = alternatives.install(
  85. "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
  86. )
  87. assert "salt" == solution
  88. mock.assert_called_once_with(
  89. [
  90. "update-alternatives",
  91. "--install",
  92. "/usr/bin/better-world",
  93. "better-world",
  94. "/usr/bin/salt",
  95. "100",
  96. ],
  97. python_shell=False,
  98. )
  99. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  100. mock = MagicMock(
  101. return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
  102. )
  103. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  104. ret = alternatives.install(
  105. "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
  106. )
  107. assert "salt-err" == ret
  108. mock.assert_called_once_with(
  109. [
  110. "alternatives",
  111. "--install",
  112. "/usr/bin/better-world",
  113. "better-world",
  114. "/usr/bin/salt",
  115. "100",
  116. ],
  117. python_shell=False,
  118. )
  119. def test_remove():
  120. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  121. mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
  122. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  123. solution = alternatives.remove("better-world", "/usr/bin/better-world",)
  124. assert "salt" == solution
  125. mock.assert_called_once_with(
  126. ["alternatives", "--remove", "better-world", "/usr/bin/better-world"],
  127. python_shell=False,
  128. )
  129. with patch.dict(alternatives.__grains__, {"os_family": "Debian"}):
  130. mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
  131. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  132. solution = alternatives.remove("better-world", "/usr/bin/better-world",)
  133. assert "salt" == solution
  134. mock.assert_called_once_with(
  135. [
  136. "update-alternatives",
  137. "--remove",
  138. "better-world",
  139. "/usr/bin/better-world",
  140. ],
  141. python_shell=False,
  142. )
  143. with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
  144. mock = MagicMock(
  145. return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
  146. )
  147. with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
  148. solution = alternatives.remove("better-world", "/usr/bin/better-world",)
  149. assert "salt-err" == solution
  150. mock.assert_called_once_with(
  151. ["alternatives", "--remove", "better-world", "/usr/bin/better-world"],
  152. python_shell=False,
  153. )