[4.1] 0dffccf Add regexp matching support to the shell command

Federico G. Schwindt fgsch at lodoss.net
Mon Jun 12 04:49:20 CEST 2017


commit 0dffccfeac8c6f4986d7aa7008b698d81a534734
Author: Federico G. Schwindt <fgsch at lodoss.net>
Date:   Sat Jan 21 21:22:02 2017 +0000

    Add regexp matching support to the shell command
    
    Change some tests to use the new parameter and more coverage.

diff --git a/bin/varnishtest/tests/u00003.vtc b/bin/varnishtest/tests/u00003.vtc
new file mode 100644
index 0000000..6369e45
--- /dev/null
+++ b/bin/varnishtest/tests/u00003.vtc
@@ -0,0 +1,79 @@
+varnishtest "varnishncsa coverage"
+
+server s1 -repeat 2 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {} -start
+
+shell "varnishncsa -n ${v1_name} -D -P ${tmpdir}/ncsa.pid -w ${tmpdir}/ncsa.log"
+
+shell -expect "Usage: varnishncsa <options>" \
+	"varnishncsa -h"
+shell -expect "Copyright (c) 2006 Verdens Gang AS" \
+	"varnishncsa -V"
+shell -err -expect "Missing -w option" \
+	{varnishncsa -D}
+shell -err -expect "Unknown format specifier at: %{foo}A" \
+	{varnishncsa -F "%{foo}A"}
+shell -err -expect "Unknown format specifier at: %A" \
+	{varnishncsa -F "%A"}
+shell -err -expect "Unknown formatting extension: foo" \
+	{varnishncsa -F "%{foo}x"}
+shell -err -expect "Usage: varnishncsa <options>" \
+	{varnishncsa extra}
+shell -err -expect "Missing tag in VSL:" \
+	{varnishncsa -F "%{VSL:}x"}
+shell -err -expect "Unknown log tag: nonexistent" \
+	{varnishncsa -F "%{VSL:nonexistent}x"}
+shell -err -expect "Tag not unique: Req" \
+	{varnishncsa -F "%{VSL:Req}x"}
+shell -err -expect "Unknown log tag: Begin[" \
+	{varnishncsa -F "%{VSL:Begin[}x"}
+shell -err -expect "Syntax error: VSL:Begin]" \
+	{varnishncsa -F "%{VSL:Begin]}x"}
+shell -err -expect "Unknown log tag: Begin[a" \
+	{varnishncsa -F "%{VSL:Begin[a}x"}
+shell -err -expect "Syntax error: VSL:Begin[a]" \
+	{varnishncsa -F "%{VSL:Begin[a]}x"}
+shell -err -expect "Syntax error. Field specifier must be positive: Begin[0]" \
+	{varnishncsa -F "%{VSL:Begin[0]}x"}
+shell -err -expect "Field specifier 999999999999 for the tag VSL:Begin[999999999999] is probably too high" \
+	{varnishncsa -F "%{VSL:Begin[999999999999]}x"}
+shell -err -expect "Can't open format file (No such file or directory)" \
+	{varnishncsa -f /nonexistent/file}
+shell -err -expect "Empty format file" \
+	{varnishncsa -f /dev/null}
+# In Linux and SunOS, getline(3) fails when the stream refers to a
+# directory but happily works in FreeBSD.
+#shell -err -expect "Can't read format from file (Is a directory)" \
+#	{varnishncsa -f ${tmpdir}}
+
+delay 1
+
+client c1 {
+	txreq -url /foo
+	rxresp
+} -run
+
+delay 1
+
+shell "mv ${tmpdir}/ncsa.log ${tmpdir}/ncsa.old.log >/dev/null 2>&1"
+shell "kill -HUP `cat ${tmpdir}/ncsa.pid`"
+
+client c1 {
+	txreq -url /bar
+	rxresp
+} -run
+
+delay 1
+
+shell "kill `cat ${tmpdir}/ncsa.pid`"
+
+shell "grep -q /foo ${tmpdir}/ncsa.old.log"
+shell "grep -q /bar ${tmpdir}/ncsa.log"
+
+shell {echo "%{VSL:Begin}x %{Varnish:vxid}x %D %T %{Varnish:handling}x %{%Z}t" >${tmpdir}/format}
+shell -match "req 1000 rxreq 1001 [0-9]{5} 0 miss [A-Z]{3,}" \
+	{varnishncsa -n ${v1_name} -d -f ${tmpdir}/format}
diff --git a/bin/varnishtest/tests/u00006.vtc b/bin/varnishtest/tests/u00006.vtc
new file mode 100644
index 0000000..cda5d73
--- /dev/null
+++ b/bin/varnishtest/tests/u00006.vtc
@@ -0,0 +1,73 @@
+varnishtest "varnishlog coverage"
+
+server s1 -repeat 2 {
+	rxreq
+	txresp
+} -start
+
+varnish v1 -vcl+backend {} -start
+
+process p1 {
+	exec varnishlog -n ${v1_name} -g raw -k 3 -w ${tmpdir}/vlog -A
+} -start
+shell {
+	exec varnishlog -n ${v1_name} -D -P ${tmpdir}/vlog.pid \
+	    -w ${tmpdir}/vlog.bin
+}
+
+shell -expect "Usage: varnishlog <options>" \
+	"varnishlog -h"
+shell -expect "Copyright (c) 2006 Verdens Gang AS" \
+	"varnishlog -V"
+shell -err -expect "Usage: varnishlog <options>" \
+	"varnishlog extra"
+shell -err -expect "Missing -w option" \
+	"varnishlog -D"
+shell -err -expect "-L: Range error" \
+	"varnishlog -L 0"
+shell -err -expect {-i: "foo" matches zero tags} \
+	"varnishlog -i foo"
+shell -err -expect {-i: "Resp" is ambiguous} \
+	"varnishlog -i Resp"
+shell -err -expect {-I: "foo" matches zero tags} \
+	"varnishlog -I foo:bar"
+shell -err -expect {-I: "Resp" is ambiguous} \
+	"varnishlog -I Resp:bar"
+shell -err -expect {-I: Regex error at position 4 (missing ))} \
+	{varnishlog -I "(foo"}
+shell -err -expect {-x: Syntax error in "**"} \
+	{varnishlog -x "**"}
+shell -err -expect {-X: Syntax error in "**"} \
+	{varnishlog -X "**:bar"}
+
+process p1 -wait
+shell {grep -q "0 CLI" ${tmpdir}/vlog}
+shell -match "0 CLI[ ]+- Wr 200 [0-9]+ PONG" \
+	{varnishlog -n ${v1_name} -d -g raw -X "Wr 200 [0-9]+ [^P]"}
+
+client c1 {
+	txreq -url /foo
+	rxresp
+} -run
+
+delay 1
+
+shell "mv ${tmpdir}/vlog.bin ${tmpdir}/vlog.bin~ >/dev/null 2>&1"
+shell "kill -HUP `cat ${tmpdir}/vlog.pid`"
+
+client c1 {
+	txreq -url /bar
+	rxresp
+} -run
+
+delay 1
+
+shell "kill `cat ${tmpdir}/vlog.pid`"
+
+shell -match {^\*[ ]+<< Request\s+>>[ ]+1001[ ]+
+-[ ]+1001 ReqURL[ ]+c /foo
+$} {
+	varnishlog -v -r ${tmpdir}/vlog.bin~ -i ReqURL -q "RespStatus == 200"
+}
+shell -match "-[ ]+BereqURL[ ]+/bar" \
+	"varnishlog -r ${tmpdir}/vlog.bin -x ReqURL"
diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index bd97fb2..ba9a18a 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -427,8 +427,9 @@ cmd_varnishtest(CMD_ARGS)
 
 /* SECTION: shell shell
  *
- * Pass the string given as argument to a shell. If you have multiple commands
- * to run, you can use curly barces to describe a multi-lines script, eg::
+ * Pass the string given as argument to a shell. If you have multiple
+ * commands to run, you can use curly barces to describe a multi-lines
+ * script, eg::
  *
  *         shell {
  *                 echo begin
@@ -449,6 +450,7 @@ cmd_varnishtest(CMD_ARGS)
  *
  *	-expect string		- expect str to be found in stdout+err
  *
+ *	-match regexp		- expect regexp to match the stdout+err output
  * The vtc will fail if the return code of the shell is not 0.
  */
 /* SECTION: client-server.spec.shell shell
@@ -457,17 +459,26 @@ cmd_varnishtest(CMD_ARGS)
  */
 
 static void
