'http://127.0.0.1:8000', 'super_admin' => '99', ]; public static function get($name, $default = null) { return self::$values[$name] ?? $default; } } } namespace App\Services\Cloud { final class CloudSecrets { public static function key(string $purpose): string { return str_repeat('k', 32); } } } namespace { require dirname(__DIR__) . '/vendor/autoload.php'; use App\Models\CloudHost; use App\Models\CloudHostCapacitySnapshot; use App\Models\CloudPublicIp; use App\Models\User; use App\Services\Cloud\CloudEngineClient; use App\Services\Cloud\CloudHostInventorySyncService; use GuzzleHttp\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; 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); } }; $json = static fn (array $value): string => json_encode( $value, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ); $capacity = static function (int $observedAt, int $cpu = 16): array { return [ 'health' => 'healthy', 'observed_at' => $observedAt, 'instances' => ['total' => 3, 'running' => 2], 'cpu' => ['total' => $cpu, 'allocated' => 4, 'available' => max(0, $cpu - 4)], 'memory_mib' => ['total' => 32768, 'allocated' => 8192, 'available' => 24576], 'storage_bytes' => ['total' => 1099511627776, 'allocated' => 274877906944, 'available' => 824633720832], 'public_ipv4' => ['configured' => true, 'total' => 6, 'reserved' => 1, 'allocated' => 2, 'available' => 3], ]; }; $network = static function (int $count = 3): array { return [ 'configuration_state' => 'ready', 'pool_cidr' => '8.8.8.0/29', 'guest_prefix' => 29, 'gateway' => '8.8.8.1', 'dns_servers' => ['1.1.1.1'], 'available_ips' => ['8.8.8.3/29', '8.8.8.5/29', '8.8.8.6/29'], 'available_count' => $count, 'message' => 'validated test response', ]; }; $client = static function (array $responses, ?array &$history = null): CloudEngineClient { $stack = HandlerStack::create(new MockHandler($responses)); if ($history !== null) { $stack->push(Middleware::history($history)); } return new CloudEngineClient(new Client(['handler' => $stack]), 3.0); }; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $schema = $capsule->schema('default'); $schema->create('user', function (Blueprint $table): void { $table->increments('id'); $table->integer('enable')->default(1); $table->integer('is_admin')->default(0); }); $schema->create('cloud_hosts', function (Blueprint $table): void { $table->increments('id'); $table->string('name'); $table->integer('enabled')->default(0); $table->string('health_state')->default('unknown'); $table->integer('last_seen_at')->nullable(); $table->integer('health_checked_at')->nullable(); $table->string('health_error_code')->default(''); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_host_capacity_snapshots', function (Blueprint $table): void { $table->integer('host_id')->primary(); foreach (['cpu_total_cores', 'cpu_allocated_cores', 'cpu_available_cores', 'memory_total_mib', 'memory_allocated_mib', 'memory_available_mib', 'storage_total_bytes', 'storage_allocated_bytes', 'storage_available_bytes', 'instance_total_count', 'instance_running_count', 'public_ip_total_count', 'public_ip_reserved_count', 'public_ip_allocated_count', 'public_ip_available_count', 'observed_at', 'last_attempt_at', 'attempt_generation', 'revision'] as $field) { $table->integer($field)->default(0); } $table->integer('public_ip_configured')->default(0); $table->string('sync_state')->default('never'); $table->string('last_error_code')->default(''); $table->integer('created_at'); $table->integer('updated_at'); }); $schema->create('cloud_public_ips', function (Blueprint $table): void { $table->increments('id'); $table->integer('host_id'); $table->string('address')->unique(); $table->integer('prefix_length'); $table->string('state')->default('available'); $table->integer('reservation_id')->nullable(); $table->integer('instance_id')->nullable(); $table->string('operation_id')->nullable(); $table->integer('created_at'); $table->integer('updated_at'); }); $admin = new User(); $admin->id = 99; $admin->enable = 1; $admin->is_admin = 1; $admin->save(); $host = new CloudHost(); $host->name = 'test-host'; $host->enabled = 1; $host->health_state = 'unknown'; $host->health_error_code = ''; $host->created_at = 900; $host->updated_at = 900; $host->save(); $insertIp = static function (string $address, string $state, ?int $reservationId = null, ?int $instanceId = null): void { $row = new CloudPublicIp(); $row->host_id = 1; $row->address = $address; $row->prefix_length = 29; $row->state = $state; $row->reservation_id = $reservationId; $row->instance_id = $instanceId; $row->operation_id = null; $row->created_at = 900; $row->updated_at = 900; $row->save(); }; $insertIp('8.8.8.2', 'available'); $insertIp('8.8.8.3', 'reserved', 501); $insertIp('8.8.8.4', 'assigned', null, 601); $insertIp('8.8.8.5', 'disabled'); $now = 1000; $history = []; $successClient = $client([ // A host clock may lead PHP by the accepted 300-second window. The // local attempt boundary, not this engine timestamp, must drive later // reservation accounting. new Response(200, [], $json($capacity($now + 300))), new Response(200, [], $json($network())), ], $history); $success = (new CloudHostInventorySyncService( $successClient, static function () use (&$now): int { return $now; } ))->sync(10); $host->refresh(); $snapshot = CloudHostCapacitySnapshot::find(1); $states = CloudPublicIp::orderBy('address')->pluck('state', 'address')->all(); $assert($success['checked'] === 1 && $success['succeeded'] === 1 && $success['errors'] === [], 'A valid host observation did not succeed.'); $assert($host->health_state === 'healthy' && $host->last_seen_at === 1000 && $host->health_checked_at === 1000 && $host->health_error_code === '', 'Successful host health markers are incorrect.'); $assert($snapshot !== null && $snapshot->sync_state === 'fresh' && $snapshot->cpu_total_cores === 16 && $snapshot->storage_total_bytes === 1099511627776 && $snapshot->public_ip_available_count === 3 && $snapshot->observed_at === 1300 && $snapshot->last_attempt_at === 1000 && $snapshot->attempt_generation === 1, 'Capacity or public-IP counts were not persisted exactly.'); $assert($states === [ '8.8.8.2' => 'disabled', '8.8.8.3' => 'reserved', '8.8.8.4' => 'assigned', '8.8.8.5' => 'available', '8.8.8.6' => 'available', ], 'Free-list sync overwrote local occupancy or retained a missing free address.'); $assert(count($history) === 2 && (string) $history[0]['request']->getUri() === 'http://127.0.0.1:8000/internal/v1/hosts/1/host/capacity' && (string) $history[1]['request']->getUri() === 'http://127.0.0.1:8000/internal/v1/hosts/1/host/network-options', 'Lifecycle inventory did not use both bounded host endpoints.'); $actorHeader = $history[0]['request']->getHeaderLine('X-Cloud-Actor'); $actorHeader .= str_repeat('=', (4 - strlen($actorHeader) % 4) % 4); $actor = json_decode(base64_decode(strtr($actorHeader, '-_', '+/'), true), true); $assert(($actor['user_id'] ?? null) === 99 && ($actor['session_id'] ?? null) === 'cloud-host-inventory-sync' && ($actor['is_super_admin'] ?? null) === true, 'Shared daemon system actor was not used for inventory requests.'); $beforeCapacity = $snapshot->toArray(); $beforeStates = $states; $now = 1010; $notDue = (new CloudHostInventorySyncService( $client([]), static function () use (&$now): int { return $now; } ))->sync(10); $assert($notDue['checked'] === 0 && $notDue['errors'] === [], 'A host inside the configured due-time window was polled again.'); $invalidClient = $client([ new Response(200, [], $json($capacity($now, 32))), new Response(200, [], $json($network(2))), ]); $invalid = (new CloudHostInventorySyncService( $invalidClient, static function () use (&$now): int { return $now; } ))->syncOne(1); $host->refresh(); $snapshot->refresh(); $states = CloudPublicIp::orderBy('address')->pluck('state', 'address')->all(); $assert($invalid['degraded'] === 1 && $invalid['errors'] === ['host_inventory_sync_failed'] && $host->health_state === 'degraded' && $host->health_error_code === 'host_inventory_network_invalid' && $host->last_seen_at === 1000 && $host->health_checked_at === 1010, 'Invalid network response did not preserve last success and mark degraded.'); foreach (['cpu_total_cores', 'storage_total_bytes', 'observed_at'] as $field) { $assert($snapshot->{$field} === $beforeCapacity[$field], 'Failure replaced last-known capacity field: ' . $field); } $assert($snapshot->sync_state === 'failed' && $snapshot->last_error_code === 'host_inventory_network_invalid' && $states === $beforeStates, 'Failure cleared capacity/IP data or exposed an unfixed diagnostic.'); $now = 1020; $unavailableClient = $client([ new Response(503, [], '{"error":"private engine path must never persist"}'), ]); $unavailable = (new CloudHostInventorySyncService( $unavailableClient, static function () use (&$now): int { return $now; } ))->syncOne(1); $host->refresh(); $snapshot->refresh(); $persisted = json_encode([ $host->health_error_code, $snapshot->last_error_code, ], JSON_THROW_ON_ERROR); $assert($unavailable['unavailable'] === 1 && $host->health_state === 'unavailable' && $host->health_error_code === 'host_inventory_unavailable' && $host->last_seen_at === 1000 && $host->health_checked_at === 1020 && !str_contains($persisted, 'private engine path'), 'Engine failure cleared last success or persisted an upstream error.'); // Same-second polls need a monotonic fence rather than timestamp ordering. // Prove both dangerous completion orders: stale success after a newer // failure, then stale failure after a newer success. $reflection = new ReflectionClass(CloudHostInventorySyncService::class); $beginAttempt = $reflection->getMethod('beginAttempt'); $apply = $reflection->getMethod('apply'); $markFailure = $reflection->getMethod('markFailure'); $parseCapacity = $reflection->getMethod('capacity'); $parseNetwork = $reflection->getMethod('network'); foreach ([$beginAttempt, $apply, $markFailure, $parseCapacity, $parseNetwork] as $method) { $method->setAccessible(true); } $now = 1030; $fenced = new CloudHostInventorySyncService( $client([]), static function () use (&$now): int { return $now; } ); $parsedCapacity = $parseCapacity->invoke(null, $capacity($now, 24), $now); $parsedNetwork = $parseNetwork->invoke(null, $network()); $oldSuccess = $beginAttempt->invoke($fenced, 1, $now); $newFailure = $beginAttempt->invoke($fenced, 1, $now); $assert($newFailure === $oldSuccess + 1 && $markFailure->invoke($fenced, 1, 'unavailable', 'host_inventory_unavailable', $now, $newFailure) === true && $apply->invoke($fenced, 1, $parsedCapacity, $parsedNetwork['available'], $now, $oldSuccess) === false, 'A stale success was not fenced after a newer same-second failure.'); $host->refresh(); $snapshot->refresh(); $assert($host->health_state === 'unavailable' && $snapshot->sync_state === 'failed' && $snapshot->attempt_generation === $newFailure, 'The stale success overwrote a newer failed observation.'); $oldFailure = $beginAttempt->invoke($fenced, 1, $now); $newSuccess = $beginAttempt->invoke($fenced, 1, $now); $assert($apply->invoke($fenced, 1, $parsedCapacity, $parsedNetwork['available'], $now, $newSuccess) === true && $markFailure->invoke($fenced, 1, 'unavailable', 'host_inventory_unavailable', $now, $oldFailure) === false, 'A stale failure was not fenced after a newer same-second success.'); $host->refresh(); $snapshot->refresh(); $assert($host->health_state === 'healthy' && $snapshot->sync_state === 'fresh' && $snapshot->cpu_total_cores === 24 && $snapshot->attempt_generation === $newSuccess, 'The stale failure overwrote a newer successful observation.'); echo "Cloud host capacity and public-IP lifecycle sync checks passed.\n"; }