Hands-on varnishtest

This tutorial will help you dive into varnishtest using self-contained and concrete examples that you can run on your own computer. Each test case adds more layers and is duly commented so you can explore on your own.

The supporting files can be found in this repository and are linked to at the beginning of each section.

1. The most basic test case#

Let’s start with basic-001.vtc:

# lines starting with a # are comment, they are ignored by varnishtest,
# same as empty lines

# every vtc MUST start with a "varnishtest DESCRIPTION" line

varnishtest "This does nothing"

We can run this with varnishtest basic-001.vtc:

$ varnishtest basic-001.vtc
#    top  TEST basic-001.vtc passed (0.093)

It’s not very exciting, but it works, and it’s fast (93ms!).

If you do not have varnishtest installed (it’s part of the regular varnish package), you can use docker instead:

# mount basic-001.vtc as /test.vtc and execute it inside the container
docker run --rm -v $(pwd)/basic-001.vtc:/test.vtc varnish varnishtest /test.vtc
#    top  TEST /test.vtc passed (0.054)

Future section will not repeat the docker instruction, but know that it’s always an option!

2. Multiline strings, curly braces and verbosity#

In basic-002.vtc we add strings:

varnishtest {this looks
weird
right}

# run that test with "varnistest -v basic-002.vtc" to confirm the
# multi-line-ness of the title

