'https://cdn.example.net/image.qcow2']), new Response(200, [ 'Content-Length' => (string) strlen($body), 'Content-Encoding' => 'identity', ], $body), ])); $stack->push(Middleware::history($history)); $resolver = static fn (string $host): array => match ($host) { 'images.example.com' => ['93.184.216.34'], 'cdn.example.net' => ['1.1.1.1'], default => [], }; $fetcher = new CloudTemplateRemoteFetcher( new Client(['handler' => $stack]), $resolver ); $file = $fetcher->fetch( 'https://images.example.com/debian-13.qcow2', $digest ); try { $assert($file['size_bytes'] === strlen($body) && $file['sha256'] === $digest && hash_file('sha256', $file['path']) === $digest && count($history) === 2 && (string) $history[1]['request']->getUri() === 'https://cdn.example.net/image.qcow2', 'Remote template acquisition lost redirect, size or digest identity.'); } finally { @unlink($file['path']); } $blocked = false; try { (new CloudTemplateRemoteFetcher( new Client(['handler' => HandlerStack::create(new MockHandler([]))]), static fn (string $host): array => ['10.0.0.10'] ))->fetch('https://internal.example/image.qcow2'); } catch (CloudError $error) { $blocked = $error->httpStatus === 422; } $assert($blocked, 'Remote template acquisition accepted a private target.'); // A hard process stop can bypass the workflow finally block. The ordinary // worker must remove only old, regular template-download files and leave a // recent in-flight file untouched. $stale = tempnam(sys_get_temp_dir(), 'virt-cloud-template-'); $recent = tempnam(sys_get_temp_dir(), 'virt-cloud-template-'); if (!is_string($stale) || !is_string($recent)) { throw new RuntimeException('Could not create template cleanup fixtures.'); } $cleanupNow = time(); touch($stale, $cleanupNow - 90000); touch($recent, $cleanupNow - 60); try { for ($attempt = 0; $attempt < 128 && is_file($stale); $attempt++) { CloudTemplateRemoteFetcher::cleanupOneStaleFile($cleanupNow); } $assert(!is_file($stale) && is_file($recent), 'Template cleanup removed a live file or retained a crash orphan.'); } finally { @unlink($stale); @unlink($recent); } echo "Remote cloud-template acquisition contract passed.\n";