varnishtest "Test VCL labels"

server s1 {
	rxreq
	txresp
} -start

varnish v1 -vcl+backend {}

# VCL name must be C-names
varnish v1 -clierr 106 {vcl.inline 0000 "vcl 4.0; backend b { .host = \"localhost\";} "}
varnish v1 -clierr 106 {vcl.inline a00/ "vcl 4.0; backend b { .host = \"localhost\";} "}
varnish v1 -clierr 106 {vcl.inline a00å "vcl 4.0; backend b { .host = \"localhost\";} "}

varnish v1 -vcl+backend {
	sub vcl_recv {
		return (synth(400));
	}
}

varnish v1 -start

client c1 {
	txreq
	rxresp
	expect resp.status == 400
} -run

varnish v1 -cliok "vcl.use vcl1"

client c1 {
	txreq
	rxresp
	expect resp.status == 200
} -run

varnish v1 -cliok "vcl.list"
varnish v1 -clierr 106 "vcl.label foo vcl0"
varnish v1 -cliok "vcl.label foo vcl2"
varnish v1 -cliok "vcl.label bar vcl2"
varnish v1 -cliok "vcl.list"
varnish v1 -clijson "vcl.list -j"
varnish v1 -cliok "vcl.show foo"
varnish v1 -cliok "vcl.show -v bar"
varnish v1 -clierr 300 "vcl.discard vcl2"
varnish v1 -cliok "vcl.discard bar"
varnish v1 -cliok "vcl.label foo vcl1"
varnish v1 -clierr 106 "vcl.label vcl1 vcl2"
varnish v1 -clierr 106 "vcl.state foo cold"
varnish v1 -clierr 300 "vcl.label bar foo"
varnish v1 -clierr 300 "vcl.discard vcl1"
varnish v1 -cliok "vcl.list"
varnish v1 -cliok "vcl.use foo"
varnish v1 -clierr 300 "vcl.discard foo"
varnish v1 -cliok "vcl.list"

client c1 -run

varnish v1 -cliok "vcl.label foo vcl2"

client c1 {
	txreq
	rxresp
	expect resp.status == 400
} -run

varnish v1 -cliok "vcl.use vcl1"
varnish v1 -cliok "vcl.list"

client c1 {
	txreq
	rxresp
	expect resp.status == 200
} -run

varnish v1 -cliok "vcl.discard foo"
varnish v1 -clierr 106 "vcl.discard foo"

varnish v1 -stop
varnish v1 -cliok "vcl.list"
varnish v1 -clierr 106 "vcl.label fo- vcl0"
varnish v1 -cliok "vcl.label fo- vcl1"
varnish v1 -clierr 300 "vcl.label bar fo-"
varnish v1 -clierr 200 "vcl.state vcl1 warm"
varnish v1 -clierr 200 "vcl.state vcl1 auto"
varnish v1 -clierr 300 "vcl.state vcl1 cold"
varnish v1 -clierr 300 "vcl.discard vcl1"
varnish v1 -cliok "vcl.list"
varnish v1 -cliok "vcl.use fo-"
varnish v1 -clierr 300 "vcl.discard fo-"
varnish v1 -cliok "vcl.list"

server s1 -start

varnish v1 -start
client c1 -run
varnish v1 -stop
varnish v1 -cliok "vcl.use vcl1"
varnish v1 -cliok "vcl.discard fo-"
varnish v1 -clierr 106 "vcl.discard fo-"

varnish v1 -cliok "vcl.label label1 vcl1"
varnish v1 -cliok "vcl.label label2 vcl1"
varnish v1 -cliok "vcl.label label3 vcl1"
varnish v1 -cliok "vcl.list"
varnish v1 -clijson "vcl.list -j"

varnish v1 -start
varnish v1 -cliok "vcl.list"
varnish v1 -cliok "vcl.label slartibartfast vcl1"
server s1 -start
client c1 -run

# Test loop detection
#######################################################################

varnish v1 -cliok vcl.list
varnish v1 -clijson "vcl.list -j"

varnish v1 -vcl+backend { }

varnish v1 -cliok "vcl.label lblA vcl3"

varnish v1 -vcl+backend { sub vcl_recv { return (vcl(lblA)); } }

varnish v1 -cliok "vcl.label lblB vcl4"

varnish v1 -vcl+backend { sub vcl_recv { return (vcl(lblB)); } }

varnish v1 -clierr 106 "vcl.label lblA vcl5"
varnish v1 -cliexpect \
	{would create a loop} \
	{vcl.label lblA vcl5}

# omitting ^vcl1 line to only get aligned fields
varnish v1 -cliexpect {
vcl2
label1           vcl1
label2           vcl1
label3           vcl1
slartibartfast   vcl1
vcl3
lblA             vcl3
vcl4             lblA
lblB             vcl4
vcl5             lblB
} vcl.deps
