You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudObjectStorageImmutable...

80 lines
2.4 KiB
PHTML

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudObjectStorageS3Client;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Promise\Create;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$path = tempnam(sys_get_temp_dir(), 'virt-template-put-');
if ($path === false) {
throw new RuntimeException('Cannot create immutable PUT fixture.');
}
file_put_contents($path, str_repeat('immutable-put-fixture-', 128));
$bytes = filesize($path);
$sha256 = hash_file('sha256', $path);
// A real HTTP handler may release its request body before request() returns.
// Closing the request body here deterministically reproduces that ownership
// boundary without crossing the network.
$requestHeaders = [];
$handler = static function (
RequestInterface $request,
array $options
) use (&$requestHeaders) {
$requestHeaders = $request->getHeaders();
$request->getBody()->close();
return Create::promiseFor(new Response(200, [
'ETag' => '"closed-body-etag"',
'x-amz-version-id' => 'closed-body-version',
]));
};
$client = new CloudObjectStorageS3Client(new Client([
'handler' => HandlerStack::create($handler),
]));
try {
$identity = $client->putImmutableFile(
[
'endpoint' => 'https://objects.example.com',
'region' => 'ewr',
'bucket' => 'template-bucket',
'prefix' => 'virt-cloud',
'path_style' => true,
],
[
'access_key_id' => 'TEMPLATEACCESSKEY',
'secret_access_key' => 'template-secret',
],
'virt-cloud/cloud-templates/template/generation/system.qcow2',
$path,
$bytes,
$sha256
);
} finally {
@unlink($path);
}
$assert($identity === [
'size_bytes' => $bytes,
'sha256' => $sha256,
'etag' => '"closed-body-etag"',
'version_id' => 'closed-body-version',
], 'Immutable PUT double-closed an HTTP-owned body stream.');
$assert(($requestHeaders['x-amz-meta-virt-sha256'][0] ?? '') === $sha256
&& str_contains(
(string) ($requestHeaders['Authorization'][0] ?? ''),
'x-amz-meta-virt-sha256'
), 'Immutable PUT did not sign its frozen SHA-256 metadata.');
echo "Immutable object PUT stream ownership passed.\n";