Files
cacti/tests/tools/test_request_coverage_dispatch.py
T
Thomas Vincent 658b320f75 fix(boost): acknowledge exact keys and isolate repeatedly failing data sources (#8351)
* Stop Boost retries at failed writes and metadata reads

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

* Retain Boost archives when temporary copies fail

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

* Reuse Boost cleanup after failed temporary copies

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

* Acknowledge exact Boost keys without deleting late arrivals

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

* test(boost): cover native acknowledgement and archive handoff

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

* fix(boost): match acknowledged keys by epoch, not session-local time

Staging fetched keys with FROM_UNIXTIME() converts each epoch through the
session time zone. On MySQL in a DST zone both instants of the fall-back
hour render as the same local time and are stored back as the first one,
so INSERT IGNORE collapses them and the second row is never deleted. It
is then fetched, skipped by --skip-past-updates and retained on every run.

Stage the epoch integers and compare them with UNIX_TIMESTAMP(time),
which reads the TIMESTAMP column's stored value without a time zone
conversion. Verified on MySQL 8.4 and MariaDB 11.8 with the session in
America/New_York.

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

* fix(boost): stop bulk drain writes after a rejected flush

The bulk drain kept writing a data source's later timestamps after one of
its mid-stream flushes failed. That advanced the RRD past the rejected
samples, so --skip-past-updates dropped them when the retained shard was
retried. Skip the remaining rows of that data source instead.

Match the on-demand update result exactly against OK. An error that only
contains the letters, for example in an RRD file name such as BOOK_7.rrd,
was counted as success and its rows were acknowledged without being
written.

Describe the acknowledgement DELETE's index use accurately and note why
the fall-back fixture shifts only int parameters.

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

* test(boost): publish bulk drain probe evidence to the changed-line gate

The bulk drain changes in poller_boost.php are exercised only by
BoostBulkDrainFailureProbe.php, which compiles the drain in a child
process from a temporary file. Xdebug credited those hits to the
temporary file, so the changed-line gate saw none of the drain's nine
changed statements and the pull request measured 66.7 percent.

Keep each loaded function at its original line numbers and map the copy
back to its source file, let the drain probe publish request coverage,
and run both drain scenarios from a request-coverage fixture whenever
poller_boost.php or the probe changes.

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

* fix(boost): isolate a failing data source instead of its whole shard

One data source whose RRD update always fails, for example a corrupt RRD,
an unwritable file or a template mismatch, made its pass return -1. The
child abandoned its remaining passes and the parent kept every archive
table. Each later run added another archive table, re-read all of them,
and failed at the same data source again, so data sources after it in
that child were never written and work and disk use grew without bound.

Park the data sources whose update was rejected on process handler 0 and
acknowledge the rest of the shard, so the child carries on. Once every
child has finished, the parent copies the parked data sources' samples
into poller_output_boost and drops the archive tables as usual. Samples
older than one day are discarded with a warning, so a data source that
never succeeds cannot grow the queue forever. If parking or the copy
fails, the shard or the archive tables are retained as before.

Database and metadata failures still fail the whole shard.

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

* test(boost): expect a rejected flush to park the data source and acknowledge the shard

A rejected mid-stream flush still stops further writes to its data source so
a later timestamp cannot advance the RRD past the rejected samples. The data
source is now parked for the parent to carry forward instead of retaining the
whole shard, so the bulk drain probe expects the shard to be acknowledged.

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

* test(boost): run the Boost probes in the changed-line coverage gate

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

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
2026-11-17 13:36:34 -05:00

123 lines
6.1 KiB
Python

"""Verify request coverage dispatch using only the runner's required tools.
Copyright (C) 2004-2026 The Cacti Group. GPL-2.0-or-later.
"""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
class RequestDispatchTest(unittest.TestCase):
def test_package_change_runs_the_signed_archive_probe(self):
for path in ['lib/package.php', 'tests/fixtures/Core/PackageArchive.php']:
with self.subTest(path=path):
self.dispatch(path, 'PackageArchive.php')
def test_queue_change_runs_the_queue_probe_without_ripgrep(self):
self.dispatch('cli/queue_worker.php', 'queue')
def test_html_change_runs_the_browser_probe(self):
self.dispatch('lib/html.php', 'accessibility')
def test_documentation_change_runs_no_request_probe(self):
self.dispatch('README.md', None)
def test_installer_change_runs_the_installer_probe(self):
self.dispatch('lib/installer.php', 'installer')
def test_retired_theme_cleanup_runs_for_upgrade_and_pruning_changes(self):
for path, expected in [
('install/functions.php', ['installer', 'RetiredThemeCleanup.php']),
('install/upgrades/1_3_0.php', ['queue', 'RetiredThemeCleanup.php']),
('tests/fixtures/Installer/RetiredThemeCleanup.php', ['RetiredThemeCleanup.php'])]:
with self.subTest(path=path):
self.dispatch(path, expected)
def test_graph_change_runs_the_native_graph_probe(self):
self.dispatch('lib/rrd.php', 'FallbackSourceIdentity.php')
def test_account_change_runs_the_native_audit_probe(self):
self.dispatch('lib/functions.php', ['ScriptSubstitutionMatrix.php', 'UserAdministrationAuditCoverage.php', 'graph_group_probe.php'])
def test_auth_change_runs_the_native_auth_probe(self):
self.dispatch('lib/auth.php', 'BoundaryContracts.php')
def test_script_server_change_runs_the_log_masking_probe(self):
for path in ['script_server.php', 'lib/poller.php', 'tests/fixtures/ScriptServerLogMaskingCoverage.php']:
with self.subTest(path=path):
self.dispatch(path, 'ScriptServerLogMaskingCoverage.php')
def test_two_factor_change_runs_the_guard_fixture(self):
self.dispatch('auth_2fa.php', 'ConstantTimeGuardMatrix.php')
def test_boost_drain_change_runs_the_drain_probe(self):
for path in ['poller_boost.php', 'lib/boost.php', 'tests/fixtures/ProbeLoader.php', 'tests/fixtures/BoostOnDemandFailureProbe.php']:
with self.subTest(path=path):
self.dispatch(path, 'BoostBulkDrainCoverage.php')
def test_repository_change_runs_manifest_and_save_fixtures(self):
self.dispatch('package_repos.php', ['PackageManifestMatrix.php', 'PackageSaveMatrix.php'])
def dispatch(self, changed, expected):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
repository = root / 'repo'
repository.mkdir()
tools = root / 'tools'
tools.mkdir()
for command in ['git', 'bash', 'grep', 'mkdir']:
(tools / command).symlink_to(shutil.which(command))
php = tools / 'php'
php.write_text('#!' + shutil.which('bash') + '\n'
'script="${!#}"\n'
'printf "%s\\n" "${script##*/}" >> "$CACTI_PR_RAW_COVERAGE/probes"\n')
php.chmod(0o700)
env = dict(os.environ, PATH=str(tools))
def git(*arguments):
return subprocess.check_output(
['git', '-c', 'commit.gpgsign=false', '-c', 'core.hooksPath=/dev/null',
'-c', 'user.name=Coverage fixture', '-c', 'user.email=fixture@example.invalid',
*arguments], cwd=repository, env=env, text=True).strip()
git('init', '--quiet')
(repository / 'README.md').write_text('fixture\n')
for name, script in [('queue', 'queue/run.sh'), ('accessibility', 'accessibility-coverage.sh'),
('installer', 'installer-coverage.sh')]:
path = repository / 'tests/e2e' / script
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('printf "%s\\n" ' + name + ' >> "$CACTI_PR_COVERAGE_OUTPUT/probes"\n')
for script in ['Core/Auth/BoundaryContracts.php', 'Core/Rrd/FallbackSourceIdentity.php',
'UserAdministrationAuditCoverage.php', 'Core/ScriptSubstitutionMatrix.php',
'Core/ConstantTimeGuardMatrix.php', 'Core/PackageManifestMatrix.php',
'Core/PackageSaveMatrix.php', 'Core/PackageArchive.php',
'Installer/RetiredThemeCleanup.php', 'ScriptServerLogMaskingCoverage.php', 'BoostBulkDrainCoverage.php']:
path = repository / 'tests/fixtures' / script
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('<?php // dispatch fixture\n')
probe = repository / 'tests/Unit/fixtures/graph_group_probe.php'
probe.parent.mkdir(parents=True, exist_ok=True)
probe.write_text('<?php // dispatch fixture\n')
git('add', '.')
git('commit', '--quiet', '-m', 'fixture base')
base = git('rev-parse', 'HEAD')
path = repository / changed
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text('changed fixture\n')
git('add', '.')
git('commit', '--quiet', '-m', 'fixture change')
output = root / 'coverage'
runner = Path(__file__).with_name('run_request_coverage.sh').resolve()
subprocess.run(['bash', str(runner)], cwd=repository, env=dict(
env, BASE_COMMIT=base, CACTI_PR_COVERAGE_OUTPUT=str(output)), check=True)
probe = output / 'probes'
self.assertEqual(probe.read_text().splitlines() if probe.exists() else [],
expected if isinstance(expected, list) else [expected] if expected else [])
if __name__ == '__main__':
unittest.main()