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

93 lines
3.7 KiB
PHP

<?php
// The Go Agent control plane owns a fresh schema. This static contract keeps
// an accidental "migration" back to Python-engine/cipher columns from being
// introduced while the panel and agent are developed independently.
$root = dirname(__DIR__);
$schemaPath = $root . '/sql/cloud_agent_v1.sql';
$schema = file_get_contents($schemaPath);
$setupSource = file_get_contents($root . '/app/Command/CloudAgentSetup.php');
if ($schema === false) {
throw new RuntimeException('Go Agent schema is missing or unreadable.');
}
if ($setupSource === false) {
throw new RuntimeException('Go Agent setup command is missing or unreadable.');
}
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$withoutComments = preg_replace('/^\s*--.*$/m', '', $schema);
$assert(is_string($withoutComments), 'Unable to strip SQL comments for the schema contract.');
$markerMatch = preg_match(
"/'([0-9a-f]{64})'(?=,\\n UNIX_TIMESTAMP\\(\\)\\);)/",
$schema,
$markerParts,
);
$assert($markerMatch === 1, 'Go Agent schema marker fingerprint is missing or malformed.');
$canonical = str_replace(["\r\n", "\r"], "\n", $schema);
$canonical = preg_replace(
"/'[0-9a-f]{64}'(?=,\\n UNIX_TIMESTAMP\\(\\)\\);)/",
"'" . str_repeat('0', 64) . "'",
$canonical,
);
$assert(is_string($canonical)
&& hash('sha256', $canonical) === $markerParts[1],
'Go Agent schema fingerprint does not match the canonical DDL.');
$assert(preg_match('/SCHEMA_VERSION\s*=\s*(\d+)/', $setupSource, $setupVersionParts) === 1,
'CloudAgentSetup schema version constant is missing.');
$assert(preg_match("/'virt-cloud-agent',\s*(\d+),/", $schema, $schemaVersionParts) === 1
&& (int) $setupVersionParts[1] === (int) $schemaVersionParts[1],
'CloudAgentSetup schema version does not match the fresh-schema marker.');
$assert(preg_match('/SCHEMA_FINGERPRINT\s*=\s*\'([0-9a-f]{64})\'/', $setupSource, $setupFingerprintParts) === 1
&& $setupFingerprintParts[1] === $markerParts[1],
'CloudAgentSetup schema fingerprint does not match the fresh-schema marker.');
foreach ([
'CREATE TABLE cloud_agents',
'CREATE TABLE cloud_agent_client_certificates',
'CREATE TABLE cloud_agent_operations',
'CREATE TABLE cloud_agent_instance_state',
'CREATE TABLE cloud_agent_artifacts',
'CREATE TABLE cloud_agent_schema_meta',
'virt-cloud-agent',
'schema_fingerprint CHAR(64)',
'UNIQUE KEY cloud_agent_operation_idempotency',
'host_id BIGINT UNSIGNED NOT NULL',
'instance_id BIGINT UNSIGNED NULL',
'instance_id BIGINT UNSIGNED NOT NULL',
'UNIQUE KEY cloud_agent_instance_identity (agent_id, host_id, instance_id, instance_uuid)',
'cloud_agent_operation_readonly_retention',
'version BIGINT UNSIGNED NOT NULL DEFAULT 1',
'desired_json JSON NOT NULL',
'observed_json JSON NULL',
'secret_ref VARCHAR(128)',
'client_certificate_fingerprint CHAR(64)',
] as $required) {
$assert(str_contains($withoutComments, $required),
'Go Agent schema lost required greenfield contract: ' . $required);
}
// These are implementation markers for the retired Python engine, not fields
// that may appear in the new control-plane schema. Comments are excluded so
// the explanatory boundary text can still mention why compatibility is not
// provided.
foreach ([
'engine_records',
'engine_jobs',
'public_key_cipher',
'private_key_cipher',
'password_cipher',
'secret_cipher',
] as $forbidden) {
$assert(!str_contains($withoutComments, $forbidden),
'Greenfield Go Agent schema unexpectedly contains a legacy field/table: ' . $forbidden);
}
echo "Go Agent greenfield schema contract passed.\n";