[master] 0109411b9 test VRT_fail from director contexts

Nils Goroll nils.goroll at uplex.de
Wed Nov 6 14:47:07 UTC 2019


commit 0109411b99e0560b99d2daf2327a9f8af03f54d8
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed May 15 13:07:40 2019 +0200

    test VRT_fail from director contexts
    
    Tests #2997

diff --git a/bin/varnishtest/tests/d00040.vtc b/bin/varnishtest/tests/d00040.vtc
new file mode 100644
index 000000000..2f16e9ed6
--- /dev/null
+++ b/bin/varnishtest/tests/d00040.vtc
@@ -0,0 +1,33 @@
+varnishtest "Test failing in director callbacks"
+
+varnish v1 -vcl {
+	import debug;
+	import std;
+
+	backend dummy { .host = "${bad_ip}"; }
+
+	sub vcl_init {
+		new d = debug.director();
+	}
+
+	sub vcl_recv {
+		if (req.url == "/healthy") {
+			if (std.healthy(d.fail())) {
+				return (synth(200));
+			} else {
+				return (synth(404));
+			}
+		}
+		set req.backend_hint = d.fail();
+	}
+} -start
+
+client c1 {
+	txreq -url "/"
+	rxresp
+	expect resp.status == 503
+
+	txreq -url "/healthy"
+	rxresp
+	expect resp.status == 503
+} -run
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index 4a40ecc9b..72b24b3ca 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -268,3 +268,9 @@ $Function IP get_ip(PRIV_TASK)
 
 Get the IP address previously stored by ``debug.store_ip()`` in the same
 transaction.
+
+$Object director()
+
+$Method BACKEND .fail()
+
+Return a backend which fails in director context
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 83b102176..d6171d677 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -922,3 +922,80 @@ const struct vmod_data Vmod_wrong3_Data = {
 };
 
 //lint -restore
+
+/*---------------------------------------------------------------------*/
+
+struct VPFX(debug_director) {
+	unsigned			magic;
+#define VMOD_DEBUG_DIRECTOR_MAGIC	0x66b9ff3d
+	VCL_BACKEND			dir;
+};
+
+/* XXX more callbacks ? */
+static vdi_healthy_f vmod_debug_director_healthy;
+static vdi_resolve_f vmod_debug_director_resolve;
+
+static const struct vdi_methods vmod_debug_director_methods[1] = {{
+	.magic =	VDI_METHODS_MAGIC,
+	.type =		"debug.director",
+	.resolve =	vmod_debug_director_resolve,
+	.healthy =	vmod_debug_director_healthy
+}};
+
+VCL_VOID v_matchproto_(td_xyzzy_debug_director__init)
+xyzzy_director__init(VRT_CTX, struct VPFX(debug_director) **dp,
+    const char *vcl_name)
+{
+	struct VPFX(debug_director) *d;
+
+	AN(dp);
+	AZ(*dp);
+	ALLOC_OBJ(d, VMOD_DEBUG_DIRECTOR_MAGIC);
+	AN(d);
+
+	*dp = d;
+	d->dir = VRT_AddDirector(ctx, vmod_debug_director_methods, d,
+	    "%s", vcl_name);
+}
+
+VCL_VOID v_matchproto_(td_xyzzy_debug_director__fini)
+xyzzy_director__fini(struct VPFX(debug_director) **dp)
+{
+	struct VPFX(debug_director) *d;
+
+	TAKE_OBJ_NOTNULL(d, dp, VMOD_DEBUG_DIRECTOR_MAGIC);
+	VRT_DelDirector(&d->dir);
+	FREE_OBJ(d);
+}
+
+VCL_BACKEND v_matchproto_(td_xyzzy_debug_director_fail)
+xyzzy_director_fail(VRT_CTX, struct VPFX(debug_director) *d)
+{
+	CHECK_OBJ_NOTNULL(d, VMOD_DEBUG_DIRECTOR_MAGIC);
+	(void) ctx;
+
+	return (d->dir);
+}
+
+static VCL_BOOL v_matchproto_(vdi_healthy_f)
+vmod_debug_director_healthy(VRT_CTX, VCL_BACKEND dir, VCL_TIME *changed)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	(void) dir;
+	(void) changed;
+
+	VRT_fail(ctx, "fail");
+	return (1);
+}
+
+static VCL_BACKEND v_matchproto_(vdi_resolve_f)
+vmod_debug_director_resolve(VRT_CTX, VCL_BACKEND dir)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+
+	(void) dir;
+
+	VRT_fail(ctx, "fail");
+	return (NULL);
+}


More information about the varnish-commit mailing list