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

145 lines
6.0 KiB
PHP

<?php
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$artifactModel = file_get_contents(
$root . '/app/Models/CloudStorageArtifact.php'
);
$publicationModel = file_get_contents(
$root . '/app/Models/CloudStoragePublication.php'
);
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$table = static function (string $name) use ($schema, $assert): string {
$matched = preg_match(
'/CREATE TABLE ' . preg_quote($name, '/')
. ' \(([\s\S]*?)\n\) ENGINE=InnoDB/',
$schema,
$match
) === 1;
$assert($matched, 'Missing storage publication table: ' . $name);
return $match[1];
};
$artifact = $table('cloud_storage_artifacts');
$publication = $table('cloud_storage_publications');
$assert(str_contains($artifact,
'maximum_size_bytes BIGINT UNSIGNED NOT NULL')
&& str_contains($artifact,
'chk_cloud_storage_artifact_identity CHECK')
&& str_contains($artifact,
"(state IN ('publishing','deleted') AND size_bytes = 0 AND sha256 = '')")
&& str_contains($artifact,
'size_bytes BETWEEN 1 AND maximum_size_bytes')
&& str_contains($artifact,
"sha256 REGEXP '^[a-f0-9]{64}$'"),
'Artifact pending/exact identity contract is incomplete.');
foreach ([
'publication_key', 'artifact_id', 'backup_id',
'template_import_intent_id', 'rescue_template_id', 'state', 'attempts',
'available_at', 'lease_token', 'lease_until', 'last_error_code',
'coverage_ready_at', 'finalized_at', 'created_at', 'updated_at',
] as $column) {
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
$publication) === 1,
'Storage publication column is missing: ' . $column);
$assert(str_contains($setup, "'" . $column . "'"),
'CloudSetup does not require publication column: ' . $column);
}
foreach ([
'cloud_storage_publication_key',
'cloud_storage_publication_artifact',
'cloud_storage_publication_backup',
'cloud_storage_publication_template_import',
'cloud_storage_publication_rescue',
'cloud_storage_publication_due',
'cloud_storage_publication_lease',
] as $index) {
$assert(str_contains($publication, $index)
&& str_contains($setup, "'" . $index . "'"),
'Storage publication index is missing: ' . $index);
}
foreach ([
'chk_cloud_storage_publication_owner',
'chk_cloud_storage_publication_state',
'chk_cloud_storage_publication_lease',
'chk_cloud_storage_publication_terminal',
'chk_cloud_storage_publication_coverage',
] as $check) {
$assert(str_contains($publication, $check)
&& str_contains($setup, "'" . $check . "'"),
'Storage publication check is missing: ' . $check);
}
$assert(str_contains($publication,
"state IN ('waiting_primary','waiting_protection','ready_to_finalize',")
&& str_contains($publication,
"'finalizing','completed','failed','retired')")
&& str_contains($publication,
'(backup_id IS NOT NULL)')
&& str_contains($publication,
'+ (template_import_intent_id IS NOT NULL)')
&& str_contains($publication,
'+ (rescue_template_id IS NOT NULL) = 1'),
'Publication owner or state-machine values are incomplete.');
$assert(str_contains($publication,
"(state = 'finalizing' AND lease_token IS NOT NULL AND lease_until > 0)")
&& str_contains($publication,
"(state <> 'finalizing' AND lease_token IS NULL AND lease_until = 0)"),
'Only a finalizing publication may hold a worker lease.');
$assert(str_contains($publication,
"state IN ('waiting_primary','waiting_protection')")
&& str_contains($publication, 'AND coverage_ready_at IS NULL')
&& str_contains($publication,
"state IN ('ready_to_finalize','finalizing','completed')")
&& str_contains($publication, 'AND coverage_ready_at IS NOT NULL')
&& str_contains($publication, "OR state = 'retired'")
&& str_contains($publication, "OR state = 'failed'"),
'Publication finalization states lack durable required-copy evidence.');
foreach ([
'fk_cloud_storage_publication_artifact',
'fk_cloud_storage_publication_backup',
'fk_cloud_storage_publication_template_import',
'fk_cloud_storage_publication_rescue',
] as $foreignKey) {
$assert(str_contains($publication, $foreignKey)
&& str_contains($setup, "'" . $foreignKey . "'"),
'Storage publication FK is missing: ' . $foreignKey);
}
$assert(str_contains($publication,
'REFERENCES cloud_storage_artifacts (id)')
&& str_contains($publication,
'REFERENCES cloud_instance_backups (id)')
&& str_contains($publication,
'REFERENCES cloud_template_import_intents (id)')
&& str_contains($publication,
'REFERENCES cloud_rescue_templates (id)'),
'Publication owner bindings are not protected by real foreign keys.');
$backupAt = strpos($schema, 'CREATE TABLE cloud_instance_backups (');
$publicationAt = strpos($schema, 'CREATE TABLE cloud_storage_publications (');
$deploymentAt = strpos($schema, 'CREATE TABLE cloud_deployment_snapshots (');
$assert(is_int($backupAt) && is_int($publicationAt) && is_int($deploymentAt)
&& $backupAt < $publicationAt && $publicationAt < $deploymentAt,
'Publication table is declared before all FK owner tables exist.');
$assert(is_string($publicationModel)
&& str_contains($publicationModel,
"protected \$table = 'cloud_storage_publications'")
&& str_contains($publicationModel, "protected \$hidden = ['lease_token']")
&& is_string($artifactModel)
&& str_contains($artifactModel, "'maximum_size_bytes' => 'int'"),
'Storage publication/artifact model contracts are incomplete.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v53-traffic-settlement-receipts'"),
'CloudSetup schema version was not advanced for durable publications.');
echo "Storage publication schema contracts passed.\n";