Repair backup revisions that recorded a mapped drive
Snapshots written before the client was fixed stored "W:\backup\partmarker\..." as their location. That mapping exists only inside the SYSTEM logon session, only during an enforcement cycle, and only on the bay that wrote it, so the path recorded against a marker resolved to nothing for the person most likely to open it. The script rewrites backuprevisions.sharepath and nothing else - no revision is created, deleted, re-filed or re-hashed, and no file on the share moves, since the bytes are already where the UNC will point. Dry run by default. It matches on the drive letter alone and leaves everything else untouched, including paths that are already UNC and paths on another drive. This repairs a specific known mistake rather than normalising whatever it can reach, which is the version of this script that would eventually rewrite something it should not have. rewrite() is kept pure so the decision of WHICH paths to touch is testable without an app context; the tests cover the already-UNC, other-drive, empty, case-difference and trailing-separator cases.
This commit is contained in:
55
tests/test_fix_drive_letter_sharepaths.py
Normal file
55
tests/test_fix_drive_letter_sharepaths.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""The path rewrite, exercised without a database.
|
||||
|
||||
rewrite() is pure on purpose: the risky part of this script is deciding WHICH
|
||||
paths to touch, and that judgement should be testable without standing up an
|
||||
app context.
|
||||
"""
|
||||
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parent.parent / 'scripts' / 'fix_drive_letter_sharepaths.py'
|
||||
|
||||
|
||||
def _rewrite(*args, **kwargs):
|
||||
# Imported lazily and by path: scripts/ is not a package, and importing the
|
||||
# module runs load_dotenv + create_app imports at module scope.
|
||||
spec = importlib.util.spec_from_file_location('fixsharepaths', SCRIPT)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
try:
|
||||
spec.loader.exec_module(module)
|
||||
except Exception as exc: # pragma: no cover - env-dependent
|
||||
pytest.skip('script imports need an app environment: {}'.format(exc))
|
||||
return module.rewrite(*args, **kwargs)
|
||||
|
||||
|
||||
UNC = r'\\tsgwp00525.wjs.geaerospace.net\shared\dt\shopfloor'
|
||||
|
||||
|
||||
def test_a_mapped_drive_path_becomes_its_unc_equivalent():
|
||||
got = _rewrite(r'W:\backup\partmarker\WJPRT\20260825-144500', 'W:', UNC)
|
||||
assert got == UNC + r'\backup\partmarker\WJPRT\20260825-144500'
|
||||
|
||||
|
||||
def test_a_path_that_is_already_unc_is_left_alone():
|
||||
"""The script repairs a known mistake; it does not normalise what it finds."""
|
||||
assert _rewrite(UNC + r'\backup\partmarker\WJPRT\x', 'W:', UNC) is None
|
||||
|
||||
|
||||
def test_another_drive_is_left_alone():
|
||||
assert _rewrite(r'S:\DT\Shopfloor\backup\x', 'W:', UNC) is None
|
||||
|
||||
|
||||
def test_an_empty_or_missing_path_is_left_alone():
|
||||
assert _rewrite('', 'W:', UNC) is None
|
||||
assert _rewrite(None, 'W:', UNC) is None
|
||||
|
||||
|
||||
def test_the_drive_letter_match_is_case_insensitive():
|
||||
assert _rewrite(r'w:\backup\x', 'W:', UNC) == UNC + r'\backup\x'
|
||||
|
||||
|
||||
def test_a_trailing_backslash_on_the_unc_does_not_double_up():
|
||||
assert _rewrite(r'W:\backup\x', 'W:', UNC + '\\') == UNC + r'\backup\x'
|
||||
Reference in New Issue
Block a user