test_alternatives.py 6.8 KB

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