Let’s start with a weird syntax quirk: while we use double quotes (") for single-line strings, we can also use {} for multi-line strings:

varnishtest {this looks
weird
right}

This will work:

$ varnishtest basic-002.vtc
#    top  TEST basic-002.vtc passed (0.023)

but that’s not very telling, let’s rerun that in verbose more, with the -v flag (we’ll snip the irrelevant log lines):

$ varnishtest basic-002.vtc -v
<snip>
**   top   === varnishtest {this looks
*    top   VTEST this looks
weird
right
<snip>

Here you can see two things:

  • the top === is varnishtest telling us where in the file it is, and what it’s about to execute

  • the top VTEST is the actual output of the actions, and we can verify it actually saw the 3-lines string

This multiline string trick will come in handy very, very soon!

3. The server#

varnishtest is centered around agents that will perform tasks independently. Those tasks are defined in specification strings that look like code blocks thanks to the {} multi-line quirk.

Let’s start easy with a server that just receives a request and sends back a response. The name of the server must start with an “s” (in general, agent names start with the first letter of the agent type).

Once the server specification is defined, we will start s1 and wait for the scenario to finish. Here is basic-003.vtc:

varnishtest "our first agent and specification block"

server s1 {
    # receive request
    rxreq
    # transmit response
    txresp -status 200 -hdr "connection: close" -hdr "beep: boop" -body "Father\n"
}

# note that this above doesn't actually start the server, it mereley defines it
# we can start the server like this
server s1 -start

# then, we can ask s1 to wait, and the call will only return once the spec is
# executed
server s1 -wait

Ok, let us run this test case:

varnishtest basic-003.vtc

It will do… nothing? This is because right now varnishtest is waiting for the s1 specification to finish, and currently it is stuck on the rxreq statement, meaning it is waiting for a request. So we are now going to open a new terminal , and send that request!

First we need to find the ip:port that s1 is listening on:

ss -plt |grep varnishtest
LISTEN 0      10         127.0.0.1:42849      0.0.0.0:*    users:(("varnishtest",pid=203008,fd=5))

Once you have the ip:port information you can simply run curl -v against it:

# curl -I 127.0.0.1:42849
HTTP/1.1 200 OK
beep: boop                        <-- THIS IS US!!!!!!!!!!!!
Date: Thu, 28 Dec 2023 17:48:29 GMT
Content-Length: 0

Once this is done, your vtc will complete inside your first terminal with results similar to

#   top  TEST basic-003.vtc passed (11.468)

Note: We can combine the two commands above with a little awk to create the below single line command:

curl -v $(ss -plt | awk '/varnishtest/ {print $4; exit}')

4. Macros and shell#

In the previous test, we had to pop up a second terminal, which worked, but wasn’t very practical. basic-004.vtc shows how we can do better with two new tools: shell and macros:

varnishtest "We need to talk about macro"

# let's get a server with an empty spec. We don't really care about what it
# does, only about its existence

server s1 -start

# we can use this macro, for example in a shell script:
shell {
    echo "we are running from ${pwd}, with s1 is listening on ${s1_sock}"
    echo "about to run the false command"
    false
}

# you can make the test pass by changing "false" to "true", or removing the
# line altogether

So, this appears to define a shell block that will actually run the shell command in it, as a regular script would, and we also see ${pwd} and ${s1_sock} which looks like variables (those are our macros).

Question is: who defines them and what do they resolve to? To answer that, werun the test:

varnishtest basic-004.vtc

Wait… what?…it failed?

Yes, but that’s on purpose! First, this shows that varnishtest automatically switches to verbose mode if a test fails, which is pretty nice. Second, it allows us to look at the logs, notably:

...
**** top   shell_out|about to run the false command
**** top   shell_status = 0x0001
---- top   shell_exit not as expected: got 0x0001 wanted 0x0000
...

There are a few cool things here:

  • shell_out shows us stdout, here that’s the output of the second echo command

  • shell_status, informs us that the shell returned 1…

  • …which apparently upset shell_exit, who was expecting a 0

It turns out it’s all normal as the shell command will fail if any of its command returns non-zero (like false, for example). To verify that, you can change the false command with true and the test will pass (but without logs).

Now that this detour is done, let’s focus on this log line:

**** top   shell_out|we are running from /home/guillaume/work/vtc-examples, with s1 is listening on 127.0.0.1:33523

So we can see the first echo output, and that both ${pwd} and ${s1_sock} have been replaced by other values. If you look a few lines above, you can actually see varnishtest logging their definitions:

**** top   extmacro def pwd=/home/guillaume/work/vtc-examples       <----- that's ${pwd}
**** top   extmacro def date(...)
**** top   extmacro def string(...)
**** top   extmacro def localhost=127.0.0.1
**** top   extmacro def bad_backend=127.0.0.1:41113
**** top   extmacro def listen_addr=127.0.0.1:0
**** top   extmacro def bad_ip=192.0.2.255
**** top   macro def testdir=/home/guillaume/work/vtc-examples
**** top   macro def tmpdir=/tmp/vtc.4005032.2f784281
**** top   macro def vtcid=vtc.4005032.2f784281
**   top   === varnishtest "We need to talk about macro"            <------ we're entering our test
*    top   VTEST We need to talk about macro
**   top   === server s1 -start
**   s1    Starting server                                          <------ s1 starts
**** s1    macro def s1_addr=127.0.0.1
**** s1    macro def s1_port=33523
**** s1    macro def s1_sock=127.0.0.1:33523                        <----- and here's ${s1_sock}

What we’re seeing here is that there’s a handful of macros that get defined as soon as varnishtest starts, and others get automatically created when the new server is started (even though its specification was empty).

That’s pretty cool, that means that we could automate the previous test (basic-003.vtc) without the need for an external terminal with something like this:

varnishtest "our first agent and specification block"
server s1 {
    rxreq
    txresp
} -start
shell {
    curl -v $(ss -plunt | awk '/varnishtest/ {print $5; exit}')
}

However, as we’re going to see next, there’s an even better approach…

5. The Client#

The client side of a varnishtest is used to connect to your test case server, it is also a real http client.

To set this up, we’re reusing our server friend from basic-003.vtc, but this time telling it to respond to two requests in a row.

Our client will look similar to the server one, with a couple of variations:

  • we use -connect ip:port so the client knows what to connect to

  • the rxreq/txresp pair is replaced with the txreq/rxresp one

  • we use -run which is the equivalent to -start -wait

Let’s call this basic-005.vtc:

varnishtest "Connecting a client to a server"

server s1 {
    rxreq
    txresp

    rxreq
    txresp
} -start

client c1 -connect ${s1_sock} {
    txreq -url "/foo" -hdr "something: anything"
    rxresp

    # interesting, we don't need to quote single words, but we usually
    # do it for consistency
    txreq -url /bar_and_maybe_baz
    rxresp
} -run

Without surprise, this passes:

$ varnishtest basic-005.vtc
top  TEST basic-005.vtc passed (0.075)

Now, the interesting is that, because clients are actual HTTP clients, we can connect them to real servers, for example:

varnishtest "Connecting a client to a server (a real one)"
client c2 -connect "example.com 80" {
    txreq
    rxresp
} -run

(and this is actually part of basic-005.vtc in the git repository)

6. Using Expect Statements#

So far, our tests “passed”, in the sense that txreq/rxreq/txresp/rxresp are succeeding i.e., they send and receive valid http messages. Now we want to be stricter and make sure that the URLs, headers, and bodies have the values we expect, and for this, using the well-named expect keyword.

basic-006.vtc is, by far, the largest test we’ve seen so far, but don’t worry, it’s just one client and server exchanging two requests/responses and making sure the expectations are correct:

varnishtest "Hope for the best, expect the worst"

server s1 {
    rxreq
    expect req.url == "/foo"
    expect req.http.something == "anything"

    # be aware: if you try to set the body before the headers, s1 will fail
    txresp -status 123 -hdr "x: 17.4" -body "tadbbb"

    # by the way, I can also read `req.* later, as long as I haven't
    # received a new one
    expect req.body == "greek god"
} -start

client c1 -connect ${s1_sock} {
    # oh, look! Line continuations!
    txreq \
        -url "/foo" \
        -hdr "something: anything" \
        -body "greek god"
    rxresp

    # that status is nonsensical, but hey, if that's what I expect...
    expect resp.status == 123
    # strings that look like a number can be numerically tested
    expect resp.http.x > 17
    expect resp.http.x <= 17.5
    expect resp.body == "tadaaa"
    # we use &lt;undef> for "without a value"
    expect resp.http.y == &lt;undef>
} -run

Before we run it, let’s note a few things:

  • the expect syntax is expect THING1 COMPARATOR THING2

  • COMPARATOR can test equality (==), order (>, >=, <, <=), regex (~)

  • THING1 and THING2 will be resolved as variables and understood as numbers, if they can and need to

  • we can use <undef> to express the absence of value

But enough looking around, let’s run this test:

varnishtest basic-006.vtc

And, yes, it’s another one of those tests that fail on purpose. Once again, as you can imagine, there’s a good reason. A passing expect isn’t really interesting itself. The fun begins when things go wrong; and when they do, you want to be able to understand what happened.

Thankfully, varnishtest is pretty clear about the issue; we just have to look for line(s) starting with ---:

**   c1    === expect resp.http.x <= 17.5
**** c1    EXPECT resp.http.x (17.4) <= "17.5" match
**   c1    === expect resp.body == "tadaaa"
---- c1    EXPECT resp.body (tadbbb) == "tadaaa" failed

So, apparently, our expect resp.body == "tadaaa" test didn’t work. In passing, notice how the logs give us both info about the name of the variable (resp.body) and its resolved value ((tadbbb))?

If you fix either the offending expect line or the txresp one, so they match, the test should pass:

varnishtest basic-006.vtc
#    top  TEST basic-006.vtc passed (0.070)

7. Varnish and some logs#

We’ve been patient, but now it’s really time to put the “varnish” back into varnishtest! The varnish agent will look like a server, but note the -vcl SPEC argument that allows us to define our VCL in basic-007.vtc:

varnishtest "it's time to introduce varnish"

server s1 {
    rxreq
    expect req.url == "/to_s1"
    txresp -hdr "server: s1"
} -start

server s2 {
    rxreq
    expect req.url == "/to_s2"
    txresp -hdr "server: s2"
} -start

# here's the star of the show, the varnish agent
varnish v1 -vcl {
    # pretty cool, we don't explicitly need the "vcl 4.1;" line
    backend s1 {
        .host = "${s1_addr}";
        .port = "${s1_port}";
    }

    # did you know you could specify .host as "HOST:PORT"?
    backend s2 { .host = "${s2_sock}"; }

    sub vcl_recv {
        if (req.url == "/to_s1") {
            set req.backend_hint = s1;
        } else {
            set req.backend_hint = s2;
        }
    }
} -start

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

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

# if we have two servers, we need two request, obviously
# even though, there's no suspense, since the data is cached
client c1 -connect ${v1_sock} {
    txreq -url "/to_s1"
    rxresp
    expect resp.http.server == "s1"

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