[PATCH 5/5] Test VCL and vmod init failures

Martin Blix Grydeland martin at varnish-software.com
Wed Jun 10 11:06:02 CEST 2015


---
 bin/varnishtest/tests/m00022.vtc | 55 ++++++++++++++++++++++++++++++++++++++++
 lib/libvmod_debug/vmod.vcc       |  4 +++
 lib/libvmod_debug/vmod_debug.c   | 16 ++++++++++++
 3 files changed, 75 insertions(+)
 create mode 100644 bin/varnishtest/tests/m00022.vtc

diff --git a/bin/varnishtest/tests/m00022.vtc b/bin/varnishtest/tests/m00022.vtc
new file mode 100644
index 0000000..0fd318d
--- /dev/null
+++ b/bin/varnishtest/tests/m00022.vtc
@@ -0,0 +1,55 @@
+varnishtest "VCL initialization failures from VCL/vmod"
+
+server s1 {
+} -start
+
+# Doing debug.failinit(1) will cause the debug vmod to return failure
+# from it's init routine in subsequent initializations. Start varnish
+# with a VCL that lets us set failinit later.
+varnish v1 -vcl {
+	backend default {
+		.host = "${s1_addr}";
+		.port = "${s1_port}";
+	}
+
+	import ${vmod_debug};
+
+	sub vcl_recv {
+		if (req.url == "/failinit") {
+			debug.set_failinit(1);
+			return (synth (200, "Failing"));
+		} else {
+			return (synth (404));
+		}
+	}
+} -start
+
+# Check that we can fail the VCL load in vcl_init
+varnish v1 -errvcl "vcl_init{} failed" {
+	backend default {
+		.host = "${s1_addr}";
+		.port = "${s1_port}";
+	}
+
+	sub vcl_init {
+		return (fail);
+	}
+}
+
+# Make vmod_debug fail on next vmod init
+client c1 {
+	txreq -url /failinit
+	rxresp
+	expect resp.status == 200
+	expect resp.msg == "Failing"
+} -run
+
+# Check that init failures from vmods are caught
+varnish v1 -errvcl {failinit=1} {
+	backend default {
+		.host = "${s1_addr}";
+		.port = "${s1_port}";
+	}
+
+	import ${vmod_debug};
+}
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 434ee78..cbd89a8 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -39,6 +39,10 @@ $Function VOID panic(STRING_LIST)
 
 Don't.
 
+$Function VOID set_failinit(INT)
+
+If set non-zero, the vmod initialization function will return failure.
+
 $Function STRING author(ENUM { phk, des, kristian, mithrandir } person="phk")
 
 Test function for ENUM arguments
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 38a8f68..519f0cd 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -43,6 +43,8 @@ struct priv_vcl {
 	uintptr_t		exp_cb;
 };
 
+static int failinit = 0;
+
 VCL_VOID __match_proto__(td_debug_panic)
 vmod_panic(VRT_CTX, const char *str, ...)
 {
@@ -56,6 +58,14 @@ vmod_panic(VRT_CTX, const char *str, ...)
 	VAS_Fail("VCL", "", 0, b, VAS_VCL);
 }
 
+VCL_VOID __match_proto__(td_debug_set_initfail)
+vmod_set_failinit(VRT_CTX, VCL_INT val)
+{
+
+	(void)ctx;
+	failinit = val;
+}
+
 VCL_STRING __match_proto__(td_debug_author)
 vmod_author(VRT_CTX, VCL_ENUM id)
 {
@@ -249,5 +259,11 @@ init_function(VRT_CTX, struct vmod_priv *priv)
 	AN(priv_vcl->foo);
 	priv->priv = priv_vcl;
 	priv->free = priv_vcl_free;
+
+	if (failinit) {
+		VRT_cliout(ctx, "vmod_debug:init:failinit=%u:", failinit);
+		return (1);
+	}
+
 	return (0);
 }
-- 
2.1.4




More information about the varnish-dev mailing list