varnishtest "VCL: check panic and restart"

barrier b1 cond 2
barrier b2 cond 2

server s1 {
	rxreq
	txresp -hdr "Foo: bar" -body "abcdef\n"
	rxreq
	txresp -hdr "Panic: fetch" -body "012345\n"
	close

	barrier b1 sync
	accept
	rxreq
	txresp -hdr "Foo: bar" -body "abcdef\n"
	rxreq
	txresp -hdr "Panic: deliver" -body "012345\n"
	close

	barrier b2 sync
	accept
	rxreq
	txresp -hdr "Foo: foo" -body "abcdef\n"
} -start

varnish v1 -arg "-sdefault,1m"
varnish v1 -cliok "param.set feature +no_coredump"

varnish v1 -vcl+backend {
	import vtc;
	import debug;

	sub vcl_init {
		new foo = debug.obj("foo");
		new bar = debug.concat("bar");
	}

	sub vcl_backend_response {
		if (beresp.http.panic == "fetch") {
			vtc.panic("Had Panic header: " + beresp.http.panic);
		}
	}
	sub vcl_deliver {
		if (resp.http.panic == "deliver") {
			vtc.panic("Had Panic header: " + resp.http.panic);
		}
	}
} -start

varnish v1 -cliok "stop"
varnish v1 -cliok "start"
varnish v1 -wait-running
varnish v1 -expect MGT.child_panic == 0

client c1 {
	txreq -url "/"
	rxresp
	txreq -url "/foo"
	# Don't expect answer, the server crashed.
} -run

varnish v1 -wait-stopped
varnish v1 -cliok "panic.show"
varnish v1 -clijson "panic.show -j"
varnish v1 -cliok "panic.clear"
varnish v1 -expect MGT.child_panic == 1
varnish v1 -clierr 300 "panic.clear"
varnish v1 -cliok "start"
varnish v1 -wait-running
barrier b1 sync

client c1 {
	txreq -url "/"
	rxresp
	txreq -url "/foo"
	# Don't expect answer, the server crashed.
} -run

varnish v1 -wait-stopped
varnish v1 -cliok "panic.show"
varnish v1 -clijson "panic.show -j"
varnish v1 -cliok "panic.clear -z"
varnish v1 -expect MGT.child_panic == 0
varnish v1 -clierr 300 "panic.clear"
varnish v1 -cliok "start"
varnish v1 -wait-running
barrier b2 sync

client c1 {
	txreq -url "/"
	rxresp
	expect resp.http.foo == "foo"
} -run

varnish v1 -cliok "panic.clear -z"
varnish v1 -expectexit 0x40
