|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
$schemaSource = file_get_contents(dirname(__DIR__) . '/sql/cloud.sql');
|
|
|
|
|
if (!is_string($schemaSource)) {
|
|
|
|
|
throw new RuntimeException('Cloud schema is unreadable.');
|
|
|
|
|
}
|
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$expectConstraintFailure = static function (
|
|
|
|
|
callable $callback,
|
|
|
|
|
string $message
|
|
|
|
|
): void {
|
|
|
|
|
try {
|
|
|
|
|
$callback();
|
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
|
if ((string) $error->getCode() === '23000'
|
|
|
|
|
|| str_contains($error->getMessage(), 'constraint failed')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
throw $error;
|
|
|
|
|
}
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Tie the executable SQLite probe to the exact MySQL contract under test.
|
|
|
|
|
foreach ([
|
|
|
|
|
'UNIQUE KEY cloud_reservation_ip_active (',
|
|
|
|
|
'reservation_id, address_family, active_claim',
|
|
|
|
|
'UNIQUE KEY cloud_reservation_ip_generation (',
|
|
|
|
|
'reservation_id, claim_generation, address_family',
|
|
|
|
|
'UNIQUE KEY cloud_public_ip_claim (reservation_ip_claim_id)',
|
|
|
|
|
'active_transition IS NOT NULL AND active_transition = 1',
|
|
|
|
|
'UNIQUE KEY cloud_public_network_transition_ip_target (target_claim_id)',
|
|
|
|
|
'UNIQUE KEY cloud_job_public_network_transition (',
|
|
|
|
|
'public_network_transition_id',
|
|
|
|
|
] as $requiredSql) {
|
|
|
|
|
$assert(str_contains($schemaSource, $requiredSql),
|
|
|
|
|
'Behavior fixture drifted from cloud.sql: ' . $requiredSql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$pdo = new PDO('sqlite::memory:');
|
|
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
|
|
|
$pdo->exec(<<<'SQL'
|
|
|
|
|
CREATE TABLE cloud_resource_reservation_ips (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
reservation_id INTEGER NOT NULL,
|
|
|
|
|
address_family INTEGER NOT NULL,
|
|
|
|
|
claim_generation INTEGER NOT NULL DEFAULT 1,
|
|
|
|
|
active_claim INTEGER DEFAULT 1,
|
|
|
|
|
UNIQUE (reservation_id, address_family, active_claim),
|
|
|
|
|
UNIQUE (reservation_id, claim_generation, address_family),
|
|
|
|
|
CHECK (claim_generation >= 1),
|
|
|
|
|
CHECK (active_claim IS NULL OR active_claim = 1)
|
|
|
|
|
);
|
|
|
|
|
CREATE TABLE cloud_public_ips (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
reservation_ip_claim_id INTEGER DEFAULT NULL,
|
|
|
|
|
UNIQUE (reservation_ip_claim_id)
|
|
|
|
|
);
|
|
|
|
|
CREATE TABLE cloud_public_network_transitions (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
instance_id INTEGER NOT NULL,
|
|
|
|
|
reservation_id INTEGER NOT NULL,
|
|
|
|
|
invoice_id INTEGER DEFAULT NULL,
|
|
|
|
|
state TEXT NOT NULL,
|
|
|
|
|
effect_resolution TEXT NOT NULL,
|
|
|
|
|
active_transition INTEGER DEFAULT 1,
|
|
|
|
|
effect_resolved_at INTEGER DEFAULT NULL,
|
|
|
|
|
terminal_at INTEGER DEFAULT NULL,
|
|
|
|
|
UNIQUE (instance_id, active_transition),
|
|
|
|
|
UNIQUE (reservation_id, active_transition),
|
|
|
|
|
UNIQUE (invoice_id),
|
|
|
|
|
CHECK (active_transition IS NULL OR active_transition = 1),
|
|
|
|
|
CHECK (
|
|
|
|
|
(state IN ('reserved','queued','awaiting_receipt')
|
|
|
|
|
AND effect_resolution = 'not_applicable'
|
|
|
|
|
AND active_transition IS NOT NULL AND active_transition = 1
|
|
|
|
|
AND effect_resolved_at IS NULL AND terminal_at IS NULL)
|
|
|
|
|
OR (state = 'effect_unknown' AND effect_resolution = 'unknown'
|
|
|
|
|
AND active_transition IS NOT NULL AND active_transition = 1
|
|
|
|
|
AND effect_resolved_at IS NULL AND terminal_at IS NULL)
|
|
|
|
|
OR (state = 'finalization_pending'
|
|
|
|
|
AND effect_resolution IN ('applied','not_applied')
|
|
|
|
|
AND active_transition IS NOT NULL AND active_transition = 1
|
|
|
|
|
AND effect_resolved_at IS NOT NULL AND terminal_at IS NULL)
|
|
|
|
|
OR (state = 'succeeded' AND effect_resolution = 'applied'
|
|
|
|
|
AND active_transition IS NULL
|
|
|
|
|
AND effect_resolved_at IS NOT NULL AND terminal_at IS NOT NULL)
|
|
|
|
|
OR (state = 'failed' AND effect_resolution = 'not_applied'
|
|
|
|
|
AND active_transition IS NULL
|
|
|
|
|
AND effect_resolved_at IS NOT NULL AND terminal_at IS NOT NULL)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
CREATE TABLE cloud_public_network_transition_ips (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
transition_id INTEGER NOT NULL,
|
|
|
|
|
address_family INTEGER NOT NULL,
|
|
|
|
|
target_claim_id INTEGER DEFAULT NULL,
|
|
|
|
|
UNIQUE (transition_id, address_family),
|
|
|
|
|
UNIQUE (target_claim_id)
|
|
|
|
|
);
|
|
|
|
|
CREATE TABLE cloud_jobs (
|
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
public_network_transition_id INTEGER DEFAULT NULL
|
|
|
|
|
);
|
|
|
|
|
CREATE UNIQUE INDEX cloud_job_public_network_transition
|
|
|
|
|
ON cloud_jobs (public_network_transition_id);
|
|
|
|
|
SQL);
|
|
|
|
|
|
|
|
|
|
$insertClaim = $pdo->prepare(
|
|
|
|
|
'INSERT INTO cloud_resource_reservation_ips '
|
|
|
|
|
. '(reservation_id, address_family, claim_generation, active_claim) '
|
|
|
|
|
. 'VALUES (?, ?, ?, ?)'
|
|
|
|
|
);
|
|
|
|
|
$insertClaim->execute([1, 4, 1, 1]);
|
|
|
|
|
$expectConstraintFailure(
|
|
|
|
|
static fn () => $insertClaim->execute([1, 4, 2, 1]),
|
|
|
|
|
'A reservation/family accepted two active claims.'
|
|
|
|
|
);
|
|
|
|
|
$insertClaim->execute([1, 4, 2, null]);
|
|
|
|
|
$insertClaim->execute([1, 4, 3, null]);
|
|
|
|
|
$assert((int) $pdo->query(
|
|
|
|
|
'SELECT COUNT(*) FROM cloud_resource_reservation_ips '
|
|
|
|
|
. 'WHERE reservation_id = 1 AND address_family = 4 '
|
|
|
|
|
. 'AND active_claim IS NULL'
|
|
|
|
|
)->fetchColumn() === 2,
|
|
|
|
|
'Released claim history did not retain multiple generations.');
|
|
|
|
|
|
|
|
|
|
$insertPublicIp = $pdo->prepare(
|
|
|
|
|
'INSERT INTO cloud_public_ips (reservation_ip_claim_id) VALUES (?)'
|
|
|
|
|
);
|
|
|
|
|
$insertPublicIp->execute([101]);
|
|
|
|
|
$expectConstraintFailure(
|
|
|
|
|
static fn () => $insertPublicIp->execute([101]),
|
|
|
|
|
'Two public addresses accepted the same exact reservation claim.'
|
|
|
|
|
);
|
|
|
|
|
$insertPublicIp->execute([null]);
|
|
|
|
|
$insertPublicIp->execute([null]);
|
|
|
|
|
|
|
|
|
|
$transitionInsertSql = 'INSERT INTO cloud_public_network_transitions '
|
|
|
|
|
. '(instance_id, reservation_id, state, effect_resolution, '
|
|
|
|
|
. 'active_transition, effect_resolved_at, terminal_at) '
|
|
|
|
|
. 'VALUES (?, ?, ?, ?, ?, ?, ?)';
|
|
|
|
|
foreach ([
|
|
|
|
|
[10, 10, 'reserved', 'not_applicable', null, null, null],
|
|
|
|
|
[11, 11, 'effect_unknown', 'unknown', null, null, null],
|
|
|
|
|
[12, 12, 'finalization_pending', 'applied', null, 100, null],
|
|
|
|
|
] as $invalid) {
|
|
|
|
|
$expectConstraintFailure(
|
|
|
|
|
static fn () => $pdo->prepare($transitionInsertSql)
|
|
|
|
|
->execute($invalid),
|
|
|
|
|
'A live transition accepted a NULL active fence: ' . $invalid[2]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$insertTransition = $pdo->prepare($transitionInsertSql);
|
|
|
|
|
foreach ([
|
|
|
|
|
[20, 20, 'effect_unknown', 'unknown', 1, null, null],
|
|
|
|
|
[21, 21, 'finalization_pending', 'applied', 1, 101, null],
|
|
|
|
|
[22, 22, 'succeeded', 'applied', null, 102, 103],
|
|
|
|
|
[23, 23, 'failed', 'not_applied', null, 104, 105],
|
|
|
|
|
] as $valid) {
|
|
|
|
|
$insertTransition->execute($valid);
|
|
|
|
|
}
|
|
|
|
|
$assert((int) $pdo->query(
|
|
|
|
|
'SELECT COUNT(*) FROM cloud_public_network_transitions'
|
|
|
|
|
)->fetchColumn() === 4,
|
|
|
|
|
'Valid transition lifecycle rows were not retained.');
|
|
|
|
|
|
|
|
|
|
$insertTransitionIp = $pdo->prepare(
|
|
|
|
|
'INSERT INTO cloud_public_network_transition_ips '
|
|
|
|
|
. '(transition_id, address_family, target_claim_id) VALUES (?, ?, ?)'
|
|
|
|
|
);
|
|
|
|
|
$insertTransitionIp->execute([1, 4, 501]);
|
|
|
|
|
$expectConstraintFailure(
|
|
|
|
|
static fn () => $insertTransitionIp->execute([2, 6, 501]),
|
|
|
|
|
'Two transitions accepted the same target claim.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$insertJob = $pdo->prepare(
|
|
|
|
|
'INSERT INTO cloud_jobs (public_network_transition_id) VALUES (?)'
|
|
|
|
|
);
|
|
|
|
|
$insertJob->execute([1]);
|
|
|
|
|
$expectConstraintFailure(
|
|
|
|
|
static fn () => $insertJob->execute([1]),
|
|
|
|
|
'Two authoritative jobs referenced the same transition.'
|
|
|
|
|
);
|
|
|
|
|
$insertJob->execute([null]);
|
|
|
|
|
$insertJob->execute([null]);
|
|
|
|
|
$assert((int) $pdo->query(
|
|
|
|
|
'SELECT COUNT(*) FROM cloud_jobs '
|
|
|
|
|
. 'WHERE public_network_transition_id IS NULL'
|
|
|
|
|
)->fetchColumn() === 2,
|
|
|
|
|
'The transition job uniqueness rejected independent legacy jobs.');
|
|
|
|
|
|
|
|
|
|
echo "Public-network transition SQLite behavior tests passed.\n";
|