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

235 lines
9.8 KiB
PHP

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudMaintenanceNoticeDeliveryService;
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);
}
};
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'sqlite', 'database' => ':memory:',
], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('user', static function (Blueprint $table): void {
$table->increments('id');
$table->string('email');
});
$schema->create('cloud_maintenance_notices', static function (Blueprint $table): void {
$table->increments('id');
$table->string('notice_key');
$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');
});
$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');
});
$now = 1793900000;
Capsule::table('user')->insert([
['id' => 1, 'email' => 'ok@example.com'],
['id' => 2, 'email' => 'unknown@example.com'],
['id' => 3, 'email' => 'blocked@example.com'],
['id' => 4, 'email' => 'new@example.com'],
]);
$insertNotice = static function (int $id, string $status = 'scheduled') use ($now): void {
Capsule::table('cloud_maintenance_notices')->insert([
'id' => $id, 'notice_key' => str_pad(dechex($id), 32, '0', STR_PAD_LEFT),
'requested_by_user_id' => 9,
'request_idempotency_key' => 'delivery-test-' . str_pad((string) $id, 4, '0', STR_PAD_LEFT),
'request_fingerprint' => str_repeat('a', 64),
'scope_type' => 'host', 'scope_id' => 1,
'scope_snapshot_json' => '{"label":"HKG-A"}',
'subject' => 'Maintenance <notice>', 'message_text' => 'Please save.',
'impact' => 'network_interruption',
'maintenance_starts_at' => $now + 3600,
'maintenance_ends_at' => $now + 7200,
'scheduled_at' => $now, 'audience_digest' => str_repeat('b', 64),
'instance_count' => 1, 'recipient_count' => 1, 'skipped_count' => 0,
'status' => $status, 'last_error_code' => '', 'terminal_at' => null,
'created_at' => $now, 'updated_at' => $now,
]);
};
$insertRecipient = static function (int $id, int $noticeId, int $userId,
string $email, string $status, int $availableAt,
?string $token = null, int $leaseUntil = 0) use ($now): void {
Capsule::table('cloud_maintenance_notice_recipients')->insert([
'id' => $id, 'notice_id' => $noticeId, 'user_id' => $userId,
'email_snapshot' => $email, 'status' => $status,
'attempts' => $status === 'sending' ? 1 : 0,
'available_at' => $availableAt, 'lease_token' => $token,
'lease_until' => $leaseUntil, 'sent_at' => null,
'last_error_code' => '', 'last_error' => '',
'created_at' => $now, 'updated_at' => $now,
]);
Capsule::table('cloud_maintenance_notice_instances')->insert([
'notice_id' => $noticeId, 'instance_id' => 100 + $noticeId,
'user_id' => $userId, 'host_id' => 1,
'public_ip_pool_id' => null, 'instance_name' => 'vm-' . $noticeId,
'ipv4' => '203.0.113.' . $noticeId,
'ipv6' => '2001:db8::' . $noticeId,
'evidence_json' => '{}', 'created_at' => $now,
]);
};
$insertNotice(1);
$insertRecipient(1, 1, 1, 'ok@example.com', 'pending', $now);
$insertRecipient(2, 1, 2, 'unknown@example.com', 'pending', $now + 60);
$sent = [];
$delivery = new CloudMaintenanceNoticeDeliveryService(
static function (string $to, string $subject, array $variables) use (&$sent): bool {
$sent[] = compact('to', 'subject', 'variables');
return $to === 'ok@example.com';
},
static fn (string $email): bool => $email === 'blocked@example.com',
static fn (): int => $now,
new CloudMaintenanceNoticeService(
new CloudMaintenanceNoticeAudience(),
static fn (): int => $now
)
);
$assert($delivery->tick() === true && count($sent) === 1,
'One due recipient must be delivered per tick.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(1);
$assert($row->status === 'sent' && (int) $row->attempts === 1
&& (int) $row->sent_at === $now,
'Accepted mail was not finalized exactly once.');
$assert($sent[0]['variables']['maintenance_starts_utc']
=== gmdate('Y-m-d H:i:s \\U\\T\\C', $now + 3600)
&& $sent[0]['variables']['instances'][0]['ipv6'] === '2001:db8::1',
'Delivery lost the frozen UTC window or recipient instance list.');
$assert($delivery->tick() === false && count($sent) === 1,
'A future recipient was delivered early.');
Capsule::table('cloud_maintenance_notice_recipients')->where('id', 2)
->update(['available_at' => $now]);
$assert($delivery->tick() === true && count($sent) === 2,
'The second due recipient was not attempted.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(2);
$notice = Capsule::table('cloud_maintenance_notices')->find(1);
$assert($row->status === 'unknown'
&& $row->last_error_code === 'delivery_effect_unknown'
&& $notice->status === 'attention',
'A transport false result was not preserved as unknown attention.');
$assert($delivery->tick() === false && count($sent) === 2,
'An unknown delivery was replayed automatically.');
$insertNotice(2, 'sending');
$insertRecipient(3, 2, 1, 'expired@example.com', 'sending',
$now - 400, str_repeat('c', 32), $now - 1);
$assert($delivery->tick() === true && count($sent) === 2,
'An expired sending lease was not converged without transport.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(3);
$assert($row->status === 'unknown'
&& $row->last_error_code === 'delivery_lease_expired',
'Expired lease was retried or lost its unknown-effect evidence.');
$insertNotice(3);
$insertRecipient(4, 3, 3, 'blocked@example.com', 'pending', $now);
$assert($delivery->tick() === true && count($sent) === 2,
'A blacklisted recipient did not settle without transport.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(4);
$notice = Capsule::table('cloud_maintenance_notices')->find(3);
$assert($row->status === 'skipped'
&& $row->last_error_code === 'recipient_blacklisted'
&& $notice->status === 'completed',
'Blacklisted delivery was sent or aggregated as a failure.');
$insertNotice(6);
$insertRecipient(7, 6, 4, 'old@example.com', 'pending', $now);
$assert($delivery->tick() === true && count($sent) === 2,
'An email-changed recipient did not settle without transport.');
$row = Capsule::table('cloud_maintenance_notice_recipients')->find(7);
$assert($row->status === 'skipped'
&& $row->last_error_code === 'recipient_email_changed',
'Frozen recipient PII was sent after the account email changed.');
$insertNotice(4);
$insertRecipient(5, 4, 99, 'deleted@example.com', 'pending', $now);
$assert($delivery->tick() === true && count($sent) === 2
&& Capsule::table('cloud_maintenance_notice_recipients')->find(5)
->status === 'skipped',
'A deleted user still reached the transport.');
$insertNotice(5);
$insertRecipient(6, 5, 1, 'ok@example.com', 'pending', $now);
$fenced = new CloudMaintenanceNoticeDeliveryService(
static function (): bool {
Capsule::table('cloud_maintenance_notice_recipients')
->where('id', 6)->update([
'status' => 'unknown', 'lease_token' => null,
'lease_until' => 0,
'last_error_code' => 'external_reconciliation',
]);
return true;
},
static fn (): bool => false,
static fn (): int => $now,
new CloudMaintenanceNoticeService(
new CloudMaintenanceNoticeAudience(),
static fn (): int => $now
)
);
$assert($fenced->tick() === true
&& Capsule::table('cloud_maintenance_notice_recipients')->find(6)
->status === 'unknown',
'A stale lease token overwrote a reconciled terminal outcome.');
echo "Maintenance notice delivery tests passed.\n";