From d79a47e58fb3d0a95a17b218d14816add1959fe9 Mon Sep 17 00:00:00 2001 From: Victor Zhestkov Date: Tue, 13 Sep 2022 21:21:03 +0300 Subject: [PATCH] Turn static test files to dynamic --- tests/pytests/unit/grains/proc-files/cmdline | 1 - .../pytests/unit/grains/proc-files/cmdline-1 | Bin 68 -> 0 bytes tests/pytests/unit/grains/proc-files/environ | 1 - tests/pytests/unit/grains/test_core.py | 145 ++++++++++-------- 4 files changed, 84 insertions(+), 63 deletions(-) delete mode 100644 tests/pytests/unit/grains/proc-files/cmdline delete mode 100644 tests/pytests/unit/grains/proc-files/cmdline-1 delete mode 100644 tests/pytests/unit/grains/proc-files/environ diff --git a/tests/pytests/unit/grains/proc-files/cmdline b/tests/pytests/unit/grains/proc-files/cmdline deleted file mode 100644 index d0f61cc9439c..000000000000 --- a/tests/pytests/unit/grains/proc-files/cmdline +++ /dev/null @@ -1 +0,0 @@ -TEST_KEY1=VAL1 TEST_KEY2=VAL2 BOOTABLE_FLAG="€" TEST_KEY_NOVAL TEST_KEY3=3 diff --git a/tests/pytests/unit/grains/proc-files/cmdline-1 b/tests/pytests/unit/grains/proc-files/cmdline-1 deleted file mode 100644 index bc4c370c54c044f4ca5e3b7f56d379b2fc0f3a19..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 68 zcmdNdEiTf}$xPBOt}HG|%}s&P47$3-<(VbP8L26{Mfv$9AU;?HkdcyFoLZEbn3GwR K%3x&CzyJVNix=7e diff --git a/tests/pytests/unit/grains/proc-files/environ b/tests/pytests/unit/grains/proc-files/environ deleted file mode 100644 index 17fe6d80b99f..000000000000 --- a/tests/pytests/unit/grains/proc-files/environ +++ /dev/null @@ -1 +0,0 @@ -KEY1=VAL1 KEY2=VAL2 KEY2=VAL2 \ No newline at end of file diff --git a/tests/pytests/unit/grains/test_core.py b/tests/pytests/unit/grains/test_core.py index 3b4612d3230b..ea2338dab3c9 100644 --- a/tests/pytests/unit/grains/test_core.py +++ b/tests/pytests/unit/grains/test_core.py @@ -11,6 +11,7 @@ import pathlib import platform import socket +import tempfile import textwrap from collections import namedtuple @@ -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"), @@ -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(): @@ -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"}