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

232 lines
8.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services\Cloud {
/** Isolated encrypted envelope used only by this focused contract test. */
final class CloudSecrets
{
private static array $values = [];
public static function encrypt(array $value, string $purpose): string
{
$key = bin2hex(random_bytes(16));
self::$values[$key] = $value;
return 'operation-recovery-policy-test:' . $key;
}
public static function decrypt(string $value, string $purpose): array
{
$key = substr(
$value,
strlen('operation-recovery-policy-test:')
);
if (!array_key_exists($key, self::$values)) {
throw new \RuntimeException('Missing recovery policy payload.');
}
return self::$values[$key];
}
}
}
namespace {
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudAgentCompletionEvent;
use App\Models\CloudJob;
use App\Services\Cloud\CloudAgentError;
use App\Services\Cloud\CloudAutomaticOperationRecoveryService;
use App\Services\Cloud\CloudOperationCompletionService;
use App\Services\Cloud\CloudOperationRecoveryPolicy;
use App\Services\Cloud\CloudOperationRecoveryService;
use App\Services\Cloud\CloudRecoveryInstanceStateConflict;
use App\Services\Cloud\CloudSecrets;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$expectedKinds = [
'action',
'reinstall',
'rescue',
'delete',
'delete_force',
'instance_migration_cleanup',
'instance_migration_rollback',
'instance_migration_activate',
'expiry_delete',
'network_change',
'instance_settings',
'instance_upgrade',
'snapshot_create',
'snapshot_restore',
'snapshot_delete',
'backup_create',
'backup_recover',
'firewall_apply',
'service_suspend',
'network_suspend',
'network_resume',
'template_import_replica',
'template_metadata_replica',
'template_delete_replica',
'rescue_template_import',
'rescue_template_delete',
'user_iso_import_replica',
'user_iso_delete_replica',
];
$assert(CloudOperationRecoveryPolicy::kinds() === $expectedKinds,
'The Agent leaf recovery policy changed without updating its contract.');
$assert(count(CloudOperationRecoveryPolicy::kinds()) === 28,
'The recovery policy contract must contain exactly 28 Agent leaf kinds.');
foreach ($expectedKinds as $kind) {
$assert(CloudOperationRecoveryPolicy::supports($kind),
'The recovery policy omitted ' . $kind . '.');
}
foreach ([
// Provisioning is finalized through its paid-order cleanup contract.
'create',
// Restores are create-like and require absence evidence before release.
'backup_restore',
// Aggregate parents never cross the Agent dispatch boundary.
'template_import', 'template_metadata_sync', 'template_delete',
'user_iso_import', 'user_iso_delete',
// Payments and host storage have independent reconciliation rules.
'payment_create', 'host_storage_configure',
] as $kind) {
$assert(!CloudOperationRecoveryPolicy::supports($kind),
'An aggregate or separately finalized kind entered leaf recovery: '
. $kind);
}
$attemptState = new ReflectionMethod(
CloudOperationRecoveryService::class,
'automaticRecoveryState'
);
$state = $attemptState->invoke(
null,
'automatic_recovery:v1:1700000000:13:pending'
);
$assert(is_array($state) && ($state['attempts'] ?? null) === 13,
'A recovery marker was still capped at twelve attempts.');
$saturated = $attemptState->invoke(
null,
'automatic_recovery:v1:1700000000:' . str_repeat('9', 80)
. ':pending'
);
$assert(is_array($saturated)
&& ($saturated['attempts'] ?? null) === PHP_INT_MAX,
'A durable saturated recovery attempt counter was rejected.');
$errorCode = new ReflectionMethod(
CloudAutomaticOperationRecoveryService::class,
'recoveryErrorCode'
);
$assert($errorCode->invoke(null, new CloudAgentError(
'invalid inspection', 502, 'agent_invalid_inspection', false, 200
)) === 'recovery_evidence_invalid'
&& $errorCode->invoke(
null,
new CloudRecoveryInstanceStateConflict('state conflict', 409)
) === 'recovery_instance_state_conflict',
'Concrete evidence/state anomalies collapsed into a generic retry code.');
$operationId = str_repeat('a', 32);
$identity = [
'action' => 'change-network',
'resource_id' => '11111111-1111-4111-8111-111111111111',
'request_fingerprint' => str_repeat('f', 64),
];
$job = new CloudJob();
$job->operation_id = $operationId;
$job->kind = 'network_change';
$job->phase = 'terminal';
$job->effect_resolution = 'not_applied';
$job->sealed_payload = CloudSecrets::encrypt([
'_operation_identity' => ['version' => 1] + $identity,
], 'job-payload');
$notApplied = new ReflectionMethod(
CloudOperationRecoveryService::class,
'assertNotAppliedOperationIdentity'
);
$notApplied->invoke(null, $job, [
'operation_id' => $operationId,
'status' => 'failed',
'effect_may_have_applied' => false,
'safe_to_resubmit' => true,
'error_code' => 'network_not_committed',
'result' => null,
] + $identity);
$event = new CloudAgentCompletionEvent();
$event->operation_id = $operationId;
$event->outcome = 'failed';
$event->effect_may_have_applied = 0;
$event->safe_to_resubmit = 1;
$event->error_code = 'network_not_committed';
$event->result_json = json_encode([
'schema' => 'virt_cloud_completion_event',
'version' => 1,
'identity' => $identity,
'result' => ['public_ip_cidr' => '203.0.113.10/24'],
], JSON_THROW_ON_ERROR);
$lateOperation = new ReflectionMethod(
CloudOperationRecoveryService::class,
'lateReceiptOperation'
);
$rebuilt = $lateOperation->invoke(null, $job, $event, 'not_applied');
$assert($rebuilt === CloudOperationCompletionService::operation($event)
&& ($rebuilt['safe_to_resubmit'] ?? null) === true
&& ($rebuilt['error_code'] ?? null) === 'network_not_committed',
'Late receipt recovery replaced the exact kind-specific Agent result.');
$preparedMarker = new ReflectionMethod(
CloudOperationRecoveryService::class,
'preparedMarker'
);
$baseOperation = [
'operation_id' => $operationId,
'status' => 'succeeded',
'effect_may_have_applied' => false,
'safe_to_resubmit' => false,
'error_code' => '',
'result' => ['snapshot_key' => str_repeat('b', 32), 'deleted' => true],
];
$otherOperation = $baseOperation;
$otherOperation['result']['snapshot_key'] = str_repeat('c', 32);
$source = 'automatic_applied_succeeded_nofence:' . str_repeat('d', 64);
$assert($preparedMarker->invoke(null, $baseOperation, $source)
!== $preparedMarker->invoke(null, $otherOperation, $source),
'Prepared evidence ignored a non-settings ledger result field.');
$payloadJob = new CloudJob();
$payloadJob->kind = 'network_change';
$payloadJob->phase = 'terminal';
$payloadJob->effect_resolution = 'applied';
$payloadJob->sealed_payload = CloudSecrets::encrypt([
'body' => ['hostname' => 'vm.example'],
'credential' => ['password' => 'never-persist'],
'_operation_identity' => ['version' => 1] + $identity,
'_operation_recovery' => [
'operation' => $baseOperation,
'resolution' => 'applied',
],
], 'job-payload');
$finalizedPayload = new ReflectionMethod(
CloudOperationRecoveryService::class,
'finalizedRecoveryPayload'
);
$clean = CloudSecrets::decrypt(
$finalizedPayload->invoke(null, $payloadJob),
'job-payload'
);
$assert(!array_key_exists('_operation_recovery', $clean)
&& !array_key_exists('credential', $clean)
&& ($clean['body']['hostname'] ?? null) === 'vm.example',
'Recovery commit retained prepared evidence or a credential.');
echo "Cloud operation recovery policy contract passed.\n";
}