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

196 lines
8.5 KiB
PHTML

<?php
$root = dirname(__DIR__);
$read = static function (string $path) use ($root): string {
$value = file_get_contents($root . '/' . ltrim($path, '/'));
if (!is_string($value)) {
throw new RuntimeException('Unable to read ' . $path);
}
return $value;
};
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$tableSql = static function (string $schema, string $table) use ($assert): string {
$matched = preg_match(
'/CREATE TABLE ' . preg_quote($table, '/')
. ' \(([\s\S]*?)\n\) ENGINE=InnoDB/',
$schema,
$match
) === 1;
$assert($matched, 'Missing dual-storage table: ' . $table);
return $match[1];
};
$schema = $read('sql/cloud.sql');
$setup = $read('app/Command/CloudSetup.php');
$tables = [
'cloud_storage_backend_catalog_mutex' => [
'id', 'updated_at',
],
'cloud_storage_backends' => [
'backend_key', 'identity_sha256', 'name', 'driver', 'endpoint',
'region', 'bucket', 'prefix', 'path_style', 'failure_domain',
'sealed_credentials', 'config_revision', 'tested_revision',
'admin_state', 'health_state', 'write_priority',
'consecutive_failures', 'consecutive_successes', 'last_error_code',
'last_tested_at', 'last_healthy_at', 'next_probe_at', 'created_at',
'updated_at',
],
'cloud_storage_artifacts' => [
'artifact_key', 'kind', 'size_bytes', 'sha256', 'durability_policy',
'desired_copies', 'required_copies', 'state', 'last_operation_id',
'verified_at', 'created_at', 'updated_at',
],
'cloud_storage_replicas' => [
'replica_key', 'artifact_id', 'backend_id', 'role', 'object_key',
'object_version_id', 'object_etag', 'size_bytes', 'sha256', 'state',
'publish_operation_id', 'delete_operation_id', 'delete_generation',
'delete_cycle_attempts', 'delete_presence_confirmations',
'delete_last_checked_at', 'delete_next_check_at',
'delete_attention_at', 'lease_token', 'lease_until', 'error_code',
'verified_at', 'created_at', 'updated_at',
],
'cloud_storage_operations' => [
'operation_id', 'request_idempotency_key', 'request_fingerprint',
'artifact_id', 'replica_id', 'source_backend_id', 'target_backend_id',
'kind', 'phase', 'outcome', 'effect_resolution',
'effect_may_have_applied', 'safe_to_resubmit', 'attempts',
'lease_token', 'lease_until', 'available_at', 'next_check_at',
'error_code', 'result_json', 'terminal_at', 'created_at', 'updated_at',
],
];
$definitions = [];
foreach ($tables as $table => $columns) {
$definitions[$table] = $tableSql($schema, $table);
$assert(str_contains($setup, "'" . $table . "'"),
'CloudSetup does not require dual-storage table: ' . $table);
foreach ($columns as $column) {
$assert(preg_match('/\b' . preg_quote($column, '/') . '\b/',
$definitions[$table]) === 1,
'Missing dual-storage column: ' . $table . '.' . $column);
$assert(str_contains($setup, "'" . $column . "'"),
'CloudSetup does not require dual-storage column: '
. $table . '.' . $column);
}
}
$assert(str_contains(
$definitions['cloud_storage_backend_catalog_mutex'],
'chk_cloud_storage_backend_catalog_mutex CHECK (id = 1)'
) && str_contains($setup,
"'cloud_storage_backend_catalog_mutex'"),
'Backend catalog mutex is not required by the clean schema.');
foreach ([
'cloud_storage_backend_key', 'cloud_storage_backend_identity',
'cloud_storage_backend_route', 'cloud_storage_backend_probe',
] as $index) {
$assert(str_contains($definitions['cloud_storage_backends'], $index)
&& str_contains($setup, "'" . $index . "'"),
'Missing backend index contract: ' . $index);
}
foreach ([
'cloud_storage_artifact_key', 'cloud_storage_artifact_coverage',
'cloud_storage_artifact_operation',
] as $index) {
$assert(str_contains($definitions['cloud_storage_artifacts'], $index)
&& str_contains($setup, "'" . $index . "'"),
'Missing artifact index contract: ' . $index);
}
foreach ([
'cloud_storage_replica_key', 'cloud_storage_replica_backend',
'cloud_storage_replica_object', 'cloud_storage_replica_due',
'cloud_storage_replica_lease', 'cloud_storage_replica_read',
] as $index) {
$assert(str_contains($definitions['cloud_storage_replicas'], $index)
&& str_contains($setup, "'" . $index . "'"),
'Missing replica index contract: ' . $index);
}
foreach ([
'cloud_storage_operation_id', 'cloud_storage_operation_request',
'cloud_storage_operation_poll', 'cloud_storage_operation_evidence',
'cloud_storage_operation_artifact',
] as $index) {
$assert(str_contains($definitions['cloud_storage_operations'], $index)
&& str_contains($setup, "'" . $index . "'"),
'Missing operation index contract: ' . $index);
}
$assert(str_contains($definitions['cloud_storage_backends'], 'admin_state IN (')
&& str_contains($definitions['cloud_storage_backends'], "'draft'")
&& str_contains($definitions['cloud_storage_backends'], "'active'")
&& str_contains($definitions['cloud_storage_backends'], "'read_only'")
&& str_contains($definitions['cloud_storage_backends'], "'draining'")
&& str_contains($definitions['cloud_storage_backends'], "'disabled'")
&& str_contains($definitions['cloud_storage_backends'], "'retired'")
&& str_contains($definitions['cloud_storage_backends'],
"health_state IN ('unknown','healthy','degraded','unreachable')"),
'Backend lifecycle values are incomplete.');
$assert(str_contains($definitions['cloud_storage_artifacts'],
"kind IN ('backup','template','rescue')")
&& str_contains($definitions['cloud_storage_artifacts'],
"durability_policy IN ('mirror_async_2','mirror_required_2')")
&& str_contains($definitions['cloud_storage_artifacts'],
'required_copies BETWEEN 1 AND desired_copies'),
'Artifact kind or two-copy durability contract is incomplete.');
$assert(str_contains($definitions['cloud_storage_replicas'],
"role IN ('primary','secondary')")
&& str_contains($definitions['cloud_storage_replicas'],
"state IN ('reserved','publishing','copying','ready','degraded',")
&& str_contains($definitions['cloud_storage_replicas'],
"'delete_pending','delete_uncertain','delete_attention','deleted')"),
'Replica role or lifecycle contract is incomplete.');
$assert(str_contains($definitions['cloud_storage_operations'],
"kind IN ('publish','copy','inspect','delete','repair')")
&& str_contains($definitions['cloud_storage_operations'],
"phase IN ('accepted','dispatching','awaiting_receipt','reconciling','terminal')")
&& str_contains($definitions['cloud_storage_operations'],
"effect_resolution IN ('not_applicable','unknown','applied','not_applied')"),
'Storage-operation effect or lifecycle contract is incomplete.');
foreach ([
'fk_cloud_storage_replica_artifact',
'fk_cloud_storage_replica_backend',
] as $foreignKey) {
$assert(str_contains($definitions['cloud_storage_replicas'], $foreignKey)
&& str_contains($setup, "'" . $foreignKey . "'"),
'Missing replica foreign-key contract: ' . $foreignKey);
}
foreach ([
'fk_cloud_storage_operation_artifact',
'fk_cloud_storage_operation_replica',
'fk_cloud_storage_operation_source_backend',
'fk_cloud_storage_operation_target_backend',
] as $foreignKey) {
$assert(str_contains($definitions['cloud_storage_operations'], $foreignKey)
&& str_contains($setup, "'" . $foreignKey . "'"),
'Missing operation foreign-key contract: ' . $foreignKey);
}
foreach ([
'cloud_instance_backups', 'cloud_template_artifacts',
'cloud_rescue_template_artifacts',
] as $ownerTable) {
$owner = $tableSql($schema, $ownerTable);
$assert(str_contains($owner, 'storage_artifact_id BIGINT UNSIGNED')
&& str_contains($owner, 'REFERENCES cloud_storage_artifacts (id)'),
'Canonical artifact owner is not linked by a real FK: ' . $ownerTable);
$assert(str_contains($setup, "'storage_artifact_id'"),
'CloudSetup does not require storage_artifact_id.');
}
foreach ([
'CloudStorageBackend', 'CloudStorageArtifact', 'CloudStorageReplica',
'CloudStorageOperation',
] as $model) {
$assert(is_file($root . '/app/Models/' . $model . '.php'),
'Missing dual-storage model: ' . $model);
}
echo "Dual object-storage schema contract passed.\n";