varnishtest "VSL compound queries"

# This test case compares individual query scenarios to their
# compounded queries counterparts.

server s1 {
	rxreq
	txresp -status 500

	rxreq
	txresp -status 503
} -start

varnish v1 -vcl+backend "" -start

client c1 {
	txreq
	rxresp
	expect resp.status == 500

	txreq
	rxresp
	expect resp.status == 503
} -run

# Let's fist create a script to reduce in all the variants below.

shell {
	cat >ncsa.sh <<-EOF
	#!/bin/sh

	varnishncsa -d -n ${v1_name} -F '%s' "\$@" |
	tr '\n' ' '
	# '
	EOF
	chmod +x ncsa.sh
}

shell -match "^500 503 $" {
	# no query
	./ncsa.sh
}

shell -err -expect "Query expression error" {
	varnishncsa -d -n ${v1_name} -q '
		# empty multiline query
	'
}

shell -err -expect "Query expression error" {
	varnishncsa -d -n ${v1_name} -q '
		* ~ "Incomplete quoted
		string"
	'
}

shell -err -expect "Query expression error" {
	echo '# empty query file' >empty.vslq
	varnishncsa -d -n ${v1_name} -Q empty.vslq
}

shell -err -expect "-Q missing.vslq: No such file or directory" {
	varnishncsa -d -n ${v1_name} -Q missing.vslq
}

shell -match "^500 $" {
	# single query 1
	./ncsa.sh -q 'RespStatus == 500'
}

shell -match "^503 $" {
	# single query 2
	./ncsa.sh -q 'RespStatus == 503'
}

shell -match "^500 503 $" {
	# query 1 OR query 2
	./ncsa.sh -q '(RespStatus == 500) or (RespStatus == 503)'
}

shell -match "^500 503 $" {
	./ncsa.sh -q '
		# query 1
		RespStatus == 500

		# query 2
		RespStatus == 503
	'
}

shell -match "^500 503 $" {
	./ncsa.sh -q '
		RespStatus == 500 # query 1
		RespStatus == 503 # query 2
	'
}

shell -match "^500 503 $" {
	cat >query.vslq <<-EOF
	# query 1
	RespStatus == 500

	# query 2
	RespStatus == 503
	EOF
	./ncsa.sh -Q query.vslq
}

shell -match "^500 503 $" {
	# multiple -q options
	./ncsa.sh -q 'RespStatus == 500' -q 'RespStatus == 503'
}

shell -match "^500 503 $" {
	# multiple -Q options

	cat >query1.vslq <<-EOF
	# query 1
	RespStatus == 500
	EOF

	cat >query2.vslq <<-EOF
	# query 2
	RespStatus == 503
	EOF

	./ncsa.sh -Q query1.vslq -Q query2.vslq
}

shell -match "^500 503 $" {
	# mix -Q and -q options

	./ncsa.sh -Q query1.vslq -q 'RespStatus == 503'
}

shell -match "^500 $" {
	set -e

	tee query1_cont.vslq <<-EOF | wc -l | grep -q 4 # ensure 4 lines
	# line continuation
	RespStatus \\
	== \\
	500
	EOF

	./ncsa.sh -Q query1_cont.vslq
}

shell -err -expect "Query expression error" {
	set -e

	tee string_cont.vslq <<-EOF | wc -l | grep -q 3 # ensure 3 lines
	# quoted string continuation
	* ~ "very long \\
	string"
	EOF

	varnishncsa -d -n ${v1_name} -Q string_cont.vslq
}

# Make v1 log a multiline VSL record and query it (hardly useful)

logexpect l1 -v v1 -g raw -q {CLI ~ "\\nvcl1.s1"} {
	expect 0 0 CLI "vcl1.s1 +healthy"
} -start

varnish v1 -cliok backend.list

logexpect l1 -wait
