fixes saltstack/salt#63042 add ability to ignore symlinks in file.tidied

This commit is contained in:
nicholasmhughes 2022-11-08 14:14:46 -05:00 committed by Megan Wilhite
parent 3ad7452ca3
commit 2b20289349
3 changed files with 60 additions and 3 deletions

1
changelog/63042.added Normal file
View file

@ -0,0 +1 @@
Add ability to ignore symlinks in file.tidied

View file

@ -1949,10 +1949,11 @@ def tidied(
time_comparison="atime",
age_size_logical_operator="OR",
age_size_only=None,
rmlinks=True,
**kwargs
):
"""
.. versionchanged:: 3006,3005
.. versionchanged:: 3006.0,3005
Remove unwanted files based on specific criteria.
@ -2016,7 +2017,7 @@ def tidied(
is not old enough will NOT get tidied. A file will need to fulfill BOTH
conditions in order to be tidied. Accepts ``OR`` or ``AND``.
.. versionadded:: 3006
.. versionadded:: 3006.0
age_size_only
This parameter can trigger the reduction of age and size conditions
@ -2027,7 +2028,12 @@ def tidied(
evaluation down to that specific condition. Path matching and
exclusions still apply.
.. versionadded:: 3006
.. versionadded:: 3006.0
rmlinks
Whether or not it's allowed to remove symbolic links
.. versionadded:: 3006.0
.. code-block:: yaml
@ -2111,6 +2117,8 @@ def tidied(
mytimestamp = os.lstat(path).st_mtime
else:
mytimestamp = os.lstat(path).st_atime
if not rmlinks:
deleteme = False
else:
# Get timestamp of file or directory
if time_comparison == "ctime":

View file

@ -1,6 +1,7 @@
import logging
import os
from datetime import datetime, timedelta
from types import SimpleNamespace
import pytest
@ -578,3 +579,50 @@ def test_tidied_filenotfound(tmp_path):
"comment": "Nothing to remove from directory {}".format(name),
}
assert ret == exp
def test_tidied_rmlinks():
name = os.sep + "test"
if salt.utils.platform.is_windows():
name = "c:" + name
walker = [
(os.path.join("test", "test1"), [], ["file1"]),
(os.path.join("test", "test2", "test3"), [], []),
(os.path.join("test", "test2"), ["test3"], ["link1"]),
("test", ["test1", "test2"], ["file3"]),
]
today_delta = (datetime.today() - timedelta(days=14)) - datetime.utcfromtimestamp(0)
mock_lstat = MagicMock(return_value=SimpleNamespace(st_atime=today_delta))
remove = MagicMock(name="file.remove")
with patch("os.walk", return_value=walker), patch(
"os.path.islink", side_effect=[False, True, False, False, False, False]
), patch("os.path.getatime", return_value=today_delta.total_seconds()), patch(
"os.lstat", return_value=mock_lstat
), patch(
"os.path.getsize", return_value=10
), patch.dict(
filestate.__opts__, {"test": False}
), patch.dict(
filestate.__salt__, {"file.remove": remove}
), patch(
"os.path.isdir", return_value=True
):
ret = filestate.tidied(
name=name,
age=1,
size=9,
rmlinks=False,
)
exp = {
"name": name,
"changes": {
"removed": [
os.path.join("test", "test1", "file1"),
os.path.join("test", "file3"),
]
},
"result": True,
"comment": "Removed 2 files or directories from directory {}".format(name),
}
assert ret == exp
assert remove.call_count == 2