-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #62772 fix kmod.* functions hard code relative command name
- Loading branch information
1 parent
68bc569
commit 12e0c87
Showing
4 changed files
with
156 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix kmod.* functions hard code relative command name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
import salt.modules.kmod as kmod | ||
from salt.exceptions import CommandExecutionError | ||
from tests.support.mock import MagicMock, patch | ||
from tests.support.unit import skipIf | ||
|
||
|
||
@pytest.fixture | ||
def configure_loader_modules(): | ||
return {kmod: {}} | ||
|
||
|
||
def test_available(): | ||
""" | ||
Tests return a list of all available kernel modules | ||
""" | ||
with patch("salt.modules.kmod.available", MagicMock(return_value=["kvm"])): | ||
assert ["kvm"] == kmod.available() | ||
|
||
|
||
def test_check_available(): | ||
""" | ||
Tests if the specified kernel module is available | ||
""" | ||
with patch("salt.modules.kmod.available", MagicMock(return_value=["kvm"])): | ||
assert kmod.check_available("kvm") is True | ||
|
||
|
||
def test_lsmod(): | ||
""" | ||
Tests return information about currently loaded modules | ||
""" | ||
ret_str = """Module Size Used by | ||
kvm_intel 233472 0 | ||
""" | ||
expected = [{"size": "233472", "module": "kvm_intel", "depcount": "0", "deps": []}] | ||
mock_cmd = MagicMock(return_value=ret_str) | ||
with patch( | ||
"salt.utils.path.which", MagicMock(side_effect=[None, "/sbin/lsmod"]) | ||
), patch.dict(kmod.__salt__, {"cmd.run": mock_cmd}): | ||
with pytest.raises(CommandExecutionError): | ||
kmod.lsmod() | ||
assert expected == kmod.lsmod() | ||
|
||
|
||
@skipIf(not os.path.isfile("/etc/modules"), "/etc/modules not present") | ||
def test_mod_list(): | ||
""" | ||
Tests return a list of the loaded module names | ||
""" | ||
with patch( | ||
"salt.modules.kmod._get_modules_conf", | ||
MagicMock(return_value="/etc/modules"), | ||
): | ||
with patch( | ||
"salt.modules.kmod._strip_module_name", MagicMock(return_value="lp") | ||
): | ||
assert ["lp"] == kmod.mod_list(True) | ||
|
||
mock_ret = [{"size": 100, "module": None, "depcount": 10, "deps": None}] | ||
with patch("salt.modules.kmod.lsmod", MagicMock(return_value=mock_ret)): | ||
assert [None] == kmod.mod_list(False) | ||
|
||
|
||
def test_load(): | ||
""" | ||
Tests to loads specified kernel module. | ||
""" | ||
mod = "cheese" | ||
err_msg = "Module too moldy, refusing to load" | ||
mock_persist = MagicMock(return_value={mod}) | ||
mock_lsmod = MagicMock( | ||
return_value=[{"size": 100, "module": None, "depcount": 10, "deps": None}] | ||
) | ||
mock_run_all_0 = MagicMock(return_value={"retcode": 0}) | ||
mock_run_all_1 = MagicMock(return_value={"retcode": 1, "stderr": err_msg}) | ||
|
||
with patch("salt.modules.kmod._set_persistent_module", mock_persist): | ||
with patch( | ||
"salt.utils.path.which", | ||
MagicMock(side_effect=[None, "/sbin/modprobe", "/sbin/modprobe"]), | ||
), patch("salt.modules.kmod.lsmod", mock_lsmod): | ||
with patch.dict( | ||
kmod.__salt__, {"cmd.run_all": mock_run_all_0} | ||
), pytest.raises(CommandExecutionError): | ||
kmod.load(mod, True) | ||
|
||
with patch.dict(kmod.__salt__, {"cmd.run_all": mock_run_all_0}): | ||
assert [mod] == kmod.load(mod, True) | ||
|
||
with patch.dict(kmod.__salt__, {"cmd.run_all": mock_run_all_1}): | ||
assert "Error loading module {}: {}".format(mod, err_msg) == kmod.load( | ||
mod | ||
) | ||
|
||
|
||
def test_is_loaded(): | ||
""" | ||
Tests if specified kernel module is loaded. | ||
""" | ||
with patch("salt.modules.kmod.mod_list", MagicMock(return_value={"lp"})): | ||
assert kmod.is_loaded("lp") is True | ||
|
||
|
||
def test_remove(): | ||
""" | ||
Tests to remove the specified kernel module | ||
""" | ||
mod = "cheese" | ||
err_msg = "Cannot find module: it has been eaten" | ||
mock_persist = MagicMock(return_value={mod}) | ||
mock_lsmod = MagicMock( | ||
return_value=[{"size": 100, "module": None, "depcount": 10, "deps": None}] | ||
) | ||
mock_run_all_0 = MagicMock(return_value={"retcode": 0}) | ||
mock_run_all_1 = MagicMock(return_value={"retcode": 1, "stderr": err_msg}) | ||
|
||
with patch("salt.modules.kmod._remove_persistent_module", mock_persist): | ||
with patch( | ||
"salt.utils.path.which", | ||
MagicMock(side_effect=[None, "/sbin/rmmod", "/sbin/rmmod", "/sbin/rmmod"]), | ||
), patch("salt.modules.kmod.lsmod", mock_lsmod): | ||
with patch.dict(kmod.__salt__, {"cmd.run_all": mock_run_all_0}): | ||
with pytest.raises(CommandExecutionError): | ||
kmod.remove(mod) | ||
|
||
assert [mod] == kmod.remove(mod, True) | ||
|
||
assert [] == kmod.remove(mod) | ||
|
||
with patch.dict(kmod.__salt__, {"cmd.run_all": mock_run_all_1}): | ||
assert "Error removing module {}: {}".format( | ||
mod, err_msg | ||
) == kmod.remove(mod, True) |
This file was deleted.
Oops, something went wrong.