Skip to content

Respect Graph Template Test Data Source in graph automation - #128

Merged
TheWitness merged 4 commits into
developfrom
fix/respect-test-data-source
Nov 9, 2026
Merged

TheWitness merged 4 commits into
developfrom
fix/respect-test-data-source

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Summary

The MikroTik graph automation (poller_graphs.php) created graphs by shelling out to Cacti's cli/add_graphs.php, which calls create_complete_graph_from_template() directly and never tests the data source. As a result, the automation ignored the per–Graph Template "Test Data Source" setting (graph_templates.test_source) that Cacti's GUI graph creation honors via create_save_graph() → test_data_sources().

In practice this created graphs (for example Login Users / Processes from the Host Resources MIB) on MikroTik/RouterOS devices that expose those OIDs but return no valid data for them.

Fix

Gate every add_graphs.php invocation on core's test_data_sources(), mirroring create_save_graph():

  • mikrotik_gt_graph() — device/health template graphs
  • the directly-assigned template loop in add_summary_graphs()
  • mikrotik_dq_graphs() — data-query graphs

cg graphs use test_data_sources($graph_template_id, $host_id); ds graphs use test_data_sources($graph_template_id, $host_id, $query_id, $snmp_index).

When a template's "Test Data Source" is off (the default), test_data_sources() returns true and behavior is unchanged. When it is on and the data source returns no valid data, the graph is skipped and a debug line is emitted.

Testing

  • php -l clean.
  • New Pest unit tests (tests/Unit/PollerGraphsTest.php) cover both the skip and create branches of mikrotik_gt_graph(), mikrotik_dq_graphs(), and add_summary_graphs() — all pass; existing suites remain green.
  • Small, backward-compatible bootstrap change so db_fetch_*_prepared() honor mikrotik_test_mock_db() fixtures (default return values unchanged).

Gate add_graphs.php invocations on test_data_sources() so a Graph Template with Test Data Source enabled is not graphed on devices whose data source returns no valid data.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 9, 2026 01:07
Overwriting GLOBALS[config] wiped library_path and broke SetupWiringTest when the full suite runs in one process. The gate tests do not assert on the command path, so the override is unnecessary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new SNMP gates can crash automation because the required library is not loaded, while the tests overwrite shared configuration without restoring it, breaking subsequent metadata assertions.

2 open issues
What changed in this PR

This PR makes MikroTik graph automation honor Cacti’s per-template Test Data Source setting, skipping graph creation when enabled validation fails.

Changes:

  • Add validation gates for device, summary-template, and data-query graphs, with debug messages for skipped graphs.
  • Add unit tests covering creation and skipping in all three paths.
  • Extend prepared-query test stubs to use database fixtures while preserving default results.
File Description
tests/​Unit/​PollerGraphsTest.php Adds tests for the three graph-creation validation gates.
tests/​bootstrap-unit.php Enables fixture responses for prepared database queries.
poller_graphs.php Checks data sources before invoking graph creation.

Review effort: Balanced

Comment thread poller_graphs.php Resolved
Comment thread tests/Unit/PollerGraphsTest.php Resolved
ladams and others added 2 commits November 8, 2026 20:24
… gate

Load poller_graphs.php via require (guarded by MIKROTIK_POLLER_GRAPHS_NO_MAIN) so Xdebug attributes coverage to the real file, and exercise the skip branch of mikrotik_gt_graph()/mikrotik_dq_graphs(). Revert the gate in the unused (commented-out) summary path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…urces

test_data_source() calls cacti_snmp_session()/cacti_snmp_session_get() from lib/snmp.php. Without it, enabling Test Data Source on an SNMP Graph Template aborts automation with a fatal undefined-function error. The include is in the untestable CLI bootstrap, so it is excluded from coverage.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Nov 9, 2026

Copy link
Copy Markdown

@TheWitness
TheWitness merged commit 73d4f70 into develop Nov 9, 2026
7 checks passed
@TheWitness
TheWitness deleted the fix/respect-test-data-source branch November 9, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants