123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- import pytest
- import salt.modules.alternatives as alternatives
- from tests.support.helpers import TstSuiteLoggingHandler
- from tests.support.mock import MagicMock, patch
- @pytest.fixture(autouse=True)
- def setup_loader():
- setup_loader_modules = {alternatives: {}}
- with pytest.helpers.loader_mock(setup_loader_modules) as loader_mock:
- yield loader_mock
- def test_display():
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.display("better-world")
- assert "salt" == solution
- mock.assert_called_once_with(
- ["alternatives", "--display", "better-world"],
- python_shell=False,
- ignore_retcode=True,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "Suse"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "undoubtedly-salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.display("better-world")
- assert "undoubtedly-salt" == solution
- mock.assert_called_once_with(
- ["update-alternatives", "--display", "better-world"],
- python_shell=False,
- ignore_retcode=True,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(
- return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
- )
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.display("better-world")
- assert "salt-err" == solution
- mock.assert_called_once_with(
- ["alternatives", "--display", "better-world"],
- python_shell=False,
- ignore_retcode=True,
- )
- def test_show_current():
- mock = MagicMock(return_value="/etc/alternatives/salt")
- with patch("salt.utils.path.readlink", mock):
- ret = alternatives.show_current("better-world")
- assert "/etc/alternatives/salt" == ret
- mock.assert_called_once_with("/etc/alternatives/better-world")
- with TstSuiteLoggingHandler() as handler:
- mock.side_effect = OSError("Hell was not found!!!")
- assert not alternatives.show_current("hell")
- mock.assert_called_with("/etc/alternatives/hell")
- assert "ERROR:alternative: hell does not exist" in handler.messages
- def test_check_installed():
- mock = MagicMock(return_value="/etc/alternatives/salt")
- with patch("salt.utils.path.readlink", mock):
- assert alternatives.check_installed("better-world", "/etc/alternatives/salt")
- mock.return_value = False
- assert not alternatives.check_installed("help", "/etc/alternatives/salt")
- def test_install():
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.install(
- "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
- )
- assert "salt" == solution
- mock.assert_called_once_with(
- [
- "alternatives",
- "--install",
- "/usr/bin/better-world",
- "better-world",
- "/usr/bin/salt",
- "100",
- ],
- python_shell=False,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "Debian"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.install(
- "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
- )
- assert "salt" == solution
- mock.assert_called_once_with(
- [
- "update-alternatives",
- "--install",
- "/usr/bin/better-world",
- "better-world",
- "/usr/bin/salt",
- "100",
- ],
- python_shell=False,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(
- return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
- )
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- ret = alternatives.install(
- "better-world", "/usr/bin/better-world", "/usr/bin/salt", 100
- )
- assert "salt-err" == ret
- mock.assert_called_once_with(
- [
- "alternatives",
- "--install",
- "/usr/bin/better-world",
- "better-world",
- "/usr/bin/salt",
- "100",
- ],
- python_shell=False,
- )
- def test_remove():
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.remove("better-world", "/usr/bin/better-world",)
- assert "salt" == solution
- mock.assert_called_once_with(
- ["alternatives", "--remove", "better-world", "/usr/bin/better-world"],
- python_shell=False,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "Debian"}):
- mock = MagicMock(return_value={"retcode": 0, "stdout": "salt"})
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.remove("better-world", "/usr/bin/better-world",)
- assert "salt" == solution
- mock.assert_called_once_with(
- [
- "update-alternatives",
- "--remove",
- "better-world",
- "/usr/bin/better-world",
- ],
- python_shell=False,
- )
- with patch.dict(alternatives.__grains__, {"os_family": "RedHat"}):
- mock = MagicMock(
- return_value={"retcode": 1, "stdout": "salt-out", "stderr": "salt-err"}
- )
- with patch.dict(alternatives.__salt__, {"cmd.run_all": mock}):
- solution = alternatives.remove("better-world", "/usr/bin/better-world",)
- assert "salt-err" == solution
- mock.assert_called_once_with(
- ["alternatives", "--remove", "better-world", "/usr/bin/better-world"],
- python_shell=False,
- )
|