VTC

Varnish Test Case Syntax#

  • Manual section: 7

OVERVIEW#

This document describes the syntax used by Varnish Test Cases files (.vtc). A vtc file describe a scenario with different scripted HTTP-talking entities, and generally one or more Varnish instances to test.

PARSING#

A vtc file will be read word after word, with very little tokenization, meaning a syntax error won’t be detected until the test actually reach the relevant action in the test.

A parsing error will most of the time result in an assert being triggered. If this happens, please refer yourself to the related source file and line number. However, this guide should help you avoid the most common mistakes.

Words and strings#

The parser splits words by detecting whitespace characters and a string is a word, or a series of words on the same line enclosed by double-quotes (”…”), or, for multi-line strings, enclosed in curly brackets ({…}).

Comments#

The leading whitespaces of lines are ignored. Empty lines (or ones consisting only of whitespaces) are ignored too, as are the lines starting with “#” that are comments.

Lines and commands#

Test files take at most one command per line, with the first word of the line being the command and the following ones being its arguments. To continue over to a new line without breaking the argument string, you can escape the newline character (\n) with a backslash (\).

MACROS#

When a string is processed, macro expansion is performed. Macros are in the form ${<name>[,<args>...]}, they have a name followed by an optional comma- or space-separated list of arguments. Leading and trailing spaces are ignored.

The macros ${foo,bar,baz} and ${ foo bar baz } are equivalent. If an argument contains a space or a comma, arguments can be quoted. For example the macro ${foo,"bar,baz"} gives one argument bar,baz to the macro called foo.

Unless documented otherwise, all macros are simple macros that don’t take arguments.

Built-in macros#

${bad_backend} A socket address that will reliably never accept connections. ${bad_ip} An unlikely IPv4 address. ${date} The current date and time formatted for HTTP. ${listen_addr} The default listen address various components use, by default a random port on localhost. ${localhost} The first IP address that resolves to “localhost”. ${pwd} The working directory from which varnishtest was executed. ${string,<action>[,<args>...]} The string macro is the entry point for text generation, it takes a specialized action with each its own set of arguments. ${string,repeat,<uint>,<str>} Repeat uint times the string str. ${testdir} The directory containing the VTC script of the ongoing test case execution. ${tmpdir} The dedicated working directory for the ongoing test case execution, which happens to also be the current working directory. Useful when an absolute path to the working directory is needed. ${topbuild} Only present when the -i option is used, to work on Varnish itself instead of a regular installation.

SYNTAX#

barrier#

NOTE: This command is available everywhere commands are given.

Barriers allows you to synchronize different threads to make sure events occur in the right order. It’s even possible to use them in VCL.

First, it’s necessary to declare the barrier:

barrier bNAME TYPE NUMBER [-cyclic]

With the arguments being:

bNAME this is the name of the barrier, used to identify it when you’ll create sync points. It must start with ‘b’. TYPE it can be “cond” (mutex) or “sock” (socket) and sets internal behavior. If you don’t need VCL synchronization, use cond. NUMBER number of sync point needed to go through the barrier. -cyclic if present, the barrier will reset itself and be ready for another round once gotten through.

Then, to add a sync point:

barrier bNAME sync

This will block the parent thread until the number of sync points for bNAME reaches the NUMBER given in the barrier declaration.

If you wish to synchronize the VCL, you need to declare a “sock” barrier. This will emit a macro definition named “bNAME_sock” that you can use in VCL (after importing the vtc vmod):

vtc.barrier_sync("${bNAME_sock}");

This function returns 0 if everything went well and is the equivalent of barrier bNAME sync at the VTC top-level.

client/server#

Client and server threads are fake HTTP entities used to test your Varnish and VCL. They take any number of arguments, and the one that are not recognized, assuming they don’t start with ‘-’, are treated as specifications, laying out the actions to undertake:

client cNAME [...]
server sNAME [...]

Clients and server are identified by a string that’s the first argument, clients’ names start with ‘c’ and servers’ names start with ‘s’.

As the client and server commands share a good deal of arguments and specification actions, they are grouped in this single section, specific items will be explicitly marked as such.

Arguments#

