Skip to content

Commit

Permalink
Turn static test files to dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Zhestkov authored and Victor Zhestkov committed Sep 13, 2022
1 parent e0fd5f6 commit d79a47e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 63 deletions.
1 change: 0 additions & 1 deletion tests/pytests/unit/grains/proc-files/cmdline

This file was deleted.

Binary file removed tests/pytests/unit/grains/proc-files/cmdline-1
Binary file not shown.
1 change: 0 additions & 1 deletion tests/pytests/unit/grains/proc-files/environ

This file was deleted.

145 changes: 84 additions & 61 deletions tests/pytests/unit/grains/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pathlib
import platform
import socket
import tempfile
import textwrap
from collections import namedtuple

Expand Down Expand Up @@ -2744,13 +2745,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"),
Expand All @@ -2760,8 +2754,23 @@ def _open_mock(file_name, *args, **kwargs):
("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():
Expand Down Expand Up @@ -3023,68 +3032,82 @@ def _raise_fnfe():
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"}

0 comments on commit d79a47e

Please sign in to comment.