Datasets:
File size: 3,150 Bytes
0e239c6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import hashlib
import io
import json
import os
from pathlib import Path
import tarfile
import tempfile
import unittest
from materialize_agent_context import materialize
def fixture(root, target='/testbed/npm/pacote', declared=True, wrong_blob=False):
expected = '/testbed/npm/pacote'
blob = hashlib.sha1(b'blob ' + str(len(expected)).encode() + b'\0' + expected.encode()).hexdigest()
link = {'path': 'node_modules/pacote', 'original_target': expected, 'expected_blob': '0' * 40 if wrong_blob else blob, 'target_repository': 'provider'}
instance = {'repositories': [{'key': 'cli', 'path': '/testbed/npm/cli', 'workspace_links': [link] if declared else []},
{'key': 'provider', 'path': expected, 'workspace_links': []}]}
payload = {'instance.json': json.dumps(instance).encode(), 'testbed/npm/pacote/package.json': b'{}'}
archive = root / 'context.tar.gz'; entries = []
with tarfile.open(archive, 'w:gz') as tf:
for name, data in payload.items():
info = tarfile.TarInfo(name); info.size = len(data); info.mode = 0o644
tf.addfile(info, io.BytesIO(data))
entries.append({'path': name, 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest(), 'mode': 0o644})
name = 'testbed/npm/cli/node_modules/pacote'
info = tarfile.TarInfo(name); info.type = tarfile.SYMTYPE; info.linkname = target; info.mode = 0o777; tf.addfile(info)
entries.append({'path': name, 'kind': 'symlink', 'target': target, 'bytes': len(target), 'sha256': hashlib.sha256(target.encode()).hexdigest(), 'mode': 0o777})
raw = archive.read_bytes()
manifest = root / 'context-manifest.json'
manifest.write_text(json.dumps({'schema_version': 3, 'archive': {'path': archive.name, 'bytes': len(raw), 'sha256': hashlib.sha256(raw).hexdigest()}, 'files': entries}))
return manifest
class ContainerLinkTests(unittest.TestCase):
def test_declared_paired_link_preserves_exact_git_blob(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp); manifest = fixture(root)
materialize(manifest, root / 'out')
self.assertEqual(os.readlink(root / 'out/testbed/npm/cli/node_modules/pacote'), '/testbed/npm/pacote')
self.assertEqual((root / 'out/testbed/npm/pacote/package.json').read_bytes(), b'{}')
def test_undeclared_absolute_link_is_rejected(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
with self.assertRaises(ValueError): materialize(fixture(root, declared=False), root / 'out')
def test_declaration_cannot_authorize_an_unrelated_absolute_path(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
with self.assertRaises(ValueError): materialize(fixture(root, target='/etc'), root / 'out')
def test_wrong_git_blob_is_rejected(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
with self.assertRaises(ValueError): materialize(fixture(root, wrong_blob=True), root / 'out')
if __name__ == '__main__': unittest.main()
|