Files
leleka/scripts/test-linux-package.py
T
Pavlo Ratushnyi 7446c70dfa
Linux / verify (pull_request) Successful in 2m44s
Linux / keyless-cuda (pull_request) Skipped
Linux / hosted-cuda-package (pull_request) Skipped
Linux / linux-cli-container (pull_request) Successful in 21s
Linux / linux-static-cli (pull_request) Successful in 7m17s
Linux / release-cli (pull_request) Skipped
fix: probe CUDA engine in compatible runtime image
2026-10-06 06:41:34 +02:00

149 lines
7.1 KiB
Python

#!/usr/bin/env python3
"""Fault boundaries for offline package publication; real assembly runs in Linux CI."""
import hashlib
import importlib.util
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch
spec = importlib.util.spec_from_file_location('package', Path(__file__).with_name('build-linux-package.py'))
pkg = importlib.util.module_from_spec(spec)
spec.loader.exec_module(pkg)
class PackageTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.addCleanup(self.temp.cleanup)
self.root = Path(self.temp.name)
def test_snapshot_digest_exact_and_bad_pin(self):
source = self.root / 'engine'
payload = b'fixture' * 20000
source.write_bytes(payload)
digest = hashlib.sha256(payload).hexdigest()
out = self.root / 'copy'
self.assertEqual(pkg.snapshot(source, out, digest), len(payload))
self.assertEqual(out.read_bytes(), payload)
self.assertEqual(out.stat().st_mode & 0o777, 0o700)
for pin in ['0' * 64, 'bad']:
with self.assertRaises(ValueError):
pkg.snapshot(source, self.root / pin, pin)
def test_snapshot_rejects_oversize_and_nonregular_without_block(self):
source = self.root / 'engine'
source.write_bytes(b'over limit')
with patch.object(pkg, 'LIMIT', 4), self.assertRaisesRegex(ValueError, 'limit'):
pkg.snapshot(source, self.root / 'copy', hashlib.sha256(source.read_bytes()).hexdigest())
fifo = self.root / 'pipe'
os.mkfifo(fifo)
with self.assertRaisesRegex(ValueError, 'regular'):
pkg.snapshot(fifo, self.root / 'fifo-copy', '0' * 64)
def test_source_pin_and_dirty_tree(self):
subprocess.run(['git', 'init', '-q', self.root], check=True)
(self.root / 'source').write_text('initial')
subprocess.run(['git', '-C', self.root, 'add', '.'], check=True)
subprocess.run(['git', '-C', self.root, '-c', 'user.email=fixture@example.invalid', '-c',
'user.name=Fixture', 'commit', '-qm', 'fixture'], check=True)
revision = pkg.run(['git', '-C', self.root, 'rev-parse', 'HEAD'])
pkg.source_check(self.root, revision)
untracked = self.root / 'untracked-source'
untracked.write_text('not in revision')
with self.assertRaisesRegex(ValueError, 'clean'):
pkg.source_check(self.root, revision)
untracked.unlink()
for wrong in ['bad', '0' * 40]:
with self.assertRaises(ValueError):
pkg.source_check(self.root, wrong)
(self.root / 'source').write_text('changed')
with self.assertRaisesRegex(ValueError, 'clean'):
pkg.source_check(self.root, revision)
def test_lock_excludes_other_builder_and_symlink(self):
path = self.root / 'lock'
with pkg.lock(path), self.assertRaises(BlockingIOError):
with pkg.lock(path):
self.fail('second writer acquired lock')
alias = self.root / 'alias'
alias.symlink_to(path)
with self.assertRaises(OSError):
with pkg.lock(alias):
self.fail('symlink lock accepted')
def test_cuda_loader_probe_can_be_deferred_only_as_declared(self):
probe_binary = self.root / 'strata-next'
stage = self.root / 'stage'
stage.mkdir()
with patch.object(pkg, 'run') as run:
self.assertEqual(pkg.probe_engine_help(probe_binary, stage, {}, defer_to_runtime_image=True),
'deferred-to-runtime-image')
run.assert_not_called()
with patch.object(pkg, 'run') as run:
self.assertEqual(pkg.probe_engine_help(probe_binary, stage, {}, defer_to_runtime_image=False),
'passed-on-package-builder-host')
run.assert_called_once()
self.assertTrue((stage / 'probe.json').is_file())
@unittest.skipUnless(sys.platform == 'linux', 'Linux atomic publication')
def test_atomic_publication_never_replaces_existing_directory(self):
staged, output = self.root / 'stage', self.root / 'out'
staged.mkdir()
(staged / 'sentinel').write_text('new')
output.mkdir()
with self.assertRaises(FileExistsError):
pkg.publish(staged, output)
self.assertEqual(list(output.iterdir()), [])
output.rmdir()
pkg.publish(staged, output)
self.assertFalse(staged.exists())
self.assertEqual((output / 'sentinel').read_text(), 'new')
def test_outer_elf_rejects_loader_or_shared_library(self):
for outputs in [('INTERP', ''), ('', '(NEEDED)')]:
with patch.object(pkg, 'run', side_effect=outputs), self.assertRaises(ValueError):
pkg.static_outer(Path('fixture'))
with patch.object(pkg, 'run', side_effect=['static headers', 'no dynamic section']):
self.assertIn('program_headers', pkg.static_outer(Path('fixture')))
def test_failed_build_cleans_stage_and_does_not_publish(self):
from types import SimpleNamespace
engine = self.root / 'engine'
engine.write_bytes(b'fixture')
output = self.root / 'final'
args = SimpleNamespace(engine=engine, sha256=pkg.sha256(engine), revision='a' * 40,
output=output, cargo_target_dir=self.root / 'target', cpu_fixture=True)
with patch.object(pkg.platform, 'system', return_value='Linux'), patch.object(pkg.platform, 'machine', return_value='x86_64'), patch.object(pkg, 'source_check'), patch.object(pkg.subprocess, 'run', side_effect=subprocess.CalledProcessError(1, 'cargo')):
with self.assertRaises(subprocess.CalledProcessError):
pkg.assemble(args)
self.assertFalse(output.exists())
self.assertFalse(list(self.root.glob('.final.stage-*')))
output.mkdir()
(output / 'old').write_text('keep')
with patch.object(pkg.platform, 'system', return_value='Linux'), patch.object(pkg.platform, 'machine', return_value='x86_64'), patch.object(pkg, 'source_check'), patch.object(pkg.subprocess, 'run') as build:
with self.assertRaisesRegex(ValueError, 'already exists'):
pkg.assemble(args)
build.assert_not_called()
self.assertEqual((output / 'old').read_text(), 'keep')
def test_cpu_fixture_cannot_defer_loader_probe(self):
from types import SimpleNamespace
args = SimpleNamespace(engine=self.root/'engine', sha256='0'*64, revision='a'*40,
output=self.root/'out', cargo_target_dir=self.root/'target',
cpu_fixture=True, defer_engine_help_probe_to_runtime_image=True)
with patch.object(pkg.platform, 'system', return_value='Linux'), \
patch.object(pkg.platform, 'machine', return_value='x86_64'), \
patch.object(pkg.subprocess, 'run') as build:
with self.assertRaisesRegex(ValueError, 'only CUDA package builds'):
pkg.assemble(args)
build.assert_not_called()
if __name__ == '__main__':
unittest.main()