Hands-on varnishtest·续

No trick here, the test passes, but there’s a bunch of things I want to show you, so we’re running the test verbosely this time:

varnishtest basic-007.vtc -v

First stop, we realize that we are running a full-blown Varnish server, and we can see the command line used to start it, as well as the work directory used and the pid:

**   top   === varnish v1 \
**** dT    0.001
**   s1    Started on 127.0.0.1:45467 (2 iterations)
**   s2    Started on 127.0.0.1:37511 (1 iterations)
**** dT    0.007
**   v1    Launch
***  v1    CMD: cd ${pwd} && exec varnishd  -d -n /tmp/vtc.4021796.2b5bb555/v1 -i v1 -l 2m -p auto_restart=off -p syslog_cli_traffic=off -p thread_pool_min=10 -p debug=+vtc_mode -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody -p h2_initial_window_size=1m -p h2_rx_window_low_water=64k -a '127.0.0.1:0' -M '127.0.0.1 45405' -P /tmp/vtc.4021796.2b5bb555/v1/varnishd.pid  -p default_grace=0
***  v1    CMD: cd /home/guillaume/work/vtc-examples && exec varnishd  -d -n /tmp/vtc.4021796.2b5bb555/v1 -i v1 -l 2m -p auto_restart=off -p syslog_cli_traffic=off -p thread_pool_min=10 -p debug=+vtc_mode -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody -p h2_initial_window_size=1m -p h2_rx_window_low_water=64k -a '127.0.0.1:0' -M '127.0.0.1 45405' -P /tmp/vtc.4021796.2b5bb555/v1/varnishd.pid  -p default_grace=0
***  v1    PID: 4021802
**** v1    macro def v1_pid=4021802                                                                              <- pid
**** v1    macro def v1_name=/tmp/vtc.4021796.2b5bb555/v1                                                        <- work directory
**** dT    0.032
***  v1    debug|Debug: Version: varnish-7.4.2 revision cd1d10ab53a6f6115b2b4f3b2a1da94c1f749f80
***  v1    debug|Debug: Platform: Linux,6.6.3-arch1-1,x86_64,-jnone,-sdefault,-sdefault,-hcritbit
***  v1    debug|200 314
***  v1    debug|-----------------------------
***  v1    debug|Varnish Cache CLI 1.0
***  v1    debug|-----------------------------
***  v1    debug|Linux,6.6.3-arch1-1,x86_64,-jnone,-sdefault,-sdefault,-hcritbit
***  v1    debug|varnish-7.4.2 revision cd1d10ab53a6f6115b2b4f3b2a1da94c1f749f80
***  v1    debug|
***  v1    debug|Type 'help' for command list.
***  v1    debug|Type 'quit' to close CLI session.
***  v1    debug|Type 'start' to launch worker process.
***  v1    debug|

If we go down a bit, we also find out that varnishtest actually straight up uses varnishadm to load the VCL!

***  v1    CLI RX  200
**** v1    CLI RX|-----------------------------
**** v1    CLI RX|Varnish Cache CLI 1.0
**** v1    CLI RX|-----------------------------
**** v1    CLI RX|Linux,6.6.3-arch1-1,x86_64,-jnone,-sdefault,-sdefault,-hcritbit
**** v1    CLI RX|varnish-7.4.2 revision cd1d10ab53a6f6115b2b4f3b2a1da94c1f749f80
**** v1    CLI RX|
**** v1    CLI RX|Type 'help' for command list.
**** v1    CLI RX|Type 'quit' to close CLI session.
**** v1    CLI RX|Type 'start' to launch worker process.
**** v1    CLI TX|vcl.inline vcl1 << %XJEIFLH|)Xspa8P                        <---- note the heredoc
**** v1    CLI TX|vcl 4.1;
**** v1    CLI TX|backend s1 { .host = "127.0.0.1"; .port = "45467"; }
**** v1    CLI TX|backend s2 { .host = "127.0.0.1"; .port = "37511"; }
**** v1    CLI TX|
**** v1    CLI TX|
**** v1    CLI TX|\t\tsub vcl_recv {
**** v1    CLI TX|\t\t\tif (req.url == "/to_s1") {
**** v1    CLI TX|\t\t\t\tset req.backend_hint = s1;
**** v1    CLI TX|\t\t\t} else {
**** v1    CLI TX|\t\t\t\tset req.backend_hint = s2;
**** v1    CLI TX|\t\t\t}
**** v1    CLI TX|\t\t}
**** v1    CLI TX|\t
**** v1    CLI TX|%XJEIFLH|)Xspa8P                                           <---- this signals the end of the VCL

I find it pretty cool, we usually only see EOF as a delimiting identifier, %XJEIFLH|)Xspa8P is quite a sight! It makes sense though, you want a delimiter unlikely to appear in a VCL.

