mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
file.replace and file.search work properly with /proc files
This commit is contained in:
parent
eb723cdc7e
commit
3b697d39a2
3 changed files with 17 additions and 1 deletions
1
changelog/63102.fixed.md
Normal file
1
changelog/63102.fixed.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
file.replace and file.search work properly with /proc files
|
|
@ -2539,7 +2539,7 @@ def replace(
|
||||||
r_data = mmap.mmap(r_file.fileno(), 0, access=mmap.ACCESS_READ)
|
r_data = mmap.mmap(r_file.fileno(), 0, access=mmap.ACCESS_READ)
|
||||||
except (ValueError, OSError):
|
except (ValueError, OSError):
|
||||||
# size of file in /proc is 0, but contains data
|
# 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:
|
if search_only:
|
||||||
# Just search; bail as early as a match is found
|
# Just search; bail as early as a match is found
|
||||||
if re.search(cpattern, r_data):
|
if re.search(cpattern, r_data):
|
||||||
|
|
|
@ -48,6 +48,7 @@ def configure_loader_modules():
|
||||||
"__grains__": grains,
|
"__grains__": grains,
|
||||||
"__utils__": {
|
"__utils__": {
|
||||||
"files.is_binary": MagicMock(return_value=False),
|
"files.is_binary": MagicMock(return_value=False),
|
||||||
|
"files.is_text": salt.utils.files.is_text,
|
||||||
"files.get_encoding": MagicMock(return_value="utf-8"),
|
"files.get_encoding": MagicMock(return_value="utf-8"),
|
||||||
"stringutils.get_diff": salt.utils.stringutils.get_diff,
|
"stringutils.get_diff": salt.utils.stringutils.get_diff,
|
||||||
},
|
},
|
||||||
|
@ -546,3 +547,17 @@ def test_unfinished_block_exception(multiline_file):
|
||||||
content="foobar",
|
content="foobar",
|
||||||
backup=False,
|
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"
|
||||||
|
|
Loading…
Add table
Reference in a new issue