varnishtest "varnishncsa coverage"

server s1 -repeat 2 {
	rxreq
	txresp -bodylen 100
} -start

varnish v1 -vcl+backend {
	import std;

	sub vcl_recv {
		if (req.url == "/2") {
			return (pipe);
		}
		return (hash);
	}

	sub vcl_miss {
		std.log("quux:quuz");
	}

	sub vcl_hit {
		return (synth(404));
	}
} -start

shell {
	varnishncsa -n ${v1_name} -D -P ${tmpdir}/ncsa.pid \
		-w ${tmpdir}/ncsa.log -R 100/s
}

process p1 -winsz 25 200 {varnishncsa -n ${v1_name}} -start

delay 1

client c1 {
	txreq -nouseragent -url /1?foo=bar -hdr "authorization: basic dXNlcjpwYXNz"
	rxresp
	txreq -nouseragent -url /1?foo=bar -hdr "baz: qux"
	rxresp
} -run

delay 1

shell "mv ${tmpdir}/ncsa.log ${tmpdir}/ncsa.old.log"
shell "kill -HUP `cat ${tmpdir}/ncsa.pid`"

client c2 {
	txreq -nouseragent -url /2
	rxresp
} -run

delay 1

shell "kill `cat ${tmpdir}/ncsa.pid`"

# default formatter and rotation
shell -match {${localhost} - user \[../.../20[1-9][0-9]:..:..:.. (?#
)[+-]....\] "GET http://${localhost}/1\?foo=bar HTTP/1.1" 200 100 "-" "-"
${localhost} - - \[../.../20[1-9][0-9]:..:..:.. [+-]....\] (?#
)"GET http://${localhost}/1\?foo=bar HTTP/1.1" 404 \d+ "-" "-"$} \
	"cat ${tmpdir}/ncsa.old.log"
shell "grep -q /2 ${tmpdir}/ncsa.log"

# command line
shell -match "Usage: .*varnishncsa <options>" \
	"varnishncsa -h"
shell -expect "Copyright (c) 2006 Verdens Gang AS" \
	"varnishncsa -V"
shell -err -expect "Missing -w option" \
	{varnishncsa -D}
shell -err -expect "Daemon cannot write to stdout" \
	"varnishlog -D -w -"
shell -err -expect "Unknown format specifier at: %{foo}A" \
	{varnishncsa -F "%{foo}A"}
shell -err -expect "Unknown format specifier at: %A" \
	{varnishncsa -F "%A"}
shell -err -expect "Unmatched bracket at: %{foo" \
	"varnishncsa -F %{foo"
shell -err -expect "Unknown formatting extension: foo" \
	{varnishncsa -F "%{foo}x"}
shell -err -expect "Missing tag in VSL:" \
	{varnishncsa -F "%{VSL:}x"}
shell -err -expect "Unknown log tag: nonexistent" \
	{varnishncsa -F "%{VSL:nonexistent}x"}
shell -err -expect "Tag not unique: Req" \
	{varnishncsa -F "%{VSL:Req}x"}
shell -err -expect "Unknown log tag: Begin[" \
	{varnishncsa -F "%{VSL:Begin[}x"}
shell -err -expect "Syntax error: VSL:Begin]" \
	{varnishncsa -F "%{VSL:Begin]}x"}
shell -err -expect "Unknown log tag: Begin[a" \
	{varnishncsa -F "%{VSL:Begin[a}x"}
shell -err -expect "Syntax error: VSL:Begin[a]" \
	{varnishncsa -F "%{VSL:Begin[a]}x"}
shell -err -match {Syntax error. (?#
)Field specifier must be between 1 and 255: Begin\[0\]} \
	{varnishncsa -F "%{VSL:Begin[0]}x"}
shell -err -match {Syntax error. (?#
)Field specifier must be between 1 and 255: Begin\[999999999999\]} \
	{varnishncsa -F "%{VSL:Begin[999999999999]}x"}
shell -err -expect "Can't open format file (No such file or directory)" \
	{varnishncsa -f /nonexistent/file}
shell -err -expect "Empty format file" \
	{varnishncsa -f /dev/null}
# In Linux and SunOS, getline(3) fails when the stream refers to a
# directory but happily works in FreeBSD.
#shell -err -expect "Can't read format from file (Is a directory)" \
#	{varnishncsa -f ${tmpdir}}
shell -err -expect "Invalid grouping mode: session" \
	{varnishncsa -g session}