-start Start the thread in background, processing the last given specification. -wait Block until the thread finishes. -run (client only) Equivalent to “-start -wait”. -repeat NUMBER Instead of processing the specification only once, do it NUMBER times. -keepalive For repeat, do not open new connections but rather run all iterations in the same connection -break (server only) Stop the server. -listen STRING (server only) Dictate the listening socket for the server. STRING is of the form “IP PORT”, or “/PATH/TO/SOCKET” for a Unix domain socket. In the latter case, the path must begin with ‘/’, and the server must be able to create it. -connect STRING (client only) Indicate the server to connect to. STRING is also of the form “IP PORT”, or “/PATH/TO/SOCKET”. As with “server -listen”, a Unix domain socket is recognized when STRING begins with a ‘/’. -dispatch (server only, s0 only) Normally, to keep things simple, server threads only handle one connection at a time, but the -dispatch switch allows to accept any number of connection and handle them following the given spec.
However, -dispatch is only allowed for the server name “s0”. -proxy1 STRING (client only) Use the PROXY protocol version 1 for this connection. STRING is of the form “CLIENTIP:PORT SERVERIP:PORT”. -proxy2 STRING (client only) Use the PROXY protocol version 2 for this connection. STRING is of the form “CLIENTIP:PORT SERVERIP:PORT [TLV [TLV … ]]”.
TLV is in the form name=val
name: 0xID or alpn, authority, crc32c, noop, unique_id, netns val: 0x… or string
ssl is currently not implemented (can be sent as hex)

Macros and automatic behaviour#

To make things easier in the general case, clients will connect by default to a Varnish server called v1. To connect to a different Varnish server, use ‘-connect ${vNAME_sock}’.

The -vcl+backend switch of the varnish command will add all the declared servers as backends. Be careful though, servers will by default listen to the 127.0.0.1 IP and will pick a random port, and publish 3 macros: sNAME_addr, sNAME_port and sNAME_sock, but only once they are started. For ‘varnish -vcl+backend’ to create the vcl with the correct values, the server must be started first.

Specification#

It’s a string, either double-quoted “like this”, but most of the time enclosed in curly brackets, allowing multilining. Write a command per line in it, empty line are ignored, and long line can be wrapped by using a backslash. For example:

client c1 {
    txreq -url /foo \
          -hdr "bar: baz"

    rxresp
} -run

accept (server only) Close the current connection, if any, and accept a new one. Note that this new connection is HTTP/1.x.

chunked STRING Send STRING as chunked encoding.

chunkedlen NUMBER Do as chunked except that the string will be generated for you, with a length of NUMBER characters.

close (server only) Close the connection. Note that if operating in HTTP/2 mode no extra (GOAWAY) frame is sent, it’s simply a TCP close.

expect STRING1 OP STRING2 Test if “STRING1 OP STRING2” is true, and if not, fails the test. OP can be ==, <, <=, >, >= when STRING1 and STRING2 represent numbers in which case it’s an order operator. If STRING1 and STRING2 are meant as strings OP is a matching operator, either == (exact match) or ~ (regex match).
varnishtest will first try to resolve STRING1 and STRING2 by looking if they have special meanings, in which case, the resolved value is use for the test. Note that this value can be a string representing a number, allowing for tests such as:

expect req.http.x-num > 2

Here’s the list of recognized strings, most should be obvious as they either match VCL logic, or the txreq/txresp options:
- remote.ip - remote.port - remote.path - req.method - req.url - req.proto - resp.proto - resp.status - resp.reason - resp.chunklen - req.bodylen - req.body - resp.bodylen - resp.body - req.http.NAME - resp.http.NAME

expect_close Reads from the connection, expecting nothing to read but an EOF.

expect_pattern

Expect as the http body the test pattern generated by chunkedlen (‘0’..’7’ repeating).

fatal|non_fatal Control whether a failure of this entity should stop the test.

gunzip Gunzip the body in place.

recv NUMBER Read NUMBER bytes from the connection.

rxchunk Receive an HTTP chunk.

rxpri (server only) Receive a preface. If valid set the server to HTTP/2, abort otherwise.

rxreq (server only) Receive and parse a request’s headers and body.

rxreqbody (server only) Receive a request’s body.

rxreqhdrs (server only) Receive and parse a request’s headers (but not the body).

rxresp [-no_obj] (client only) Receive and parse a response’s headers and body. If -no_obj is present, only get the headers.

rxrespbody (client only) Receive (part of) a response’s body.

-max : max length of this receive, 0 for all

rxresphdrs (client only) Receive and parse a response’s headers.

send STRING Push STRING on the connection.

send_n NUMBER STRING Write STRING on the socket NUMBER times.

