|
7 | 7 |
|
8 | 8 | # imports |
9 | 9 | import os |
| 10 | +import time |
10 | 11 | from os.path import abspath |
11 | 12 | import re |
12 | 13 | import sys |
| 14 | +from pathlib import Path |
13 | 15 |
|
14 | 16 | from hypothesis import given |
15 | 17 | from hypothesis.strategies import floats |
16 | 18 |
|
17 | 19 | from pytest import mark |
| 20 | +from unittest.mock import MagicMock |
18 | 21 |
|
19 | 22 | # sut |
20 | 23 | from DIRAC.Core.Utilities.File import ( |
21 | 24 | checkGuid, |
| 25 | + cleanDirectory, |
22 | 26 | makeGuid, |
23 | 27 | getSize, |
24 | 28 | getMD5ForFiles, |
@@ -134,3 +138,185 @@ def test_convert_loop(nb, srcUnit, dstUnit): |
134 | 138 | # We exclude the infinity case |
135 | 139 | if converted != float("Inf"): |
136 | 140 | assert converted == nb |
| 141 | + |
| 142 | + |
| 143 | +def _set_old_mtime(path: str, age_seconds: int = 3600) -> None: |
| 144 | + """Set a file's mtime to `age_seconds` ago.""" |
| 145 | + old_time = time.time() - age_seconds |
| 146 | + os.utime(path, (old_time, old_time)) |
| 147 | + |
| 148 | + |
| 149 | +def _build_tree(tmp_path, files, subdirs=None): |
| 150 | + """Create a file tree and return a mapping of name -> pathlib.Path.""" |
| 151 | + mapping = {} |
| 152 | + if subdirs: |
| 153 | + for sd in subdirs: |
| 154 | + (tmp_path / sd).mkdir(parents=True, exist_ok=True) |
| 155 | + for name, content in files.items(): |
| 156 | + mapping[name] = tmp_path / name |
| 157 | + mapping[name].write_text(content) |
| 158 | + return mapping |
| 159 | + |
| 160 | + |
| 161 | +def test_clean_directory_basic(tmp_path): |
| 162 | + """Check old file removed, newer file kept and old-non-matching kept.""" |
| 163 | + files = _build_tree( |
| 164 | + tmp_path, |
| 165 | + {"DIRAC_old_job": "abc", "DIRAC_new_job": "def", "other.log": "ghi"}, |
| 166 | + ) |
| 167 | + _set_old_mtime(str(files["DIRAC_old_job"])) |
| 168 | + _set_old_mtime(str(files["other.log"])) |
| 169 | + |
| 170 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"]) |
| 171 | + assert err == [] |
| 172 | + assert not files["DIRAC_old_job"].exists() |
| 173 | + assert files["DIRAC_new_job"].exists() |
| 174 | + assert files["other.log"].exists() |
| 175 | + |
| 176 | + |
| 177 | +def test_clean_directory_empty_dir(tmp_path): |
| 178 | + assert cleanDirectory(str(tmp_path)) == [] |
| 179 | + |
| 180 | + |
| 181 | +def test_clean_directory_maxDepth_restricts_recursion(tmp_path): |
| 182 | + files = _build_tree( |
| 183 | + tmp_path, |
| 184 | + {"root": "a", "DIRAC_root_job": "b"}, |
| 185 | + subdirs=["subdir"], |
| 186 | + ) |
| 187 | + _set_old_mtime(str(files["root"])) |
| 188 | + _set_old_mtime(str(files["DIRAC_root_job"])) |
| 189 | + deep = tmp_path / "subdir" / "DIRAC_deep" |
| 190 | + deep.write_text("c") |
| 191 | + _set_old_mtime(str(deep)) |
| 192 | + |
| 193 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"], maxDepth=1) |
| 194 | + assert err == [] |
| 195 | + assert not files["DIRAC_root_job"].exists() |
| 196 | + assert (tmp_path / "subdir" / "DIRAC_deep").exists() |
| 197 | + |
| 198 | + |
| 199 | +def test_clean_directory_recursive_without_maxDepth(tmp_path): |
| 200 | + nested = tmp_path / "a" / "b" |
| 201 | + nested.mkdir(parents=True) |
| 202 | + deep = nested / "DIRAC_nested.log" |
| 203 | + deep.write_text("data") |
| 204 | + _set_old_mtime(str(deep)) |
| 205 | + |
| 206 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["*.log"]) |
| 207 | + assert err == [] |
| 208 | + assert not deep.exists() |
| 209 | + |
| 210 | + |
| 211 | +def test_clean_directory_symlinks_not_deleted(tmp_path): |
| 212 | + target = tmp_path / "real" |
| 213 | + target.write_text("data") |
| 214 | + symlink = tmp_path / "DIRAC_link" |
| 215 | + symlink.symlink_to(target) |
| 216 | + _set_old_mtime(str(symlink)) |
| 217 | + |
| 218 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"]) |
| 219 | + assert err == [] |
| 220 | + assert symlink.exists() |
| 221 | + assert target.exists() |
| 222 | + |
| 223 | + |
| 224 | +def test_clean_directory_multiple_patterns(tmp_path): |
| 225 | + files = _build_tree( |
| 226 | + tmp_path, |
| 227 | + {"a.out": "x", "b.err": "y", "c.txt": "z"}, |
| 228 | + ) |
| 229 | + for f in files.values(): |
| 230 | + _set_old_mtime(str(f)) |
| 231 | + |
| 232 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["*.out", "*.err"]) |
| 233 | + assert err == [] |
| 234 | + assert not files["a.out"].exists() |
| 235 | + assert not files["b.err"].exists() |
| 236 | + assert files["c.txt"].exists() |
| 237 | + |
| 238 | + |
| 239 | +def test_clean_directory_returns_errors_on_unable_delete(mocker, tmp_path): |
| 240 | + """When unlink raises OSError, errors are collected.""" |
| 241 | + old_file = tmp_path / "DIRAC_blocked" |
| 242 | + old_file.write_text("data") |
| 243 | + _set_old_mtime(str(old_file)) |
| 244 | + |
| 245 | + mocker.patch("pathlib.Path.unlink", side_effect=PermissionError("Denied")) |
| 246 | + |
| 247 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"]) |
| 248 | + assert len(err) == 1 |
| 249 | + assert "DIRAC_blocked" in err[0] |
| 250 | + |
| 251 | + |
| 252 | +def test_clean_directory_empty_dirs_removed(tmp_path): |
| 253 | + """With delEmptyDirs=True, empty directories are removed after file cleanup.""" |
| 254 | + subdir = tmp_path / "subdir" / "nested" |
| 255 | + subdir.mkdir(parents=True) |
| 256 | + log_file = subdir / "old.log" |
| 257 | + log_file.write_text("data") |
| 258 | + _set_old_mtime(str(log_file)) |
| 259 | + |
| 260 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["*.log"], delEmptyDirs=True) |
| 261 | + assert err == [] |
| 262 | + assert not subdir.exists() |
| 263 | + |
| 264 | + |
| 265 | +def test_clean_directory_empty_dirs_kept_by_default(tmp_path): |
| 266 | + """Without delEmptyDirs, empty directories remain after file cleanup.""" |
| 267 | + subdir = tmp_path / "subdir" |
| 268 | + subdir.mkdir(parents=True) |
| 269 | + log_file = subdir / "old.log" |
| 270 | + log_file.write_text("data") |
| 271 | + _set_old_mtime(str(log_file)) |
| 272 | + |
| 273 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["*.log"]) |
| 274 | + assert err == [] |
| 275 | + assert subdir.exists() |
| 276 | + |
| 277 | + |
| 278 | +def test_clean_directory_callback_fn(mocker, tmp_path): |
| 279 | + """When callbackFn is provided, it replaces unlink.""" |
| 280 | + callback = mocker.Mock(side_effect=lambda p: (p.unlink(), True)[1]) |
| 281 | + |
| 282 | + old_file = tmp_path / "DIRAC_test" |
| 283 | + old_file.write_text("data") |
| 284 | + _set_old_mtime(str(old_file)) |
| 285 | + |
| 286 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"], callbackFn=callback) |
| 287 | + assert err == [] |
| 288 | + assert not old_file.exists() |
| 289 | + callback.assert_called_once() |
| 290 | + |
| 291 | + |
| 292 | +def test_clean_directory_callback_false_recorded_as_error(tmp_path): |
| 293 | + """Callback returning False records the file as an error; file is left untouched.""" |
| 294 | + |
| 295 | + def skip_all(_path): |
| 296 | + return False |
| 297 | + |
| 298 | + old_file = tmp_path / "DIRAC_test" |
| 299 | + old_file.write_text("data") |
| 300 | + _set_old_mtime(str(old_file)) |
| 301 | + |
| 302 | + err = cleanDirectory(str(tmp_path), maxSecs=60, filePatterns=["DIRAC_*"], callbackFn=skip_all) |
| 303 | + assert len(err) == 1 |
| 304 | + assert "DIRAC_test" in err[0] |
| 305 | + assert old_file.exists() |
| 306 | + |
| 307 | + |
| 308 | +def test_clean_directory_callback_oserror_recorded(mocker, tmp_path): |
| 309 | + """Callback raising OSError is treated as a deletion error.""" |
| 310 | + mocker.patch("pathlib.Path.unlink", side_effect=PermissionError("Denied")) |
| 311 | + |
| 312 | + old_file = tmp_path / "DIRAC_test" |
| 313 | + old_file.write_text("data") |
| 314 | + _set_old_mtime(str(old_file)) |
| 315 | + |
| 316 | + err = cleanDirectory( |
| 317 | + str(tmp_path), |
| 318 | + maxSecs=60, |
| 319 | + filePatterns=["DIRAC_*"], |
| 320 | + callbackFn=lambda p: (p.unlink(), True)[1], |
| 321 | + ) |
| 322 | + assert len(err) == 1 |
0 commit comments