Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/BmcRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/Unit/BmcRepositoryDeleteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/*
* SPDX-FileCopyrightText: 2026 Thomas Vincent <thomasvincent@gmail.com>
* 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);
});
29 changes: 22 additions & 7 deletions tests/bootstrap-unit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down