"""Test cleanup gates on disposable dummy folders. No network access. Requires Windows PowerShell. --work-dir must be a new or empty folder. """ import argparse import hashlib import json import os from pathlib import Path import subprocess import sys import time FAKE_VERIFIER = '''import argparse,hashlib,json from pathlib import Path p=argparse.ArgumentParser() p.add_argument('--plan');p.add_argument('--expected-plan-sha');p.add_argument('--proof') a=p.parse_args() plan=json.loads(Path(a.plan).read_text()) assert hashlib.sha256(Path(a.plan).read_bytes()).hexdigest()==a.expected_plan_sha Path(a.proof).write_text(json.dumps({'approved':True,'plan_sha256':a.expected_plan_sha,'repo_id':plan['repo_id'],'source_unchanged':True,'revision':'0'*40})) ''' def sha(path): return hashlib.sha256(path.read_bytes()).hexdigest() def make_case(base, name, controller): case = base / name source = case / 'source/AASpotify' workspace = case / 'workspace' targets = [source, workspace / 'outputs/spotify_parquet', workspace / 'outputs/hf_spotify_dataset', workspace / 'work'] for target in targets: target.mkdir(parents=True) (target / 'dummy.txt').write_text('Disposable test data only.') resolved = target.resolve(strict=True) assert resolved.is_relative_to(base) and resolved != base (case / 'keep.txt').write_text('Must remain outside the cleanup targets.') verifier = workspace / 'work/fake_verifier.py' verifier.write_text(FAKE_VERIFIER) status = workspace / 'outputs/hf_upload_status.json' status.write_text(json.dumps({'status': 'complete', 'repo_id': 'local-test/spotify', 'supervisor_pid': None, 'uploader_pid': None})) plan = { 'authorized_action': 'delete_local_spotify_project_after_verified_upload', 'workspace': str(workspace.resolve()), 'source_dir': str(source.resolve()), 'targets': [str(target.resolve()) for target in targets], 'repo_id': 'local-test/spotify', 'verifier_script': str(verifier.resolve()), 'verifier_sha256': sha(verifier), 'controller_script': str(controller), 'controller_sha256': sha(controller), 'python_executable': sys.executable, 'upload_process_ids': [], 'estimated_unique_GiB': 0, } plan_path = workspace / 'outputs/hf_cleanup_plan.json' plan_path.write_text(json.dumps(plan)) return case, workspace, targets, plan, plan_path def command(controller, plan, checksum): return ['powershell.exe', '-NoProfile', '-NonInteractive', '-File', str(controller), '-PlanPath', str(plan), '-ExpectedPlanSha256', checksum, '-Run'] def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--controller', type=Path, default=Path(__file__).with_name('cleanup_after_upload.ps1')) p.add_argument('--work-dir', type=Path, required=True) a = p.parse_args() if os.name != 'nt': print('SKIP: Windows PowerShell is required.') return controller = a.controller.resolve(strict=True) base = a.work_dir.resolve() if base.exists() and any(base.iterdir()): p.error('Use a new or empty test directory') base.mkdir(parents=True, exist_ok=True) flags = subprocess.CREATE_NO_WINDOW case, workspace, targets, plan, path = make_case(base, 'plan_changed', controller) result = subprocess.run(command(controller, path, '0'*64), capture_output=True, text=True, creationflags=flags, timeout=30) assert result.returncode != 0 and all(target.exists() for target in targets) assert 'Authorized cleanup plan changed' in result.stderr + result.stdout print('PASS: changed plan checksum blocks deletion.') case, workspace, targets, plan, path = make_case(base, 'verifier_changed', controller) plan['verifier_sha256'] = '0'*64 path.write_text(json.dumps(plan)) result = subprocess.run(command(controller, path, sha(path)), capture_output=True, text=True, creationflags=flags, timeout=30) assert result.returncode != 0 and all(target.exists() for target in targets) assert 'Verifier code changed' in result.stderr + result.stdout print('PASS: changed verifier blocks deletion.') case, workspace, targets, plan, path = make_case(base, 'wrong_target', controller) outside = case / 'outside' outside.mkdir() (outside / 'keep.txt').write_text('Must never be deleted.') plan['targets'][0] = str(outside.resolve()) path.write_text(json.dumps(plan)) result = subprocess.run(command(controller, path, sha(path)), capture_output=True, text=True, creationflags=flags, timeout=30) assert result.returncode != 0 and all(target.exists() for target in targets) assert 'Cleanup target differs from the authorized directory' in result.stderr + result.stdout assert (outside / 'keep.txt').exists() print('PASS: a target outside the authorized list is rejected.') case, workspace, targets, plan, path = make_case(base, 'upload_incomplete', controller) status_path = workspace / 'outputs/hf_upload_status.json' status_path.write_text(json.dumps({'status': 'uploading', 'repo_id': plan['repo_id']})) process = subprocess.Popen(command(controller, path, sha(path)), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=flags) try: time.sleep(4) assert process.poll() is None, 'Controller stopped instead of waiting' assert all(target.exists() for target in targets) assert not (workspace / 'work/hf_cleanup_verified.json').exists() finally: process.kill() process.wait(timeout=10) print('PASS: an incomplete upload waits without running verification or deleting.') case, workspace, targets, plan, path = make_case(base, 'verified_cleanup', controller) result = subprocess.run(command(controller, path, sha(path)), capture_output=True, text=True, creationflags=flags, timeout=30) assert result.returncode == 0, result.stderr + result.stdout assert all(not target.exists() for target in targets) assert (case / 'keep.txt').exists() status = json.loads((workspace / 'outputs/hf_upload_status.json').read_text()) assert status['status'] == 'complete_and_local_cleaned' assert not path.exists() print('PASS: valid approval removes only the four test folders and records completion.') print('All five cleanup controller tests passed. No real dataset files were deleted.') if __name__ == '__main__': main()