#!/usr/bin/env python3 """Verify the current version in actual GPT/EROFS bytes and execute its ARM commands.""" from pathlib import Path import hashlib import json import shlex import subprocess import sys import tarfile import tempfile import tomllib project=Path(__file__).resolve().parents[2] sys.path.insert(0,str(project/'tools')) from fds_version import version expected=version(project) image=(project/'out/fds-system-cli.img').resolve(strict=True) work=Path(tempfile.mkdtemp(prefix='version-image-test.',dir=project/'out')) table=json.loads(subprocess.check_output(['sfdisk','--json',str(image)]))['partitiontable'] assert len(table['partitions'])==1 and table['partitions'][0]['name']=='FDS_SYSTEM' offset=table['partitions'][0]['start']*table['sectorsize'] def extract(path,name): target=work/name subprocess.run([str(project/'tools/in-image-tools'),'fsck.erofs',f'--offset={offset}',f'--path=/{path}',f'--extract={target}',str(image)],check=True) return target identity=extract('usr/lib/os-release','os-release').read_text() values=dict(line.split('=',1) for line in shlex.split(identity,comments=True)) assert values['VERSION_ID']==values['VERSION']==expected,values assert values['PRETTY_NAME']==f'FDS/OS {expected}',values assert extract('etc/issue','issue').read_text()==f'FDS/OS {expected}\n' metadata=tomllib.loads(extract('FDS/CARTRIDGE.TOML','CARTRIDGE.TOML').read_text()) assert metadata['cartridge']['version']==expected,metadata with tarfile.open(project/'out/rootfs-cli.tar') as archive: assert archive.getmember('etc/os-release').linkname=='../usr/lib/os-release' assert archive.extractfile('usr/lib/os-release').read().decode()==identity commands={} for name in ['fds','fds-control','fds-program','fds-boottrace','fds-cartridged','fds-profile','fds-burn','fds-release','fds-inspect','fds-eject','fds-power','dasungd']: binary=extract(f'usr/bin/{name}',name) output=subprocess.check_output([str(project/'tools/in-void'),'qemu-aarch64',str(binary),'--version'],text=True).strip() assert output==f'{name} {expected}',output commands[name]=output with image.open('rb') as stream: checksum=hashlib.file_digest(stream,'sha256').hexdigest() record=dict(status='passed',version=expected,image=str(image),image_sha256=checksum,identity=values,cartridge_version=metadata['cartridge']['version'],arm_commands=commands,physical_hardware=False) (work/'acceptance.json').write_text(json.dumps(record,indent=2)+'\n') (project/'out/version-image-current.txt').write_text(str(work)+'\n') print(f'PASS: {expected} in GPT/EROFS identity, cartridge metadata and all installed FDS commands: {work}')