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.
299 lines
11 KiB
PHP
299 lines
11 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\Cloud\CloudPublicNetworkMutationAdmission;
|
|
use App\Services\Cloud\CloudPublicNetworkMutationDecision;
|
|
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);
|
|
}
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite', 'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$assert((new ReflectionClass(
|
|
CloudPublicNetworkMutationDecision::class
|
|
))->isReadOnly(), 'Admission decision is not immutable.');
|
|
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('status');
|
|
$table->string('service_status');
|
|
$table->integer('expires_at')->nullable();
|
|
});
|
|
$schema->create('cloud_instance_migrations', static function (
|
|
Blueprint $table
|
|
): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id');
|
|
$table->string('state');
|
|
});
|
|
$schema->create('cloud_backup_recoveries', static function (
|
|
Blueprint $table
|
|
): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id');
|
|
$table->string('state');
|
|
});
|
|
$schema->create('cloud_public_network_transitions', static function (
|
|
Blueprint $table
|
|
): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id');
|
|
$table->integer('active_transition')->nullable();
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('operation_id')->nullable();
|
|
$table->string('request_idempotency_key')->nullable();
|
|
$table->string('request_fingerprint')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->integer('parent_job_id')->nullable();
|
|
$table->integer('host_id')->nullable();
|
|
$table->string('kind');
|
|
$table->string('execution_mode')->nullable();
|
|
$table->string('phase');
|
|
$table->string('outcome')->nullable();
|
|
$table->string('status');
|
|
$table->integer('attempts')->nullable();
|
|
$table->integer('dispatch_attempts')->nullable();
|
|
$table->integer('dispatch_deadline_at')->nullable();
|
|
$table->integer('dispatch_started_at')->nullable();
|
|
$table->integer('agent_accepted_at')->nullable();
|
|
$table->integer('dispatched_at')->nullable();
|
|
$table->integer('receipt_deadline_at')->nullable();
|
|
$table->integer('terminal_at')->nullable();
|
|
$table->boolean('effect_may_have_applied')->nullable();
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
$table->string('effect_resolution')->nullable();
|
|
$table->integer('effect_resolved_at')->nullable();
|
|
$table->boolean('finalization_incident')->default(false);
|
|
$table->string('finalization_error_code')->nullable();
|
|
$table->string('completion_event_id')->nullable();
|
|
$table->string('error_code')->nullable();
|
|
$table->text('result_json')->nullable();
|
|
$table->integer('updated_at');
|
|
});
|
|
|
|
$now = time();
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 1,
|
|
'status' => 'active',
|
|
'service_status' => 'active',
|
|
'expires_at' => $now + 86400,
|
|
]);
|
|
$instance = static fn (): CloudInstance => CloudInstance::findOrFail(1);
|
|
$reason = static fn (): string =>
|
|
CloudPublicNetworkMutationAdmission::inspect($instance())->reasonCode;
|
|
|
|
// Exercise the same catalog reads directly so the fixture fails with the
|
|
// actual SQL error instead of being reduced to the public fail-closed code.
|
|
\App\Models\CloudInstanceMigration::where('instance_id', 1)
|
|
->whereIn(
|
|
'state',
|
|
\App\Services\Cloud\CloudInstanceMigrationWorkflow::blockingStates()
|
|
)->exists();
|
|
\App\Models\CloudBackupRecovery::where('instance_id', 1)
|
|
->whereIn('state', ['recovering', 'uncertain'])->exists();
|
|
\App\Services\Cloud\CloudJobService::blockingQueryForInstance(1)
|
|
->orderByDesc('id')->first(['id', 'kind', 'status', 'updated_at']);
|
|
|
|
$ready = CloudPublicNetworkMutationAdmission::inspect($instance());
|
|
$assert($ready->allowed()
|
|
&& $ready->reasonCode === CloudPublicNetworkMutationDecision::READY
|
|
&& $ready->message === '' && $ready->activeJob === null,
|
|
'A healthy instance was not admitted: ' . $ready->reasonCode);
|
|
|
|
Capsule::table('cloud_instances')->where('id', 1)->update([
|
|
'status' => 'stopped',
|
|
]);
|
|
$assert($reason() === CloudPublicNetworkMutationDecision::INSTANCE_INACTIVE,
|
|
'Inactive instance reason drifted.');
|
|
Capsule::table('cloud_instances')->where('id', 1)->update([
|
|
'status' => 'active', 'expires_at' => $now - 1,
|
|
]);
|
|
$assert($reason() === CloudPublicNetworkMutationDecision::SERVICE_UNAVAILABLE,
|
|
'Unavailable service reason drifted.');
|
|
Capsule::table('cloud_instances')->where('id', 1)->update([
|
|
'expires_at' => $now + 86400,
|
|
]);
|
|
|
|
Capsule::table('cloud_instance_migrations')->insert([
|
|
'id' => 1, 'instance_id' => 1, 'state' => 'preparing_source',
|
|
]);
|
|
$assert($reason() === CloudPublicNetworkMutationDecision::MIGRATION_ACTIVE,
|
|
'Maintenance migration did not fence public-network mutation.');
|
|
Capsule::table('cloud_instance_migrations')->delete();
|
|
|
|
Capsule::table('cloud_backup_recoveries')->insert([
|
|
'id' => 1, 'instance_id' => 1, 'state' => 'uncertain',
|
|
]);
|
|
$assert($reason()
|
|
=== CloudPublicNetworkMutationDecision::BACKUP_RECOVERY_ACTIVE,
|
|
'Backup recovery did not fence public-network mutation.');
|
|
Capsule::table('cloud_backup_recoveries')->delete();
|
|
|
|
Capsule::table('cloud_jobs')->insert([
|
|
'id' => 1,
|
|
'operation_id' => str_repeat('a', 32),
|
|
'request_idempotency_key' => 'admission-job-0001',
|
|
'request_fingerprint' => str_repeat('b', 64),
|
|
'instance_id' => 1,
|
|
'parent_job_id' => null,
|
|
'host_id' => 1,
|
|
'kind' => 'restart',
|
|
'execution_mode' => 'agent',
|
|
'phase' => 'active',
|
|
'outcome' => null,
|
|
'status' => 'running',
|
|
'attempts' => 1,
|
|
'dispatch_attempts' => 1,
|
|
'dispatch_deadline_at' => $now + 60,
|
|
'dispatch_started_at' => $now,
|
|
'agent_accepted_at' => $now,
|
|
'dispatched_at' => $now,
|
|
'receipt_deadline_at' => $now + 60,
|
|
'terminal_at' => null,
|
|
'effect_may_have_applied' => null,
|
|
'safe_to_resubmit' => null,
|
|
'effect_resolution' => 'not_applicable',
|
|
'effect_resolved_at' => null,
|
|
'finalization_incident' => 0,
|
|
'finalization_error_code' => null,
|
|
'completion_event_id' => null,
|
|
'error_code' => null,
|
|
'result_json' => null,
|
|
'updated_at' => $now,
|
|
]);
|
|
$job = CloudPublicNetworkMutationAdmission::inspect($instance());
|
|
$assert($job->reasonCode === CloudPublicNetworkMutationDecision::JOB_ACTIVE
|
|
&& ($job->activeJob['id'] ?? null) === '1',
|
|
'Blocking job did not project its immutable admission reason.');
|
|
Capsule::table('cloud_jobs')->delete();
|
|
|
|
Capsule::table('cloud_public_network_transitions')->insert([
|
|
'id' => 1, 'instance_id' => 1, 'active_transition' => 1,
|
|
]);
|
|
$assert($reason() === CloudPublicNetworkMutationDecision::TRANSITION_ACTIVE,
|
|
'Active transition did not fence public-network mutation.');
|
|
Capsule::table('cloud_public_network_transitions')->delete();
|
|
|
|
foreach ([2, 3, 4, 5] as $instanceId) {
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => $instanceId,
|
|
'status' => 'active',
|
|
'service_status' => 'active',
|
|
'expires_at' => $now + 86400,
|
|
]);
|
|
}
|
|
Capsule::table('cloud_instance_migrations')->insert([
|
|
'id' => 2, 'instance_id' => 2, 'state' => 'preparing_source',
|
|
]);
|
|
Capsule::table('cloud_backup_recoveries')->insert([
|
|
['id' => 2, 'instance_id' => 2, 'state' => 'recovering'],
|
|
['id' => 3, 'instance_id' => 3, 'state' => 'uncertain'],
|
|
]);
|
|
$jobRow = static function (int $id, int $instanceId) use ($now): array {
|
|
return [
|
|
'id' => $id,
|
|
'operation_id' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT),
|
|
'request_idempotency_key' => 'admission-job-' . str_pad(
|
|
(string) $id,
|
|
4,
|
|
'0',
|
|
STR_PAD_LEFT
|
|
),
|
|
'request_fingerprint' => hash('sha256', 'admission-' . $id),
|
|
'instance_id' => $instanceId,
|
|
'parent_job_id' => null,
|
|
'host_id' => 1,
|
|
'kind' => 'restart',
|
|
'execution_mode' => 'agent',
|
|
'phase' => 'active',
|
|
'outcome' => null,
|
|
'status' => 'running',
|
|
'attempts' => 1,
|
|
'dispatch_attempts' => 1,
|
|
'dispatch_deadline_at' => $now + 60,
|
|
'dispatch_started_at' => $now,
|
|
'agent_accepted_at' => $now,
|
|
'dispatched_at' => $now,
|
|
'receipt_deadline_at' => $now + 60,
|
|
'terminal_at' => null,
|
|
'effect_may_have_applied' => null,
|
|
'safe_to_resubmit' => null,
|
|
'effect_resolution' => 'not_applicable',
|
|
'effect_resolved_at' => null,
|
|
'finalization_incident' => 0,
|
|
'finalization_error_code' => null,
|
|
'completion_event_id' => null,
|
|
'error_code' => null,
|
|
'result_json' => null,
|
|
'updated_at' => $now + $id,
|
|
];
|
|
};
|
|
Capsule::table('cloud_jobs')->insert([
|
|
$jobRow(20, 2),
|
|
$jobRow(30, 3),
|
|
$jobRow(40, 4),
|
|
$jobRow(41, 4),
|
|
]);
|
|
Capsule::table('cloud_public_network_transitions')->insert([
|
|
['id' => 2, 'instance_id' => 2, 'active_transition' => 1],
|
|
['id' => 3, 'instance_id' => 3, 'active_transition' => 1],
|
|
['id' => 4, 'instance_id' => 4, 'active_transition' => 1],
|
|
['id' => 5, 'instance_id' => 5, 'active_transition' => 1],
|
|
]);
|
|
$manyInstances = CloudInstance::whereIn('id', [1, 2, 3, 4, 5])
|
|
->orderBy('id')->get();
|
|
$connection = Capsule::connection('default');
|
|
$connection->flushQueryLog();
|
|
$connection->enableQueryLog();
|
|
$many = CloudPublicNetworkMutationAdmission::inspectMany($manyInstances);
|
|
$queries = $connection->getQueryLog();
|
|
$connection->disableQueryLog();
|
|
$assert(count($queries) === 4,
|
|
'Multi-instance admission introduced N+1 catalog queries: '
|
|
. count($queries));
|
|
$assert($many[1]->reasonCode === CloudPublicNetworkMutationDecision::READY
|
|
&& $many[2]->reasonCode
|
|
=== CloudPublicNetworkMutationDecision::MIGRATION_ACTIVE
|
|
&& $many[3]->reasonCode
|
|
=== CloudPublicNetworkMutationDecision::BACKUP_RECOVERY_ACTIVE
|
|
&& $many[4]->reasonCode === CloudPublicNetworkMutationDecision::JOB_ACTIVE
|
|
&& ($many[4]->activeJob['id'] ?? null) === '41'
|
|
&& $many[5]->reasonCode
|
|
=== CloudPublicNetworkMutationDecision::TRANSITION_ACTIVE,
|
|
'Multi-instance admission priority or latest-job projection drifted.');
|
|
Capsule::table('cloud_instance_migrations')->delete();
|
|
Capsule::table('cloud_backup_recoveries')->delete();
|
|
Capsule::table('cloud_jobs')->delete();
|
|
Capsule::table('cloud_public_network_transitions')->delete();
|
|
|
|
$schema->drop('cloud_backup_recoveries');
|
|
$unavailable = CloudPublicNetworkMutationAdmission::inspect($instance());
|
|
$assert(!$unavailable->allowed()
|
|
&& $unavailable->reasonCode
|
|
=== CloudPublicNetworkMutationDecision::CATALOG_UNAVAILABLE,
|
|
'A missing runtime catalog failed open.');
|
|
try {
|
|
CloudPublicNetworkMutationAdmission::assertCanBegin($instance());
|
|
throw new RuntimeException('Missing catalog was accepted by writer seam.');
|
|
} catch (CloudError $error) {
|
|
$assert($error->httpStatus === 409,
|
|
'Admission writer seam returned the wrong status.');
|
|
}
|
|
|
|
echo "Cloud public-network mutation admission behavior passed.\n";
|