"""Portable frozen AuthConfig native adapter; requires Python and Docker only.""" from pathlib import Path import argparse import hashlib import json import subprocess import uuid parser = argparse.ArgumentParser() parser.add_argument('--task-dir', type=Path, required=True) parser.add_argument('--image', required=True, help='Immutable linux/amd64 repository@sha256 reference') parser.add_argument('--world', choices=['all_buggy', 'all_fixed', 'candidate'], required=True) parser.add_argument('--candidate-patches', type=Path) parser.add_argument('--output', type=Path, required=True) parser.add_argument('--prepare-only', action='store_true') args = parser.parse_args() I = args.task_dir.resolve() D = args.output.resolve() D.mkdir(parents=True, exist_ok=False) assert '@sha256:' in args.image task = json.loads((I / 'task.json').read_text()) contract = json.loads((I / 'evaluation/contract.json').read_text()) assert task['instance_id'] == 'cloudnative-moby-51059' and contract['kind'] == 'native_contract' N = 'crossswe-authconfig-' + uuid.uuid4().hex[:12] result = {'state': 'PREPARING', 'instance_id': task['instance_id'], 'world': args.world, 'image': args.image, 'commands': [], 'cases': [], 'prepare_only': args.prepare_only} def sha(path): return hashlib.sha256(Path(path).read_bytes()).hexdigest() def save(): (D / 'result.json').write_text(json.dumps(result, indent=2) + '\n') def run(label, command, data=None, check=True): with (D / (label + '.stdout.raw')).open('xb') as out, (D / (label + '.stderr.raw')).open('xb') as err: p = subprocess.run(command, input=data, stdout=out, stderr=err, timeout=1800) result['commands'].append({'label': label, 'argv': command, 'returncode': p.returncode, 'stdout_sha256': sha(D / (label + '.stdout.raw')), 'stderr_sha256': sha(D / (label + '.stderr.raw'))}) save() assert not check or p.returncode == 0, label return p.returncode for row in task['artifacts']: assert sha(I / row['path']) == row['sha256'], row['path'] candidate = {} if args.world == 'candidate': assert args.candidate_patches candidate = json.loads(args.candidate_patches.read_text()) save() try: run('create', ['docker', 'run', '-d', '--name', N, '--label', 'crossswe.portable-authconfig=1', '--network=none', '--cpus=16', '--memory=8g', '-e', 'GOCACHE=/tmp/crossswe-native-cache', args.image, 'sleep', 'infinity']) info = json.loads(subprocess.check_output(['docker', 'inspect', N]))[0] assert not info['Mounts'] and info['HostConfig']['NetworkMode'] == 'none' (D / 'container-inspect.json').write_text(json.dumps(info, indent=2) + '\n') for repo in task['repositories']: key, root = repo['key'], repo['container_path'] assert subprocess.check_output(['docker', 'exec', '-w', root, N, 'git', 'rev-parse', 'HEAD'], text=True).strip() == repo['base_commit'] if args.world == 'all_fixed': run('gold-' + key, ['docker', 'exec', '-i', '-w', root, N, 'git', 'apply', '-'], (I / repo['gold_patch']['path']).read_bytes()) elif key in candidate: path = Path(candidate[key]) path = path if path.is_absolute() else args.candidate_patches.resolve().parent / path run('candidate-' + key, ['docker', 'exec', '-i', '-w', root, N, 'git', 'apply', '-'], path.read_bytes()) package = 'api/types/registry' if key == 'moby' else 'cli/config/types' fixture = next(row for row in contract['test_files'] if Path(row['path']).name.startswith(key + '.')) assert sha(I / fixture['path']) == fixture['sha256'] run('native-fixture-' + key, ['docker', 'cp', str(I / fixture['path']), N + ':' + root + '/' + package + '/crossrepo_authconfig_test.go']) cfg = next(row for row in contract['source_record_commands'] if row['repository'] == key) prefix = ['docker', 'exec', '-w', root] for name, value in cfg['environment'].items(): prefix += ['-e', name + '=' + value] run('module-preparation-' + key, prefix + [N, 'bash', '-c', cfg['evaluation_setup_command']]) run('source-bindings-' + key, prefix + [N, 'go', 'list', '-mod=vendor', '-json', './' + package, 'github.com/moby/moby/api/types/registry']) if args.prepare_only: continue for role, selector, count in [('pass_to_pass', 'TestCrossRepoD07CredentialsP2P', 20), ('fail_to_pass', 'TestCrossRepoD07EmailRemoval', 6)]: label = key + '-' + role rc = run(label, prefix + [N, 'go', 'test', '-json', '-mod=vendor', '-count=1', '-run', selector, './' + package], check=False) events = [json.loads(line) for line in (D / (label + '.stdout.raw')).read_text().splitlines() if line.startswith('{')] terminal = [row for row in events if row.get('Test') and row.get('Action') in ['pass', 'fail', 'skip']] names = {row['Test'] for row in terminal} leaves = [row for row in terminal if not any(name.startswith(row['Test'] + '/') for name in names)] assert len(leaves) == count and not any(row['Action'] == 'skip' for row in leaves) result['cases'] += [{'id': row['Package'] + ':' + row['Test'], 'repository': key, 'role': role, 'outcome': row['Action']} for row in leaves] result.update(state='PREPARED_ONLY_NO_TESTS_EXECUTED' if args.prepare_only else 'NATIVE_CASES_EXECUTED', task_sha256=sha(I / 'task.json'), contract_sha256=sha(I / 'evaluation/contract.json'), executed=len(result['cases']), passed=sum(c['outcome'] == 'pass' for c in result['cases']), failed=sum(c['outcome'] == 'fail' for c in result['cases'])) finally: if subprocess.run(['docker', 'inspect', N], capture_output=True).returncode == 0: run('retire', ['docker', 'rm', '-f', N]) save() print(json.dumps({k: result[k] for k in ['state', 'world', 'executed', 'passed', 'failed']})) raise SystemExit(0 if args.prepare_only or result['failed'] == 0 else 1)