fix: preserve cloud image upload responses

dev
admin 2 months ago
parent b2654590c1
commit a6cccf5951

@ -272,7 +272,12 @@ final class CloudEngineClient
try {
return $this->send('POST', $base . $path, ['headers' => $headers, 'body' => $stream, 'timeout' => 900], null, $path);
} finally {
fclose($stream);
// Guzzle's PSR-7 stream owns and may close the supplied resource
// before request() returns. Avoid replacing the real engine
// response with a PHP 8 TypeError from closing it a second time.
if (is_resource($stream)) {
fclose($stream);
}
}
}

@ -40,12 +40,20 @@ namespace GuzzleHttp {
class Client
{
public int $closedUploadBodies = 0;
public function __construct(private array $responses = [])
{
}
public function request(string $method, string $url, array $options): TestResponse
{
// Match Guzzle PSR-7 ownership: a resource supplied as the body
// may already be closed by the time request() returns.
if (is_resource($options['body'] ?? null)) {
fclose($options['body']);
$this->closedUploadBodies++;
}
$response = array_shift($this->responses);
if (!$response instanceof TestResponse) {
throw new \RuntimeException('No fake engine response was queued.');
@ -151,5 +159,39 @@ try {
'An arbitrary engine error escaped instead of the generic failure.');
}
$uploadFile = tempnam(sys_get_temp_dir(), 'cloud-upload-contract-');
if ($uploadFile === false) {
throw new RuntimeException('Unable to prepare the upload resource contract fixture.');
}
if (file_put_contents($uploadFile, 'qcow2-contract') === false) {
@unlink($uploadFile);
throw new RuntimeException('Unable to write the upload resource contract fixture.');
}
try {
$uploadTransport = new Client([
new TestResponse(202, '{"job_id":"upload-contract"}'),
]);
$uploadClient = new CloudEngineClient($uploadTransport);
$uploadResult = $uploadClient->upload($uploadPath, $actor, $uploadFile);
$assert(($uploadResult['job_id'] ?? null) === 'upload-contract'
&& $uploadTransport->closedUploadBodies === 1,
'A client-owned upload stream replaced a successful engine response with TypeError.');
$failedUploadTransport = new Client([
new TestResponse(422, json_encode(['error' => $uploadMessage], JSON_UNESCAPED_UNICODE)),
]);
$failedUploadClient = new CloudEngineClient($failedUploadTransport);
try {
$failedUploadClient->upload($uploadPath, $actor, $uploadFile);
throw new RuntimeException('A rejected cloud-template upload did not raise CloudError.');
} catch (CloudError $error) {
$assert($error->getMessage() === $uploadMessage && $error->httpStatus === 422
&& $failedUploadTransport->closedUploadBodies === 1,
'Closing a client-owned upload stream masked the reviewed engine error.');
}
} finally {
@unlink($uploadFile);
}
echo "Cloud-template engine error allowlist contract passed.\n";
}

Loading…
Cancel
Save