vtest "Test VCL failures 2/2"

# part 1 is in v00051.vtc

#######################################################################
# Fail in vcl_backend_fetch

vinyl v1 -vcl {
	backend proforma none;

	import debug;
	sub vcl_backend_fetch {
		debug.fail();
		set bereq.http.not = "Should not happen";
	}
} -start

logexpect l1 -v v1 -g raw {
	expect * 1002	VCL_call	"BACKEND_FETCH"
	expect 0    =	VCL_Error	"Forced failure"
	expect 0    =	VCL_return	"fail"
} -start

client c1 {
	txreq -url /backend_fetch
	rxresp
	expect resp.status == 503
	expect resp.reason == "Service Unavailable"
} -run

vinyl v1 -expect vcl_fail == 1
vinyl v1 -expect sc_vcl_failure == 0

logexpect l1 -wait

#######################################################################
# Fail in vcl_backend_error

server s1 {
	rxreq
	expect req.url == /backend_error
} -start

vinyl v1 -vcl+backend {
	import debug;
	sub vcl_backend_error {
		debug.fail();
		set bereq.http.not = "Should not happen";
	}
}

logexpect l1 -v v1 -g raw {
	expect * 1005	VCL_call	"BACKEND_ERROR"
	expect 0    =	VCL_Error	"Forced failure"
	expect 0    =	VCL_return	"fail"
} -start

client c1 {
	txreq -url /backend_error
	rxresp
	expect resp.status == 503
	expect resp.reason == "Service Unavailable"
} -run

vinyl v1 -expect vcl_fail == 2
vinyl v1 -expect sc_vcl_failure == 0

logexpect l1 -wait

#######################################################################
# Fail in vcl_backend_response

server s1 {
	rxreq
	expect req.url == /backend_response
	txresp
} -start

vinyl v1 -vcl+backend {
	import debug;
	sub vcl_backend_response {
		debug.fail();
		set bereq.http.not = "Should not happen";
	}
}

logexpect l1 -v v1 -g raw {
	expect * 1008	VCL_call	"BACKEND_RESPONSE"
	expect 0    =	VCL_Error	"Forced failure"
	expect 0    =	VCL_return	"fail"
} -start

client c1 {
	txreq -url /backend_response
	rxresp
	expect resp.status == 503
	expect resp.reason == "Service Unavailable"
} -run

vinyl v1 -expect vcl_fail == 3
vinyl v1 -expect sc_vcl_failure == 0

logexpect l1 -wait

#######################################################################
# Fail in vmod call used in an if test

vinyl v1 -vcl+backend {
	import debug;
	sub vcl_recv {
		if (debug.fail2()) {
			return (hash);
		}
	}
}

logexpect l1 -v v1 -g raw {
	expect * 1010	VCL_call	"RECV"
	expect 0    =	VCL_Error	"Forced failure"
	expect 0    =	VCL_return	"fail"
} -start

client c1 {
	txreq
	rxresp
	expect resp.status == 503
	expect resp.reason == "VCL failed"
} -run

vinyl v1 -expect vcl_fail == 4
vinyl v1 -expect sc_vcl_failure == 1

logexpect l1 -wait

#######################################################################
# Fail in vcl_init with direct VRT_fail()

vinyl v1 -errvcl {Forced failure} {
	import debug;
	backend proforma none;
	sub vcl_init {
		debug.fail();
	}
}

#######################################################################
# Fail in vcl_init via fini callback

vinyl v1 -errvcl {thou shalt not fini} {
	import debug;
	backend proforma none;
	sub vcl_init {
		debug.fail_task_fini();
	}
}

#######################################################################
# Fail in vcl_fini with direct VRT_fail()

vinyl v1 -vcl {
	import debug;
	backend proforma none;
	sub vcl_fini {
		debug.fail();
	}
}

vinyl v1 -vcl+backend {
}

logexpect l1 -v v1 -g raw {
	expect * 0	CLI		"^Rd vcl.state vcl7 0cold"
	expect 3 0	VCL_Error	"^Forced failure"
	expect 0 0	VCL_Error	{^\QVRT_fail() from vcl_fini{} has no effect\E$}
} -start

vinyl v1 -cliok "vcl.discard vcl7"
logexpect l1 -wait

#######################################################################
# Fail in vcl_fini via fini callback - ignored but logged as VMOD BUG

vinyl v1 -vcl {
	import debug;
	backend proforma none;
	sub vcl_fini {
		debug.fail_task_fini();
	}
}

vinyl v1 -vcl+backend {
}

logexpect l1 -v v1 -g raw {
	expect * 0	CLI		"^Rd vcl.state vcl9 0cold"
	expect 3 0	VCL_Error	"^thou shalt not fini"
	expect 0 0	VCL_Error	{^\QVRT_fail() from vcl_fini{} has no effect\E$}
} -start

vinyl v1 -cliok "vcl.discard vcl9"
logexpect l1 -wait

#######################################################################
# Fail vcl - debug.client_ip function restricted to client and backend

vinyl v1 -errvcl {Not available in subroutine 'vcl_init'} {
	import debug;

	sub vcl_init {
		debug.client_ip();
    }
}

#######################################################################
# Fail vcl - obj.test_priv_top method restricted to client

vinyl v1 -errvcl {Not available in subroutine 'vcl_backend_response'} {
	import debug;

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

	sub vcl_backend_response {
		set beresp.http.foo = test_obj.test_priv_top();
	}
}