-cmd_shell_engine(struct vtclog *vl, int ok,
-    const char *cmd, const char *expect)
+cmd_shell_engine(struct vtclog *vl, int ok, const char *cmd,
+    const char *expect, const char *re)
 {
 	struct vsb *vsb;
 	FILE *fp;
+	vre_t *vre = NULL;
+	const char *errptr;
 	int r, c;
+	int err;
 
 	AN(vl);
 	AN(cmd);
 	vsb = VSB_new_auto();
 	AN(vsb);
+	if (re != NULL) {
+		vre = VRE_compile(re, 0, &errptr, &err);
+		if (vre == NULL)
+			vtc_log(vl, 0, "shell_match invalid regexp (\"%s\")",
+			    re);
+	}
 	VSB_printf(vsb, "exec 2>&1 ; %s", cmd);
 	AZ(VSB_finish(vsb));
 	vtc_dump(vl, 4, "shell_cmd", VSB_data(vsb), -1);
@@ -499,6 +510,14 @@ cmd_shell_engine(struct vtclog *vl, int ok,
 			    "shell_expect not found: (\"%s\")", expect);
 		else
 			vtc_log(vl, 4, "shell_expect found");
+	} else if (vre != NULL) {
+		if (VRE_exec(vre, VSB_data(vsb), VSB_len(vsb), 0, 0,
+		    NULL, 0, NULL) < 1)
+			vtc_log(vl, 0,
+			    "shell_match failed: (\"%s\")", re);
+		else
+			vtc_log(vl, 4, "shell_match succeeded");
+		VRE_free(&vre);
 	}
 	VSB_destroy(&vsb);
 }
