httpStatus === $status, 'Unexpected registry error status: ' . $error->httpStatus); return $error; } throw new RuntimeException('Expected registry error was not raised.'); }; $s3 = static function (int $status): CloudObjectStorageS3Client { return new CloudObjectStorageS3Client(new Client([ 'handler' => HandlerStack::create(new MockHandler([ new Response($status), ])), ])); }; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:']); $capsule->setAsGlobal(); $capsule->bootEloquent(); $capsule->schema()->create('configurations', static function (Blueprint $table): void { $table->increments('id'); $table->string('name')->unique(); $table->text('value')->nullable(); }); $capsule->schema()->create('cloud_storage_backends', static function (Blueprint $table): void { $table->increments('id'); $table->string('backend_key')->unique(); $table->string('identity_sha256')->unique(); $table->string('name'); $table->string('driver'); $table->string('endpoint'); $table->string('region'); $table->string('bucket'); $table->string('prefix'); $table->boolean('path_style'); $table->string('failure_domain'); $table->text('sealed_credentials'); $table->unsignedBigInteger('config_revision'); $table->unsignedBigInteger('tested_revision'); $table->string('admin_state'); $table->string('health_state'); $table->unsignedInteger('write_priority'); $table->unsignedInteger('consecutive_failures'); $table->unsignedInteger('consecutive_successes'); $table->string('last_error_code'); $table->unsignedBigInteger('last_tested_at'); $table->unsignedBigInteger('last_healthy_at'); $table->unsignedBigInteger('next_probe_at'); $table->unsignedBigInteger('created_at'); $table->unsignedBigInteger('updated_at'); }); $capsule->schema()->create('cloud_storage_backend_catalog_mutex', static function (Blueprint $table): void { $table->unsignedTinyInteger('id')->primary(); $table->unsignedBigInteger('updated_at'); }); Capsule::table('cloud_storage_backend_catalog_mutex')->insert([ 'id' => 1, 'updated_at' => 0, ]); $capsule->schema()->create('cloud_storage_replicas', static function (Blueprint $table): void { $table->increments('id'); $table->unsignedBigInteger('backend_id'); }); Config::$dataKey = base64_encode(str_repeat('r', 32)); $legacy = [ 'name' => 'Legacy Primary', 'endpoint' => 'https://primary.storage.example', 'region' => 'us-east-1', 'bucket' => 'primary-bucket', 'prefix' => 'virt-cloud', 'path_style' => 'true', 'access_key_id' => 'PRIMARYKEY', 'secret_access_key' => 'primary-secret-value', 'expected_revision' => 0, ]; CloudObjectStorageConfig::save($legacy); CloudObjectStorageConfig::testAndEnable(1, $s3(200)); $imported = CloudObjectStorageBackendRegistry::importLegacyConfiguration(1); $assert(is_array($imported) && $imported['admin_state'] === 'active' && $imported['health_state'] === 'healthy' && $imported['ready'] && $imported['revision'] === 1, 'Ready legacy configuration was not imported as the first active backend.'); $assert(CloudObjectStorageBackendRegistry::importLegacyConfiguration(1)['id'] === $imported['id'] && CloudStorageBackend::count() === 1, 'Legacy import was not idempotent by immutable backend identity.'); $database = CloudStorageBackend::firstOrFail(); $databaseJson = json_encode($database->toArray(), JSON_THROW_ON_ERROR); $publicJson = json_encode($imported, JSON_THROW_ON_ERROR); $assert(!str_contains((string) $database->sealed_credentials, 'PRIMARYKEY') && !str_contains((string) $database->sealed_credentials, 'primary-secret') && !str_contains($databaseJson, 'PRIMARYKEY') && !str_contains($publicJson, 'PRIMARYKEY') && !array_key_exists('sealed_credentials', $imported), 'Backend registry leaked plaintext or sealed credentials.'); $metadata = CloudObjectStorageBackendRegistry::update( (int) $imported['id'], [ 'name' => 'Primary Renamed', 'endpoint' => $imported['endpoint'], 'region' => $imported['region'], 'bucket' => $imported['bucket'], 'prefix' => $imported['prefix'], 'path_style' => $imported['path_style'], 'failure_domain' => 'provider-a', 'write_priority' => 5, ], 1, 1 ); $assert($metadata['revision'] === 2 && $metadata['tested_revision'] === 2 && $metadata['ready'] && $metadata['admin_state'] === 'active', 'Metadata-only update incorrectly invalidated tested runtime evidence.'); $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::update( (int) $imported['id'], ['name' => 'stale'], 1, 1 ), 409); $secondaryInput = [ 'name' => 'Secondary', 'endpoint' => 'https://secondary.storage.example', 'region' => 'us-west-2', 'bucket' => 'secondary-bucket', 'prefix' => 'virt-cloud', 'path_style' => false, 'failure_domain' => 'provider-b', 'write_priority' => 20, 'access_key_id' => 'SECONDARYKEY', 'secret_access_key' => 'secondary-secret-value', ]; $secondary = CloudObjectStorageBackendRegistry::create($secondaryInput, 1); $assert($secondary['admin_state'] === 'draft' && $secondary['health_state'] === 'unknown' && !$secondary['ready'], 'New backend bypassed explicit connectivity test and activation.'); $tested = CloudObjectStorageBackendRegistry::test( (int) $secondary['id'], 1, 1, $s3(200) ); $assert($tested['health_state'] === 'healthy' && $tested['tested_revision'] === 1 && $tested['admin_state'] === 'draft' && $tested['can_activate'], 'Connectivity test silently activated storage or lost health evidence.'); $active = CloudObjectStorageBackendRegistry::activate( (int) $secondary['id'], 1, 1 ); $assert($active['admin_state'] === 'active' && $active['revision'] === 2 && CloudObjectStorageBackendRegistry::summary()['writable_backends'] === 2, 'Explicit activation did not expose two healthy writable backends.'); $third = CloudObjectStorageBackendRegistry::create( array_replace($secondaryInput, [ 'name' => 'Third', 'endpoint' => 'https://third.storage.example', 'bucket' => 'third-bucket', ]), 1 ); $assert($third['admin_state'] === 'draft' && CloudStorageBackend::count() === 3, 'A replacement backend could not be registered beside two live rows.'); CloudObjectStorageBackendRegistry::test( (int) $third['id'], 1, 1, $s3(200) ); $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::activate( (int) $third['id'], 1, 1 ), 409); $assert(CloudObjectStorageBackendRegistry::summary()['active_backends'] === 2, 'The registry allowed more than two active storage backends.'); Capsule::table('cloud_storage_replicas')->insert([ 'backend_id' => (int) $secondary['id'], ]); $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::update( (int) $secondary['id'], array_replace($secondaryInput, [ 'endpoint' => 'https://replacement.storage.example', 'access_key_id' => '', 'secret_access_key' => '', ]), 2, 1 ), 409); $failed = $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::test( (int) $secondary['id'], 2, 1, $s3(503) ), 502); $failedState = CloudObjectStorageBackendRegistry::listView()[1]; $assert($failedState['admin_state'] === 'read_only' && $failedState['health_state'] === 'unreachable' && !$failedState['ready'] && !str_contains($failed->getMessage(), 'secondary-secret'), 'Failed health test did not stop new writes or leaked credentials.'); $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::activate( (int) $secondary['id'], 2, 1 ), 409); $retested = CloudObjectStorageBackendRegistry::test( (int) $secondary['id'], 2, 1, $s3(200) ); $reactivated = CloudObjectStorageBackendRegistry::activate( (int) $secondary['id'], 2, 1 ); $draining = CloudObjectStorageBackendRegistry::drain( (int) $secondary['id'], 3, 1 ); $resumed = CloudObjectStorageBackendRegistry::resume( (int) $secondary['id'], 4, 1 ); $disabled = CloudObjectStorageBackendRegistry::disable( (int) $secondary['id'], 5, 1 ); $runtime = CloudObjectStorageBackendRegistry::runtime( (int) $secondary['id'], true ); $assert($retested['ready'] && $reactivated['admin_state'] === 'active' && $draining['admin_state'] === 'draining' && $resumed['admin_state'] === 'active' && $disabled['admin_state'] === 'disabled' && $runtime['credentials']['access_key_id'] === 'SECONDARYKEY', 'Backend lifecycle lost exact historical read capability.'); CloudStorageBackend::query()->whereKey((int) $imported['id'])->update([ 'admin_state' => 'retired', ]); $expectCloudError(static fn () => CloudObjectStorageBackendRegistry::importLegacyConfiguration(1), 409); echo "Object-storage backend registry contracts passed.\n"; }