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.
115 lines
5.0 KiB
PHTML
115 lines
5.0 KiB
PHTML
|
4 weeks ago
|
<?php
|
||
|
|
|
||
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
||
|
|
|
||
|
|
use App\Services\Cloud\CloudAgentClient;
|
||
|
|
use App\Services\Cloud\CloudAgentError;
|
||
|
|
use App\Services\Cloud\CloudAgentGatewayEndpoint;
|
||
|
|
use GuzzleHttp\Client;
|
||
|
|
use GuzzleHttp\Exception\ConnectException;
|
||
|
|
use GuzzleHttp\HandlerStack;
|
||
|
|
use GuzzleHttp\Promise\Create;
|
||
|
|
use GuzzleHttp\TransferStats;
|
||
|
|
|
||
|
|
$assert = static function (bool $condition, string $message): void {
|
||
|
|
if (!$condition) {
|
||
|
|
throw new RuntimeException($message);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
$notSent = [
|
||
|
|
'errno' => 7,
|
||
|
|
'url' => CloudAgentGatewayEndpoint::URL,
|
||
|
|
'request_size' => 0,
|
||
|
|
'size_upload' => 0.0,
|
||
|
|
'size_download' => 0.0,
|
||
|
|
'header_size' => 0,
|
||
|
|
'http_code' => 0,
|
||
|
|
'redirect_count' => 0,
|
||
|
|
'connect_time' => 0.0,
|
||
|
|
'pretransfer_time' => 0.0,
|
||
|
|
'starttransfer_time' => 0.0,
|
||
|
|
];
|
||
|
|
$cases = [
|
||
|
|
'gateway stopped' => [$notSent, true],
|
||
|
|
'failed connect includes elapsed error timings' => [array_replace($notSent, [
|
||
|
|
'pretransfer_time' => 0.00013, 'starttransfer_time' => 0.00013,
|
||
|
|
]), true],
|
||
|
|
'connect timeout before HTTP' => [array_replace($notSent, ['errno' => 28]), true],
|
||
|
|
'response timeout after send' => [array_replace($notSent, [
|
||
|
|
'errno' => 28, 'request_size' => 750, 'size_upload' => 500.0,
|
||
|
|
'connect_time' => 0.001, 'pretransfer_time' => 0.002,
|
||
|
|
]), false],
|
||
|
|
'reused connection sent request' => [array_replace($notSent, [
|
||
|
|
'errno' => 28, 'request_size' => 750,
|
||
|
|
]), false],
|
||
|
|
'empty response is not absence proof' => [array_replace($notSent, ['errno' => 52]), false],
|
||
|
|
'exception has no transport evidence' => [[], false],
|
||
|
|
'missing byte counter' => [array_diff_key($notSent, ['request_size' => true]), false],
|
||
|
|
'invalid byte counter' => [array_replace($notSent, ['size_upload' => '0']), false],
|
||
|
|
'partial HTTP headers' => [array_replace($notSent, ['request_size' => 1]), false],
|
||
|
|
'established connection timeout' => [array_replace($notSent, [
|
||
|
|
'errno' => 28, 'connect_time' => 0.001,
|
||
|
|
]), false],
|
||
|
|
'unexpected redirect' => [array_replace($notSent, ['redirect_count' => 1]), false],
|
||
|
|
'wrong endpoint' => [array_replace($notSent, ['url' => 'http://127.0.0.1:8002/']), false],
|
||
|
|
];
|
||
|
|
foreach ($cases as $name => [$context, $safe]) {
|
||
|
|
$calls = 0;
|
||
|
|
$handler = static function ($request, array $options) use ($context, &$calls) {
|
||
|
|
$calls++;
|
||
|
|
if (isset($options['on_stats'])) {
|
||
|
|
$options['on_stats'](new TransferStats($request, null, 0.01,
|
||
|
|
$context['errno'] ?? null, $context));
|
||
|
|
}
|
||
|
|
return Create::rejectionFor(new ConnectException(
|
||
|
|
'private upstream diagnostic', $request, null, $context
|
||
|
|
));
|
||
|
|
};
|
||
|
|
$client = new CloudAgentClient('42', str_repeat('a', 32),
|
||
|
|
new Client(['handler' => HandlerStack::create($handler)]), 20,
|
||
|
|
CloudAgentGatewayEndpoint::URL, str_repeat('g', 32));
|
||
|
|
try {
|
||
|
|
$client->request('POST', '/v1/vms/vm-test/actions', [
|
||
|
|
'user_id' => 17, 'is_admin' => false,
|
||
|
|
'is_super_admin' => false, 'can_manage' => false,
|
||
|
|
'operation_id' => 'transport-proof-12345678',
|
||
|
|
], ['operation_id' => 'transport-proof-12345678', 'action' => 'start']);
|
||
|
|
throw new RuntimeException($name . ': failure was accepted');
|
||
|
|
} catch (CloudAgentError $error) {
|
||
|
|
$assert($error->safePreDispatchFailure === $safe,
|
||
|
|
$name . ': incorrect no-dispatch classification');
|
||
|
|
$assert(!$error->safeValidationFailure && $calls === 1,
|
||
|
|
$name . ': mutation was replayed or confused with validation');
|
||
|
|
$assert(!str_contains($error->getMessage(), 'private upstream'),
|
||
|
|
$name . ': raw transport diagnostic leaked');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Guzzle can make another transfer after a failed rewind. Evidence about
|
||
|
|
// only its last connect attempt must not erase an earlier possible send.
|
||
|
|
$handler = static function ($request, array $options) use ($notSent) {
|
||
|
|
if (isset($options['on_stats'])) {
|
||
|
|
$options['on_stats'](new TransferStats($request, null, 0.01, 65,
|
||
|
|
array_replace($notSent, ['request_size' => 750, 'size_upload' => 500.0])));
|
||
|
|
$options['on_stats'](new TransferStats($request, null, 0.01, 7, $notSent));
|
||
|
|
}
|
||
|
|
return Create::rejectionFor(new ConnectException('connect refused', $request,
|
||
|
|
null, $notSent));
|
||
|
|
};
|
||
|
|
$client = new CloudAgentClient('42', str_repeat('a', 32),
|
||
|
|
new Client(['handler' => HandlerStack::create($handler)]), 20,
|
||
|
|
CloudAgentGatewayEndpoint::URL, str_repeat('g', 32));
|
||
|
|
try {
|
||
|
|
$client->request('POST', '/v1/vms', [
|
||
|
|
'user_id' => 17, 'is_admin' => false,
|
||
|
|
'is_super_admin' => false, 'can_manage' => false,
|
||
|
|
'operation_id' => 'transport-proof-12345678',
|
||
|
|
], ['operation_id' => 'transport-proof-12345678']);
|
||
|
|
throw new RuntimeException('multi-transfer failure was accepted');
|
||
|
|
} catch (CloudAgentError $error) {
|
||
|
|
$assert(!$error->safePreDispatchFailure,
|
||
|
|
'Earlier possible dispatch was erased by a later connection failure');
|
||
|
|
}
|
||
|
|
|
||
|
|
echo "Cloud Agent transport failure tests passed.\n";
|