Testing Varnish with varnishtest
The varnishtest program is a script-driven testing tool that uses Varnish Test Case (VTC) files to test specific scenarios. It is a functional testing tool, where the various elements of the delivery chain can be defined:
- The Varnish server
- The client
- The origin server
- Extra proxy servers
- Varnishlog output
- Other server processes that are called
The varnishtest program#
The varnishtest program allows you to run isolated test cases that leverage the varnishd program without interfering with any live Varnish environment on the server.
varnishtest takes a couple of arguments, most of which are optional. There’s only one non-optional argument and that’s the VTC script.
Here’s how varnishtest can be used in its most basic form:
varnishtest test.vtc
This command loads the test.vtc file that contains the VTC logic. You can also run multiple VTC scripts in a single test run:
varnishtest test1.vtc test2.vtc test3.vtc
Arguments and options can be added to the command:
- The
-boption will set the internal buffer size, which defaults to 1 MB - The
-Doption can be used to define macros that can be used in the VTC scripts - The
-ioption overrides thePATHandvmod_pathwhere binaries and VMODs can be found - The
-joption defines how many tests can be run in parallel - The
-koption will continue the test run when test failures occur - The
-noption sets the number of test iterations - The
-poption overridesvarnishdruntime parameters within the VTC scope - The
-qoption enables quiet mode and only displays failures - The
-toption defines the execution timeout of the test run, which defaults to 60 seconds - The
-voption enables verbose mode and provides more verbose output
Here’s an example of a macro that is set using the -D option:
varnishtest -D name=Varnish test.vtc
Because of the -D name=Varnish option, the ${name} macro becomes available in the VTC files. Its value will be Varnish.
The following example will run each test five times and will run three tests in parallel:
varnishtest -j 3 -n 5 test.vtc
For more information about varnishtest and its configuration options, please have a look at the varnishtest documentation page.
Varnish Test Case language (VTC)#
The Varnish Test Case (VTC) language defines the HTTP components and test scenarios to test the behavior of Varnish and your VCL code.
In VTC you can define the various elements of the delivery chain:
- The Varnish server
- The client
- The origin server
- Extra proxy servers
- Varnishlog output
- Other server processes that are called
The testing scenarios involve interactions between these elements and through the expect keyword assertions can be made. These assertions determine whether or not the test case was successful.
A typical Varnish Test Case contains three HTTP components:
- The HTTP client, defined as
client c1 - The Varnish instance, defined as
varnish v1 - The web server, defined as
server s1

