From: Alexander Stephan Date: Mon, 6 Jul 2026 09:22:30 +0000 (+0000) Subject: REGTESTS: stats: add test for 'clear counters server' X-Git-Tag: v3.5-dev4~45 X-Git-Url: https://git.kaiwu.me/$%7BGITURL%7D/doc/static/gitweb.js?a=commitdiff_plain;h=1f36b6e57626042695ebf00c39f57851282331c4;p=haproxy.git REGTESTS: stats: add test for 'clear counters server' Cover the new 'clear counters server /' CLI command: - error cases: * unknown backend * unknown server - success case: drive two requests pinned to a single server (the second server is a backup, so it stays idle), verify via 'show stat' that the served server accumulated sessions while the backup stayed at zero, clear only the served server's counters, then verify its cumulative sessions dropped back to zero while the backup is still untouched. - the optional 'force' argument is accepted (a no-op override here, since these process-local counters are not backed by a shared-memory stats file) and the command is discoverable via 'help'. The success path also implicitly exercises the counters_be_reset() helper: if the reset were unsafe against the shared.tg pointer, the vtest run would segfault on the next scheduled scrape or health check. --- diff --git a/reg-tests/stats/clear_counters_server.vtc b/reg-tests/stats/clear_counters_server.vtc new file mode 100644 index 000000000..173be9579 --- /dev/null +++ b/reg-tests/stats/clear_counters_server.vtc @@ -0,0 +1,89 @@ +varnishtest "Clear a single server's counters via CLI" + +feature ignore_unknown_macro + +#REGTEST_TYPE=devel + +# echo server providing s1_addr / s1_port macros; serves two requests +server s1 { + rxreq + txresp +} -repeat 2 -start + +server s1_extra { + rxreq + txresp +} -start + +haproxy h1 -conf { + global + .if feature(THREAD) + thread-groups 1 + .endif + + defaults + mode http + timeout connect "${HAPROXY_TEST_TIMEOUT-5s}" + timeout client "${HAPROXY_TEST_TIMEOUT-5s}" + timeout server "${HAPROXY_TEST_TIMEOUT-5s}" + + frontend fe + bind "fd@${feS}" + default_backend be + + backend be + server s1 ${s1_addr}:${s1_port} + server s2 ${s1_extra_addr}:${s1_extra_port} backup +} -start + +# Drive two requests; both go to s1 (s2 is backup, only used if s1 is down) +# so s1 accumulates two sessions while s2 stays at zero. +client c1 -connect ${h1_feS_sock} { + txreq + rxresp + expect resp.status == 200 +} -run + +client c2 -connect ${h1_feS_sock} { + txreq + rxresp + expect resp.status == 200 +} -run + +haproxy h1 -cli { + # ---- error cases ---- + + # unknown backend + send "clear counters server nosuch/s1" + expect ~ "No such backend" + + # unknown server + send "clear counters server be/nosuch" + expect ~ "No such server" + + # ---- success case ---- + + # precondition: s1 handled both requests (stot, CSV field 8) while the + # backup s2 stayed at zero. Both server rows are matched from a single + # response (one expect per send; the CLI connection closes afterwards). + send "show stat be 4 -1" + expect ~ "be,s1,[0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*,2,[^\n]*\nbe,s2,[0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*,0," + + # clear only s1's counters + send "clear counters server be/s1" + expect ~ "Server counters cleared." + + # s1's cumulative sessions are now back to 0; s2 is untouched (still 0), + # proving the reset is scoped to the named server. + send "show stat be 4 -1" + expect ~ "be,s1,[0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*,0,[^\n]*\nbe,s2,[0-9]*,[0-9]*,[0-9]*,[0-9]*,[0-9]*,0," + + # 'force' is accepted and is a no-op override for process-local counters + # (there is no shared-memory stats file here), so it still succeeds. + send "clear counters server be/s2 force" + expect ~ "Server counters cleared." + + # sanity: the command (with its 'force' option) is discoverable via help + send "help" + expect ~ "clear counters .*server.*force" +}