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/CloudVpcRootDeletionTest.php

429 lines
19 KiB
PHP

<?php
use App\Models\CloudVpc;
use App\Models\CloudVpcHostDeployment;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudVpcService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$root = dirname(__DIR__);
$serviceSource = (string) file_get_contents(
$root . '/app/Services/Cloud/CloudVpcService.php'
);
$digestFixture = json_decode((string) file_get_contents(
dirname($root) . '/engine/tests/fixtures/vpc_delete_digest.json'
), true);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert(str_contains($serviceSource, "'vpc_delete'")
&& str_contains($serviceSource, 'deletionDocument')
&& str_contains($serviceSource, 'recordDeleted')
&& str_contains($serviceSource, "->where('state', '<>', 'deleted')")
&& str_contains($serviceSource, 'removed_host_ids')
&& str_contains($serviceSource, 'CloudPrivateCidrAllocator::release'),
'VPC root deletion lost its tombstone, receipt or CIDR-release boundary.');
$assert(is_array($digestFixture)
&& is_array($digestFixture['document'] ?? null)
&& preg_match('/^[0-9a-f]{64}$/D',
(string) ($digestFixture['expected_digest'] ?? '')) === 1,
'The shared PHP/engine deletion digest fixture is missing or malformed.');
$missingHost = strpos(
$serviceSource, '$item = $hosts[(string) $hostId] ?? null;'
);
$missingHostEnd = $missingHost === false ? false
: strpos($serviceSource, '// A deleting host remains a tombstone', $missingHost);
$missingHostBlock = ($missingHost !== false && $missingHostEnd !== false)
? substr($serviceSource, $missingHost, $missingHostEnd - $missingHost) : '';
$assert($missingHostBlock !== ''
&& str_contains($missingHostBlock, '$hasRemovalTarget = true')
&& str_contains($missingHostBlock, '$allRemovalProof = false'),
'A missing deleting host can still be mistaken for complete multi-host removal.');
if (!is_file($root . '/vendor/autoload.php')) {
echo "VPC root deletion source contracts passed (runtime skipped).\n";
return;
}
require $root . '/vendor/autoload.php';
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_regions', static function (Blueprint $table): void {
$table->increments('id');
});
$schema->create('cloud_vpcs', static function (Blueprint $table): void {
$table->increments('id');
$table->string('vpc_key', 32)->unique();
$table->unsignedInteger('user_id');
$table->unsignedInteger('region_id');
$table->unsignedInteger('private_network_id');
$table->string('name', 63);
$table->string('cidr', 18);
$table->string('fabric_id', 64);
$table->string('fabric_region', 64);
$table->unsignedBigInteger('fabric_revision');
$table->unsignedInteger('max_members');
$table->string('state', 16);
$table->unsignedBigInteger('desired_revision');
$table->unsignedBigInteger('observed_revision');
$table->string('desired_digest', 64);
$table->string('observed_digest', 64)->nullable();
$table->string('sync_state', 16);
$table->string('last_operation_id', 32)->nullable();
$table->string('last_error_code', 64)->nullable();
$table->unsignedBigInteger('last_success_at')->default(0);
$table->unsignedBigInteger('last_failure_at')->default(0);
$table->unsignedBigInteger('created_at');
$table->unsignedBigInteger('updated_at');
});
foreach ([
'cloud_vpc_subnets' => 'subnet_key',
'cloud_vpc_routes' => 'route_key',
'cloud_vpc_acl_rules' => 'rule_key',
] as $tableName => $keyColumn) {
$schema->create($tableName, static function (Blueprint $table) use ($keyColumn): void {
$table->increments('id');
$table->unsignedInteger('vpc_id');
$table->string($keyColumn, 32);
$table->string('state', 16);
if ($keyColumn === 'rule_key') {
$table->unsignedInteger('priority')->default(1);
}
});
}
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('uuid', 36);
});
$schema->create('cloud_vpc_members', static function (Blueprint $table): void {
$table->increments('id');
$table->unsignedInteger('vpc_id');
$table->string('member_key', 32);
$table->unsignedInteger('subnet_id')->nullable();
$table->unsignedInteger('private_ip_id')->nullable();
$table->unsignedInteger('instance_id');
$table->unsignedInteger('host_id');
$table->string('private_ip', 15);
$table->string('state', 16);
$table->unsignedBigInteger('desired_revision')->default(0);
$table->string('desired_digest', 64)->nullable();
$table->unsignedBigInteger('observed_revision')->default(0);
$table->string('observed_digest', 64)->nullable();
$table->string('last_operation_id', 32)->nullable();
$table->string('last_error_code', 64)->nullable();
$table->unsignedBigInteger('updated_at')->default(0);
$table->unique('instance_id');
});
$schema->create('cloud_vpc_private_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->unsignedInteger('vpc_id');
$table->unsignedInteger('subnet_id')->nullable();
$table->unsignedInteger('member_id')->nullable();
$table->unsignedInteger('instance_id')->nullable();
$table->string('private_ip', 15);
$table->string('state', 16);
$table->unsignedBigInteger('desired_revision')->default(0);
$table->string('desired_digest', 64)->nullable();
$table->unsignedBigInteger('observed_revision')->default(0);
$table->string('observed_digest', 64)->nullable();
$table->string('last_error_code', 64)->nullable();
$table->unsignedBigInteger('updated_at')->default(0);
});
$schema->create('cloud_vpc_host_deployments', static function (Blueprint $table): void {
$table->increments('id');
$table->unsignedInteger('vpc_id');
$table->unsignedInteger('host_id');
$table->string('state', 16);
$table->unsignedBigInteger('observed_revision')->default(0);
$table->string('observed_digest', 64)->nullable();
$table->string('last_error_code', 64)->nullable();
$table->unsignedBigInteger('last_success_at')->default(0);
$table->unsignedBigInteger('updated_at')->default(0);
});
$schema->create('cloud_vpc_operations', static function (Blueprint $table): void {
$table->increments('id');
$table->string('operation_id', 32)->unique();
$table->unsignedInteger('vpc_id');
$table->unsignedInteger('user_id');
$table->string('kind', 24);
$table->unsignedBigInteger('desired_revision');
$table->string('status', 16);
$table->text('payload_json');
$table->text('observed_json')->nullable();
$table->unsignedSmallInteger('evidence_version')->default(0);
$table->string('error_code', 64)->nullable();
$table->string('lease_token', 32)->nullable();
$table->unsignedBigInteger('lease_until')->default(0);
$table->unsignedBigInteger('finished_at')->nullable();
$table->unsignedBigInteger('updated_at');
});
$schema->create('cloud_private_networks', static function (Blueprint $table): void {
$table->increments('id');
$table->string('vpc_key', 32)->unique();
$table->string('allocation_key', 32)->unique();
$table->unsignedInteger('user_id');
$table->unsignedInteger('region_id');
$table->string('cidr', 18);
$table->string('active_cidr', 18)->nullable();
$table->unsignedBigInteger('network_address');
$table->unsignedBigInteger('broadcast_address');
$table->unsignedTinyInteger('prefix_length');
$table->string('state', 16);
$table->unsignedBigInteger('revision');
$table->unsignedBigInteger('released_at')->nullable();
$table->unsignedBigInteger('updated_at');
});
$schema->create('cloud_private_cidr_claims', static function (Blueprint $table): void {
$table->increments('id');
$table->string('block_cidr', 18)->unique();
$table->string('allocation_key', 32);
});
Capsule::table('cloud_regions')->insert(['id' => 1]);
$vpc = new CloudVpc();
$vpc->id = 7;
$vpc->vpc_key = str_repeat('a', 32);
$vpc->region_id = 1;
$vpc->fabric_region = 'test-region';
$vpc->fabric_id = 'test-fabric';
$vpc->fabric_revision = 3;
$vpc->cidr = '10.20.0.0/24';
$vpc->desired_revision = 9;
$method = new ReflectionMethod(CloudVpcService::class, 'deletionDocument');
$canonicalize = new ReflectionMethod(CloudVpcService::class, 'canonicalize');
$historical = new CloudVpcHostDeployment();
$historical->vpc_id = 7;
$historical->host_id = 11;
$historical->state = 'deleted';
$historical->save();
$document = $method->invoke(null, $vpc);
$assert($document['hosts'] === [],
'A historical deleted host was reintroduced into the engine delete target.');
$pending = new CloudVpcHostDeployment();
$pending->vpc_id = 7;
$pending->host_id = 2;
$pending->state = 'active';
$pending->save();
$document = $method->invoke(null, $vpc);
$assert($document['hosts'] === [[
'host_id' => '2', 'members' => [],
]], 'The delete target did not contain exactly the unresolved host subset.');
$historical->state = 'active';
$historical->save();
$document = $method->invoke(null, $vpc);
$assert($document['hosts'] === [
['host_id' => '11', 'members' => []],
['host_id' => '2', 'members' => []],
], 'Delete host IDs were not canonicalized with lexical string ordering.');
$historical->state = 'deleted';
$historical->save();
$vpcKey = str_repeat('a', 32);
$operationId = str_repeat('b', 32);
$leaseToken = str_repeat('c', 32);
$allocationKey = str_repeat('d', 32);
Capsule::table('cloud_vpcs')->insert([
'id' => 7, 'vpc_key' => $vpcKey, 'user_id' => 9, 'region_id' => 1,
'private_network_id' => 20, 'name' => 'delete-test',
'cidr' => '10.20.0.0/24', 'fabric_id' => 'test-fabric',
'fabric_region' => 'test-region', 'fabric_revision' => 3,
'max_members' => 32, 'state' => 'deleting', 'desired_revision' => 10,
'observed_revision' => 9, 'desired_digest' => str_repeat('e', 64),
'observed_digest' => str_repeat('f', 64), 'sync_state' => 'pending',
'last_operation_id' => $operationId, 'last_error_code' => null,
'last_success_at' => 0, 'last_failure_at' => 0,
'created_at' => time(), 'updated_at' => time(),
]);
Capsule::table('cloud_vpc_host_deployments')->where('host_id', 2)->update([
'state' => 'deleting',
]);
Capsule::table('cloud_instances')->insert([
'id' => 30, 'uuid' => '11111111-2222-4333-8444-555555555555',
]);
$memberKey = str_repeat('1', 32);
Capsule::table('cloud_vpc_members')->insert([
'id' => 32, 'vpc_id' => 7, 'member_key' => $memberKey,
'subnet_id' => null, 'private_ip_id' => 31, 'instance_id' => 30, 'host_id' => 2,
'private_ip' => '10.20.0.10', 'state' => 'deleting',
'desired_revision' => 10, 'desired_digest' => str_repeat('e', 64),
'observed_revision' => 9, 'observed_digest' => str_repeat('f', 64),
'last_operation_id' => str_repeat('8', 32),
'last_error_code' => null, 'updated_at' => time(),
]);
Capsule::table('cloud_vpc_private_ips')->insert([
'id' => 31, 'vpc_id' => 7, 'subnet_id' => null,
'member_id' => 32, 'instance_id' => 30,
'private_ip' => '10.20.0.10', 'state' => 'deleting',
'desired_revision' => 10, 'desired_digest' => str_repeat('e', 64),
'observed_revision' => 9, 'observed_digest' => str_repeat('f', 64),
'last_error_code' => null, 'updated_at' => time(),
]);
$persistedVpc = CloudVpc::findOrFail(7);
Capsule::table('cloud_vpc_host_deployments')->where('host_id', 11)->update([
'state' => 'deleting',
]);
$fixtureDocument = $method->invoke(null, $persistedVpc);
$fixtureDigest = hash('sha256', json_encode(
$fixtureDocument, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
));
$fixtureTarget = $digestFixture['document'];
unset($fixtureTarget['operation_id']);
$assert($fixtureDocument === $canonicalize->invoke(null, $fixtureTarget)
&& $fixtureDigest === $digestFixture['expected_digest'],
'PHP deletion canonicalization drifted from the shared engine digest fixture.');
Capsule::table('cloud_vpc_host_deployments')->where('host_id', 11)->update([
'state' => 'deleted',
]);
$deleteDocument = $method->invoke(null, $persistedVpc);
$assert($deleteDocument['hosts'] === [[
'host_id' => '2', 'members' => [],
]], 'The final delete plan reintroduced a historical deleted host.');
$assert(count($deleteDocument['retiring_members']) === 1
&& $deleteDocument['retiring_members'][0]['member_id'] === $memberKey
&& $deleteDocument['retiring_members'][0]['instance_uuid']
=== '11111111-2222-4333-8444-555555555555'
&& $deleteDocument['retiring_members'][0]['host_id'] === '2'
&& $deleteDocument['retiring_members'][0]['private_ip_cidr']
=== '10.20.0.10/32'
&& preg_match('/^(?:[0-9a-f]{2}:){5}[0-9a-f]{2}$/D',
$deleteDocument['retiring_members'][0]['private_mac']) === 1,
'Delete manifest did not preserve the exact retiring private NIC identity.');
$digest = hash('sha256', json_encode(
$deleteDocument, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
));
Capsule::table('cloud_vpcs')->where('id', 7)->update([
'desired_digest' => $digest,
]);
Capsule::table('cloud_vpc_members')->where('id', 32)->update([
'desired_digest' => $digest,
]);
Capsule::table('cloud_vpc_private_ips')->where('id', 31)->update([
'desired_digest' => $digest,
]);
$lifecycleOperationId = str_repeat('9', 32);
$assert(CloudVpcService::recordPrivateObservation(30, [
'ok' => true,
'state' => [
'desired_state' => 'removed',
'desired_revision' => 10,
'desired_digest' => $digest,
'observed_revision' => 10,
'observed_digest' => $digest,
],
'error_code' => null,
], $lifecycleOperationId) === true,
'An exact lifecycle readback was not recorded during root deletion.');
$assert(Capsule::table('cloud_vpc_members')->where('id', 32)->value('state')
=== 'deleting'
&& Capsule::table('cloud_vpc_private_ips')->where('id', 31)->value('state')
=== 'deleting'
&& Capsule::table('cloud_vpc_members')->where('id', 32)
->value('last_operation_id') === str_repeat('8', 32),
'A lifecycle readback shrank the immutable root-delete manifest or replaced its proof marker.');
$operationPayload = $deleteDocument;
$operationPayload['desired_digest'] = $digest;
$operationPayload['operation_id'] = $operationId;
Capsule::table('cloud_vpc_operations')->insert([
'operation_id' => $operationId, 'vpc_id' => 7, 'user_id' => 9,
'kind' => 'vpc_delete', 'desired_revision' => 10, 'status' => 'running',
'payload_json' => json_encode($operationPayload,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES),
'observed_json' => null, 'error_code' => null, 'lease_token' => $leaseToken,
'lease_until' => time() + 120, 'finished_at' => null, 'updated_at' => time(),
]);
Capsule::table('cloud_private_networks')->insert([
'id' => 20, 'vpc_key' => $vpcKey, 'allocation_key' => $allocationKey,
'user_id' => 9, 'region_id' => 1, 'cidr' => '10.20.0.0/24',
'active_cidr' => '10.20.0.0/24', 'network_address' => 169082880,
'broadcast_address' => 169083135, 'prefix_length' => 24,
'state' => 'active', 'revision' => 1, 'released_at' => null,
'updated_at' => time(),
]);
Capsule::table('cloud_private_cidr_claims')->insert([
'block_cidr' => '10.20.0.0/24', 'allocation_key' => $allocationKey,
]);
$missingPendingReceipt = [
'vpc_id' => $vpcKey, 'operation_id' => $operationId,
'desired_revision' => 10, 'desired_digest' => $digest,
'state' => 'deleted', 'deleted' => true, 'readback' => 'confirmed',
'removed_host_ids' => [], 'completed_at' => time(),
];
try {
CloudVpcService::recordDeleted(
$vpcKey, 10, $digest, $missingPendingReceipt, $operationId, $leaseToken
);
throw new RuntimeException('A receipt missing the pending host was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'Missing-pending-host receipt returned the wrong status.');
}
try {
CloudVpcService::recordDeleted($vpcKey, 10, $digest, [
'vpc_id' => $vpcKey, 'operation_id' => $operationId,
'desired_revision' => 10, 'desired_digest' => $digest,
'state' => 'deleted', 'deleted' => true, 'readback' => 'confirmed',
'removed_host_ids' => ['2', '99'], 'completed_at' => time(),
], $operationId, $leaseToken);
throw new RuntimeException('A receipt containing an unknown host was accepted.');
} catch (CloudError $error) {
$assert($error->httpStatus === 502,
'Unknown-host receipt returned the wrong status.');
}
$assert(Capsule::table('cloud_vpcs')->where('id', 7)->value('state') === 'deleting'
&& Capsule::table('cloud_private_networks')->where('id', 20)->value('state')
=== 'active',
'Incomplete host receipt partially finalized root or CIDR state.');
CloudVpcService::recordDeleted($vpcKey, 10, $digest, [
'vpc_id' => $vpcKey, 'operation_id' => $operationId,
'desired_revision' => 10, 'desired_digest' => $digest,
'state' => 'deleted', 'deleted' => true, 'readback' => 'confirmed',
// Host 11 was already exactly retired and may no longer exist in the
// engine's latest durable record. Only the signed pending host is
// mandatory in this receipt.
'removed_host_ids' => ['2'], 'completed_at' => time(),
], $operationId, $leaseToken);
$storedReceipt = json_decode((string) Capsule::table('cloud_vpc_operations')
->where('operation_id', $operationId)->value('observed_json'), true);
$assert(Capsule::table('cloud_vpcs')->where('id', 7)->value('state') === 'deleted'
&& Capsule::table('cloud_vpc_operations')->where('operation_id', $operationId)
->value('status') === 'succeeded'
&& ($storedReceipt['removed_host_ids'] ?? null) === ['2'],
'Exact delete receipt did not finalize the VPC root and operation atomically.');
$assert(Capsule::table('cloud_private_networks')->where('id', 20)->value('state')
=== 'released'
&& Capsule::table('cloud_private_networks')->where('id', 20)
->value('active_cidr') === null
&& Capsule::table('cloud_private_cidr_claims')->count() === 0,
'Exact delete receipt did not release the CIDR claim.');
$assert(CloudVpcHostDeployment::where('vpc_id', 7)
->where('state', '<>', 'deleted')->count() === 0,
'Exact delete receipt left a host deployment outside deleted state.');
$assert((int) CloudVpcHostDeployment::where('vpc_id', 7)
->where('host_id', 11)->value('observed_revision') === 0
&& (int) CloudVpcHostDeployment::where('vpc_id', 7)
->where('host_id', 2)->value('observed_revision') === 10,
'Root finalization overwrote an omitted historical proof or missed the pending host.');
$assert(Capsule::table('cloud_vpc_members')->where('vpc_id', 7)->count() === 0
&& Capsule::table('cloud_vpc_private_ips')->where('vpc_id', 7)->count() === 0,
'Exact delete receipt did not physically clear the child identity ledgers.');
Capsule::table('cloud_vpc_members')->insert([
'vpc_id' => 8, 'member_key' => str_repeat('2', 32),
'private_ip_id' => null, 'instance_id' => 30, 'host_id' => 2,
'private_ip' => '10.21.0.10', 'state' => 'applying',
]);
$assert(Capsule::table('cloud_vpc_members')->where('instance_id', 30)->count() === 1,
'A completed root delete still occupied the global VM membership key.');
echo "VPC root deletion contracts passed.\n";