For more information about the Varnish Test Case syntax , please have a look at the VTC documentation page.
Varnish Test Case documentation →
Naming convention#
The varnishtest program comes with dozens of pre-defined tests that are used in the Varnish development process for feature testing and bug fixing.
The filename of each of these VTC files adheres to the following naming convention:
[id]%05d.vtc
One example is v00032.vtc. It starts with v, which represents the [id], followed by five digits.
The list below describes the meaning of the [id] part:
id ~ ^a --> varnishtest(1) tests
id ~ ^a02 --> HTTP2
id ~ ^b --> Basic functionality tests
id ~ ^c --> Complex functionality tests
id ~ ^d --> Director VMOD tests
id ~ ^e --> ESI tests
id ~ ^f --> Security-related tests
id ~ ^g --> GZIP tests
id ~ ^h --> HAproxy tests
id ~ ^j --> JAIL tests
id ~ ^l --> VSL tests
id ~ ^m --> VMOD tests excluding director
id ~ ^o --> prOxy protocol
id ~ ^p --> Persistent tests
id ~ ^r --> Regression tests, same number as ticket
id ~ ^s --> Slow tests, expiry, grace, etc.
id ~ ^t --> Transport protocol tests
id ~ ^t02 --> HTTP2
id ~ ^u --> Utilities and background processes
id ~ ^v --> VCL tests: execute VRT functions
This means that v00032.vtc is a VCL test and b00012 .vtc is a basic functionality test.
Although you are free to use any name you want for your tests, it does make sense to use the naming convention. It will help you avoid writing tests that already exist or that are similar.
Declaring a test case#
A VTC file starts with a varnishtest declaration that describes the purpose of the test.
Here’s an example of such a declaration:
varnishtest "Language cookie cache variation test"
As you can see, the test case tests how language cookie cache variations behave in Varnish.
Declaring a server#
In VTC a server declaration mimics the behavior of an actual origin web server. You define a sequence of expected input and the corresponding output.
A minimal VTC server declaration looks like this:
server s1 {
rxreq
txresp
} -start
s1is the name of the serverrxreqwill accept and receive an incoming HTTP request, and parse its headers and request bodytxrespwill transmit an HTTP response to the requesting server-startwill start a server thread in the background
Here’s an example of a server declaration that contains an expect assertion:
server s1 {
rxreq
expect req.url == "/blog"
txresp -body "blog"
} -start
This server declaration expects incoming requests to match the /blog URL. When this is the case a response will be returned with blog as the response body.
Declaring a Varnish instance#
To test the behavior of Varnish, you can use a Varnish VTC declaration. Here’s a minimal example that includes a -vcl+backend statement:
varnish v1 -vcl+backend { } -start
This -vcl+backend statement ensures that all defined servers in VTC are added as backends in the VCL file.
It is also possible to include your own VCL code, as you can see in the following example:
varnish v1 -vcl+backend {
vcl 4.1;
sub vcl_recv {
if(req.url ~ "^/admin") {
return(pass);
}
unset req.http.Cookie;
return(hash);
}
} -start
This example also features the -vcl+backend option, which ensures backends are automatically defined. But there’s also some VCL logic that bypasses the cache for URLs that match the ^/admin regular expression pattern and that caches all other requests.
Declaring a client#
In VTC a client declaration is used to send actual HTTP requests to Varnish. Keywords like txreq and rxresp are used to send a request and get the response. By adding an assertion through the expect keyword, you can test whether or not the logic is behaving as expected:
client c1 {
txreq
rxresp
expect resp.body == "Hello world"
} -run
This example sends a client request to Varnish for the / URL. When the response is received the client expects the response body to be Hello world. The -run option is required to start the client.
It is also possible to add a lot more extra information to the request:
- The URL can be overridden.
- Custom headers can be added.
- Multiple requests can be sent by a single client.
Here’s an example:
client c1 {
txreq -hdr "Cookie: language=nl" -url "/greeting"
rxresp
expect resp.body == "Goede morgen"
txreq -hdr "Cookie: language=en" -url "/greeting"
rxresp
expect resp.body == "Good morning"
txreq -url "/greeting"
rxresp
expect resp.body == "Good morning"
} -run
This example will send three requests to the /greeting endpoint. Depending on the value of the language cookie, a specific response body is expected. If the three expectations are met, the test case will be successful.
Running a test#
Imagine the following VTC file :
varnishtest "Language cookie cache variation"
server s1 {
rxreq
expect req.http.Cookie == "language=nl"
txresp -body "Goede morgen"
rxreq
expect req.http.Cookie != "language=nl"
txresp -body "Good morning"
rxreq
expect req.http.Cookie != "language=nl"
txresp -body "Good morning"
rxreq
expect req.http.Cookie != "language=nl"
txresp -body "Good morning"
} -start
varnish v1 -vcl+backend {
vcl 4.1;
sub vcl_recv {
if (req.http.Cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(language)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.cookie ~ "^\s*$") {
unset req.http.cookie;
}
return(hash);
}
}
sub vcl_hash {
if(req.http.Cookie ~ "^.*language=(nl|en);*.*$") {
hash_data(regsub( req.http.Cookie, "^.*language=(nl|en);*.*$", "\1" ));
} else {
hash_data("en");
}
}
} -start
client c1 {
txreq -hdr "Cookie: a=1; b=2; language=nl; c=3"
rxresp
expect resp.body == "Goede morgen"
txreq -hdr "Cookie: a=1; language=en"
rxresp
expect resp.body == "Good morning"
txreq -hdr "Cookie: a=1; language=fr"
rxresp
expect resp.body == "Good morning"
txreq
rxresp
expect resp.body == "Good morning"
} -run
This Varnish Test Case sends HTTP requests to a Varnish instance that uses the value of the language cookie to create cache variations. The VCL ensures that only the values nl for Dutch and en for English are supported. Any other value will cause a fallback to English.
The test case checks whether or not the right cookie values are extracted by the VCL file and whether or not the right variations are cached.
Through varnishtest test.vtc the test.vtc file that includes this VTC will be executed.
Here’s the output:
$ varnishtest test.vtc
# top TEST test.vtc passed (0.649)
Test failures#
When a VTC fails, verbose output will explain exactly what was going on when the failure occurred.
Let’s trigger a failure by taking the previous VTC and changing the last assertion from expect resp.body == "Good morning" to expect resp.body == "Bonjour".
