[master] 17a456b Harmonize name checks in varnishtest

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Mar 8 20:28:05 CET 2017


commit 17a456bdc85359e4ec0d75f1d0a285a3db517d5b
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed Mar 8 19:52:49 2017 +0100

    Harmonize name checks in varnishtest
    
    It takes the barrier as a template and provides a consistent error
    message logged with the caller's vtclog. The name is the first thing
    checked when a new named entity is created, saving useless setup.
    
    Fixes #2249

diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index 5919913..4db8e78 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -41,6 +41,15 @@
 #include "vqueue.h"
 #include "vsb.h"
 
+#define VTC_CHECK_NAME(vl, name, type, chr)				\
+	do {								\
+		AN(name);						\
+		if (*(name) != chr)					\
+			vtc_fatal(vl,					\
+			    type " name must start with '%c' (got %s)",	\
+			    chr, name);					\
+	} while (0)
+
 struct vtclog;
 struct cmds;
 struct suckaddr;
diff --git a/bin/varnishtest/vtc_barrier.c b/bin/varnishtest/vtc_barrier.c
index bafcb79..41cbf20 100644
--- a/bin/varnishtest/vtc_barrier.c
+++ b/bin/varnishtest/vtc_barrier.c
@@ -75,11 +75,9 @@ barrier_new(char *name, struct vtclog *vl)
 {
 	struct barrier *b;
 
+	VTC_CHECK_NAME(vl, name, "Barrier", 'b');
 	ALLOC_OBJ(b, BARRIER_MAGIC);
 	AN(b);
-	AN(name);
-	if (*name != 'b')
-		vtc_fatal(vl, "Barrier name must start with 'b' (%s)", name);
 	if (pthread_self() != vtc_thread)
 		vtc_fatal(vl,
 		    "Barrier %s can only be created on the top thread", name);
diff --git a/bin/varnishtest/vtc_client.c b/bin/varnishtest/vtc_client.c
index 3b7e6c8..18f5cef 100644
--- a/bin/varnishtest/vtc_client.c
+++ b/bin/varnishtest/vtc_client.c
@@ -165,18 +165,16 @@ client_thread(void *priv)
  */
 
 static struct client *
-client_new(const char *name)
+client_new(char *name, struct vtclog *vl)
 {
 	struct client *c;
 
-	AN(name);
+	VTC_CHECK_NAME(vl, name, "Client", 'c');
 	ALLOC_OBJ(c, CLIENT_MAGIC);
 	AN(c);
 	REPLACE(c->name, name);
 	c->vl = vtc_logopen(name);
 	AN(c->vl);
-	if (*c->name != 'c')
-		vtc_fatal(c->vl, "Client name must start with 'c'");
 
 	bprintf(c->connect, "%s", "${v1_sock}");
 	VTAILQ_INSERT_TAIL(&clients, c, list);
@@ -256,7 +254,6 @@ cmd_client(CMD_ARGS)
 
 	(void)priv;
 	(void)cmd;
-	(void)vl;
 
 	if (av == NULL) {
 		/* Reset and free */
@@ -276,7 +273,7 @@ cmd_client(CMD_ARGS)
 		if (!strcmp(c->name, av[0]))
 			break;
 	if (c == NULL)
-		c = client_new(av[0]);
+		c = client_new(av[0], vl);
 	av++;
 
 	for (; *av != NULL; av++) {
diff --git a/bin/varnishtest/vtc_logexp.c b/bin/varnishtest/vtc_logexp.c
index 3f205fb..df47ec4 100644
--- a/bin/varnishtest/vtc_logexp.c
+++ b/bin/varnishtest/vtc_logexp.c
@@ -199,11 +199,11 @@ logexp_delete(struct logexp *le)
 }
 
 static struct logexp *
-logexp_new(const char *name)
+logexp_new(char *name, struct vtclog *vl)
 {
 	struct logexp *le;
 
-	AN(name);
+	VTC_CHECK_NAME(vl, name, "Logexpect", 'l');
 	ALLOC_OBJ(le, LOGEXP_MAGIC);
 	AN(le);
 	REPLACE(le->name, name);
@@ -510,7 +510,6 @@ cmd_logexpect(CMD_ARGS)
 
 	(void)priv;
 	(void)cmd;
-	(void)vl;
 
 	if (av == NULL) {
 		/* Reset and free */
@@ -534,7 +533,7 @@ cmd_logexpect(CMD_ARGS)
 			break;
 	}
 	if (le == NULL)
-		le = logexp_new(av[0]);
+		le = logexp_new(av[0], vl);
 	av++;
 
 	for (; *av != NULL; av++) {
diff --git a/bin/varnishtest/vtc_process.c b/bin/varnishtest/vtc_process.c
index 3c0b73c..b9249e0 100644
--- a/bin/varnishtest/vtc_process.c
+++ b/bin/varnishtest/vtc_process.c
@@ -85,13 +85,13 @@ static VTAILQ_HEAD(, process)	processes =
 	} while (0)
 
 static struct process *
-process_new(const char *name)
+process_new(char *name, struct vtclog *vl)
 {
 	struct process *p;
 	struct vsb *vsb;
 	char buf[1024];
 
-	AN(name);
+	VTC_CHECK_NAME(vl, name, "Process", 'p');
 	ALLOC_OBJ(p, PROCESS_MAGIC);
 	AN(p);
 	REPLACE(p->name, name);
@@ -111,9 +111,6 @@ process_new(const char *name)
 	p->fd_to = -1;
 	p->fd_from = -1;
 
-	if (*p->name != 'p')
-		vtc_fatal(p->vl, "Process name must start with 'p'");
-
 	VTAILQ_INSERT_TAIL(&processes, p, list);
 	return (p);
 }
@@ -436,7 +433,6 @@ cmd_process(CMD_ARGS)
 
 	(void)priv;
 	(void)cmd;
-	(void)vl;
 
 	if (av == NULL) {
 		/* Reset and free */
@@ -463,7 +459,7 @@ cmd_process(CMD_ARGS)
 		if (!strcmp(p->name, av[0]))
 			break;
 	if (p == NULL)
-		p = process_new(av[0]);
+		p = process_new(av[0], vl);
 	av++;
 
 	for (; *av != NULL; av++) {
diff --git a/bin/varnishtest/vtc_server.c b/bin/varnishtest/vtc_server.c
index f9b01a1..18fe363 100644
--- a/bin/varnishtest/vtc_server.c
+++ b/bin/varnishtest/vtc_server.c
@@ -71,18 +71,16 @@ static VTAILQ_HEAD(, server)	servers =
  */
 
 static struct server *
-server_new(const char *name)
+server_new(char *name, struct vtclog *vl)
 {
 	struct server *s;
 
-	AN(name);
+	VTC_CHECK_NAME(vl, name, "Server", 's');
 	ALLOC_OBJ(s, SERVER_MAGIC);
 	AN(s);
 	REPLACE(s->name, name);
 	s->vl = vtc_logopen(s->name);
 	AN(s->vl);
-	if (*s->name != 's')
-		vtc_fatal(s->vl, "Server name must start with 's'");
 
 	bprintf(s->listen, "%s", "127.0.0.1 0");
 	s->repeat = 1;
@@ -240,6 +238,7 @@ server_dispatch_thread(void *priv)
 	assert(s->sock >= 0);
 
 	vl = vtc_logopen(s->name);
+	AN(vl);
 	vtc_log(vl, 2, "Dispatch started on %s", s->listen);
 
 	while (1) {
@@ -250,7 +249,7 @@ server_dispatch_thread(void *priv)
 			vtc_fatal(vl, "Accepted failed: %s", strerror(errno));
 		bprintf(snbuf, "s%d", sn++);
 		vtc_log(vl, 3, "dispatch fd %d -> %s", fd, snbuf);
-		s2 = server_new(snbuf);
+		s2 = server_new(snbuf, vl);
 		s2->spec = s->spec;
 		strcpy(s2->listen, s->listen);
 		s2->fd = fd;
@@ -336,7 +335,6 @@ cmd_server(CMD_ARGS)
 
 	(void)priv;
 	(void)cmd;
-	(void)vl;
 
 	if (av == NULL) {
 		/* Reset and free */
@@ -369,7 +367,7 @@ cmd_server(CMD_ARGS)
 			break;
 	AZ(pthread_mutex_unlock(&server_mtx));
 	if (s == NULL)
-		s = server_new(av[0]);
+		s = server_new(av[0], vl);
 	CHECK_OBJ_NOTNULL(s, SERVER_MAGIC);
 	av++;
 
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index f22e5cc..a58bf6b 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -278,13 +278,13 @@ varnishlog_thread(void *priv)
  */
 
 static struct varnish *
-varnish_new(const char *name)
+varnish_new(char *name, struct vtclog *vl)
 {
 	struct varnish *v;
 	struct vsb *vsb;
 	char buf[1024];
 
-	AN(name);
+	VTC_CHECK_NAME(vl, name, "Varnish", 'v');
 	ALLOC_OBJ(v, VARNISH_MAGIC);
 	AN(v);
 	REPLACE(v->name, name);
@@ -306,9 +306,6 @@ varnish_new(const char *name)
 	bprintf(buf, "rm -rf %s ; mkdir -p %s", v->workdir, v->workdir);
 	AZ(system(buf));
 
-	if (*v->name != 'v')
-		vtc_fatal(v->vl, "Varnish name must start with 'v'");
-
 	v->args = VSB_new_auto();
 
 	v->cli_fd = -1;
@@ -1077,7 +1074,6 @@ cmd_varnish(CMD_ARGS)
 
 	(void)priv;
 	(void)cmd;
-	(void)vl;
 
 	if (av == NULL) {
 		/* Reset and free */
@@ -1097,7 +1093,7 @@ cmd_varnish(CMD_ARGS)
 		if (!strcmp(v->name, av[0]))
 			break;
 	if (v == NULL)
-		v = varnish_new(av[0]);
+		v = varnish_new(av[0], vl);
 	av++;
 
 	for (; *av != NULL; av++) {



More information about the varnish-commit mailing list