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

117 lines
4.4 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudStorageDurabilityPolicy;
use App\Services\Cloud\CloudStorageReadRouter;
use App\Services\Cloud\CloudStoragePlacementPolicy;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$sha = str_repeat('a', 64);
$artifact = static fn (string $kind): array => [
'kind' => $kind,
'size_bytes' => 1024,
'sha256' => $sha,
] + CloudStorageDurabilityPolicy::forKind($kind) + ['state' => 'publishing'];
$replica = static fn (int $id, int $backendId, string $state = 'ready'): array => [
'id' => $id,
'backend_id' => $backendId,
'role' => $id === 1 ? 'primary' : 'secondary',
'state' => $state,
'size_bytes' => 1024,
'sha256' => $sha,
'verified_at' => $state === 'ready' ? 100 : 0,
];
$backup = $artifact('backup');
$one = CloudStorageDurabilityPolicy::project($backup, [$replica(1, 10)]);
$assert($one['state'] === 'available' && $one['can_read']
&& !$one['protected'],
'One verified backup copy must be available but not protected.');
$two = CloudStorageDurabilityPolicy::project(
$backup,
[$replica(1, 10), $replica(2, 20)]
);
$assert($two['state'] === 'protected' && $two['protected'],
'Two verified backup copies must be protected.');
$template = $artifact('template');
$oneTemplate = CloudStorageDurabilityPolicy::project(
$template,
[$replica(1, 10), $replica(2, 20, 'copying')]
);
$assert($oneTemplate['state'] === 'publishing'
&& !$oneTemplate['can_read'],
'A required-two template must not become readable after one copy.');
$twoTemplate = CloudStorageDurabilityPolicy::project(
$template,
[$replica(1, 10), $replica(2, 20)]
);
$assert($twoTemplate['state'] === 'protected'
&& $twoTemplate['can_read'],
'A required-two template must become readable after both copies verify.');
$candidates = [
$replica(1, 10) + ['backend' => [
'admin_state' => 'active', 'health_state' => 'unreachable',
'write_priority' => 1,
]],
$replica(2, 20) + ['backend' => [
'admin_state' => 'draining', 'health_state' => 'healthy',
'write_priority' => 100,
]],
];
$selected = CloudStorageReadRouter::select($backup, $candidates);
$assert($selected['backend_id'] === 20,
'Reads must fail over to a verified healthy replica on a draining backend.');
$candidates[0]['backend']['health_state'] = 'healthy';
$selected = CloudStorageReadRouter::select($backup, $candidates);
$assert($selected['backend_id'] === 10,
'Reads must prefer a healthy active primary replica.');
$backends = [
['id' => 1, 'config_revision' => 2, 'tested_revision' => 2,
'admin_state' => 'active', 'health_state' => 'healthy',
'write_priority' => 10, 'failure_domain' => 'provider-a'],
['id' => 2, 'config_revision' => 1, 'tested_revision' => 1,
'admin_state' => 'active', 'health_state' => 'healthy',
'write_priority' => 30, 'failure_domain' => 'provider-b'],
['id' => 3, 'config_revision' => 1, 'tested_revision' => 1,
'admin_state' => 'active', 'health_state' => 'healthy',
'write_priority' => 20, 'failure_domain' => 'provider-a'],
];
$placed = CloudStoragePlacementPolicy::select($backends, 2, 2);
$assert(array_column($placed, 'id') === [1, 2],
'Placement must prefer a distinct failure domain over a lower-priority peer.');
$backends[1]['health_state'] = 'unreachable';
$backupPlacement = CloudStoragePlacementPolicy::select($backends, 2, 1);
$assert(array_column($backupPlacement, 'id') === [1, 3],
'Async backup placement may use two healthy backends in one domain.');
$backends[2]['tested_revision'] = 0;
try {
CloudStoragePlacementPolicy::select($backends, 2, 2);
$assert(false, 'Required-two placement accepted only one eligible backend.');
} catch (CloudError $error) {
$assert($error->httpStatus === 503,
'Insufficient required placement must be a 503 business error.');
}
$badIdentity = $candidates;
$badIdentity[0]['sha256'] = str_repeat('b', 64);
$badIdentity[1]['state'] = 'degraded';
try {
CloudStorageReadRouter::select($backup, $badIdentity);
$assert(false, 'A mismatched or non-ready replica was selected.');
} catch (CloudError $error) {
$assert($error->httpStatus === 503,
'Unavailable replica routing must be a 503 business error.');
}
echo "Storage durability policy tests passed.\n";