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

109 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
$root = dirname(__DIR__);
$service = file_get_contents(
$root . '/app/Services/Cloud/CloudServiceCompensationService.php'
);
$catalog = file_get_contents(
$root . '/app/Services/Cloud/CloudOperationCatalog.php'
);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$assert($service !== false && $catalog !== false,
'Service-compensation sources could not be read.');
foreach ([
"'scope_type', 'scope_id', 'incident_key', 'reason_text'",
"'incident_starts_at', 'incident_ends_at'",
"'compensation_seconds', 'notify_users', 'audience_digest'",
"'max_range' => 31622400",
"!is_bool(\$notify)",
"'/^[a-f0-9]{64}\$/D'",
"'/^[a-z0-9][a-z0-9._:-]{0,127}\$/D'",
'mb_strlen($reason) > 500',
"(\$startsAt === null) !== (\$endsAt === null)",
"\$endsAt <= \$startsAt",
] as $validationContract) {
$assert(str_contains($service, $validationContract),
'Request validation lost contract: ' . $validationContract);
}
foreach ([
"hash('sha256', self::json(\$request))",
"'requested_by_user_id', \$actorId",
"'request_idempotency_key', \$idempotencyKey",
"hash_equals((string) \$batch->request_fingerprint, \$fingerprint)",
"'幂等键已用于其他实例补偿请求'",
] as $idempotencyContract) {
$assert(str_contains($service, $idempotencyContract),
'Idempotency contract is missing: ' . $idempotencyContract);
}
$previewPosition = strpos($service, '$plan = $this->planner->preview(');
$digestPosition = strpos($service, "!hash_equals(\n \$request['audience_digest']");
$batchSavePosition = strpos($service, '$batch->save();');
$assert($previewPosition !== false && $digestPosition !== false
&& $batchSavePosition !== false
&& $previewPosition < $digestPosition
&& $digestPosition < $batchSavePosition,
'Audience digest must be recalculated and checked before freezing a batch.');
$assert(str_contains($service,
"'受影响实例或服务周期已变化,请重新预览后再补偿'"),
'Audience drift no longer has a stable conflict response.');
foreach ([
"CloudServiceCompensationItem::where(\n 'incident_key', \$request['incident_key']",
"->whereIn('instance_id', \$instanceIds)->exists()",
"'同一事故已经包含这些实例,不能重复补偿'",
] as $incidentContract) {
$assert(str_contains($service, $incidentContract),
'Incident overlap contract is missing: ' . $incidentContract);
}
$incidentWhere = strpos($service,
"'incident_key', \$request['incident_key']");
$incidentOverlap = strpos($service, "->whereIn('instance_id', \$instanceIds)");
$incidentConflict = strpos($service, "'同一事故已经包含这些实例,不能重复补偿'");
$assert($incidentWhere !== false && $incidentOverlap !== false
&& $incidentConflict !== false
&& $incidentWhere < $incidentOverlap
&& $incidentOverlap < $incidentConflict,
'Incident-key conflicts must be narrowed to overlapping instances.');
foreach ([
"->count() !== (int) \$batch->instance_count",
"'实例补偿明细冻结不完整'",
"\$items->count() !== (int) \$batch->instance_count",
"\$batch->state = 'attention'",
"\$batch->last_error_code = 'item_catalog_incomplete'",
] as $catalogContract) {
$assert(str_contains($service, $catalogContract),
'Frozen-item catalog contract is missing: ' . $catalogContract);
}
foreach ([
"\$open = \$count('pending') + \$count('applying') + \$count('deferred')",
"\$applied = \$count('applied')",
"\$rejected = \$count('rejected') + \$count('cancelled')",
"\$attention = \$count('attention')",
"\$batch->state = 'completed'",
"\$batch->state = 'partial'",
"\$batch->last_error_code = 'item_attention'",
"'终态补偿批次不能重新进入执行状态'",
] as $projectionContract) {
$assert(str_contains($service, $projectionContract),
'Batch-state projection lost contract: ' . $projectionContract);
}
$assert(substr_count($catalog, "'service_compensation'") >= 2,
'Service compensation must be registered in labels and instance logs.');
$assert(str_contains($catalog,
"'service_compensation' => '实例可用时间补偿'"),
'Service-compensation task label is missing.');
echo "Service compensation service contracts passed.\n";