mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Turn static test files to dynamic
This commit is contained in:
parent
f323dd5773
commit
7ab9b06b1b
4 changed files with 84 additions and 63 deletions
|
@ -1 +0,0 @@
|
|||
TEST_KEY1=VAL1 TEST_KEY2=VAL2 BOOTABLE_FLAG="€" TEST_KEY_NOVAL TEST_KEY3=3
|
Binary file not shown.
|
@ -1 +0,0 @@
|
|||
KEY1=VAL1 KEY2=VAL2 KEY2=VAL2
|
|
@ -11,6 +11,7 @@ import os
|
|||
import pathlib
|
||||
import platform
|
||||
import socket
|
||||
import tempfile
|
||||
import textwrap
|
||||
from collections import namedtuple
|
||||
|
||||
|
@ -2766,13 +2767,6 @@ def test_kernelparams_return_linux(cmdline, expectation):
|
|||
def test_kernelparams_return_linux_non_utf8():
|
||||
_salt_utils_files_fopen = salt.utils.files.fopen
|
||||
|
||||
def _open_mock(file_name, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
pathlib.Path(__file__).parent.joinpath("proc-files").joinpath("cmdline"),
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
expected = {
|
||||
"kernelparams": [
|
||||
("TEST_KEY1", "VAL1"),
|
||||
|
@ -2782,8 +2776,23 @@ def test_kernelparams_return_linux_non_utf8():
|
|||
("TEST_KEY3", "3"),
|
||||
]
|
||||
}
|
||||
with patch("salt.utils.files.fopen", _open_mock):
|
||||
assert core.kernelparams() == expected
|
||||
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
|
||||
def _open_mock(file_name, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
os.path.join(tempdir, "cmdline"), *args, **kwargs
|
||||
)
|
||||
|
||||
with salt.utils.files.fopen(
|
||||
os.path.join(tempdir, "cmdline"),
|
||||
"wb",
|
||||
) as cmdline_fh, patch("salt.utils.files.fopen", _open_mock):
|
||||
cmdline_fh.write(
|
||||
b'TEST_KEY1=VAL1 TEST_KEY2=VAL2 BOOTABLE_FLAG="\x80" TEST_KEY_NOVAL TEST_KEY3=3\n'
|
||||
)
|
||||
cmdline_fh.close()
|
||||
assert core.kernelparams() == expected
|
||||
|
||||
|
||||
def test_linux_gpus():
|
||||
|
@ -3045,68 +3054,82 @@ def test_linux_devicetree_data(test_input, expected):
|
|||
def test_linux_proc_files_with_non_utf8_chars():
|
||||
_salt_utils_files_fopen = salt.utils.files.fopen
|
||||
|
||||
def _mock_open(filename, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
pathlib.Path(__file__).parent.joinpath("proc-files").joinpath("cmdline-1"),
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
empty_mock = MagicMock(return_value={})
|
||||
|
||||
with patch("os.path.isfile", return_value=False), patch(
|
||||
"salt.utils.files.fopen", _mock_open
|
||||
), patch.dict(
|
||||
core.__salt__,
|
||||
{
|
||||
"cmd.retcode": salt.modules.cmdmod.retcode,
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
},
|
||||
), patch.object(
|
||||
core, "_linux_bin_exists", return_value=False
|
||||
), patch.object(
|
||||
core, "_parse_lsb_release", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_parse_os_release", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_hw_data", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_virtual", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_bsd_cpudata", return_value=empty_mock
|
||||
), patch.object(
|
||||
os, "stat", side_effect=OSError()
|
||||
):
|
||||
os_grains = core.os_data()
|
||||
assert os_grains != {}
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
|
||||
def _mock_open(filename, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
os.path.join(tempdir, "cmdline-1"), *args, **kwargs
|
||||
)
|
||||
|
||||
with salt.utils.files.fopen(
|
||||
os.path.join(tempdir, "cmdline-1"),
|
||||
"wb",
|
||||
) as cmdline_fh, patch("os.path.isfile", return_value=False), patch(
|
||||
"salt.utils.files.fopen", _mock_open
|
||||
), patch.dict(
|
||||
core.__salt__,
|
||||
{
|
||||
"cmd.retcode": salt.modules.cmdmod.retcode,
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
},
|
||||
), patch.object(
|
||||
core, "_linux_bin_exists", return_value=False
|
||||
), patch.object(
|
||||
core, "_parse_lsb_release", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_parse_os_release", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_hw_data", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_virtual", return_value=empty_mock
|
||||
), patch.object(
|
||||
core, "_bsd_cpudata", return_value=empty_mock
|
||||
), patch.object(
|
||||
os, "stat", side_effect=OSError()
|
||||
):
|
||||
cmdline_fh.write(
|
||||
b"/usr/lib/systemd/systemd\x00--switched-root\x00--system\x00--deserialize\x0028\x80\x00"
|
||||
)
|
||||
cmdline_fh.close()
|
||||
os_grains = core.os_data()
|
||||
assert os_grains != {}
|
||||
|
||||
|
||||
@pytest.mark.skip_on_windows
|
||||
def test_virtual_linux_proc_files_with_non_utf8_chars():
|
||||
_salt_utils_files_fopen = salt.utils.files.fopen
|
||||
|
||||
def _mock_open(filename, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
pathlib.Path(__file__).parent.joinpath("proc-files").joinpath("environ"),
|
||||
*args,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
def _is_file_mock(filename):
|
||||
if filename == "/proc/1/environ":
|
||||
return True
|
||||
return False
|
||||
|
||||
with patch("os.path.isfile", _is_file_mock), patch(
|
||||
"salt.utils.files.fopen", _mock_open
|
||||
), patch.object(salt.utils.path, "which", MagicMock(return_value=None)), patch.dict(
|
||||
core.__salt__,
|
||||
{
|
||||
"cmd.run_all": MagicMock(
|
||||
return_value={"retcode": 1, "stderr": "", "stdout": ""}
|
||||
),
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
},
|
||||
):
|
||||
virt_grains = core._virtual({"kernel": "Linux"})
|
||||
assert virt_grains == {"virtual": "physical"}
|
||||
with tempfile.TemporaryDirectory() as tempdir:
|
||||
|
||||
def _mock_open(filename, *args, **kwargs):
|
||||
return _salt_utils_files_fopen(
|
||||
os.path.join(tempdir, "environ"), *args, **kwargs
|
||||
)
|
||||
|
||||
with salt.utils.files.fopen(
|
||||
os.path.join(tempdir, "environ"),
|
||||
"wb",
|
||||
) as environ_fh, patch("os.path.isfile", _is_file_mock), patch(
|
||||
"salt.utils.files.fopen", _mock_open
|
||||
), patch.object(
|
||||
salt.utils.path, "which", MagicMock(return_value=None)
|
||||
), patch.dict(
|
||||
core.__salt__,
|
||||
{
|
||||
"cmd.run_all": MagicMock(
|
||||
return_value={"retcode": 1, "stderr": "", "stdout": ""}
|
||||
),
|
||||
"cmd.run": MagicMock(return_value=""),
|
||||
},
|
||||
):
|
||||
environ_fh.write(b"KEY1=VAL1 KEY2=VAL2\x80 KEY2=VAL2")
|
||||
environ_fh.close()
|
||||
virt_grains = core._virtual({"kernel": "Linux"})
|
||||
assert virt_grains == {"virtual": "physical"}
|
||||
|
|
Loading…
Add table
Reference in a new issue