|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
|
|
use App\Models\CloudTemplate;
|
|
|
|
|
use App\Models\CloudTemplateReplica;
|
|
|
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
|
use App\Services\Cloud\CloudTemplateReplicaCoordinator;
|
|
|
|
|
use App\Services\Cloud\CloudTemplateStateMachine;
|
|
|
|
|
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$template = new CloudTemplate();
|
|
|
|
|
$template->state = 'preparing';
|
|
|
|
|
$template->revision = 3;
|
|
|
|
|
$template->generation_token = str_repeat('a', 32);
|
|
|
|
|
$template->image_bytes = 4096;
|
|
|
|
|
$template->sha256 = str_repeat('b', 64);
|
|
|
|
|
CloudTemplateStateMachine::moveTemplate($template, 'importing');
|
|
|
|
|
CloudTemplateStateMachine::moveTemplate($template, 'degraded');
|
|
|
|
|
$assert((string) $template->state === 'degraded',
|
|
|
|
|
'Template state machine did not permit a partial initial import.');
|
|
|
|
|
|
|
|
|
|
$invalidRejected = false;
|
|
|
|
|
try {
|
|
|
|
|
CloudTemplateStateMachine::moveTemplate($template, 'source_failed');
|
|
|
|
|
} catch (CloudError $error) {
|
|
|
|
|
$invalidRejected = $error->httpStatus === 409;
|
|
|
|
|
}
|
|
|
|
|
$assert($invalidRejected,
|
|
|
|
|
'Template state machine accepted an impossible source transition.');
|
|
|
|
|
|
|
|
|
|
$ready = new CloudTemplateReplica();
|
|
|
|
|
$ready->host_id = 11;
|
|
|
|
|
$ready->state = 'pending';
|
|
|
|
|
$ready->revision = 3;
|
|
|
|
|
$ready->generation_token = str_repeat('a', 32);
|
|
|
|
|
$ready->image_bytes = 4096;
|
|
|
|
|
$ready->sha256 = str_repeat('b', 64);
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($ready, 'importing');
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($ready, 'active');
|
|
|
|
|
|
|
|
|
|
$failed = new CloudTemplateReplica();
|
|
|
|
|
$failed->host_id = 12;
|
|
|
|
|
$failed->state = 'pending';
|
|
|
|
|
$failed->revision = 3;
|
|
|
|
|
$failed->generation_token = str_repeat('a', 32);
|
|
|
|
|
$failed->image_bytes = 0;
|
|
|
|
|
$failed->sha256 = str_repeat('b', 64);
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($failed, 'importing');
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($failed, 'failed');
|
|
|
|
|
|
|
|
|
|
$assert(CloudTemplateStateMachine::availability(
|
|
|
|
|
$template, [$ready, $failed], [11, 12]
|
|
|
|
|
) === 'degraded',
|
|
|
|
|
'One healthy and one failed eligible replica was not degraded.');
|
|
|
|
|
$assert(CloudTemplateStateMachine::availability(
|
|
|
|
|
$template, [$ready, $failed], [11]
|
|
|
|
|
) === 'active',
|
|
|
|
|
'A complete eligible-host projection was not active.');
|
|
|
|
|
$assert(CloudTemplateStateMachine::availability(
|
|
|
|
|
$template, [$failed], [12]
|
|
|
|
|
) === 'error',
|
|
|
|
|
'A catalog without any exact healthy replica was exposed as available.');
|
|
|
|
|
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($failed, 'importing');
|
|
|
|
|
CloudTemplateStateMachine::moveReplica($failed, 'active');
|
|
|
|
|
$failed->image_bytes = 4096;
|
|
|
|
|
$assert(CloudTemplateStateMachine::availability(
|
|
|
|
|
$template, [$ready, $failed], [11, 12]
|
|
|
|
|
) === 'active',
|
|
|
|
|
'A safely retried exact replica did not restore full availability.');
|
|
|
|
|
|
|
|
|
|
$stale = new CloudTemplateReplica();
|
|
|
|
|
$stale->state = 'stale';
|
|
|
|
|
$stale->revision = 3;
|
|
|
|
|
$stale->generation_token = str_repeat('a', 32);
|
|
|
|
|
$stale->image_bytes = 4096;
|
|
|
|
|
$stale->sha256 = str_repeat('b', 64);
|
|
|
|
|
$stale->observed_state = 'stale';
|
|
|
|
|
$stale->observed_revision = 2;
|
|
|
|
|
$stale->observed_generation_token = str_repeat('a', 32);
|
|
|
|
|
$stale->observed_image_bytes = 4096;
|
|
|
|
|
$stale->observed_sha256 = str_repeat('b', 64);
|
|
|
|
|
$stale->last_verified_at = time();
|
|
|
|
|
$divergent = new CloudTemplateReplica();
|
|
|
|
|
$divergent->state = 'failed';
|
|
|
|
|
$divergent->error_code = 'template_observed_divergent';
|
|
|
|
|
$divergent->observed_state = 'divergent';
|
|
|
|
|
$assert(CloudTemplateReplicaCoordinator::repairPlan(
|
|
|
|
|
$template,
|
|
|
|
|
null,
|
|
|
|
|
true
|
|
|
|
|
) === ['action' => 'import', 'reason' => 'replica_missing']
|
|
|
|
|
&& CloudTemplateReplicaCoordinator::repairPlan(
|
|
|
|
|
$template,
|
|
|
|
|
null,
|
|
|
|
|
false
|
|
|
|
|
) === ['action' => null, 'reason' => 'host_ineligible']
|
|
|
|
|
&& CloudTemplateReplicaCoordinator::repairPlan(
|
|
|
|
|
$template,
|
|
|
|
|
$ready,
|
|
|
|
|
true
|
|
|
|
|
) === ['action' => null, 'reason' => 'exact']
|
|
|
|
|
&& CloudTemplateReplicaCoordinator::repairPlan(
|
|
|
|
|
$template,
|
|
|
|
|
$stale,
|
|
|
|
|
true
|
|
|
|
|
) === ['action' => 'metadata', 'reason' => 'metadata_stale']
|
|
|
|
|
&& CloudTemplateReplicaCoordinator::repairPlan(
|
|
|
|
|
$template,
|
|
|
|
|
$divergent,
|
|
|
|
|
true
|
|
|
|
|
) === ['action' => null, 'reason' => 'content_divergent'],
|
|
|
|
|
'Administrator repair projection exposed an unsafe or missing action.');
|
|
|
|
|
|
|
|
|
|
echo "Template replica state-machine behavior passed.\n";
|