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.
328 lines
13 KiB
PHP
328 lines
13 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudInstance;
|
|
use App\Services\Cloud\CloudInstanceMetadata;
|
|
use App\Services\Cloud\CloudInstanceRuntimeService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$assert = static function ($condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$assert(CloudInstanceMetadata::state('shutdown') === 'shutdown'
|
|
&& CloudInstanceRuntimeService::normalizeState('shutdown') === 'shutdown'
|
|
&& CloudInstanceMetadata::state('pmsuspended') === 'pmsuspended'
|
|
&& CloudInstanceRuntimeService::normalizeState('pmsuspended') === 'pmsuspended',
|
|
'Graceful shutdown was collapsed into an unknown or terminal state.');
|
|
$assert(CloudInstanceRuntimeService::stateAfterSuccessfulAction('running', 'shutdown')
|
|
=== 'shutdown'
|
|
&& CloudInstanceRuntimeService::stateAfterSuccessfulAction(null, 'shutdown')
|
|
=== 'shutdown'
|
|
&& CloudInstanceRuntimeService::stateAfterSuccessfulAction('shutdown', 'shutdown')
|
|
=== 'shutdown'
|
|
&& CloudInstanceRuntimeService::stateAfterSuccessfulAction('shutoff', 'shutdown')
|
|
=== 'stopped'
|
|
&& CloudInstanceRuntimeService::stateAfterSuccessfulAction(null, 'poweroff')
|
|
=== 'stopped',
|
|
'Successful power actions confused accepted graceful shutdown with confirmed shutoff.');
|
|
$workerSource = file_get_contents(
|
|
dirname(__DIR__) . '/app/Services/Cloud/CloudWorkerService.php'
|
|
);
|
|
$assert(is_string($workerSource)
|
|
&& str_contains($workerSource,
|
|
'CloudInstanceRuntimeService::stateAfterSuccessfulAction(')
|
|
&& !str_contains($workerSource, "'shutdown', 'poweroff' => 'stopped'"),
|
|
'The worker bypassed graceful-shutdown transition normalization.');
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schema = $capsule->schema('default');
|
|
$schema->create('cloud_instances', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->string('uuid');
|
|
$table->integer('host_id');
|
|
$table->integer('user_id');
|
|
$table->string('name');
|
|
$table->string('status');
|
|
$table->text('spec_json');
|
|
$table->integer('updated_at');
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table): void {
|
|
$table->increments('id');
|
|
$table->integer('instance_id')->nullable();
|
|
$table->string('kind');
|
|
$table->string('execution_mode')->default('agent');
|
|
$table->string('phase');
|
|
$table->string('outcome')->nullable();
|
|
$table->string('status')->default('active');
|
|
$table->integer('dispatch_attempts')->default(0);
|
|
$table->string('error_code')->nullable();
|
|
$table->boolean('effect_may_have_applied')->nullable();
|
|
$table->boolean('safe_to_resubmit')->nullable();
|
|
$table->string('effect_resolution')->default('not_applicable');
|
|
$table->boolean('finalization_incident')->default(false);
|
|
$table->string('finalization_error_code')->nullable();
|
|
$table->integer('host_id')->nullable();
|
|
$table->integer('attempts')->default(0);
|
|
$table->string('operation_id')->nullable();
|
|
$table->string('request_fingerprint')->nullable();
|
|
$table->string('request_idempotency_key')->nullable();
|
|
$table->integer('dispatch_started_at')->nullable();
|
|
$table->integer('agent_accepted_at')->nullable();
|
|
$table->integer('dispatched_at')->nullable();
|
|
$table->integer('dispatch_deadline_at')->nullable();
|
|
$table->integer('receipt_deadline_at')->nullable();
|
|
$table->string('completion_event_id')->nullable();
|
|
$table->text('result_json')->nullable();
|
|
$table->integer('terminal_at')->nullable();
|
|
});
|
|
|
|
$firstUuid = '11111111-1111-4111-8111-111111111111';
|
|
$secondUuid = '22222222-2222-4222-8222-222222222222';
|
|
$publicNetwork = [
|
|
'contract' => 1,
|
|
'network_selector' => 'bridge:br0',
|
|
'ipv4' => [
|
|
'pool_key' => str_repeat('1', 32),
|
|
'pool_cidr' => '8.8.8.0/24',
|
|
'address_cidr' => '8.8.8.20/24',
|
|
'gateway' => '8.8.8.1',
|
|
'gateway_on_link' => false,
|
|
],
|
|
'ipv6' => [
|
|
'pool_key' => str_repeat('2', 32),
|
|
'pool_cidr' => '2404:c140:156:3a::/64',
|
|
'address_cidr' => '2404:c140:156:3a::20/64',
|
|
'gateway' => '2404:c140:156::1',
|
|
'gateway_on_link' => true,
|
|
],
|
|
'dns_servers' => ['1.1.1.1', '2606:4700:4700::1111'],
|
|
];
|
|
$publicNetworkProjection = CloudInstanceMetadata::publicSpec([
|
|
'public_network' => $publicNetwork,
|
|
]);
|
|
foreach ([1 => $firstUuid, 2 => $secondUuid] as $id => $uuid) {
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => $id,
|
|
'uuid' => $uuid,
|
|
'host_id' => 7,
|
|
'user_id' => $id,
|
|
'name' => 'vm-' . $id,
|
|
'status' => 'active',
|
|
'spec_json' => json_encode([
|
|
'plan_id' => 91,
|
|
'validity_days' => 30,
|
|
'traffic_quota_bytes' => 1099511627776,
|
|
'template_id' => 'alma10-template',
|
|
'state' => 'running',
|
|
'console_port' => 5999,
|
|
'password_available' => true,
|
|
'public_network' => $publicNetwork,
|
|
], JSON_THROW_ON_ERROR),
|
|
'updated_at' => 1,
|
|
]);
|
|
}
|
|
$damagedUuid = '33333333-3333-4333-8333-333333333333';
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 3, 'uuid' => $damagedUuid, 'host_id' => 7, 'user_id' => 1,
|
|
'name' => 'damaged-vm', 'status' => 'active',
|
|
'spec_json' => '{damaged-business-snapshot', 'updated_at' => 1,
|
|
]);
|
|
|
|
$live = [
|
|
'uuid' => $firstUuid,
|
|
'name' => 'engine-name',
|
|
'state' => 'running',
|
|
'vcpu' => 4,
|
|
'memory_mib' => 4096,
|
|
'disks' => [
|
|
['capacity_gib' => 40, 'source' => '/home/kvm/private.qcow2',
|
|
'target' => 'vda', 'format' => 'qcow2'],
|
|
['size_gib' => '2.5', 'source' => 'secret-pool-volume'],
|
|
],
|
|
'autostart' => true,
|
|
'managed' => true,
|
|
'public_ip_cidr' => '203.0.113.18/28',
|
|
'os_source' => 'cloud_template',
|
|
'os_name' => 'AlmaLinux 10',
|
|
'hostname' => 'node-01.example.test',
|
|
'install_iso' => 'must-not-win.iso',
|
|
'login_name' => 'root',
|
|
'console_port' => 5908,
|
|
'credential_generation' => 'panel-seed-secret.iso',
|
|
'password_available' => true,
|
|
'unexpected' => ['private_key' => 'must-not-persist'],
|
|
];
|
|
|
|
$view = CloudInstanceMetadata::publicView(array_merge([
|
|
'id' => '1', 'api_id' => '1', 'uuid' => $firstUuid,
|
|
'name' => 'database-name', 'disk_gib' => 20,
|
|
], $publicNetworkProjection), $live);
|
|
$assert($view['id'] === '1' && $view['uuid'] === $firstUuid
|
|
&& $view['name'] === 'database-name'
|
|
&& $view['public_ipv4_cidr'] === '8.8.8.20/24'
|
|
&& $view['public_ipv6'] === '2404:c140:156:3a::20'
|
|
&& $view['os_source'] === 'cloud_template'
|
|
&& $view['os_name'] === 'AlmaLinux 10'
|
|
&& $view['login_name'] === 'root'
|
|
&& $view['guest_hostname'] === 'node-01.example.test'
|
|
&& $view['agent_metadata_version'] === 1
|
|
&& $view['disk_gib'] === 42.5,
|
|
'Matching Agent metadata was not exposed without replacing the database display name.');
|
|
$assert(array_keys($view['disks']) === [0, 1]
|
|
&& array_keys($view['disks'][0]) === ['capacity_gib']
|
|
&& !array_key_exists('state', $view)
|
|
&& !array_key_exists('console_port', $view)
|
|
&& !array_key_exists('hostname', $view)
|
|
&& !array_key_exists('credential_generation', $view)
|
|
&& !array_key_exists('password_available', $view)
|
|
&& !array_key_exists('install_iso', $view),
|
|
'The public VM metadata view leaked dynamic, storage or credential fields.');
|
|
|
|
$publicSpec = CloudInstanceMetadata::publicSpec([
|
|
'region' => 'Tokyo',
|
|
'country_code' => 'JP',
|
|
'host_id' => 7,
|
|
'host_name' => 'internal-host-name',
|
|
'resource_pool_id' => 9,
|
|
'resource_pool_name' => 'internal-resource-pool',
|
|
'pool_id' => 9,
|
|
'pool_name' => 'internal-resource-pool',
|
|
'line_id' => 3,
|
|
'line_name' => 'premium-line',
|
|
]);
|
|
$assert(($publicSpec['region'] ?? null) === 'Tokyo'
|
|
&& ($publicSpec['country_code'] ?? null) === 'JP'
|
|
&& !array_key_exists('host_id', $publicSpec)
|
|
&& !array_key_exists('host_name', $publicSpec)
|
|
&& !array_key_exists('resource_pool_id', $publicSpec)
|
|
&& !array_key_exists('resource_pool_name', $publicSpec)
|
|
&& !array_key_exists('pool_id', $publicSpec)
|
|
&& !array_key_exists('pool_name', $publicSpec)
|
|
&& !array_key_exists('line_id', $publicSpec)
|
|
&& !array_key_exists('line_name', $publicSpec),
|
|
'The public specification exposed physical placement or internal sales topology.');
|
|
|
|
$mismatch = CloudInstanceMetadata::publicView(array_merge([
|
|
'id' => '1', 'uuid' => $firstUuid, 'name' => 'database-name',
|
|
], $publicNetworkProjection), array_replace($live, ['uuid' => $secondUuid]));
|
|
$assert($mismatch['name'] === 'database-name'
|
|
&& $mismatch['public_ipv4_cidr'] === '8.8.8.20/24'
|
|
&& $mismatch['public_ipv6'] === '2404:c140:156:3a::20'
|
|
&& !isset($mismatch['os_name'], $mismatch['agent_metadata_version']),
|
|
'A mismatched live UUID decorated another database instance.');
|
|
|
|
$operatorUuid = 'aaaaaaaa-aaaa-0aaa-0aaa-aaaaaaaaaaaa';
|
|
$operatorView = CloudInstanceMetadata::publicView([
|
|
'id' => '9', 'uuid' => $operatorUuid, 'name' => 'operator-vm',
|
|
], array_replace($live, ['uuid' => $operatorUuid]));
|
|
$assert(($operatorView['os_name'] ?? null) === 'AlmaLinux 10',
|
|
'A canonical libvirt UUID was rejected solely for non-RFC version nibbles.');
|
|
|
|
// The Go Agent intentionally reports libvirt/runtime metadata only. Template
|
|
// presentation and panel ownership stay authoritative in the PHP snapshot.
|
|
$goWireLive = $live;
|
|
unset($goWireLive['managed'], $goWireLive['os_source'],
|
|
$goWireLive['os_name'], $goWireLive['login_name'], $goWireLive['hostname']);
|
|
$phpOwnedMerge = CloudInstanceMetadata::merge([
|
|
'source' => 'cloud',
|
|
'template_id' => 'alma10-template',
|
|
'os_source' => 'cloud_template',
|
|
'os_name' => 'AlmaLinux 10',
|
|
'login_name' => 'root',
|
|
'managed' => true,
|
|
'public_network' => $publicNetwork,
|
|
], $goWireLive);
|
|
$assert(($phpOwnedMerge['os_source'] ?? null) === 'cloud_template'
|
|
&& ($phpOwnedMerge['os_name'] ?? null) === 'AlmaLinux 10'
|
|
&& ($phpOwnedMerge['login_name'] ?? null) === 'root'
|
|
&& ($phpOwnedMerge['managed'] ?? null) === true
|
|
&& ($phpOwnedMerge['public_network'] ?? null) === $publicNetwork
|
|
&& !isset($phpOwnedMerge['public_ip_cidr'])
|
|
&& ($phpOwnedMerge['agent_metadata_version'] ?? null) === 1
|
|
&& !isset($phpOwnedMerge['disks'][0]['source']),
|
|
'A real Go VMMetadata response discarded PHP-owned template metadata.');
|
|
|
|
$emptyMerge = CloudInstanceMetadata::merge([
|
|
'plan_id' => 91,
|
|
'state' => 'running',
|
|
'agent_metadata_version' => 1,
|
|
], []);
|
|
$assert(($emptyMerge['plan_id'] ?? null) === 91
|
|
&& ($emptyMerge['agent_metadata_version'] ?? null) === 1
|
|
&& !isset($emptyMerge['state']),
|
|
'An empty live result discarded a valid metadata marker or skipped sanitization.');
|
|
|
|
// Only the actor-scoped snapshot is eligible even when the live inventory
|
|
// contains another tenant's UUID.
|
|
CloudInstanceMetadata::persistBatch([
|
|
['id' => '1', 'uuid' => $firstUuid, 'name' => 'vm-1'],
|
|
['id' => '3', 'uuid' => $damagedUuid, 'name' => 'damaged-vm'],
|
|
], [
|
|
$firstUuid => $live,
|
|
$secondUuid => array_replace($live, [
|
|
'uuid' => $secondUuid,
|
|
'public_ip_cidr' => '203.0.113.19/28',
|
|
]),
|
|
$damagedUuid => array_replace($live, ['uuid' => $damagedUuid]),
|
|
]);
|
|
CloudInstanceMetadata::persistBatch([
|
|
['id' => '2', 'uuid' => $secondUuid, 'name' => 'vm-2'],
|
|
], [
|
|
// A numeric/list key must not be accepted merely because the row itself
|
|
// contains a syntactically valid UUID.
|
|
array_replace($live, ['uuid' => $secondUuid]),
|
|
]);
|
|
|
|
$first = json_decode((string) CloudInstance::find(1)->spec_json, true,
|
|
flags: JSON_THROW_ON_ERROR);
|
|
$second = json_decode((string) CloudInstance::find(2)->spec_json, true,
|
|
flags: JSON_THROW_ON_ERROR);
|
|
$assert($first['plan_id'] === 91 && $first['validity_days'] === 30
|
|
&& $first['traffic_quota_bytes'] === 1099511627776
|
|
&& $first['template_id'] === 'alma10-template'
|
|
&& $first['agent_metadata_version'] === 1
|
|
&& $first['public_network'] === $publicNetwork
|
|
&& !isset($first['public_ip_cidr'])
|
|
&& $first['login_name'] === 'root'
|
|
&& $first['guest_hostname'] === 'node-01.example.test'
|
|
&& $first['disks'] === [
|
|
['capacity_gib' => 40], ['capacity_gib' => 2.5],
|
|
]
|
|
&& !isset($first['state'], $first['console_port'],
|
|
$first['credential_generation'], $first['unexpected'],
|
|
$first['install_iso']),
|
|
'Persisted metadata lost business fields or escaped the static allowlist.');
|
|
$assert(!isset($second['agent_metadata_version'], $second['public_ip_cidr']),
|
|
'Batch backfill crossed the caller-provided instance scope.');
|
|
$assert(CloudInstance::find(3)->spec_json === '{damaged-business-snapshot',
|
|
'Batch backfill overwrote a damaged business specification.');
|
|
|
|
$beforeUpgradeRefresh = (string) CloudInstance::find(1)->spec_json;
|
|
Capsule::table('cloud_jobs')->insert([
|
|
'instance_id' => 1,
|
|
'kind' => 'instance_upgrade',
|
|
'phase' => 'accepted',
|
|
'status' => 'accepted',
|
|
]);
|
|
CloudInstanceMetadata::persistBatch([
|
|
['id' => '1', 'uuid' => $firstUuid, 'name' => 'vm-1'],
|
|
], [
|
|
$firstUuid => array_replace($live, [
|
|
'vcpu' => 8,
|
|
'memory_mib' => 16384,
|
|
'disks' => [['capacity_gib' => 160]],
|
|
]),
|
|
]);
|
|
$assert((string) CloudInstance::find(1)->spec_json === $beforeUpgradeRefresh,
|
|
'Agent metadata rewrote the source specification during a blocking upgrade.');
|
|
|
|
echo "Cloud instance metadata tests passed.\n";
|