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.
334 lines
16 KiB
PHP
334 lines
16 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\CloudBackupRestoreAttempt;
|
|
use App\Models\CloudInstanceMigrationSourceDebt;
|
|
use App\Models\CloudResourceReservation;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
|
$root = dirname(__DIR__);
|
|
$schema = file_get_contents($root . '/sql/cloud.sql');
|
|
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
|
|
$reservationModel = file_get_contents(
|
|
$root . '/app/Models/CloudResourceReservation.php'
|
|
);
|
|
$attemptModel = file_get_contents(
|
|
$root . '/app/Models/CloudBackupRestoreAttempt.php'
|
|
);
|
|
$debtModel = file_get_contents(
|
|
$root . '/app/Models/CloudInstanceMigrationSourceDebt.php'
|
|
);
|
|
$coordinator = file_get_contents(
|
|
$root . '/app/Services/Cloud/CloudPortableRestoreAttemptCoordinator.php'
|
|
);
|
|
$allocator = file_get_contents(
|
|
$root . '/app/Services/Cloud/CloudPublicNetworkAllocationService.php'
|
|
);
|
|
if (!is_string($schema) || !is_string($setup)
|
|
|| !is_string($reservationModel) || !is_string($attemptModel)
|
|
|| !is_string($debtModel) || !is_string($coordinator)
|
|
|| !is_string($allocator)) {
|
|
throw new RuntimeException('Restore/migration ledger sources are unreadable.');
|
|
}
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$table = static function (string $name) use ($schema): string {
|
|
$start = strpos($schema, 'CREATE TABLE ' . $name . ' (');
|
|
if ($start === false) {
|
|
return '';
|
|
}
|
|
$end = strpos(
|
|
$schema,
|
|
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;',
|
|
$start
|
|
);
|
|
return $end === false ? '' : substr($schema, $start, $end - $start);
|
|
};
|
|
$normalized = static function (string $value): string {
|
|
return trim((string) preg_replace('/\s+/', ' ', $value));
|
|
};
|
|
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection([
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
], 'default');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$schemaBuilder = $capsule->schema('default');
|
|
$schemaBuilder->create('cloud_backup_restore_attempts', static function (
|
|
$table
|
|
): void {
|
|
$table->integer('id')->primary();
|
|
$table->string('attempt_key');
|
|
$table->integer('reservation_id');
|
|
});
|
|
$schemaBuilder->create('cloud_instance_migration_source_debts', static function (
|
|
$table
|
|
): void {
|
|
$table->integer('id')->primary();
|
|
$table->integer('source_reservation_id');
|
|
});
|
|
$schemaBuilder->create('cloud_resource_reservations', static function (
|
|
$table
|
|
): void {
|
|
$table->integer('id')->primary();
|
|
$table->string('backup_restore_attempt_key')->nullable();
|
|
$table->integer('migration_source_debt_id')->nullable();
|
|
});
|
|
|
|
$attempts = $table('cloud_backup_restore_attempts');
|
|
$restores = $table('cloud_backup_restores');
|
|
$debts = $table('cloud_instance_migration_source_debts');
|
|
$reservations = $table('cloud_resource_reservations');
|
|
$assert($attempts !== ''
|
|
&& str_contains($attempts,
|
|
'reservation_id BIGINT UNSIGNED NOT NULL')
|
|
&& str_contains($attempts,
|
|
'UNIQUE KEY cloud_backup_restore_attempt_generation')
|
|
&& str_contains($attempts,
|
|
'UNIQUE KEY cloud_backup_restore_attempt_active')
|
|
&& str_contains($attempts,
|
|
'UNIQUE KEY cloud_backup_restore_attempt_operation (operation_id)')
|
|
&& str_contains($attempts,
|
|
'target_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($attempts,
|
|
'target_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($attempts,
|
|
'target_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($attempts,
|
|
'target_plan_json LONGTEXT NOT NULL')
|
|
&& str_contains($attempts,
|
|
'target_plan_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($attempts,
|
|
'CONSTRAINT chk_cloud_backup_restore_attempt_plan CHECK')
|
|
&& str_contains($attempts,
|
|
'CONSTRAINT chk_cloud_backup_restore_attempt_evidence CHECK')
|
|
&& str_contains($attempts,
|
|
'CONSTRAINT chk_cloud_backup_restore_attempt_shape CHECK'),
|
|
'Backup restore attempts do not fence immutable generations and network identity.');
|
|
$attemptShape = $normalized($attempts);
|
|
$assert(str_contains($attemptShape,
|
|
"state = 'reserved' AND job_id IS NULL AND effect_resolution = 'not_applicable' AND evidence_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1")
|
|
&& str_contains($attemptShape,
|
|
"state IN ('queued','awaiting_receipt') AND job_id IS NOT NULL AND effect_resolution = 'not_applicable' AND evidence_resolution = 'not_applicable' AND active_attempt IS NOT NULL AND active_attempt = 1")
|
|
&& str_contains($attemptShape,
|
|
"state = 'effect_unknown' AND effect_resolution = 'unknown' AND evidence_resolution = 'unknown' AND job_id IS NOT NULL AND active_attempt IS NOT NULL AND active_attempt = 1")
|
|
&& str_contains($attemptShape,
|
|
"state = 'finalization_pending' AND job_id IS NOT NULL AND effect_resolution IN ('applied','not_applied') AND evidence_resolution = effect_resolution AND active_attempt IS NOT NULL AND active_attempt = 1")
|
|
&& str_contains($attemptShape,
|
|
"state = 'succeeded' AND effect_resolution = 'applied' AND evidence_resolution = 'applied' AND job_id IS NOT NULL AND active_attempt IS NULL")
|
|
&& str_contains($attemptShape,
|
|
"state = 'failed' AND effect_resolution = 'not_applied' AND evidence_resolution = 'not_applied' AND job_id IS NOT NULL AND active_attempt IS NULL"),
|
|
'Restore-attempt terminal and unknown-effect fencing can diverge.');
|
|
|
|
$assert($restores !== ''
|
|
&& str_contains($restores,
|
|
'requested_by_user_id BIGINT UNSIGNED DEFAULT NULL')
|
|
&& str_contains($restores,
|
|
'request_idempotency_key VARCHAR(128) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
|
&& str_contains($restores,
|
|
'request_fingerprint CHAR(64) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
|
&& str_contains($restores, 'selected_plan_json LONGTEXT DEFAULT NULL')
|
|
&& str_contains($restores,
|
|
'selected_plan_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
|
&& str_contains($restores,
|
|
'UNIQUE KEY cloud_backup_restore_request')
|
|
&& str_contains($restores,
|
|
'CONSTRAINT chk_cloud_backup_restore_request CHECK'),
|
|
'Portable restore request identity and selected plan are not durable.');
|
|
|
|
$assert($debts !== ''
|
|
&& str_contains($debts,
|
|
'UNIQUE KEY cloud_migration_source_debt_migration (migration_id)')
|
|
&& str_contains($debts,
|
|
'UNIQUE KEY cloud_migration_source_debt_active_reservation')
|
|
&& str_contains($debts,
|
|
'source_identity_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($debts,
|
|
'source_claim_set_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($debts,
|
|
'source_public_network_sha256 CHAR(64) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
|
|
&& str_contains($debts,
|
|
"state IN ('prepared','retained','cleanup_running','released',")
|
|
&& str_contains($debts,
|
|
'CONSTRAINT chk_cloud_migration_source_debt_shape CHECK'),
|
|
'Maintenance migration source cleanup debt is not durable and exclusive.');
|
|
$debtShape = $normalized($debts);
|
|
$assert(str_contains($debtShape,
|
|
"(cleanup_operation_id IS NULL AND cleanup_job_id IS NULL) OR (cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL)")
|
|
&& str_contains($debtShape,
|
|
"state IN ('prepared','retained') AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NULL")
|
|
&& str_contains($debtShape,
|
|
"state = 'cleanup_running' AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NULL")
|
|
&& str_contains($debtShape,
|
|
"state = 'attention' AND active_debt IS NOT NULL AND active_debt = 1 AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NULL")
|
|
&& str_contains($debtShape,
|
|
"state = 'released' AND active_debt IS NULL AND cleanup_operation_id IS NOT NULL AND cleanup_job_id IS NOT NULL AND resolved_at IS NOT NULL")
|
|
&& str_contains($debtShape,
|
|
"state = 'rollback_restored' AND active_debt IS NULL AND cleanup_operation_id IS NULL AND cleanup_job_id IS NULL AND resolved_at IS NOT NULL"),
|
|
'Source debt can become terminal without confirmed resolution.');
|
|
// MySQL accepts CHECK expressions that evaluate to UNKNOWN. Every live-state
|
|
// branch must therefore reject NULL explicitly before comparing the nullable
|
|
// unique-index fence with 1.
|
|
$assert(!str_contains($attemptShape,
|
|
"effect_resolution = 'unknown' AND active_attempt = 1")
|
|
&& !str_contains($debtShape,
|
|
"state = 'attention' AND active_debt = 1"),
|
|
'A nullable active fence can satisfy MySQL CHECK through UNKNOWN.');
|
|
|
|
$assert($reservations !== ''
|
|
&& str_contains($reservations,
|
|
'backup_restore_attempt_key CHAR(32) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL')
|
|
&& str_contains($reservations,
|
|
'migration_source_debt_id BIGINT UNSIGNED DEFAULT NULL')
|
|
&& str_contains($reservations,
|
|
'UNIQUE KEY cloud_resource_reservation_backup_attempt')
|
|
&& str_contains($reservations,
|
|
'UNIQUE KEY cloud_resource_reservation_migration_debt')
|
|
&& !str_contains($reservations, 'backup_restore_attempt_id')
|
|
&& substr_count($reservations,
|
|
'backup_restore_attempt_key IS NOT NULL') === 1
|
|
&& substr_count($reservations, 'migration_source_debt_id IS NOT NULL') === 1,
|
|
'Reservation ownership does not enforce one source ledger.');
|
|
|
|
foreach ([
|
|
"'cloud_backup_restore_attempts'",
|
|
"'cloud_instance_migration_source_debts'",
|
|
"'backup_restore_attempt_key'",
|
|
"'migration_source_debt_id'",
|
|
"'chk_cloud_backup_restore_attempt_shape'",
|
|
"'chk_cloud_backup_restore_attempt_evidence'",
|
|
"'chk_cloud_backup_restore_attempt_plan'",
|
|
"'chk_cloud_backup_restore_request'",
|
|
"'chk_cloud_migration_source_debt_shape'",
|
|
"'chk_cloud_migration_source_debt_cleanup_pair'",
|
|
"'cloud_backup_restore_attempt_active'",
|
|
"'cloud_backup_restore_request'",
|
|
"'cloud_migration_source_debt_active_reservation'",
|
|
"'columns' => ['backup_restore_attempt_key']",
|
|
"'columns' => ['migration_source_debt_id']",
|
|
] as $required) {
|
|
$assert(str_contains($setup, $required),
|
|
'CloudSetup omits restore/migration ledger contract: ' . $required);
|
|
}
|
|
$assert(str_contains($setup,
|
|
"SCHEMA_VERSION = 'go-agent-wss-v52-daily-traffic-settlement'")
|
|
&& str_contains($attemptModel,
|
|
"protected \$table = 'cloud_backup_restore_attempts';")
|
|
&& str_contains($attemptModel, "'active_attempt' => 'int'")
|
|
&& str_contains($debtModel,
|
|
"protected \$table = 'cloud_instance_migration_source_debts';")
|
|
&& str_contains($debtModel, "'active_debt' => 'int'")
|
|
&& str_contains($reservationModel,
|
|
"function backupRestoreAttemptQuery()")
|
|
&& str_contains($attemptModel,
|
|
"function boundReservationQuery()")
|
|
&& str_contains($debtModel,
|
|
"function boundReservationQuery()")
|
|
&& str_contains($reservationModel,
|
|
"'migration_source_debt_id' => 'int'"),
|
|
'Models or setup version do not expose the new ownership boundary.');
|
|
|
|
$assert(str_contains($coordinator, 'CloudAccess::manage($actor)')
|
|
&& str_contains($coordinator,
|
|
'CloudPublicNetworkAllocationService::reservePlannedBundle(')
|
|
&& str_contains($coordinator,
|
|
'CloudPublicNetworkAllocationService::verifyExistingBundle(')
|
|
&& !str_contains($coordinator, 'reserveBackupRestore(')
|
|
&& !str_contains($coordinator, 'preparedBackupRestore(')
|
|
&& !str_contains($coordinator, 'finishBackupRestore(')
|
|
&& !str_contains($coordinator, 'new CloudJob()')
|
|
&& !str_contains($coordinator, 'new CloudInstance()')
|
|
&& str_contains($allocator,
|
|
'public static function verifyExistingBundle(')
|
|
&& str_contains($allocator,
|
|
'公网网络资源预留缺少既有双栈声明'),
|
|
'Portable restore admission can dispatch, create instances, or reallocate a missing replay bundle.');
|
|
|
|
$frozenIdentity = file_get_contents(
|
|
$root . '/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php'
|
|
);
|
|
$assert(is_string($frozenIdentity)
|
|
&& str_contains($frozenIdentity,
|
|
'public static function fromClaimSet(string $uuid,')
|
|
&& str_contains($frozenIdentity,
|
|
'CloudActivePublicNetworkClaimSet $claimSet')
|
|
&& str_contains($frozenIdentity, '$hostId = $claimSet->hostId();')
|
|
&& str_contains($frozenIdentity,
|
|
'$claimSet->hostId() !== $hostId')
|
|
&& str_contains($frozenIdentity,
|
|
'CloudPublicNetworkAllocationService::currentClaimSet()')
|
|
&& !str_contains($frozenIdentity,
|
|
'public static function fromLedger(')
|
|
&& !str_contains($frozenIdentity, 'array $claims')
|
|
&& !str_contains($frozenIdentity, 'array $addresses'),
|
|
'Frozen identity can bypass the authoritative claim-set module.');
|
|
|
|
$attemptRecord = new CloudBackupRestoreAttempt();
|
|
$attemptRecord->id = 91;
|
|
$attemptRecord->attempt_key = str_repeat('e', 32);
|
|
$attemptRecord->reservation_id = 92;
|
|
$reservationRecord = new CloudResourceReservation();
|
|
$reservationRecord->id = 92;
|
|
$reservationRecord->backup_restore_attempt_key = str_repeat('e', 32);
|
|
Capsule::table('cloud_backup_restore_attempts')->insert([
|
|
'id' => 91,
|
|
'attempt_key' => str_repeat('e', 32),
|
|
'reservation_id' => 92,
|
|
]);
|
|
Capsule::table('cloud_resource_reservations')->insert([
|
|
'id' => 92,
|
|
'backup_restore_attempt_key' => str_repeat('e', 32),
|
|
'migration_source_debt_id' => null,
|
|
]);
|
|
$assert($attemptRecord->matchesReservation($reservationRecord)
|
|
&& (int) $attemptRecord->boundReservationQuery()->first()?->id === 92
|
|
&& (int) $reservationRecord->backupRestoreAttemptQuery()->first()?->id
|
|
=== 91,
|
|
'Restore attempt and reservation are not joined by id and stable key.');
|
|
$reservationRecord->backup_restore_attempt_key = str_repeat('f', 32);
|
|
Capsule::table('cloud_resource_reservations')->where('id', 92)->update([
|
|
'backup_restore_attempt_key' => str_repeat('f', 32),
|
|
]);
|
|
$assert(!$attemptRecord->matchesReservation($reservationRecord)
|
|
&& $attemptRecord->boundReservationQuery()->first() === null
|
|
&& $reservationRecord->backupRestoreAttemptQuery()->first() === null,
|
|
'Restore attempt accepted a cross-bound reservation identity.');
|
|
|
|
$debtRecord = new CloudInstanceMigrationSourceDebt();
|
|
$debtRecord->id = 101;
|
|
$debtRecord->source_reservation_id = 102;
|
|
$debtReservation = new CloudResourceReservation();
|
|
$debtReservation->id = 102;
|
|
$debtReservation->migration_source_debt_id = 101;
|
|
Capsule::table('cloud_instance_migration_source_debts')->insert([
|
|
'id' => 101,
|
|
'source_reservation_id' => 102,
|
|
]);
|
|
Capsule::table('cloud_resource_reservations')->insert([
|
|
'id' => 102,
|
|
'backup_restore_attempt_key' => null,
|
|
'migration_source_debt_id' => 101,
|
|
]);
|
|
$assert($debtRecord->matchesReservation($debtReservation)
|
|
&& (int) $debtRecord->boundReservationQuery()->first()?->id === 102
|
|
&& (int) $debtReservation->migrationSourceDebtQuery()->first()?->id
|
|
=== 101,
|
|
'Migration source debt does not bind the exact source reservation both ways.');
|
|
$debtReservation->migration_source_debt_id = 103;
|
|
Capsule::table('cloud_resource_reservations')->where('id', 102)->update([
|
|
'migration_source_debt_id' => 103,
|
|
]);
|
|
$assert(!$debtRecord->matchesReservation($debtReservation)
|
|
&& $debtRecord->boundReservationQuery()->first() === null
|
|
&& $debtReservation->migrationSourceDebtQuery()->first() === null,
|
|
'Migration source debt accepted a cross-bound reservation identity.');
|
|
|
|
echo "Restore/migration durable-ledger contract tests passed.\n";
|