send_urgent STRING Send string as TCP OOB urgent data. You will never need this.

sendhex STRING Send bytes as described by STRING. STRING should consist of hex pairs possibly separated by whitespace or newlines. For example: “0F EE a5 3df2”.

settings -dectbl INT Force internal HTTP/2 settings to certain values. Currently only support setting the decoding table size.

shell Same as for the top-level shell.

shutdown

Initiate shutdown.

-read Shutdown the read direction.
-write Shutdown the write direction.
-notconn OK to get ENOTCONN
The default is both direction.

stream HTTP/2 introduces the concept of streams, and these come with their own specification, and as it’s quite big, have been moved to their own chapter.

timeout NUMBER Set the TCP timeout for this entity.

txpri (client only) Send an HTTP/2 preface (“PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n”) and set client to HTTP/2.

txreq|txresp […]

Send a minimal request or response, but overload it if necessary.

txreq is client-specific and txresp is server-specific.

The only thing different between a request and a response, apart from who can send them is that the first line (request line vs status line), so all the options are pretty much the same.

-method STRING (txreq only) What method to use (default: “GET”).
-req STRING (txreq only) Alias for -method.
-url STRING (txreq only) What location to use (default “/”).
-proto STRING What protocol use in the status line. (default: “HTTP/1.1”).
-status NUMBER (txresp only) What status code to return (default 200).
-reason STRING (txresp only) What message to put in the status line (default: “OK”).
-noserver (txresp only) Don’t include a Server header with the id of the server.
-nouseragent (txreq only) Don’t include a User-Agent header with the id of the client.
These three switches can appear in any order but must come before the following ones.
-nohost Don’t include a Host header in the request. Also Implied by the addition of a Host header with -hdr.
-nolen Don’t include a Content-Length header. Also implied by the addition of a Content-Length or Transfer-Encoding header with -hdr.
-nodate Don’t include a Date header in the response. Also implied by the addition of a Date header with -hdr.
-hdr STRING Add STRING as a header, it must follow this format: “name: value”. It can be called multiple times.
-hdrlen STRING NUMBER Add STRING as a header with NUMBER bytes of content.
You can then use the arguments related to the body:
-body STRING Input STRING as body.
-bodyfrom FILE Same as -body but content is read from FILE.
-bodylen NUMBER Generate and input a body that is NUMBER bytes-long.
-gziplevel NUMBER Set the gzip level (call it before any of the other gzip switches).
-gzipresidual NUMBER Add extra gzip bits. You should never need it.
-gzipbody STRING Gzip STRING and send it as body.
-gziplen NUMBER Combine -bodylen and -gzipbody: generate a string of length NUMBER, gzip it and send as body.

write_body STRING Write the body of a request or a response to a file. By using the shell command, higher-level checks on the body can be performed (eg. XML, JSON, …) provided that such checks can be delegated to an external program.

delay#

NOTE: This command is available everywhere commands are given.

Sleep for the number of seconds specified in the argument. The number can include a fractional part, e.g. 1.5.

feature#

Test that the required feature(s) for a test are available, and skip the test otherwise; or change the interpretation of the test, as documented below. feature takes any number of arguments from this list:

64bit The environment is 64 bits ipv4 127.0.0.1 works ipv6 [::1] works dns DNS lookups are working topbuild The test has been started with ‘-i’ root The test has been invoked by the root user user The user is present group The group is present user_varnish The varnish user is present user_vcache The vcache user is present group_varnish The varnish group is present cmd A command line that should execute with a zero exit status ignore_unknown_macro Do not fail the test if a string of the form ${…} is not recognized as a macro. persistent_storage Varnish was built with the deprecated persistent storage. coverage Varnish was built with code coverage enabled. asan Varnish was built with the address sanitizer. msan Varnish was built with the memory sanitizer. tsan Varnish was built with the thread sanitizer. ubsan Varnish was built with the undefined behavior sanitizer. sanitizer Varnish was built with a sanitizer. workspace_emulator Varnish was built with its workspace emulator. abstract_uds Creation of an abstract unix domain socket succeeded. disable_aslr ASLR can be disabled. vtest_cmd vtest command is available

A feature name can be prefixed with an exclamation mark (!) to skip a test if the feature is present.

Be careful with ignore_unknown_macro, because it may cause a test with a misspelled macro to fail silently. You should only need it if you must run a test with strings of the form “${…}”.