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

166 lines
6.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudOperationCompletionService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$validate = new ReflectionMethod(CloudOperationCompletionService::class,
'validate');
$base = [
'type' => 'completion',
'id' => str_repeat('e', 32),
'agent_id' => '7',
'operation_id' => str_repeat('a', 32),
'status' => 'succeeded',
'effect_may_have_applied' => false,
'safe_to_resubmit' => false,
'error_code' => '',
'result' => ['uuid' => 'test'],
];
$valid = $validate->invoke(null, $base);
$assert(($valid['event_id'] ?? null) === $base['id']
&& ($valid['host_id'] ?? null) === 7
&& ($valid['status'] ?? null) === 'succeeded',
'A valid immutable completion receipt was rejected or altered.');
$reject = static function (array $receipt) use ($validate): bool {
try {
$validate->invoke(null, $receipt);
return false;
} catch (ReflectionException $error) {
throw $error;
} catch (Throwable $error) {
$cause = $error instanceof ReflectionException
? $error : ($error->getPrevious() ?? $error);
return $cause instanceof CloudError
&& $cause->httpStatus === 400;
}
};
$invalid = $base;
$invalid['effect_may_have_applied'] = true;
$assert($reject($invalid),
'A succeeded receipt claimed an ambiguous effect.');
$invalid = $base;
$invalid['status'] = 'failed';
$assert($reject($invalid),
'A failed receipt without a stable error code was accepted.');
$invalid['error_code'] = 'transport_lost';
$invalid['effect_may_have_applied'] = true;
$invalid['safe_to_resubmit'] = true;
$assert($reject($invalid),
'An ambiguous failed receipt was marked safe to resubmit.');
$validFailure = $invalid;
$validFailure['safe_to_resubmit'] = false;
$validFailure = $validate->invoke(null, $validFailure);
$assert(($validFailure['status'] ?? null) === 'failed'
&& ($validFailure['effect_may_have_applied'] ?? null) === true
&& ($validFailure['safe_to_resubmit'] ?? null) === false,
'A conservative terminal failure receipt was rejected.');
// Crash window: finish() committed the terminal task and completion_event_id,
// but PHP died before marking the durable receipt applied. The same immutable
// receipt must repair that marker; it is not a late or rejected completion.
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_jobs', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id')->nullable();
$table->string('operation_id');
$table->string('execution_mode');
$table->string('phase');
$table->string('outcome')->nullable();
$table->integer('dispatch_attempts');
$table->string('completion_event_id')->nullable();
$table->boolean('finalization_incident')->default(0);
$table->string('finalization_error_code')->nullable();
});
$schema->create('cloud_agent_completion_events',
static function (Blueprint $table): void {
$table->string('event_id')->primary();
$table->integer('host_id');
$table->string('operation_id');
$table->string('payload_sha256');
$table->string('outcome');
$table->boolean('effect_may_have_applied');
$table->boolean('safe_to_resubmit');
$table->string('error_code');
$table->text('result_json')->nullable();
$table->string('disposition');
$table->integer('received_at');
$table->integer('applied_at')->nullable();
$table->unique(['host_id', 'operation_id']);
});
$normalized = $validate->invoke(null, $base);
$payloadHash = hash('sha256', json_encode($normalized,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
Capsule::table('cloud_jobs')->insert([
'id' => 1, 'host_id' => 7, 'operation_id' => $base['operation_id'],
'execution_mode' => 'agent', 'phase' => 'terminal',
'outcome' => 'succeeded', 'dispatch_attempts' => 1,
'completion_event_id' => $base['id'],
]);
Capsule::table('cloud_agent_completion_events')->insert([
'event_id' => $base['id'], 'host_id' => 7,
'operation_id' => $base['operation_id'],
'payload_sha256' => $payloadHash, 'outcome' => 'succeeded',
'effect_may_have_applied' => 0, 'safe_to_resubmit' => 0,
'error_code' => '',
'result_json' => json_encode(['uuid' => 'test'], JSON_THROW_ON_ERROR),
'disposition' => 'received', 'received_at' => time(),
'applied_at' => null,
]);
$replayed = CloudOperationCompletionService::accept($base);
$event = Capsule::table('cloud_agent_completion_events')
->where('event_id', $base['id'])->first();
$assert($replayed === ['accepted' => true, 'disposition' => 'applied']
&& $event !== null && $event->disposition === 'applied'
&& (int) $event->applied_at > 0,
'A post-commit receipt retransmission was misclassified after the PHP crash window.');
$late = $base;
$late['id'] = str_repeat('f', 32);
$late['operation_id'] = str_repeat('b', 32);
$lateNormalized = $validate->invoke(null, $late);
$lateHash = hash('sha256', json_encode($lateNormalized,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
Capsule::table('cloud_jobs')->insert([
'id' => 2, 'host_id' => 7, 'operation_id' => $late['operation_id'],
'execution_mode' => 'agent', 'phase' => 'terminal',
'outcome' => 'failed', 'dispatch_attempts' => 1,
'completion_event_id' => str_repeat('c', 32),
]);
Capsule::table('cloud_agent_completion_events')->insert([
'event_id' => $late['id'], 'host_id' => 7,
'operation_id' => $late['operation_id'],
'payload_sha256' => $lateHash, 'outcome' => 'succeeded',
'effect_may_have_applied' => 0, 'safe_to_resubmit' => 0,
'error_code' => '',
'result_json' => json_encode(['uuid' => 'test'], JSON_THROW_ON_ERROR),
'disposition' => 'received', 'received_at' => time(),
'applied_at' => null,
]);
$lateResult = CloudOperationCompletionService::accept($late);
$assert($lateResult === ['accepted' => true, 'disposition' => 'late'],
'A different receipt overwrote an already-terminal task.');
echo "Cloud completion receipt contract passed.\n";