Files
cacti/tests/Unit/Core/Rrd/BandwidthEffectiveStepTest.php
Thomas VincentandTheWitness 182b21dc53 fix(csv): align export and graph sample windows (#7987)
* fix(csv): align export and graph sample windows

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* ci(csv): satisfy release policy checks

* test(csv): align legacy capacity contract

* test(csv): add standard license headers

* fix(rrd): infer step from trailing fetch row

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* ci(csv): trigger on resolution query coverage

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): mirror fetch helper arguments

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): isolate percentile globals

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): annotate intentional eval

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): restore integration config

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): enable strict types

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(rrd): restore font config

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* ci: align CSV coverage with supported PHP versions

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
Co-authored-by: TheWitness <thewitness@cacti.net>
2026-09-12 20:18:00 -04:00

68 lines
2.4 KiB
PHP

<?php
declare(strict_types = 1);
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/
namespace BandwidthEffectiveStepTest;
$GLOBALS['bandwidth_fetch_result'] = [];
function rrdtool_function_fetch(...$arguments) : array {
return $GLOBALS['bandwidth_fetch_result'];
}
function cacti_count(array $value) : int {
return count($value);
}
$source = file_get_contents(CACTI_PATH_LIBRARY . '/graph_variables.php');
if ($source === false) {
throw new \RuntimeException('Unable to read lib/graph_variables.php for bandwidth tests.');
}
if (preg_match('/function bandwidth_summation\(.*?^}\R/ms', $source, $matches) !== 1) {
throw new \RuntimeException('Unable to extract bandwidth_summation() for tests.');
}
eval('namespace BandwidthEffectiveStepTest;' . $matches[0]); // nosemgrep: php.lang.security.eval-use.eval-use
beforeEach(function () {
$GLOBALS['bandwidth_fetch_result'] = [
'data_source_names' => ['traffic'],
'timestamp' => ['step' => 1800],
'values' => [[100 => 2.0, 1900 => 3.0]],
];
});
test('bandwidth summation uses the interval observed in fetched rows', function () {
expect(bandwidth_summation(1, 0, 3600, 300, 1))->toBe(['traffic' => 9000.0]);
});
test('bandwidth summation falls back to the requested interval when one row cannot reveal a step', function () {
$GLOBALS['bandwidth_fetch_result']['timestamp']['step'] = 0;
expect(bandwidth_summation(1, 0, 3600, 300, 2))->toBe(['traffic' => 3000.0]);
});
test('bandwidth summation handles empty names values and samples', function () {
$GLOBALS['bandwidth_fetch_result'] = [];
expect(bandwidth_summation(1, 0, 3600, 300, 1))->toBe([]);
$GLOBALS['bandwidth_fetch_result'] = [
'data_source_names' => ['missing', 'empty'],
'timestamp' => ['step' => 300],
'values' => [1 => []],
];
expect(bandwidth_summation(1, 0, 3600, 300, 1))->toBe(['empty' => 0]);
});