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

125 lines
5.9 KiB
PHP

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$full = $root . '/' . $path;
return is_file($full) ? (string) file_get_contents($full) : '';
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$section = static function (string $source, string $start,
string $end): string {
$offset = strpos($source, $start);
if ($offset === false) {
return '';
}
$finish = strpos($source, $end, $offset + strlen($start));
return $finish === false ? substr($source, $offset)
: substr($source, $offset, $finish - $offset);
};
$schema = $read('sql/cloud.sql');
$orders = $read('app/Services/Cloud/CloudOrderService.php');
$backups = $read('app/Services/Cloud/CloudBackupService.php');
$recovery = $read(
'app/Services/Cloud/CloudOperationRecoveryIdentity.php'
);
$placement = $read('app/Services/Cloud/CloudPlacementService.php');
$worker = $read('app/Services/Cloud/CloudWorkerService.php');
$catalog = $read('app/Services/Cloud/CloudDeploymentSnapshotService.php')
. $read('app/Services/Cloud/CloudDeploymentSnapshotCatalog.php');
$purchase = $section($orders, 'public static function purchase(',
'private static function createPurchaseItems(');
$payment = $section($orders, 'public static function queuePaidProvision(',
'/** Release a finite plan reservation exactly once. */');
$assert($purchase !== ''
&& str_contains($purchase, "['cloud', 'deployment_snapshot']")
&& str_contains($purchase, "'cloud_snapshot_key'")
&& str_contains($purchase, "'deployment_snapshot'")
&& str_contains($purchase, "array_key_exists('backup_id', \$input)")
&& !str_contains($purchase, "['cloud', 'backup']")
&& !str_contains($purchase, "\$source === 'backup'"),
'Public provisioning must accept a cloud snapshot key and must not accept a cloud backup as an instance source.');
$assert(str_contains($schema, 'CREATE TABLE cloud_deployment_snapshots')
&& str_contains($schema,
'deployment_snapshot_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'cloud_order_deployment_snapshot')
&& str_contains($orders, '$order->deployment_snapshot_id')
&& str_contains($orders, '$sourceSnapshot->id'),
'Provisioning orders must persist the selected deployment snapshot instead of relying on a request-only source key.');
$assert(str_contains($purchase, "'deployment_snapshot_id'")
&& str_contains($purchase, "'cloud_snapshot_key'")
&& str_contains($purchase, "'backing_backup_id'")
&& str_contains($purchase, "'backing_backup_key'")
&& str_contains($purchase, "'backing_backup_size_bytes'")
&& str_contains($purchase, "'backing_backup_virtual_size_bytes'")
&& str_contains($purchase, "'backing_backup_sha256'")
&& str_contains($purchase, "'backing_backup_identity'"),
'The safe order snapshot must freeze both the public cloud snapshot and its immutable backing object identity.');
$jobBound = str_contains($schema,
'deployment_snapshot_id BIGINT UNSIGNED DEFAULT NULL')
&& str_contains($schema, 'cloud_job_deployment_snapshot')
&& str_contains($orders, '$job->deployment_snapshot_id')
|| (str_contains($orders, '$job->backup_id')
&& str_contains($payment, 'backing_backup_id'));
$assert($jobBound,
'Provisioning jobs must remain audibly bound to the deployment snapshot or its frozen backing backup.');
$assert(str_contains($recovery, "!== 'deployment_snapshot'")
&& !str_contains($recovery, "!== 'backup'")
&& str_contains($placement, "=== 'deployment_snapshot'")
&& !str_contains($placement, "=== 'backup'"),
'Placement and operation recovery must use the cloud snapshot source contract while preserving the internal restore-backup protocol.');
$assert(!str_contains($orders, 'backup_sealed_credential')
&& !str_contains($recovery, 'backup_sealed_credential')
&& str_contains($worker, '$instance->sealed_credential = null;'),
'Cloud-snapshot provisioning must preserve the system-disk password and must not expose the historical panel credential as current.');
$assert($catalog !== ''
&& substr_count($catalog, 'lockForUpdate()') >= 2
&& str_contains($catalog, "'ready'")
&& str_contains($catalog, 'sha256')
&& str_contains($catalog, 'object_size_bytes')
&& str_contains($catalog, 'virtual_size_bytes')
&& str_contains($purchase, 'CloudDeploymentSnapshot')
&& str_contains($payment, 'CloudDeploymentSnapshot'),
'Purchase and paid provisioning must both re-lock a ready snapshot and validate the immutable object identity.');
$automaticCapacity = $section($backups,
'$usedQuery = CloudInstanceBackup::where(',
'$used = (int) $usedQuery->count');
$assert(str_contains($automaticCapacity, 'whereNotExists')
&& str_contains($automaticCapacity, 'cloud_deployment_snapshots')
&& str_contains($automaticCapacity, 'cloud_deployment_snapshots.state')
&& str_contains($automaticCapacity, "'deleted'"),
'Promoted snapshot backings must not consume the rolling automatic-backup retention capacity.');
$assert(str_contains($catalog, 'CloudOrder')
&& str_contains($catalog, 'deployment_snapshot_id')
&& str_contains($catalog, "'awaiting_payment'")
&& str_contains($catalog, "'queued'")
&& str_contains($catalog, 'effect_resolution')
&& str_contains($catalog, "'unknown'")
&& str_contains($catalog, 'finalization_incident'),
'Snapshot deletion must remain fenced by unpaid, queued, and unknown-effect provisioning references.');
$assert(!preg_match(
"/whereIn\\('status',\\s*\\[[^]]*'succeeded'[^]]*\\]\\)/s",
$catalog
)
&& !preg_match(
"/whereIn\\('status',\\s*\\[[^]]*'expired'[^]]*\\]\\)/s",
$catalog
),
'Terminal safe and expired provisioning orders must not permanently block manual snapshot deletion.');
echo "Cloud deployment snapshot order and deletion contracts passed.\n";