Files
cacti/tests/Unit/Core/Rrd/RrdFetchWindowTest.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

65 lines
2.3 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 |
+-------------------------------------------------------------------------+
*/
require_once CACTI_PATH_LIBRARY . '/rrd.php';
test('fetch output is bounded by the requested end and reports its observed step', function () {
$output = " traffic_in traffic_out\n" .
"1700000400: 1.0 2.0\n" .
"1700000700: 3.0 4.0\n" .
"1700001000: 5.0 6.0\n" .
"1700001300: 7.0 8.0\n";
$fetch = rrdtool_parse_fetch_output($output, 1700001000);
expect($fetch['data_source_names'])->toBe(['traffic_in', 'traffic_out'])
->and($fetch['timestamp'])->toBe([
'start_time' => 1700000400,
'end_time' => 1700001000,
'step' => 300,
])
->and(array_keys($fetch['values'][0]))->toBe([1700000400, 1700000700, 1700001000])
->and($fetch['values'][1][1700001000])->toBe('6.0')
->and($fetch['values'][0])->not->toHaveKey(1700001300);
});
test('fetch output handles unknown, malformed, sparse, and empty rows safely', function () {
$output = "value\n" .
"not-a-time: 9\n" .
"1700000400: NaN\n" .
"malformed row\n" .
"1700001000: -NaN\n";
$with_unknown = rrdtool_parse_fetch_output($output, 1700001000, true);
$without = rrdtool_parse_fetch_output($output, 1700001000);
$empty = rrdtool_parse_fetch_output("\n\n", 1700001000);
expect($with_unknown['values'][0])->toBe([
1700000400 => 'U',
1700001000 => 'U',
])
->and($with_unknown['timestamp']['step'])->toBe(600)
->and($without)->not->toHaveKey('values')
->and($empty)->toBe([]);
});
test('a single in-window row derives its observed step from the excluded trailing row', function () {
$fetch = rrdtool_parse_fetch_output("value\n1700000400: 1\n1700000700: 2\n", 1700000400);
expect($fetch['timestamp'])->toBe([
'start_time' => 1700000400,
'end_time' => 1700000400,
'step' => 300,
]);
});