file.replace and file.search work properly with /proc files

This commit is contained in:
Tyler Levy Conde 2024-07-09 13:42:25 -06:00 committed by Daniel Wozniak
parent eb723cdc7e
commit 3b697d39a2
3 changed files with 17 additions and 1 deletions

1
changelog/63102.fixed.md Normal file
View file

@ -0,0 +1 @@
file.replace and file.search work properly with /proc files

View file

@ -2539,7 +2539,7 @@ def replace(
r_data = mmap.mmap(r_file.fileno(), 0, access=mmap.ACCESS_READ)
except (ValueError, OSError):
# size of file in /proc is 0, but contains data
r_data = salt.utils.stringutils.to_bytes("".join(r_file))
r_data = b"".join(r_file)
if search_only:
# Just search; bail as early as a match is found
if re.search(cpattern, r_data):

View file

@ -48,6 +48,7 @@ def configure_loader_modules():
"__grains__": grains,
"__utils__": {
"files.is_binary": MagicMock(return_value=False),
"files.is_text": salt.utils.files.is_text,
"files.get_encoding": MagicMock(return_value="utf-8"),
"stringutils.get_diff": salt.utils.stringutils.get_diff,
},
@ -546,3 +547,17 @@ def test_unfinished_block_exception(multiline_file):
content="foobar",
backup=False,
)
def test_search_proc_file():
"""
Test that searching content in a /proc file does not raise a TypeError
and handles bytes correctly.
"""
proc_file_path = "/proc/cpuinfo"
if not os.path.exists(proc_file_path):
pytest.skip(f"{proc_file_path} not available")
match_found = filemod.search(proc_file_path, "processor")
assert match_found, "Failed to find 'processor' in /proc/cpuinfo"