shell -err -expect "Can't open output file (No such file or directory)" \
	{varnishncsa -w /nonexistent/file}
shell -err -match "Usage: .*varnishncsa <options>" \
	{varnishncsa extra}

# -b
shell -match {^${localhost} 100
${localhost} 0$} \
	{varnishncsa -n ${v1_name} -b -d -F "%{Host}i %b"}
# -c
shell -match {^${localhost} 100
${localhost} \d+
${localhost} -$} \
	{varnishncsa -n ${v1_name} -c -d -F "%{Host}i %b"}

# -f and standard formatters
shell {echo "%b %D %H %h %I %{baz}i %l %m %{Age}o %O %q %s %t %{%Y}t \
%T %U %u" > ${tmpdir}/format}
shell -match {^100 \d+ HTTP/1.1 (?:\d+.\d+.\d+.\d+|[a-f0-9:]+) \d+ - - GET 0 \d+ (?#
)\?foo=bar 200 \[../.../(20[1-9][0-9]):..:..:.. [+-]....\] \1 \d+ /1 user
\d+ \d+ HTTP/1.1 (?:\d+.\d+.\d+.\d+|[a-f0-9:]+) \d+ qux - GET - \d+ (?#
)\?foo=bar 404 \[../.../(20[1-9][0-9]):..:..:.. [+-]....\] \1 \d+ /1 -
- \d+ HTTP/1.1 (?:\d+.\d+.\d+.\d+|[a-f0-9:]+) \d+ - - GET - \d+ (?#
) - \[../.../(20[1-9][0-9]):..:..:.. [+-]....\] \1 \d+ /2 -$} \
	{varnishncsa -n ${v1_name} -d -f ${tmpdir}/format}

# extended variables
shell -match {^\d+.\d{6} miss miss c 1001 quuz
\d+.\d{6} miss synth c 1003\s
\d+.\d{6} miss pipe c 1005\s$} \
	{varnishncsa -n ${v1_name} -d -F "%{Varnish:time_firstbyte}x \
%{Varnish:hitmiss}x %{Varnish:handling}x %{Varnish:side}x \
%{Varnish:vxid}x %{VCL_Log:quux}x"}

# %{VSL:..}x
shell -match {^req (\d+) rxreq \1 \d{10}\.\d{6} (\d+\.\d{6}) \d+\.\d{6} \2 - -
req (\d+) rxreq \3 \d{10}\.\d{6} (\d+\.\d{6}) \d+\.\d{6} \4 - -
req (\d+) rxreq \5 - - - -$} \
	{varnishncsa -n ${v1_name} -d -F "%{VSL:Begin}x \
%{VSL:Begin[2]}x %{VSL:Timestamp:Resp}x \
%{VSL:Timestamp:Resp[2]}x %{VSL:Timestamp:foo}x %{VSL:ReqURL[2]}x"}

# times
shell -match {^\{\d{4}-\d{2}-\d{2}\}T\{\d{2}:\d{2}:\d{2}\} \{\{\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\}\} \d+ \d+ \d+ \d{10,} \d{13,} \d{16,} \d{3} \d{6} usecx msecy} \
	{varnishncsa -n ${v1_name} -d -F "%{{%F}T{%T}}t %{{{%FT%T}}}t \
%{s}T %{ms}T %{us}T %{sec}t %{msec}t %{usec}t %{msec_frac}t %{usec_frac}t \
%{usecx}t %{msecy}t"}

process p1 -stop -screen-dump
process p1 -expect-text 1 0 {/1?foo=bar HTTP/1.1" 200 100 "-" "-"}
process p1 -expect-text 1 0 { - user [}
process p1 -expect-text 2 0 {/1?foo=bar HTTP/1.1" 404 248 "-" "-"}
process p1 -expect-text 3 0 {/2 HTTP/1.1" - - "-" "-"}

process p2 {varnishncsa -t 5 -n nonexistent} -start
delay 1
process p2 -expect-exit 1 -kill INT -wait
shell {grep -q "VSM: Attach interrupted" ${p2_err}}

# IP address for bad requests

client c3 {
	txreq -url "/bad path"
	rxresp
	expect resp.status == 400
} -run

varnish v1 -vsl_catchup

shell -expect ${localhost} {
	varnishncsa -n ${v1_name} -d -q 'RespStatus == 400'
}

# ESI coverage in e00003.vtc
