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

107 lines
5.0 KiB
PHTML

<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Services\Cloud\CloudOperationRecoveryContract;
use App\Services\Cloud\CloudOperationRecoveryIdentity;
use App\Services\Cloud\CloudOperationRecoveryPolicy;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$contract = json_decode(file_get_contents(
dirname(__DIR__, 2) . '/agent/operation/recovery_contract.json'
), true, 512, JSON_THROW_ON_ERROR);
$assert($contract['schema_version'] === 1, 'Unsupported recovery contract version.');
$expected = [];
foreach ($contract['operations'] as $entry) {
$kind = $entry['kind'];
$assert(!isset($expected[$kind]), 'Duplicate recovery kind: ' . $kind);
unset($entry['kind']);
$expected[$kind] = $entry;
}
$assert(CloudOperationRecoveryContract::OPERATIONS === $expected,
'PHP/Agent recovery inventory drift: run php tools/generate-recovery-contract.php');
$generic = [];
$automatic = [];
foreach ($expected as $kind => $entry) {
$mode = $entry['recovery_mode'];
if ($mode === 'generic') {
$generic[] = $kind;
$assert($entry['system_resolutions'] === ['applied', 'not_applied'],
'A generic leaf lost a recovery direction: ' . $kind);
}
if (in_array($mode, ['generic', 'create'], true)) {
$automatic[] = $kind;
}
$assert(CloudOperationRecoveryPolicy::supports($kind) === ($mode === 'generic'),
'Generic policy mode differs from inventory: ' . $kind);
$assert(CloudOperationRecoveryIdentity::supports($kind)
=== ($entry['identity_scope'] !== 'none'),
'Identity support differs from inventory: ' . $kind);
foreach (['applied', 'not_applied', 'unknown', ''] as $resolution) {
$assert(CloudOperationRecoveryPolicy::allowsSystemResolution($kind, $resolution)
=== in_array($resolution, $entry['system_resolutions'], true),
'System resolution differs from inventory: ' . $kind);
}
foreach ($entry['actions'] as $action) {
$assert(CloudOperationRecoveryPolicy::allowsAction($kind, $action),
'Declared wire action is not recognized: ' . $kind . '/' . $action);
$assert(!CloudOperationRecoveryPolicy::allowsAction($kind, $action . ':future')
&& !CloudOperationRecoveryPolicy::allowsAction($kind, $action . "\n"),
'Action matching is not exact: ' . $kind);
}
}
$assert(CloudOperationRecoveryPolicy::kinds() === $generic,
'Generic selector does not follow the inventory.');
$assert(CloudOperationRecoveryPolicy::automaticKinds() === $automatic,
'Automatic selector does not follow the inventory.');
$assert($expected['create']['recovery_mode'] === 'create'
&& $expected['create']['system_resolutions'] === ['not_applied']
&& !CloudOperationRecoveryPolicy::supports('create'),
'Create must retain dedicated finance recovery and one-way fence resolution.');
foreach (['host_storage_configure', 'payment_create', 'template_import',
'template_metadata_sync', 'template_delete', 'user_iso_import', 'user_iso_delete'] as $kind) {
$assert($expected[$kind]['system_resolutions'] === []
&& !in_array($kind, $automatic, true),
'A separately finalized operation gained system recovery: ' . $kind);
}
foreach (['', 'future_kind'] as $kind) {
$assert(!CloudOperationRecoveryPolicy::supports($kind)
&& !CloudOperationRecoveryIdentity::supports($kind)
&& !CloudOperationRecoveryPolicy::allowsAction($kind, 'create-vm'),
'Unknown kinds must be denied.');
}
$key = str_repeat('ab', 16);
foreach ([
['snapshot_create', 'create-snapshot:' . $key . ':filesystem'],
['snapshot_create', 'create-snapshot:' . $key . ':crash'],
['snapshot_restore', 'restore-snapshot:' . $key],
['snapshot_delete', 'delete-snapshot:' . $key],
['backup_create', 'create-backup:' . $key . ':filesystem'],
['backup_create', 'create-backup:' . $key . ':crash'],
['backup_restore', 'restore-backup:' . $key],
] as [$kind, $action]) {
$assert(CloudOperationRecoveryPolicy::allowsAction($kind, $action),
'Valid parameterized action was rejected.');
foreach ([$action . "\n", $action . ':extra', strtoupper($action),
str_replace($key, substr($key, 1), $action)] as $invalid) {
$assert(!CloudOperationRecoveryPolicy::allowsAction($kind, $invalid),
'Malformed parameterized action was accepted.');
}
$assert(!CloudOperationRecoveryPolicy::allowsAction('instance_settings', $action),
'An action can escape its owning kind.');
}
$assert($expected['backup_restore']['recovery_mode'] === 'create'
&& $expected['backup_restore']['identity_scope'] === 'instance'
&& $expected['backup_restore']['system_resolutions'] === ['not_applied']
&& CloudOperationRecoveryPolicy::isCreateLike('backup_restore'),
'Backup restore must retain instance identity and create-like absence recovery.');
echo "PHP/Agent generated recovery inventory contract passed.\n";