[master] 2f5ac24 Add regexp matching support to the shell command

Federico G. Schwindt fgsch at lodoss.net
Sat Jan 21 22:36:05 CET 2017


commit 2f5ac240ab3cf82d292f17d4b4b009f713c32929
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
index bb47954..6369e45 100644
--- a/bin/varnishtest/tests/u00003.vtc
+++ b/bin/varnishtest/tests/u00003.vtc
@@ -9,6 +9,10 @@ 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" \
@@ -71,5 +75,5 @@ 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 -expect "0 miss" \
+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
index 8b2861a..cda5d73 100644
--- a/bin/varnishtest/tests/u00006.vtc
+++ b/bin/varnishtest/tests/u00006.vtc
@@ -42,7 +42,8 @@ shell -err -expect {-X: Syntax error in "**"} \
 
 process p1 -wait
 shell {grep -q "0 CLI" ${tmpdir}/vlog}
-shell -expect "0 CLI" "varnishlog -n ${v1_name} -d -g raw"
+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
@@ -63,7 +64,10 @@ delay 1
 
 shell "kill `cat ${tmpdir}/vlog.pid`"
 
-shell -expect "/foo" {
-	varnishlog -r ${tmpdir}/vlog.bin~ -i ReqURL -q "RespStatus == 200"
+shell -match {^\*[ ]+<< Request\s+>>[ ]+1001[ ]+
+-[ ]+1001 ReqURL[ ]+c /foo
+$} {
+	varnishlog -v -r ${tmpdir}/vlog.bin~ -i ReqURL -q "RespStatus == 200"
 }
-shell -expect "/bar" "varnishlog -r ${tmpdir}/vlog.bin -x ReqURL"
+shell -match "-[ ]+BereqURL[ ]+/bar" \
+	"varnishlog -r ${tmpdir}/vlog.bin -x ReqURL"
diff --git a/bin/varnishtest/vtc.c b/bin/varnishtest/vtc.c
index a5d51ac..597e0c3 100644
--- a/bin/varnishtest/vtc.c
+++ b/bin/varnishtest/vtc.c
@@ -430,8 +430,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
@@ -452,8 +453,11 @@ cmd_varnishtest(CMD_ARGS)
  * \-exit N
  * 	Expect exit code N instead of zero.
  *
- * \-expect string
- * 	Expect str to be found in stdout+err.
+ * \-expect STRING
+ * 	Expect string to be found in stdout+err.
+ *
+ * \-match REGEXP
+ * 	Expect regexp to match the stdout+err output.
  */
 /* SECTION: client-server.spec.shell shell
  *
@@ -461,17 +465,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_fatal(vl, "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);
@@ -503,6 +516,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_fatal(vl,
+			    "shell_match failed: (\"%s\")", re);
+		else
+			vtc_log(vl, 4, "shell_match succeeded");
+		VRE_free(&vre);
 	}
 	VSB_destroy(&vsb);
 }
@@ -511,11 +532,13 @@ cmd_shell_engine(struct vtclog *vl, int ok,
 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;
@@ -526,14 +549,23 @@ cmd_shell(CMD_ARGS)
 			n += 1;
 			ok = atoi(av[n]);
 		} else if (!strcmp(av[n], "-expect")) {
+			if (re != NULL)
+				vtc_fatal(vl,
+				    "Cannot use -expect with -match");
 			n += 1;
 			expect = av[n];
+		} else if (!strcmp(av[n], "-match")) {
+			if (expect != NULL)
+				vtc_fatal(vl,
+				    "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);
 }
 
 /* SECTION: err_shell err_shell
@@ -560,7 +592,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);
 }
 
 /* SECTION: delay delay



More information about the varnish-commit mailing list