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

109 lines
3.5 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudRescueCatalogStateMachine;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
CloudRescueCatalogStateMachine::assertIntegrity();
$definition = CloudRescueCatalogStateMachine::definition();
$mutated = $definition;
$mutated['replica']['import_unknown']['recovery_actions'] = [];
$missingRecoveryRejected = false;
try {
CloudRescueCatalogStateMachine::assertIntegrity($mutated);
} catch (LogicException $error) {
$missingRecoveryRejected = str_contains(
$error->getMessage(),
'Missing required rescue recovery action'
);
}
$assert($missingRecoveryRejected,
'Integrity check accepted a failure state without its recovery exit.');
$mutated = $definition;
$mutated['artifact']['publish_unknown']['transitions'][] = 'invented';
$unknownTransitionRejected = false;
try {
CloudRescueCatalogStateMachine::assertIntegrity($mutated);
} catch (LogicException $error) {
$unknownTransitionRejected = str_contains(
$error->getMessage(),
'Unknown rescue state transition'
);
}
$assert($unknownTransitionRejected,
'Integrity check accepted a transition to an undefined state.');
CloudRescueCatalogStateMachine::assertTransition(
'catalog', 'preparing', 'validating'
);
$invalidTransitionRejected = false;
try {
CloudRescueCatalogStateMachine::assertTransition(
'catalog', 'active', 'preparing'
);
} catch (CloudError $error) {
$invalidTransitionRejected = $error->httpStatus === 409;
}
$assert($invalidTransitionRejected,
'Catalog state machine accepted an impossible backward transition.');
$assert(CloudRescueCatalogStateMachine::recoveryActions(
'artifact', 'publish_unknown'
) === ['reconcile_publish'],
'Unknown publish state did not expose reconciliation only.');
$assert(CloudRescueCatalogStateMachine::recoveryActions(
'catalog', 'source_failed'
) === ['retire'],
'Ephemeral rescue source failure incorrectly exposed a replay action.');
$assert(CloudRescueCatalogStateMachine::recoveryActions(
'artifact', 'publish_failed'
) === ['delete_artifact'],
'Failed rescue publication incorrectly exposed a provider replay action.');
$assert(CloudRescueCatalogStateMachine::recoveryActions(
'replica', 'failed'
) === ['retry_import', 'delete_replica'],
'Known-not-applied import failure did not expose the safe retry.');
$catalog = [
'revision' => 3,
'generation_token' => str_repeat('a', 32),
'size_bytes' => 4096,
'sha256' => str_repeat('b', 64),
];
$exact = [
'host_id' => 11,
'state' => 'active',
'revision' => 3,
'generation_token' => str_repeat('a', 32),
'size_bytes' => 4096,
'sha256' => str_repeat('b', 64),
];
$failed = $exact;
$failed['host_id'] = 12;
$failed['state'] = 'failed';
$assert(CloudRescueCatalogStateMachine::availability(
$catalog, [$exact, $failed], [11, 12]
) === 'degraded',
'One exact replica and one failed replica were not degraded.');
$assert(CloudRescueCatalogStateMachine::availability(
$catalog, [$exact], [11]
) === 'active',
'All eligible exact replicas were not active.');
$stale = $exact;
$stale['generation_token'] = str_repeat('c', 32);
$assert(CloudRescueCatalogStateMachine::availability(
$catalog, [$stale], [11]
) === 'error',
'A mismatched immutable generation was exposed as usable.');
echo "Rescue catalog state-machine behavior passed.\n";