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/CloudObjectStorageKnownMult...

80 lines
2.4 KiB
PHP

<?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);
}
};
$sha256 = str_repeat('a', 64);
$requests = [];
$handler = static function (RequestInterface $request) use (
&$requests,
$sha256
) {
$requests[] = $request;
if ($request->getMethod() === 'POST') {
return Create::promiseFor(new Response(200, [],
'<InitiateMultipartUploadResult><UploadId>upload-1'
. '</UploadId></InitiateMultipartUploadResult>'));
}
return Create::promiseFor(new Response(200, [
'Content-Length' => '1048576',
'ETag' => '"known-etag"',
'x-amz-version-id' => 'known-version',
'x-amz-meta-virt-sha256' => $sha256,
]));
};
$client = new CloudObjectStorageS3Client(new Client([
'handler' => HandlerStack::create($handler),
]));
$config = [
'endpoint' => 'https://objects.example.com',
'region' => 'ewr',
'bucket' => 'template-bucket',
'prefix' => 'virt-cloud',
'path_style' => true,
];
$credentials = [
'access_key_id' => 'TEMPLATEACCESSKEY',
'secret_access_key' => 'template-secret',
];
$key = 'virt-cloud/cloud-templates/template/generation/system.qcow2';
$plan = $client->prepareKnownIdentityMultipartUpload(
$config,
$credentials,
$key,
1048576,
$sha256,
1793894400,
3600
);
$create = $requests[0] ?? null;
$assert($create instanceof RequestInterface
&& $create->getHeaderLine('x-amz-meta-virt-sha256') === $sha256
&& str_contains(
$create->getHeaderLine('Authorization'),
'x-amz-meta-virt-sha256'
), 'Known multipart creation did not sign immutable SHA metadata.');
$assert(($plan['upload_id'] ?? '') === 'upload-1'
&& ($plan['maximum_size_bytes'] ?? 0) === 1048576,
'Known multipart upload plan was not created.');
$identity = $client->inspectObject($config, $credentials, $key);
$assert(($identity['sha256'] ?? '') === $sha256
&& ($identity['size_bytes'] ?? 0) === 1048576
&& ($identity['version_id'] ?? '') === 'known-version',
'Known multipart HEAD did not preserve immutable identity.');
echo "Known-identity multipart publication passed.\n";