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

95 lines
4.1 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(str_contains($schema,
'CREATE TABLE IF NOT EXISTS cloud_instance_firewalls (')
&& str_contains($schema,
'CREATE TABLE IF NOT EXISTS cloud_instance_firewall_versions (')
&& str_contains($schema,
'CREATE TABLE IF NOT EXISTS cloud_instance_firewall_rules (')
&& 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, '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::markJobUncertain($job)')
&& str_contains($worker,
'CloudFirewallService::finishInstanceDeleteLocked($job, $status)')
&& str_contains($service, "'firewall_result_uncertain'")
&& str_contains($service, "'cleanup_pending'"),
'Worker success, uncertainty, delete cleanup, or operation reconciliation is incomplete.');
$lifecycle = $read('app/Services/Cloud/CloudLifecycleService.php');
$jobs = $read('app/Services/Cloud/CloudJobService.php');
$assert(str_contains($lifecycle,
"\$nonBlockingActiveKinds = ['firewall_apply'];")
&& str_contains($lifecycle,
"\$nonBlockingUncertainKinds = ['action', 'firewall_apply'];")
&& substr_count($jobs,
"->where('kind', '<>', 'firewall_apply')") >= 2,
'Firewall uncertainty must not block billing or expiry lifecycle policy.');
$assert(str_contains($service,
"CloudInstance::where('id', (int) $instance->id)")
&& str_contains($service, '->lockForUpdate()->first()')
&& str_contains($service,
"CloudJob::where('instance_id', (int) $locked->id)")
&& str_contains($service, "if (!\$activeJobs->isEmpty())")
&& str_contains($service, "'job' => null")
&& str_contains($service, "'unchanged' => true")
&& !str_contains($service, "\$activeJob->status = 'cancelled'")
&& !str_contains($service, "CloudHost::")
&& !str_contains($service, 'global_lock'),
'Firewall admission or converged-policy deduplication regressed.');
echo "Cloud firewall schema, API, CSRF and worker contract passed.\n";