Of course, we also get our network macros, once v1 is fully started:

**** v1    macro def v1_addr=127.0.0.1
**** v1    macro def v1_port=44447
**** v1    macro def v1_sock=127.0.0.1:44447
**** v1    macro def v1_a0_addr=127.0.0.1
**** v1    macro def v1_a0_port=44447
**** v1    macro def v1_a0_sock=127.0.0.1:44447

And later on, varnishtest is kind enough to provide us with the varnishlog output:

<...>
**** v1    vsl|       1002 BereqMethod     b GET
**** v1    vsl|       1002 BereqURL        b /to_s1
**** v1    vsl|       1002 BereqProtocol   b HTTP/1.1
**** v1    vsl|       1002 BereqHeader     b Host: 127.0.0.1
**** v1    vsl|       1002 BereqHeader     b User-Agent: c1
**** v1    vsl|       1002 BereqHeader     b X-Forwarded-For: 127.0.0.1
**** v1    vsl|       1002 BereqHeader     b Via: 1.1 v1 (Varnish/7.4)
**** v1    vsl|       1002 BereqHeader     b Accept-Encoding: gzip
<...>

Logs are essential for debugging, and as you can see, varnishtest takes those very seriously. While you are exploring those logs, have a look at what the clients and servers have to say. You’ll notice that they will tell you all about the buffers they receive and how they interpret them:

**   c1    === rxresp
**** dT    0.713
**** c1    rxhdr|HTTP/1.1 200 OK\r
**** c1    rxhdr|server: s2\r
**** c1    rxhdr|Date: Sat, 30 Dec 2023 20:23:02 GMT\r
**** c1    rxhdr|Content-Length: 0\r
**** c1    rxhdr|X-Varnish: 1007 1004\r
**** c1    rxhdr|Age: 0\r
**** c1    rxhdr|Via: 1.1 v1 (Varnish/7.4)\r
**** c1    rxhdr|Accept-Ranges: bytes\r
**** c1    rxhdr|Connection: keep-alive\r
**** c1    rxhdr|\r
**** c1    rxhdrlen = 190
**** c1    http[ 0] |HTTP/1.1
**** c1    http[ 1] |200
**** c1    http[ 2] |OK
**** c1    http[ 3] |server: s2
**** c1    http[ 4] |Date: Sat, 30 Dec 2023 20:23:02 GMT
**** c1    http[ 5] |Content-Length: 0
**** c1    http[ 6] |X-Varnish: 1007 1004
**** c1    http[ 7] |Age: 0
**** c1    http[ 8] |Via: 1.1 v1 (Varnish/7.4)
**** c1    http[ 9] |Accept-Ranges: bytes
**** c1    http[10] |Connection: keep-alive
**** c1    bodylen = 0

8. Working smarter, not harder#

Now it’s time to show off a couple of cheat codes. Believe me, there are so many more found inside the documentation and man pages for varnishtest and vtc (seriously, have a look!).

This page breaks down basic-008.vtc, the test is a bit big, we’ll show the whole thing and then dive into snippets.

Here is the vtc:

varnishtest "Can we be lazier? And also do more work?"

# we can repeat a full spec...
server s1 -repeat 2 {
    rxreq
    expect req.url == "/to_s1"
    txresp -hdr "server: s1"
} -start

# ...or a subsection of it
server s2 {
    loop 2 {
        rxreq
        expect req.url == "/to_s2"
        txresp -hdr "server: s2"
    }
} -start

# -vcl+backend will populate backends based on the started servers
# and also, we can pass arguments to the varnishd command line
varnish v1 \
    -arg "-p default_grace=0" \
    -vcl+backend {
        sub vcl_recv {
            if (req.url == "/to_s1") {
                set req.backend_hint = s1;
            } else {
                set req.backend_hint = s2;
            }
        }
    } -start

# we can also use varnishadm, here to reduce the default ttl
varnish v1 -cliok "param.set default_ttl 1"

# by default, clients connect to ${s1_sock}, so no need to specify -connect
client c1 {
    txreq -url "/to_s1"
    rxresp
    expect resp.http.server == "s1"

    txreq -url "/to_s2"
    rxresp
    expect resp.http.server == "s2"

    # let's wait a bit so the objects expire
    delay 1

    # and fetch them again
    txreq -url "/to_s1"
    rxresp
    expect resp.http.server == "s1"

    txreq -url "/to_s2"
    rxresp
    expect resp.http.server == "s2"
} -run

# check that "varnishtest basic-008.vtc" passes

If you need to do the same thing over and over, there are a couple of nifty solutions to help avoid duplicating a block of code. First, there is -repeat, which will repeat your server specification with the value you provide. Second, we have loop, which you can utilize to repeat just a subsection of your server specification:

