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

215 lines
8.4 KiB
PHP

<?php
$root = dirname(__DIR__);
$schema = file_get_contents($root . '/sql/cloud.sql');
$setup = file_get_contents($root . '/app/Command/CloudSetup.php');
$assert = static function (bool $condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
foreach ([
'cloud_service_compensations',
'cloud_service_compensation_items',
'cloud_service_compensation_recipients',
] as $table) {
$assert(str_contains($schema, 'CREATE TABLE ' . $table . ' ('),
'Service compensation schema is missing ' . $table . '.');
$assert(str_contains($setup, "'" . $table . "'"),
'CloudSetup does not require ' . $table . '.');
}
foreach ([
'UNIQUE KEY cloud_service_compensation_request',
'UNIQUE KEY cloud_service_compensation_incident_identity (id, incident_key)',
'UNIQUE KEY cloud_service_compensation_item_incident (incident_key, instance_id)',
'UNIQUE KEY cloud_service_compensation_recipient (batch_id, user_id)',
'KEY cloud_service_compensation_item_due (state, available_at, id)',
'KEY cloud_service_compensation_item_lease (state, lease_until, id)',
'KEY cloud_service_compensation_item_open (instance_id, state, id)',
'KEY cloud_service_compensation_recipient_due (status, available_at, id)',
'KEY cloud_service_compensation_recipient_lease (status, lease_until, id)',
'fk_cloud_service_compensation_item_batch',
'FOREIGN KEY (batch_id, incident_key)',
'fk_cloud_service_compensation_recipient_batch',
"scope_type IN ('instance','host','public_ip_pool')",
'compensation_seconds BETWEEN 1 AND 31622400',
'AND attempts <= 8 AND available_at > 0',
'AND service_period_id IS NOT NULL',
'AND after_period_ends_at IS NOT NULL',
'traffic_entitlement_id IS NULL',
'traffic_entitlement_id IS NOT NULL',
'AND after_traffic_ends_at IS NOT NULL',
'backup_entitlement_id IS NULL',
'backup_entitlement_id IS NOT NULL',
'AND after_backup_ends_at IS NOT NULL',
'AND audit_job_id IS NOT NULL',
"kind = 'service_compensation'",
] as $contract) {
$assert(str_contains($schema, $contract),
'Service compensation schema lost contract: ' . $contract);
}
foreach ([
'chk_cloud_service_compensation_scope',
'chk_cloud_service_compensation_request',
'chk_cloud_service_compensation_window',
'chk_cloud_service_compensation_counts',
'chk_cloud_service_compensation_state',
'chk_cloud_service_compensation_terminal',
'chk_cloud_service_compensation_item_state',
'chk_cloud_service_compensation_item_window',
'chk_cloud_service_compensation_item_lease',
'chk_cloud_service_compensation_item_terminal',
'chk_cloud_service_compensation_item_applied',
'chk_cloud_service_compensation_item_error',
'chk_cloud_service_compensation_recipient_status',
'chk_cloud_service_compensation_recipient_payload',
'chk_cloud_service_compensation_recipient_shape',
] as $check) {
$assert(str_contains($setup, "'" . $check . "'"),
'CloudSetup lost service compensation check: ' . $check);
}
foreach ([
"'columns' => ['id', 'incident_key']",
"'columns' => ['incident_key', 'instance_id']",
"'columns' => ['state', 'available_at', 'id']",
"'columns' => ['state', 'lease_until', 'id']",
"'columns' => ['instance_id', 'state', 'id']",
"'columns' => ['batch_id', 'user_id']",
"'columns' => ['status', 'available_at', 'id']",
"'columns' => ['status', 'lease_until', 'id']",
"'fk_cloud_service_compensation_item_batch'",
"'fk_cloud_service_compensation_recipient_batch'",
] as $setupContract) {
$assert(str_contains($setup, $setupContract),
'CloudSetup lost service compensation contract: ' . $setupContract);
}
$targetShape = preg_match(
'/CONSTRAINT chk_cloud_job_target_shape CHECK \((.*?)\n \),\n CONSTRAINT chk_cloud_job_effect_shape/s',
$schema,
$targetMatch
) === 1 ? $targetMatch[1] : '';
$assert($targetShape !== ''
&& str_contains($targetShape, "kind = 'service_compensation'")
&& str_contains($targetShape, 'parent_job_id IS NULL')
&& str_contains($targetShape, 'instance_id IS NOT NULL'),
'Local compensation audit jobs are rejected by the cloud-job target shape.');
$assert(str_contains($setup,
"'cloud_service_compensation_item_open'")
&& str_contains($setup,
"'columns' => ['instance_id', 'state', 'id']"),
'CloudSetup lost the open-compensation fence index contract.');
$assert(str_contains($setup,
"SCHEMA_VERSION = 'go-agent-wss-v42-abuse-observation'"),
'CloudSetup schema version was not advanced for service compensation.');
$appliedShape = preg_match(
'/CONSTRAINT chk_cloud_service_compensation_item_applied CHECK \((.*?)\n \),\n CONSTRAINT chk_cloud_service_compensation_item_error/s',
$schema,
$appliedMatch
) === 1 ? $appliedMatch[1] : '';
$assert($appliedShape !== '',
'Applied compensation CHECK could not be extracted for insertion tests.');
$probe = new PDO('sqlite::memory:');
$probe->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$probe->exec('CREATE TABLE applied_compensation_probe (
state TEXT NOT NULL,
requested_seconds INTEGER NOT NULL,
service_period_id INTEGER NULL,
applied_at INTEGER NULL,
before_expires_at INTEGER NULL,
after_expires_at INTEGER NULL,
before_period_ends_at INTEGER NULL,
after_period_ends_at INTEGER NULL,
traffic_entitlement_id INTEGER NULL,
before_traffic_ends_at INTEGER NULL,
after_traffic_ends_at INTEGER NULL,
backup_entitlement_id INTEGER NULL,
before_backup_ends_at INTEGER NULL,
after_backup_ends_at INTEGER NULL,
after_snapshot_json TEXT NULL,
audit_job_id INTEGER NULL,
last_error_code TEXT NOT NULL,
last_error TEXT NOT NULL,
CHECK (' . $appliedShape . ')
)');
$columns = [
'state', 'requested_seconds', 'service_period_id', 'applied_at',
'before_expires_at', 'after_expires_at', 'before_period_ends_at',
'after_period_ends_at', 'traffic_entitlement_id',
'before_traffic_ends_at', 'after_traffic_ends_at',
'backup_entitlement_id', 'before_backup_ends_at',
'after_backup_ends_at', 'after_snapshot_json', 'audit_job_id',
'last_error_code', 'last_error',
];
$statement = $probe->prepare('INSERT INTO applied_compensation_probe ('
. implode(', ', $columns) . ') VALUES (:'
. implode(', :', $columns) . ')');
$valid = [
'state' => 'applied', 'requested_seconds' => 60,
'service_period_id' => 7, 'applied_at' => 100,
'before_expires_at' => 1000, 'after_expires_at' => 1060,
'before_period_ends_at' => 1000, 'after_period_ends_at' => 1060,
'traffic_entitlement_id' => null,
'before_traffic_ends_at' => null, 'after_traffic_ends_at' => null,
'backup_entitlement_id' => null,
'before_backup_ends_at' => null, 'after_backup_ends_at' => null,
'after_snapshot_json' => '{}', 'audit_job_id' => 8,
'last_error_code' => '', 'last_error' => '',
];
$statement->execute($valid);
$statement->execute(array_replace($valid, [
'traffic_entitlement_id' => 11,
'before_traffic_ends_at' => 1000,
'after_traffic_ends_at' => 1060,
'backup_entitlement_id' => 12,
'before_backup_ends_at' => 1000,
'after_backup_ends_at' => 1060,
]));
$assert((int) $probe->query(
'SELECT COUNT(*) FROM applied_compensation_probe'
)->fetchColumn() === 2,
'Applied compensation CHECK rejected valid entitlement evidence.');
$expectRejected = static function (array $changes) use (
$assert,
$statement,
$valid
): void {
try {
$statement->execute(array_replace($valid, $changes));
} catch (PDOException $error) {
return;
}
$assert(false, 'Applied compensation CHECK accepted incomplete evidence: '
. implode(', ', array_keys($changes)));
};
$expectRejected(['service_period_id' => null]);
$expectRejected(['after_period_ends_at' => null]);
$expectRejected([
'before_traffic_ends_at' => 1000,
'after_traffic_ends_at' => 1060,
]);
$expectRejected(['traffic_entitlement_id' => 11]);
$expectRejected([
'traffic_entitlement_id' => 11,
'before_traffic_ends_at' => 1000,
'after_traffic_ends_at' => null,
]);
$expectRejected([
'before_backup_ends_at' => 1000,
'after_backup_ends_at' => 1060,
]);
$expectRejected(['backup_entitlement_id' => 12]);
$expectRejected([
'backup_entitlement_id' => 12,
'before_backup_ends_at' => 1000,
'after_backup_ends_at' => null,
]);
echo "Service compensation schema contracts passed.\n";