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.'); echo "Template replica state-machine behavior passed.\n";