server s1 -repeat 2 {
    rxreq
    expect req.url == "/to_s1"
    txresp -hdr "server: s1"
} -start
server s2 {
    loop 2 {
        rxreq
        expect req.url == "/to_s2"
        txresp -hdr "server: s2"
    }
} -start

Next is varnish management; you’re going to see I’ve been holding back a bit:

varnish v1 \
    -arg "-p default_grace=0" \
    -vcl+backend {
        sub vcl_recv {
            if (req.url == "/to_s1") {
                set req.backend_hint = s1;
            } else {
                set req.backend_hint = s2;
            }
        }
    } -start

We can pass command-line arguments to varnish, which is cool, but the big piece here is -vcl+backend. Its job is simple; generate backend VCL definitions for all the started servers and prepend them to your VCL. This simplifies test cases a lot!

Going above and beyond, we can also use varnishadm with -cliok (there’s also -clierr and -clijson)

varnish v1 -cliok "param.set default_ttl 1"

On the client side, we can now omit the -connect, because by default, a client will connect to ${s1_sock}:

client c1 {
    txreq -url "/to_s1"
    rxresp
    expect resp.http.server == "s1"

    txreq -url "/to_s2"
    rxresp
    expect resp.http.server == "s2"

    # let's wait a bit so the objects expire
    delay 1

    # and fetch them again
    txreq -url "/to_s1"
    rxresp
    expect resp.http.server == "s1"

    txreq -url "/to_s2"
    rxresp
    expect resp.http.server == "s2"
} -run

Even though we are all fancy, the test still passes, which is reassuring:

varnishtest basic-008.vtc
# top  TEST basic-008.vtc passed (1.932)

9. Varnish Counters#

The last stop (for now) is counters. Varnish keeps tracks of hundreds of metrics that you can notably access though varnishstat, and all of them are listed in the varnish-counters page, if you need to see what they’re about.

In basic-009.vtc expect on some counter values and dump some of them:

varnishtest "Checking counters"

server s1 {
    rxreq
    txresp
} -start

# the builtin.vcl will do here, we just need s1 populated
varnish v1 -vcl+backend {} -start

# and finally a simple client
client c1 -repeat 1 {
    txreq
    rxresp
} -run

# we can then ensure some counters have the right value
varnish v1 -expect MAIN.client_req == 1
varnish v1 -expect MAIN.n_object == 1
varnish v1 -expect MAIN.cache_miss == 1
varnish v1 -expect MAIN.cache_hit == 0
varnish v1 -vsc "VBE.*.s1.*"

Lets run it (verbosely) and view our results:

varnishtest basic-009.vtc

which produces this on my machine:

**   top   === varnish v1 -expect MAIN.client_req == 1
**** dT    0.695
**   v1    as expected: MAIN.client_req (1) == 1 (1)
**   top   === varnish v1 -expect MAIN.n_object == 1
**   v1    as expected: MAIN.n_object (1) == 1 (1)
**   top   === varnish v1 -expect MAIN.cache_miss == 1
**   v1    as expected: MAIN.cache_miss (1) == 1 (1)
**   top   === varnish v1 -expect MAIN.cache_hit == 0
**   v1    as expected: MAIN.cache_hit (0) == 0 (0)
**   top   === varnish v1 -vsc "VBE.*.s1.*"
**** v1    VSC VBE.vcl1.s1.happy 0
**** v1    VSC VBE.vcl1.s1.bereq_hdrbytes 146
**** v1    VSC VBE.vcl1.s1.bereq_bodybytes 0
**** v1    VSC VBE.vcl1.s1.beresp_hdrbytes 87
**** v1    VSC VBE.vcl1.s1.beresp_bodybytes 0
**** v1    VSC VBE.vcl1.s1.pipe_hdrbytes 0
**** v1    VSC VBE.vcl1.s1.pipe_out 0
**** v1    VSC VBE.vcl1.s1.pipe_in 0
**** v1    VSC VBE.vcl1.s1.conn 0
**** v1    VSC VBE.vcl1.s1.req 1
**** v1    VSC VBE.vcl1.s1.unhealthy 0
**** v1    VSC VBE.vcl1.s1.busy 0
**** v1    VSC VBE.vcl1.s1.fail 0
**** v1    VSC VBE.vcl1.s1.fail_eacces 0
**** v1    VSC VBE.vcl1.s1.fail_eaddrnotavail 0
**** v1    VSC VBE.vcl1.s1.fail_econnrefused 0
**** v1    VSC VBE.vcl1.s1.fail_enetunreach 0
**** v1    VSC VBE.vcl1.s1.fail_etimedout 0
**** v1    VSC VBE.vcl1.s1.fail_other 0
**** v1    VSC VBE.vcl1.s1.helddown 0