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

123 lines
5.7 KiB
PHTML

<?php
$root = dirname(__DIR__);
$read = static fn (string $path): string => (string) file_get_contents(
$root . '/' . $path
);
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$schema = $read('sql/cloud.sql');
$routes = $read('config/routes_cloud.php');
$middleware = $read('app/Middleware/CloudApi.php');
$controller = $read('app/Controllers/CloudApiController.php');
$service = $read('app/Services/Cloud/CloudFirewallService.php');
$worker = $read('app/Services/Cloud/CloudWorkerService.php');
$assert(preg_match(
'/public function firewall\(.*?(?=\n public function snapshots\()/s',
$controller,
$firewallControllerMatch
) === 1, 'Firewall controller method could not be isolated for contract checks.');
$firewallController = $firewallControllerMatch[0];
$assert(str_contains($schema,
'CREATE TABLE cloud_instance_firewalls (')
&& str_contains($schema,
'CREATE TABLE cloud_instance_firewall_versions (')
&& str_contains($schema,
'CREATE TABLE cloud_instance_firewall_rules (')
&& str_contains($schema,
'source_cidr VARCHAR(43) CHARACTER SET ascii COLLATE ascii_bin NOT NULL')
&& str_contains($schema,
'cloud_instance_firewall_idempotency (instance_id, idempotency_key)')
&& !preg_match('/(?:xml|command|host_path|filter_xml)\s+(?:TEXT|LONGTEXT)/i',
$schema),
'Fresh schema lost firewall policy/version/rules separation or stores host syntax.');
$assert(str_contains($routes,
"['GET', 'PUT'], '/vms/{id:[0-9]+}/firewall'")
&& str_contains($middleware,
"#^/api/v1/vms/[0-9]+/firewall$#D")
&& str_contains($middleware,
'AdminCsrf::requiresProtection($request->getMethod())')
&& str_contains($controller, 'CloudFirewallService::listing(')
&& str_contains($controller, 'CloudFirewallService::apply('),
'Firewall API is missing its bounded route, CSRF, or controller boundary.');
$assert(str_contains($service, "(\$spec['os_source'] ?? null) !== 'cloud_template'")
&& str_contains($service,
'(int) $instance->user_id !== (int) ($actor[\'user_id\'] ?? 0)')
&& str_contains($service, "'idempotency_key'")
&& str_contains($service, "'expected_revision'")
&& str_contains($service, "'port_from'")
&& str_contains($service, "'port_to'")
&& str_contains($service, '$source = self::cidr(')
&& str_contains($service, 'FILTER_FLAG_IPV4')
&& str_contains($service, 'FILTER_FLAG_IPV6')
&& str_contains($service, 'inet_pton($address)')
&& str_contains($service, 'self::ipv6Text($bytes)')
&& str_contains($service, "return '::ffff:' . implode(")
&& !str_contains($service, 'inet_ntop(')
&& !str_contains($service, 'filter_xml')
&& !str_contains($service, 'nwfilter XML'),
'Firewall ownership, cloud-template, revision or typed DTO boundary regressed.');
$assert(str_contains($worker,
'CloudFirewallService::assertJobRunnable($job, $payload)')
&& str_contains($worker,
"'error_code' => 'firewall_apply_superseded'")
&& str_contains($worker,
"'operation_id' => (string) \$job->operation_id")
&& str_contains($worker,
'CloudFirewallService::markJobRunning($job)')
&& str_contains($worker,
'CloudFirewallService::finishJobLocked(')
&& str_contains($worker,
'CloudFirewallService::finishInstanceDeleteLocked(')
&& str_contains($worker, "\$job->phase = 'terminal'")
&& str_contains($worker, '$job->outcome = $status')
&& str_contains($worker, '$effectMayHaveApplied')
&& str_contains($service, "'firewall_result_uncertain'")
&& str_contains($service, "'cleanup_pending'"),
'Worker terminal receipt, effect fencing, or firewall cleanup is incomplete.');
$lifecycle = $read('app/Services/Cloud/CloudLifecycleService.php');
$jobs = $read('app/Services/Cloud/CloudJobService.php');
$assert(str_contains($jobs, 'blockingQueryForInstance')
&& str_contains($jobs, "->where('phase', '<>', 'terminal')")
&& str_contains($jobs, "->where('effect_resolution', 'unknown')")
&& !str_contains($jobs, "->where('kind', '<>', 'firewall_apply')")
&& str_contains($lifecycle,
"['delete', 'expiry_delete', 'delete_force']"),
'Lifecycle must block active or effect-unknown firewall work without a legacy uncertainty queue.');
$assert(str_contains($service,
"CloudInstance::where('id', (int) \$instance->id)")
&& str_contains($service, '->lockForUpdate()->first()')
&& str_contains($service,
'CloudJobService::blockingQueryForInstance(')
&& str_contains($service, "if (!\$activeJobs->isEmpty())")
&& str_contains($service,
'CloudJobService::completeWithoutDispatchLocked($job, [')
&& str_contains($service, "\$job->kind = 'firewall_apply'")
&& str_contains($service, "\$job->execution_mode = 'agent'")
&& str_contains($service, "'unchanged' => true")
&& str_contains($service, 'jobWasUndispatchedNoop($priorJob)')
&& !str_contains($service, "'job' => null")
&& !str_contains($service, "\$activeJob->status = 'cancelled'")
&& !str_contains($service, "CloudHost::")
&& !str_contains($service, 'global_lock'),
'Firewall admission or terminal no-op receipt deduplication regressed.');
$assert(str_contains($firewallController, "'task_id' => (string) \$job->id")
&& str_contains($firewallController,
"'status_url' => '/api/v1/tasks/' . \$job->id")
&& str_contains($firewallController, '], 202);')
&& !str_contains($firewallController, 'if ($job === null)'),
'Firewall mutation no longer returns the strict 202 task receipt on every accepted request.');
echo "Cloud firewall schema, API, CSRF and worker contract passed.\n";