vtest "Test VCL labels"

server s1 {
	rxreq
	txresp
} -start

vinyl v1 -vcl+backend {}

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

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

vinyl v1 -start

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

vinyl v1 -cliok "vcl.use vcl1"

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

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

client c1 -run

vinyl v1 -cliok "vcl.label foo vcl2"

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

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

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

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

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

server s1 -start

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

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

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

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

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

vinyl v1 -vcl+backend { }

vinyl v1 -cliok "vcl.label lblA vcl3"

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

vinyl v1 -cliok "vcl.label lblB vcl4"

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

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

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