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

284 lines
9.8 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudMaintenanceNoticeAudience;
use App\Services\Cloud\CloudMaintenanceNoticeService;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expect = static function (callable $callback, int $status,
string $message) use ($assert): void {
try {
$callback();
} catch (CloudError $error) {
$assert($error->httpStatus === $status, $message);
return;
}
throw new RuntimeException($message);
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_hosts', static function (Blueprint $table): void {
$table->increments('id');
$table->string('name');
$table->string('region')->default('');
$table->boolean('enabled')->default(false);
});
$schema->create('cloud_public_ip_pools', static function (Blueprint $table): void {
$table->increments('id');
$table->string('pool_key');
$table->integer('host_id');
$table->integer('address_family');
$table->string('name');
$table->string('cidr');
$table->string('state')->default('active');
});
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('email');
});
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('user_id');
$table->integer('host_id');
$table->integer('reservation_id')->nullable();
$table->string('name');
$table->string('status');
$table->text('spec_json');
});
$schema->create('cloud_resource_reservations', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('instance_id')->nullable();
$table->integer('host_id');
$table->integer('user_id');
$table->string('state');
});
$schema->create('cloud_resource_reservation_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('reservation_id');
$table->integer('address_family');
$table->integer('claim_generation');
$table->integer('active_claim')->nullable();
$table->integer('public_ip_pool_id');
$table->integer('public_ip_id');
$table->string('operation_id');
$table->string('state');
});
$schema->create('cloud_public_ips', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('host_id');
$table->integer('reservation_id')->nullable();
$table->integer('reservation_ip_claim_id')->nullable();
$table->integer('instance_id')->nullable();
$table->integer('public_ip_pool_id')->nullable();
$table->integer('address_family');
$table->string('operation_id')->nullable();
$table->string('state');
$table->string('address');
$table->integer('prefix_length');
});
$schema->create('cloud_maintenance_notices', static function (Blueprint $table): void {
$table->increments('id');
$table->string('notice_key')->unique();
$table->integer('requested_by_user_id');
$table->string('request_idempotency_key');
$table->string('request_fingerprint');
$table->string('scope_type');
$table->integer('scope_id');
$table->text('scope_snapshot_json');
$table->string('subject');
$table->text('message_text');
$table->string('impact');
$table->integer('maintenance_starts_at');
$table->integer('maintenance_ends_at');
$table->integer('scheduled_at');
$table->string('audience_digest');
$table->integer('instance_count');
$table->integer('recipient_count');
$table->integer('skipped_count');
$table->string('status');
$table->string('last_error_code');
$table->integer('terminal_at')->nullable();
$table->integer('created_at');
$table->integer('updated_at');
$table->unique(['requested_by_user_id', 'request_idempotency_key']);
});
$schema->create('cloud_maintenance_notice_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('notice_id');
$table->integer('instance_id');
$table->integer('user_id');
$table->integer('host_id');
$table->integer('public_ip_pool_id')->nullable();
$table->string('instance_name');
$table->string('ipv4');
$table->string('ipv6');
$table->text('evidence_json');
$table->integer('created_at');
});
$schema->create('cloud_maintenance_notice_recipients', static function (Blueprint $table): void {
$table->increments('id');
$table->integer('notice_id');
$table->integer('user_id');
$table->string('email_snapshot');
$table->string('status');
$table->integer('attempts');
$table->integer('available_at');
$table->string('lease_token')->nullable();
$table->integer('lease_until');
$table->integer('sent_at')->nullable();
$table->string('last_error_code');
$table->string('last_error');
$table->integer('created_at');
$table->integer('updated_at');
});
Capsule::table('cloud_hosts')->insert([
'id' => 1, 'name' => 'HKG-A', 'region' => 'HongKong', 'enabled' => 1,
]);
Capsule::table('cloud_public_ip_pools')->insert([
'id' => 7, 'pool_key' => str_repeat('a', 32), 'host_id' => 1,
'address_family' => 4, 'name' => 'HKG public v4',
'cidr' => '192.0.2.0/29', 'state' => 'draining',
]);
Capsule::table('user')->insert([
['id' => 1, 'email' => 'owner@example.com'],
['id' => 2, 'email' => 'invalid'],
]);
Capsule::table('cloud_instances')->insert([
['id' => 11, 'user_id' => 1, 'host_id' => 1,
'reservation_id' => null, 'name' => 'vm-a', 'status' => 'active',
'spec_json' => '{}'],
['id' => 12, 'user_id' => 2, 'host_id' => 1,
'reservation_id' => null, 'name' => 'vm-b', 'status' => 'active',
'spec_json' => '{}'],
]);
$now = 1793900000;
$audience = new CloudMaintenanceNoticeAudience();
$service = new CloudMaintenanceNoticeService(
$audience,
static fn (): int => $now
);
$catalog = $service->catalog();
$assert($catalog['hosts'][0]['enabled'] === true
&& $catalog['pools'][0]['state'] === 'draining',
'The page catalog must retain disabled or draining audience scopes.');
$assert($service->prefill([
'scope_type' => 'public_ip_pool', 'host_id' => 1,
'pool_key' => str_repeat('a', 32),
], $catalog) === ['scope_type' => 'public_ip_pool', 'scope_id' => 7],
'A trusted pool shortcut must resolve through the server catalog.');
$assert($service->prefill([
'scope_type' => 'public_ip_pool', 'host_id' => 2,
'pool_key' => str_repeat('a', 32),
], $catalog) === [],
'A shortcut cannot resolve a pool under a different host.');
$preview = $service->preview('host', 1);
$request = [
'scope_type' => 'host',
'scope_id' => 1,
'subject' => 'HKG-A maintenance',
'message_text' => "Network maintenance\nPlease save your work.",
'impact' => 'network_interruption',
'maintenance_starts_at' => $now + 3600,
'maintenance_ends_at' => $now + 7200,
'scheduled_at' => $now + 60,
'audience_digest' => $preview['audience_digest'],
];
$created = $service->schedule(
9,
'maintenance-test-0001',
$request
);
$assert($created['status'] === 'scheduled'
&& $created['instance_count'] === 2
&& $created['recipient_count'] === 1
&& $created['skipped_count'] === 1,
'Schedule must freeze reviewed counts.');
$assert(count($created['recipients']) === 2
&& $created['recipients'][0]['status'] === 'pending'
&& $created['recipients'][1]['status'] === 'skipped',
'Schedule must persist pending and skipped recipients.');
$assert($service->schedule(9, 'maintenance-test-0001', $request)
['notice_key'] === $created['notice_key'],
'An identical idempotent request must return its original notice.');
$different = $request;
$different['subject'] = 'Changed subject';
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-0001',
$different
),
409,
'An idempotency key must not accept a different request.'
);
$invalidType = $request;
$invalidType['subject'] = ['unexpected'];
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-typed-input',
$invalidType
),
422,
'Array input must not be coerced into an email subject.'
);
$invalidHeader = $request;
$invalidHeader['subject'] = "Maintenance\r\nBcc: attacker@example.com";
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-header-input',
$invalidHeader
),
422,
'Email subjects must reject control characters.'
);
$cancelled = $service->cancel($created['notice_key']);
$assert($cancelled['status'] === 'cancelled'
&& $cancelled['terminal_at'] === $now,
'Cancelling an unsent notice must make it terminal.');
$assert($cancelled['recipients'][0]['status'] === 'cancelled'
&& $cancelled['recipients'][1]['status'] === 'skipped',
'Cancellation must only change pending recipients.');
$oldPreview = $service->preview('host', 1);
Capsule::table('cloud_instances')->insert([
'id' => 13, 'user_id' => 1, 'host_id' => 1,
'reservation_id' => null, 'name' => 'vm-c', 'status' => 'active',
'spec_json' => '{}',
]);
$drifted = $request;
$drifted['audience_digest'] = $oldPreview['audience_digest'];
$expect(
static fn () => $service->schedule(
9,
'maintenance-test-0002',
$drifted
),
409,
'Audience drift must reject scheduling.'
);
$assert(Capsule::table('cloud_maintenance_notices')->count() === 1,
'A rejected drifted request must not create a notice.');
echo "Maintenance notice service tests passed.\n";