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

311 lines
12 KiB
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstanceMigration;
use App\Services\Cloud\CloudInstanceMigrationService;
use App\Services\Cloud\CloudInstanceMigrationWorkflow;
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$migration = static function (
string $state,
string $recoveryAction = ''
): CloudInstanceMigration {
$value = new CloudInstanceMigration();
$value->state = $state;
$value->recovery_action = $recoveryAction;
$value->cleanup_mode = 'delete';
$value->vpc_port_id = null;
$value->source_prepare_job_id = 1;
$value->target_receive_job_id = null;
$value->firewall_job_id = null;
$value->activate_job_id = null;
$value->rollback_job_id = null;
$value->cleanup_job_id = null;
$value->source_was_running = true;
$value->updated_at = 1_800_000_000;
return $value;
};
$step = static function (array $projection, string $key): ?array {
foreach ($projection['steps'] ?? [] as $item) {
if (($item['key'] ?? null) === $key) {
return $item;
}
}
return null;
};
CloudInstanceMigrationWorkflow::assertIntegrity();
$stages = CloudInstanceMigrationWorkflow::stages();
$assert(array_keys($stages) === [
'prepare', 'vpc_target', 'transfer', 'cutover', 'firewall', 'activation',
'cleanup', 'vpc_source', 'rollback',
], 'The executable migration-stage registry is incomplete or reordered.');
$assert(method_exists(
CloudInstanceMigrationWorkflow::class,
'assertDefinitions'
),
'Migration workflow definitions cannot be mutation-tested.');
$expectedFailureContracts = [
'prepare' => ['conditional_abort', null, ['abort_prepare']],
'vpc_target' => [
'external_orchestration', 'retry_vpc_target',
['retry_vpc_target', 'abort_vpc_target'],
],
'transfer' => [
'manual_recovery', 'retry_transfer',
['retry_transfer', 'abort_transfer'],
],
'cutover' => ['inline_transaction', null, []],
'firewall' => [
'manual_recovery', 'retry_firewall', ['retry_firewall'],
],
'activation' => [
'manual_recovery', 'retry_activation', ['retry_activation'],
],
'cleanup' => ['source_retained', null, []],
'vpc_source' => [
'external_orchestration', 'retry_vpc_source',
['retry_vpc_source'],
],
'rollback' => [
'manual_recovery', 'retry_rollback', ['retry_rollback'],
],
];
foreach ($expectedFailureContracts as $stageKey => $contract) {
[$policy, $failureAction, $requiredActions] = $contract;
$assert(($stages[$stageKey]['failure_policy'] ?? null) === $policy
&& ($stages[$stageKey]['failure_action'] ?? null) === $failureAction
&& ($stages[$stageKey]['required_recovery_actions'] ?? null)
=== $requiredActions,
"Stage {$stageKey} lost its explicit failure contract.");
foreach ($requiredActions as $requiredAction) {
$mutated = $stages;
unset($mutated[$stageKey]['recovery_actions'][$requiredAction]);
$missingExitRejected = false;
try {
CloudInstanceMigrationWorkflow::assertDefinitions($mutated);
} catch (LogicException $error) {
$missingExitRejected = true;
}
$assert($missingExitRejected,
"Stage {$stageKey} accepted a missing recovery exit.");
}
}
$service = new ReflectionClass(CloudInstanceMigrationService::class);
$jobKinds = [];
$jobFields = [];
$recoveryActions = [];
foreach ($stages as $key => $definition) {
if ($definition['job_kind'] !== null) {
$jobKinds[] = $definition['job_kind'];
$jobFields[] = $definition['job_field'];
$assert($service->hasMethod($definition['finish_handler']),
"Stage {$key} has no executable completion handler.");
}
foreach ($definition['recovery_actions'] as $action => $recovery) {
$recoveryActions[] = $action;
$assert(CloudInstanceMigrationWorkflow::recoveryHandler($action)
=== $recovery['handler']
&& $service->hasMethod($recovery['handler']),
"Recovery action {$action} has no executable handler.");
}
}
$assert(count($jobKinds) === count(array_unique($jobKinds))
&& count($jobFields) === count(array_unique($jobFields))
&& count($recoveryActions) === count(array_unique($recoveryActions)),
'Migration stages contain duplicate job or recovery definitions.');
$transferFailure = $migration('attention_required', 'retry_transfer');
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$transferFailure,
false
) === ['retry_transfer', 'abort_transfer'],
'Transfer recovery buttons and execution policy diverged.');
$vpcTargetFailure = $migration(
'attention_required',
'retry_vpc_target'
);
$vpcTargetFailure->vpc_port_id = 9;
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$vpcTargetFailure,
false
) === ['retry_vpc_target', 'abort_vpc_target'],
'Target VPC routing has no complete retry and abort policy.');
$vpcUnknown = $migration('attention_required', 'reconcile_vpc_target');
$vpcUnknown->vpc_port_id = 9;
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$vpcUnknown,
false
) === [],
'Unknown VPC routing effect exposed an unsafe replay action.');
$firewallFailure = $migration('attention_required', 'retry_firewall');
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$firewallFailure,
false
) === ['retry_firewall'],
'Firewall recovery policy is incomplete.');
$activationFailure = $migration('attention_required', 'retry_activation');
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$activationFailure,
false
) === ['retry_activation'],
'Activation recovery policy is incomplete.');
$rollbackFailure = $migration('attention_required', 'retry_rollback');
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$rollbackFailure,
false
) === ['retry_rollback'],
'Rollback recovery policy is incomplete.');
$prepareUnknown = $migration('attention_required', 'reconcile_prepare');
$assert(CloudInstanceMigrationWorkflow::recoveryActions(
$prepareUnknown,
false
) === []
&& CloudInstanceMigrationWorkflow::recoveryActions(
$prepareUnknown,
true
) === ['abort_prepare'],
'Prepare abort was exposed without evidence or hidden after proof.');
$invalidStateRejected = false;
try {
CloudInstanceMigrationWorkflow::assertState(
'attention_required',
'missing_recovery_exit'
);
} catch (LogicException $error) {
$invalidStateRejected = true;
}
$assert($invalidStateRejected,
'An attention state accepted an unregistered recovery exit.');
$transferring = $migration('transferring');
$transferring->target_receive_job_id = 2;
$projection = CloudInstanceMigrationWorkflow::progress($transferring);
$assert(($projection['current_stage']['key'] ?? null) === 'transfer'
&& ($projection['current_stage']['index'] ?? null) === 3
&& ($projection['stage_count'] ?? null) === 7
&& ($projection['byte_progress_available'] ?? null) === false
&& ($step($projection, 'prepare')['status'] ?? null) === 'completed'
&& ($step($projection, 'transfer')['status'] ?? null) === 'running',
'Transfer progress projection is not deterministic.');
$vpcRouting = $migration('vpc_target_routing');
$vpcRouting->vpc_port_id = 9;
$vpcProjection = CloudInstanceMigrationWorkflow::progress($vpcRouting);
$assert(($vpcProjection['current_stage']['key'] ?? null) === 'vpc_target'
&& ($vpcProjection['current_stage']['index'] ?? null) === 2
&& ($step($vpcProjection, 'prepare')['status'] ?? null) === 'completed'
&& ($step($vpcProjection, 'vpc_target')['status'] ?? null) === 'running'
&& ($step($vpcProjection, 'transfer')['status'] ?? null) === 'pending',
'Target VPC routing is missing from migration progress.');
$firewallAttention = CloudInstanceMigrationWorkflow::progress(
$firewallFailure
);
$assert(($firewallAttention['status'] ?? null) === 'attention_required'
&& ($firewallAttention['current_stage']['key'] ?? null) === 'firewall'
&& ($step($firewallAttention, 'firewall')['status'] ?? null)
=== 'attention_required',
'Recovery attention was projected onto the wrong stage.');
$retained = $migration('source_retained');
$retainedProjection = CloudInstanceMigrationWorkflow::progress($retained);
$assert(($retainedProjection['status'] ?? null) === 'source_retained'
&& ($retainedProjection['blocks_instance'] ?? null) === false
&& ($retainedProjection['blocks_new_migration'] ?? null) === true
&& ($step($retainedProjection, 'cleanup')['status'] ?? null)
=== 'available',
'A retained source copy has an incorrect progress or locking projection.');
$retained->recovery_action = 'reconcile_cleanup';
$cleanupUnknown = CloudInstanceMigrationWorkflow::progress($retained);
$assert(($cleanupUnknown['status'] ?? null) === 'attention_required'
&& ($step($cleanupUnknown, 'cleanup')['status'] ?? null)
=== 'attention_required',
'Unknown source cleanup was presented as an ordinary retained copy.');
$rolledBack = $migration('rolled_back');
$rolledBack->error_code = 'migration_transfer_aborted';
$rolledBack->target_receive_job_id = 2;
$rollbackProjection = CloudInstanceMigrationWorkflow::progress($rolledBack);
$assert(($rollbackProjection['status'] ?? null) === 'rolled_back'
&& ($step($rollbackProjection, 'prepare')['status'] ?? null)
=== 'completed'
&& ($step($rollbackProjection, 'transfer')['status'] ?? null)
=== 'interrupted'
&& ($step($rollbackProjection, 'rollback')['status'] ?? null)
=== 'completed',
'Rollback progress hid the interrupted migration stage.');
$retryingRollback = $migration('rollback_running');
$retryingRollback->error_code = 'migration_source_rollback_failed';
$retryingRollback->target_receive_job_id = 2;
$retryingRollback->vpc_port_id = 9;
$retryProjection = CloudInstanceMigrationWorkflow::progress(
$retryingRollback
);
$assert(($step($retryProjection, 'prepare')['status'] ?? null)
=== 'completed'
&& ($step($retryProjection, 'transfer')['status'] ?? null)
=== 'interrupted'
&& ($step($retryProjection, 'vpc_source')['status'] ?? null)
=== 'completed'
&& ($step($retryProjection, 'rollback')['status'] ?? null)
=== 'running',
'Retrying rollback rewrote completed source preparation as interrupted.');
$completed = $migration('completed');
$completed->target_receive_job_id = 2;
$completed->activate_job_id = 3;
$completed->cleanup_job_id = 4;
$completedProjection = CloudInstanceMigrationWorkflow::progress($completed);
$assert(($completedProjection['status'] ?? null) === 'completed'
&& ($step($completedProjection, 'firewall')['status'] ?? null)
=== 'skipped'
&& ($step($completedProjection, 'cleanup')['status'] ?? null)
=== 'completed',
'Completed migration stages were not projected truthfully.');
$serviceSource = (string) file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudInstanceMigrationService.php'
);
$settlementSource = (string) file_get_contents(
dirname(__DIR__)
. '/app/Services/Cloud/CloudInstanceMigrationSettlement.php'
);
$vpcSource = (string) file_get_contents(
dirname(__DIR__) . '/app/Services/Cloud/CloudVpcPortService.php'
);
$assert(substr_count($serviceSource,
'CloudInstanceMigrationWorkflow::job(') === 2
&& !str_contains($serviceSource,
'$field = match ((string) $job->kind)')
&& str_contains($serviceSource,
'CloudInstanceMigrationWorkflow::recoveryActions(')
&& !str_contains($serviceSource,
"'maintenance_migration_transferred'")
&& str_contains($settlementSource,
"'maintenance_migration_transferred'")
&& str_contains($serviceSource,
'CloudVpcPortService::migrationPortIdLocked(')
&& str_contains($serviceSource,
'CloudVpcPortService::beginMigrationToTargetLocked(')
&& str_contains($serviceSource,
'CloudVpcPortService::migrationTargetMetadataLocked(')
&& str_contains($serviceSource,
'CloudVpcPortService::finalizeMigrationCutoverLocked(')
&& str_contains($serviceSource,
'CloudVpcPortService::beginMigrationToSourceLocked(')
&& str_contains($vpcSource,
'CloudInstanceMigrationService::finishVpcRoutingJobLocked('),
'Migration routing or resource settlement escaped its single module.');
echo "Maintenance migration workflow integrity and progress tests passed.\n";