diff --git a/lib/BmcRepository.php b/lib/BmcRepository.php index 52ef173..364dee2 100644 --- a/lib/BmcRepository.php +++ b/lib/BmcRepository.php @@ -25,6 +25,7 @@ */ final class BmcRepository { private const DELETE_ATTEMPTS = 3; + private const DELETE_SAVEPOINT = 'redfish_delete'; public function __construct(private readonly \PDO|false $connection, private readonly Clock $clock) { } @@ -271,6 +272,12 @@ public function delete(int $id, int $userId): bool { return false; } + if (!db_execute_prepared('SAVEPOINT ' . self::DELETE_SAVEPOINT, [], true, $this->connection)) { + db_rollback_transaction($this->connection); + + return false; + } + $intact = true; foreach ($statements as [$sql, $params]) { @@ -409,8 +416,18 @@ private function afterWrite(int $id, Bmc $bmc, ?Bmc $before, int $userId, string } } + /** + * Whether the transaction begun by delete() is still open. A savepoint + * lives only as long as its transaction, so releasing it succeeds only + * while the transaction does, and it is set again for the next check. + * This works on MariaDB and MySQL alike; @@in_transaction is MariaDB only. + */ private function inTransaction(): bool { - return (int) db_fetch_cell_prepared('SELECT @@in_transaction', [], '', true, $this->connection) === 1; + if (!db_execute_prepared('RELEASE SAVEPOINT ' . self::DELETE_SAVEPOINT, [], false, $this->connection)) { + return false; + } + + return (bool) db_execute_prepared('SAVEPOINT ' . self::DELETE_SAVEPOINT, [], true, $this->connection); } /** diff --git a/tests/Unit/BmcRepositoryDeleteTest.php b/tests/Unit/BmcRepositoryDeleteTest.php new file mode 100644 index 0000000..65c1a3a --- /dev/null +++ b/tests/Unit/BmcRepositoryDeleteTest.php @@ -0,0 +1,81 @@ + + * SPDX-License-Identifier: GPL-2.0-or-later + * + * This file is part of plugin_redfish, a plugin for Cacti. + * https://github.com/somethingwithproof/plugin_redfish + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + */ + +use Somethingwithproof\Redfish\BmcRepository; +use Somethingwithproof\Redfish\FixedClock; + +function redfish_delete_repo(): BmcRepository { + $GLOBALS['__redfish_db_exec'] = []; + $GLOBALS['__redfish_end_tx'] = []; + $GLOBALS['__redfish_savepoint'] = false; + $GLOBALS['__redfish_db_rows'] = ['FROM plugin_redfish_bmc WHERE id' => [['id' => '7', 'name' => 'r660', 'address' => '10.0.0.5', 'port' => '443', 'credential_id' => '3', 'tls_mode' => 'system', 'admin_state' => 'enabled']]]; + + return new BmcRepository(false, new FixedClock(1_800_000_000)); +} + +function redfish_delete_log(): array { + return array_count_values(array_column($GLOBALS['__redfish_db_exec'], 'sql')); +} + +// REQ-BMC-006: the transaction check must not use @@in_transaction, which +// MySQL lacks; a savepoint lives only as long as its transaction. +it('deletes the BMC and its cache in one transaction without @@in_transaction', function () { + expect(redfish_delete_repo()->delete(7, 1))->toBeTrue(); + + $log = redfish_delete_log(); + + expect($log['BEGIN'])->toBe(1) + ->and($log['COMMIT'])->toBe(1) + ->and($log)->not->toHaveKey('ROLLBACK') + ->and($log['DELETE FROM plugin_redfish_bmc WHERE id = ?'])->toBe(1); + + foreach (array_keys($log) as $sql) { + expect($sql)->not->toContain('@@in_transaction'); + } +}); + +it('reruns the delete when the server ends the transaction part way', function () { + $repo = redfish_delete_repo(); + // A deadlock rolls the transaction back after the member table delete. + $GLOBALS['__redfish_end_tx'] = ['DELETE FROM plugin_redfish_member']; + + expect($repo->delete(7, 1))->toBeTrue(); + + $log = redfish_delete_log(); + + expect($log['BEGIN'])->toBe(2) + ->and($log['COMMIT'])->toBe(1) + ->and($log)->not->toHaveKey('ROLLBACK'); +}); + +it('rolls back and reports failure when the BMC row delete keeps failing', function () { + $repo = redfish_delete_repo(); + $GLOBALS['__redfish_db_fail'] = ['DELETE FROM plugin_redfish_bmc']; + + try { + expect($repo->delete(7, 1))->toBeFalse(); + } finally { + unset($GLOBALS['__redfish_db_fail']); + } + + $log = redfish_delete_log(); + + expect($log)->not->toHaveKey('COMMIT') + ->and($log['ROLLBACK'])->toBe(3); +}); diff --git a/tests/bootstrap-unit.php b/tests/bootstrap-unit.php index 7b9dcd2..7692990 100644 --- a/tests/bootstrap-unit.php +++ b/tests/bootstrap-unit.php @@ -101,6 +101,22 @@ function redfish_test_rows(string $sql): mixed { function db_execute_prepared($sql, $params = [], $log = true, $db_conn = false, $log_function = null) { $GLOBALS['__redfish_db_exec'][] = ['sql' => $sql, 'params' => $params]; + if (str_starts_with($sql, 'SAVEPOINT ')) { + $GLOBALS['__redfish_savepoint'] = true; + } elseif (str_starts_with($sql, 'RELEASE SAVEPOINT ')) { + $alive = $GLOBALS['__redfish_savepoint']; + $GLOBALS['__redfish_savepoint'] = false; + + return $alive; + } + + foreach ($GLOBALS['__redfish_end_tx'] as $i => $needle) { + if (str_contains($sql, $needle)) { + unset($GLOBALS['__redfish_end_tx'][$i]); + $GLOBALS['__redfish_savepoint'] = false; + } + } + foreach ($GLOBALS['__redfish_db_fail'] ?? [] as $needle) { if (str_contains($sql, $needle)) { return false; @@ -126,9 +142,12 @@ function db_fetch_row_prepared($sql, $params = [], $log = true, $db_conn = false } // Transactions are recorded as BEGIN, COMMIT, and ROLLBACK statements. -// @@in_transaction answers from $GLOBALS['__redfish_in_tx'] in order, then 1, -// so a test can play InnoDB ending a transaction on a deadlock. -$GLOBALS['__redfish_in_tx'] = []; +// A savepoint lives as long as its transaction, so the stubs track whether one +// is set. $GLOBALS['__redfish_end_tx'] lists SQL needles; the first statement +// matching one (each fires once) ends the transaction after it runs, as InnoDB +// does on a deadlock, so RELEASE SAVEPOINT then fails. +$GLOBALS['__redfish_end_tx'] = []; +$GLOBALS['__redfish_savepoint'] = false; if (!function_exists('db_begin_transaction')) { function db_begin_transaction($db_conn = false) { @@ -152,10 +171,6 @@ function db_rollback_transaction($db_conn = false) { if (!function_exists('db_fetch_cell_prepared')) { function db_fetch_cell_prepared($sql, $params = [], $col_name = '', $log = true, $db_conn = false, $log_function = null) { - if (str_contains($sql, '@@in_transaction')) { - return $GLOBALS['__redfish_in_tx'] === [] ? 1 : array_shift($GLOBALS['__redfish_in_tx']); - } - $rows = redfish_test_rows($sql); return is_array($rows) && isset($rows[0]) ? reset($rows[0]) : 0;