vtest "vinyllog coverage"

server s1 -repeat 2 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {} -start

# We use this to make sure we know there is a "0 CLI" in the binary log.
process p1 {
	exec vinyllog -n ${v1_name} -g raw -u -A -w -
} -start

shell {
	exec vinyllog -n ${v1_name} -D -P ${tmpdir}/vlog.pid \
	    -w ${tmpdir}/vlog.bin -R 10/s \
	    -i RespStatus -i ReqURL -i BereqURL
}

shell -match "Usage: .*vinyllog <options>" \
	"vinyllog -h"
shell -expect "Copyright (c) 2006 Verdens Gang AS" \
	"vinyllog -V"
shell -err -match "Usage: .*vinyllog <options>" \
	"vinyllog extra"
shell -err -expect "Missing -w option" \
	"vinyllog -D"
shell -err -expect "Daemon cannot write to stdout" \
	"vinyllog -D -w -"
shell -err -expect "Ambiguous grouping type: r" \
	"vinyllog -g r"
shell -err -expect "Unknown grouping type: foo" \
	"vinyllog -g foo"
shell -err -expect "-k: Invalid number 'foo'" \
	"vinyllog -k foo"
shell -err -expect "-L: Range error" \
	"vinyllog -L 0"

shell -err -expect {-i: "foo" matches zero tags} \
	"vinyllog -i foo"
shell -err -expect {-i: "Resp" is ambiguous} \
	"vinyllog -i Resp"
shell -err -expect {-i: Syntax error in "Re**sp"} \
	"vinyllog -i Re**sp"

shell -err -expect {-I: "foo" matches zero tags} \
	"vinyllog -I foo:bar"
shell -err -expect {-I: "Resp" is ambiguous} \
	"vinyllog -I Resp:bar"
shell -err -expect {-I: Regex error at position 4 (missing closing parenthesis)} \
	{vinyllog -I "(foo"}
shell -err -expect "-t: Invalid argument" \
	"vinyllog -t -1"
shell -err -expect "-t: Invalid argument" \
	"vinyllog -t foo"
shell -err -expect "-t: Invalid argument" \
	"vinyllog -t NaN"
shell -err -expect {-x: Syntax error in "**"} \
	{vinyllog -x "**"}
shell -err -expect {-X: Syntax error in "**"} \
	{vinyllog -X "**:bar"}
shell -err -expect "Cannot open output file (No such file or directory)" \
	{vinyllog -w /nonexistent/file}
shell -err -expect "Only one of -n and -r options may be used" \
	{vinyllog -n ${v1_name} -r ${v1_name}/_.vsm}

shell -err -expect "Unterminated string" \
	{vinyllog -q '"b\\la'}
shell -err -expect "Syntax error" \
	{vinyllog -q ' / '}
shell -err -expect "Expected integer got '}" \
	"vinyllog -q {}"
shell -err -expect "Expected positive integer" \
	"vinyllog -q {-1}"
shell -err -expect "Syntax error in level limit" \
	"vinyllog -q {1a}"
shell -err -expect "Expected VSL tag name got '['" \
	{vinyllog -q "[]"}
shell -err -expect "Tag name matches zero tags" \
	"vinyllog -q foo"
shell -err -expect "Tag name is ambiguous" \
	"vinyllog -q Resp"
shell -err -expect "Expected string got '['" \
	{vinyllog -q "ReqHeader:[]"}
shell -err -expect "Expected integer got ']'" \
	{vinyllog -q "ReqHeader:foo[]"}
shell -err -expect "Expected positive integer" \
	{vinyllog -q "ReqHeader:foo[a]"}
shell -err -expect "Syntax error in tag name" \
	{vinyllog -q '*x* eq "foo"'}
shell -err -expect "Expected number got '>'" \
	{vinyllog -q "RespStatus > >"}
shell -err -expect "Floating point parse error" \
	{vinyllog -q "Timestamp:Start[1] > 0.foo"}
shell -err -expect "Integer parse error" \
	{vinyllog -q "RespStatus > foo"}
shell -err -expect "Expected string got '>'" \
	{vinyllog -q "ReqMethod eq >"}
shell -err -expect "Expected regular expression got '>'" \
	{vinyllog -q "ReqMethod ~ >"}
shell -err -expect "Regular expression error: " \
	{vinyllog -q 'ReqMethod ~ "("'}
shell -err -expect "Expected operator got 'ReqMethod'" \
	{vinyllog -q "RespStatus ReqMethod 200"}

shell -err -expect "-R: Syntax error" \
	"vinyllog -R 1foo"
shell -err -expect "-R: Range error" \
	"vinyllog -R 0"
shell -err -expect "-R: Range error" \
	"vinyllog -R -1"
shell -err -expect "-R: Range error" \
	"vinyllog -R 3000000000"
shell -err -expect "-R: Syntax error: Invalid duration" \
	"vinyllog -R 10/0s"
shell -err -expect "-R: Syntax error: Invalid duration" \
	"vinyllog -R 10/-10s"
shell -err -expect "-R: Syntax error: Invalid duration" \
	"vinyllog -R 10/1q"
shell -err -expect "-R: Syntax error: Invalid duration" \
	"vinyllog -R 10/11f09s"
shell -err -expect "-R: Syntax error" \
	"vinyllog -R 1000000000/1000000000000000000000000000000s"

# Wait until the binary also (must) contain a "0 CLI"
process p1 -expect-text 0 0 "0 CLI"
process p1 -screen_dump
process p1 -stop

shell -match "0 CLI[ ]+- Wr 200 [0-9]+ PONG" \
	{vinyllog -n ${v1_name} -d -g raw -X "Wr 200 [0-9]+ [^P]"}

shell -match "0 CLI[ ]+- Wr 200 [0-9]+ PONG" \
	{VINYL_DEFAULT_N="${v1_name}" vinyllog -d -g raw -X "Wr 200 [0-9]+ [^P]"}

client c1 {
	txreq -url /foo
	rxresp
} -run

delay 1

shell "mv ${tmpdir}/vlog.bin ${tmpdir}/vlog.bin~"
shell "kill -HUP `cat ${tmpdir}/vlog.pid`"

client c1 {
	txreq -url /bar
	rxresp
} -run

delay 1

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

shell -match {^\*[ ]+<< Request\s+>>[ ]+1001[ ]+
-[ ]+1001 ReqURL[ ]+c /foo
$} \
	{vinyllog -r ${tmpdir}/vlog.bin~ -i ReqURL -v \
	-q "RespStatus == 200"}
shell -match "-[ ]+BereqURL[ ]+/bar" \
	"vinyllog -r ${tmpdir}/vlog.bin -x ReqURL"
shell -match {^\*[ ]+<< BeReq\s+>>[ ]+1005[ ]+
-[ ]+BereqURL[ ]+/bar
$} \
	"vinyllog -r ${tmpdir}/vlog.bin -b -C -I BAR"

shell -match {^\*[ ]+<< BeReq\s+>>[ ]+1005[ ]+} \
	"cat ${tmpdir}/vlog.bin | vinyllog -r - -b -C -I BAR"

shell "rm -f ${tmpdir}/foo"
shell -err -expect "Cannot open ${tmpdir}/foo: " \
	"vinyllog -r ${tmpdir}/foo"
shell "echo foobar > ${tmpdir}/foo"
shell -err -expect "Not a VSL file: ${tmpdir}/foo" \
	"vinyllog -r ${tmpdir}/foo"

# ESI coverage in e00003.vtc
