]> git.kaiwu.me - haproxy.git/commitdiff
REGTESTS: converter: add a test for the when() and debug() converters
authorWilly Tarreau <w@1wt.eu>
Mon, 21 Sep 2026 20:39:39 +0000 (20:39 +0000)
committerWilly Tarreau <w@1wt.eu>
Wed, 30 Sep 2026 14:55:57 +0000 (16:55 +0200)
Neither converter was covered. when() only lets its input through when
the condition is met, so the test builds a string made of one bracketed
field per condition and compares it for three kinds of transactions: one
forwarded to a healthy server, one whose server cannot be reached, and
one answered by the stats applet. This covers the error, normal,
forwarded, processed, toapplet, stopping and acl conditions as well as
the negated form.

debug() is checked to be transparent for the sample it is inserted in,
and its copy is read back from the default sink with "show events".

reg-tests/converter/when_debug.vtc [new file with mode: 0644]

diff --git a/reg-tests/converter/when_debug.vtc b/reg-tests/converter/when_debug.vtc
new file mode 100644 (file)
index 0000000..83a21a5
--- /dev/null
@@ -0,0 +1,86 @@
+varnishtest "Test the when() and debug() converters"
+
+# Neither when() nor debug() was covered. when() only lets the input
+# sample through when its condition is met, so the test builds a string
+# made of one bracketed field per condition and compares it for three
+# kinds of transactions: one forwarded to a server, one which fails to
+# reach its server, and one answered by an applet.
+#
+# debug() passes its input through and copies it to a sink, which is
+# then read back with "show events".
+
+feature ignore_unknown_macro
+
+server s1 {
+       rxreq
+       txresp
+} -start
+
+haproxy h1 -conf {
+    global
+    .if feature(THREAD)
+        thread-groups 1
+    .endif
+        chroot /
+
+    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@${fe}"
+
+       acl yes always_true
+
+       # debug() is transparent, the variable keeps the original value
+       http-request set-var(txn.dbg) str(hello),debug(mytag)
+
+       use_backend dead  if { path /dead }
+       use_backend stats if { path /stats }
+       default_backend be
+
+       http-after-response set-header x-dbg  "%[var(txn.dbg)]"
+       http-after-response set-header x-when "[%[str(v),when(error)]][%[str(v),when(normal)]][%[str(v),when(forwarded)]][%[str(v),when(processed)]][%[str(v),when(toapplet)]][%[str(v),when(stopping)]][%[str(v),when(acl,yes)]][%[str(v),when(!error)]]"
+
+    backend be
+       server s1 ${s1_addr}:${s1_port}
+
+    backend dead
+       retries 0
+       server s1 ${bad_backend}
+
+    backend stats
+       stats enable
+       stats uri /stats
+} -start
+
+client c1 -connect ${h1_fe_sock} {
+       # forwarded to a server, no error
+       txreq -url "/srv"
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-dbg  == "hello"
+       expect resp.http.x-when == "[][v][v][v][][][v][v]"
+
+       # the server could not be reached: this is an error, but the request
+       # was still considered as forwarded and processed since a connection
+       # attempt was made
+       txreq -url "/dead"
+       rxresp
+       expect resp.status == 503
+       expect resp.http.x-when == "[v][][v][v][][][v][]"
+
+       # answered by the stats applet
+       txreq -url "/stats"
+       rxresp
+       expect resp.status == 200
+       expect resp.http.x-when == "[][v][][v][v][][v][v]"
+} -run
+
+haproxy h1 -cli {
+       # debug() copied the sample to the default sink
+       send "show events buf0 -0"
+       expect ~ "mytag: type=str <hello>"
+}