"""Offline producer fixtures; executed only by normal release source checks.""" import importlib.util import gzip import hashlib from pathlib import Path import tempfile import unittest ROOT = Path(__file__).resolve().parents[2] spec = importlib.util.spec_from_file_location('producer', ROOT / 'builder/adapters/indexed-overlay.py') producer = importlib.util.module_from_spec(spec) spec.loader.exec_module(producer) checker = producer.load_checker(ROOT / 'composer') capture_spec = importlib.util.spec_from_file_location('capture', ROOT / 'builder/adapters/capture-overlay.py') capture = importlib.util.module_from_spec(capture_spec) capture_spec.loader.exec_module(capture) class Producer(unittest.TestCase): def test_capture_selects_owned_defaults_without_copying_unowned_files(self): import json with tempfile.TemporaryDirectory() as temp: work = Path(temp) root = work / 'installed' record = root / 'var/lib/pacman/local/example-1-1' record.mkdir(parents=True) (root / 'etc').mkdir() (root / 'etc/example').write_bytes(b'default') (root / 'etc/unowned').write_bytes(b'not package payload') (record / 'desc').write_bytes(b'%NAME%\nexample\n\n%VERSION%\n1-1\n\n%ARCH%\nany\n\n') (record / 'files').write_bytes(b'%FILES%\netc/example\n\n') (record / 'mtree').write_bytes(gzip.compress(b'#mtree\n', mtime=0)) (work / 'cache').mkdir() (work / 'source').mkdir() archive = work / 'cache/example.pkg.tar.zst' archive.write_bytes(b'fixture') lock = dict(snapshot_id='a' * 64, requirements=[dict(requirement='example', package='example')], packages=[dict(name='example', version='1-1', architecture='any', dependencies=[], filename=archive.name, archive_sha256=hashlib.sha256(archive.read_bytes()).hexdigest())]) installed = dict(version=0, root=str(root), database=str(root / 'var/lib/pacman'), resolution_sha256=hashlib.sha256(json.dumps(lock, sort_keys=True, separators=(',', ':')).encode()).hexdigest()) request = capture.capture(checker, lock, installed, work / 'cache', work / 'source') self.assertEqual(request['packages'][0]['path_sources'], {'/usr/etc/example': 'etc/example'}) manifest = dict(apiVersion='archipelago/v1', kind='Layer', metadata=dict(name='example', version='1'), spec=dict(type='overlay', architecture='x86_64', packages=dict(runtime=[dict(name='example')]))) result = producer.pack(checker, manifest, request, work / 'output') self.assertEqual([row['path'] for row in result['objects']], ['/usr/etc/example']) self.assertEqual((root / 'etc/unowned').read_bytes(), b'not package payload') def test_virtual_requirement_retains_the_resolved_provider(self): with tempfile.TemporaryDirectory() as temp: work = Path(temp) for name in ('source', 'root', 'record'): (work / name).mkdir() archive = work / 'provider.pkg.tar.zst' archive.write_bytes(b'fixture authenticated archive input') (work / 'record/desc').write_bytes(b'%NAME%\nprovider\n\n%VERSION%\n1-1\n\n%ARCH%\nany\n\n%PROVIDES%\nvirtual=1\n\n') (work / 'record/files').write_bytes(b'%FILES%\n\n') (work / 'record/mtree').write_bytes(gzip.compress(b'#mtree\n', mtime=0)) manifest = dict(apiVersion='archipelago/v1', kind='Layer', metadata=dict(name='example', version='1'), spec=dict(type='overlay', architecture='x86_64', packages=dict(runtime=[dict(name='virtual', constraint='>=1')]))) request = dict(version=0, snapshot_id='a' * 64, source_root=str(work / 'source'), requirement_bindings=[dict(requirement='virtual>=1', package='provider')], packages=[dict( name='provider', version='1-1', architecture='any', archive=str(archive), archive_sha256=hashlib.sha256(archive.read_bytes()).hexdigest(), dependencies=[], root=str(work / 'root'), record=str(work / 'record'))]) result = producer.pack(checker, manifest, request, work / 'output') self.assertEqual(result['lock']['requirements'], ['provider']) self.assertEqual(result['lock']['requirement_bindings'], request['requirement_bindings']) request['requirement_bindings'][0]['requirement'] = 'virtual>=2' with self.assertRaisesRegex(ValueError, 'requirement binding'): producer.pack(checker, manifest, request, work / 'mismatch') def test_rebuild_binds_new_snapshot_and_same_authored_intent(self): with tempfile.TemporaryDirectory() as temp: work = Path(temp) source = work / 'source' source.mkdir() (source / 'example').write_bytes(b'vendor configuration\n') manifest = dict(apiVersion='archipelago/v1', kind='Layer', metadata=dict(name='example', version='1'), spec=dict(type='overlay', architecture='x86_64', files=[dict(path='/etc/example', source='example', mode='0644', owner='root', group='root')])) request = dict(version=0, snapshot_id='a' * 64, source_root=str(source), packages=[]) first = producer.pack(checker, manifest, request, work / 'first') request['snapshot_id'] = 'b' * 64 second = producer.pack(checker, manifest, request, work / 'second') self.assertNotEqual(first['lock_sha256'], second['lock_sha256']) self.assertEqual(first['objects'], second['objects']) self.assertEqual((work / 'second/payload/files/root/usr/etc/example').read_bytes(), b'vendor configuration\n') def test_authored_symlink_cannot_redirect_a_later_write(self): with tempfile.TemporaryDirectory() as temp: work = Path(temp) source = work / 'source' source.mkdir() (source / 'example').write_bytes(b'payload') outside = work / 'outside' outside.mkdir() manifest = dict(spec=dict(type='overlay', architecture='x86_64', files=[ dict(path='/usr/share/escape', type='symlink', target=str(outside), mode='0777', owner='root', group='root'), dict(path='/usr/share/escape/example', source='example', mode='0644', owner='root', group='root')])) with self.assertRaisesRegex(ValueError, 'payload ancestor'): producer.pack(checker, manifest, dict(version=0, snapshot_id='a' * 64, source_root=str(source), packages=[]), work / 'output') self.assertFalse((outside / 'example').exists()) if __name__ == '__main__': unittest.main()