@@ -507,11 +526,13 @@ cmd_shell_engine(struct vtclog *vl, int ok,
 static void
 cmd_shell(CMD_ARGS)
 {
-	(void)priv;
-	(void)cmd;
+	const char *expect = NULL;
+	const char *re = NULL;
 	int n;
 	int ok = 0;
-	const char *expect = NULL;
+
+	(void)priv;
+	(void)cmd;
 
 	if (av == NULL)
 		return;
@@ -522,14 +543,23 @@ cmd_shell(CMD_ARGS)
 			n += 1;
 			ok = atoi(av[n]);
 		} else if (!strcmp(av[n], "-expect")) {
+			if (re != NULL)
+				vtc_log(vl, 0,
+				    "Cannot use -expect with -match");
 			n += 1;
 			expect = av[n];
+		} else if (!strcmp(av[n], "-match")) {
+			if (expect != NULL)
+				vtc_log(vl, 0,
+				    "Cannot use -match with -expect");
+			n += 1;
+			re = av[n];
 		} else {
 			break;
 		}
 	}
 	AN(av[n]);
-	cmd_shell_engine(vl, ok, av[n], expect);
+	cmd_shell_engine(vl, ok, av[n], expect, re);
 }
 
 /**********************************************************************
@@ -549,7 +579,7 @@ cmd_err_shell(CMD_ARGS)
 	AZ(av[3]);
 	vtc_log(vl, 1,
 	    "NOTICE: err_shell is deprecated, use 'shell -err -expect'");
-	cmd_shell_engine(vl, -1, av[2], av[1]);
+	cmd_shell_engine(vl, -1, av[2], av[1], NULL);
 }
 
 /**********************************************************************



More information about the varnish-commit mailing list