r2666 - in trunk/varnish-cache/bin: . varnishtest

phk at projects.linpro.no phk at projects.linpro.no
Sun Jun 15 11:50:13 CEST 2008


Author: phk
Date: 2008-06-15 11:50:13 +0200 (Sun, 15 Jun 2008)
New Revision: 2666

Added:
   trunk/varnish-cache/bin/varnishtest/
   trunk/varnish-cache/bin/varnishtest/Makefile
   trunk/varnish-cache/bin/varnishtest/t000.vtc
   trunk/varnish-cache/bin/varnishtest/vtc.c
   trunk/varnish-cache/bin/varnishtest/vtc.h
   trunk/varnish-cache/bin/varnishtest/vtc_server.c
Log:
Add Skelton "varnishtest" program.

This is a test-driver which will be able to drive low-level tests of
varnish functionalty.

The present code manages to parse the example testcase description,
more work to follow.

XXX: needs to be auto*'ed, right now it uses FreeBSD makefile syntax.



Added: trunk/varnish-cache/bin/varnishtest/Makefile
===================================================================
--- trunk/varnish-cache/bin/varnishtest/Makefile	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/Makefile	2008-06-15 09:50:13 UTC (rev 2666)
@@ -0,0 +1,15 @@
+PROG		=	vtc
+
+SRCS		+=	vtc.c
+SRCS		+=	vtc_server.c
+
+CFLAGS		+= 	-g
+
+NO_MAN		= sorry
+
+WARNS		?=	6
+
+.include <bsd.prog.mk>
+
+test:		${PROG}
+		./${PROG} ${.CURDIR}/t000.vtc

Added: trunk/varnish-cache/bin/varnishtest/t000.vtc
===================================================================
--- trunk/varnish-cache/bin/varnishtest/t000.vtc	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/t000.vtc	2008-06-15 09:50:13 UTC (rev 2666)
@@ -0,0 +1,75 @@
+# Test that we get anything through at all
+#
+
+server s1 -repeat 3 {
+	rxreq
+	expect url == "/"
+	txresponse -body "0123456789"
+}
+
+server s1 -start 
+
+client c1 {
+	txreq -url "/"
+	rxresponse
+	expect status == 200
+	expect length == 10
+}
+
+
+#######################################################################
+# Test trivial pipe mode
+
+vcl {
+	$s1;
+	sub vcl_recv {
+		set req.backend = s1;
+		pipe;
+	}
+}
+
+client c1 -run
+
+#######################################################################
+# Test trivial pass mode
+
+vcl {
+	$s1;
+	sub vcl_recv {
+		set req.backend = s1;
+		pass;
+	}
+}
+
+client c1 -run
+
+#######################################################################
+# Test trivial cache mode
+
+vcl {
+	$s1;
+	sub vcl_recv {
+		set req.backend = s1;
+	}
+}
+
+client c1 -run
+
+server s1 -wait
+
+#######################################################################
+# And see that it stuck in cache
+
+client c1 -run
+
+varnish stop
+
+stats {
+	expect client_conn == 4
+	expect client_req == 4
+	expect cache_hit == 1
+	expect cache_miss == 1
+	expect s_pipe == 1
+	expect s_pass == 1
+	expect s_fetch == 2
+}

Added: trunk/varnish-cache/bin/varnishtest/vtc.c
===================================================================
--- trunk/varnish-cache/bin/varnishtest/vtc.c	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/vtc.c	2008-06-15 09:50:13 UTC (rev 2666)
@@ -0,0 +1,170 @@
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <err.h>
+
+#include "vtc.h"
+
+#define		MAX_FILESIZE		(1024 * 1024)
+#define		MAX_TOKENS		20
+
+/**********************************************************************
+ * Read a file into memory
+ */
+
+static char *
+read_file(const char *fn)
+{
+	char *buf;
+	ssize_t sz = MAX_FILESIZE;
+	ssize_t s;
+	int fd;
+
+	fd = open(fn, O_RDONLY);
+	if (fd < 0)
+		err(1, "Cannot open %s", fn);
+	buf = malloc(sz);
+	assert(buf != NULL);
+	s = read(fd, buf, sz);
+	if (s <= 0) 
+		err(1, "Cannot read %s", fn);
+	assert(s < sz);		/* XXX: increase MAX_FILESIZE */
+	close (fd);
+	buf[s] = '\0';
+	buf = realloc(buf, s + 1);
+	assert(buf != NULL);
+	return (buf);
+}
+
+/**********************************************************************
+ * Execute a file
+ */
+
+void
+parse_string(char *buf, const struct cmds *cmd, void *priv)
+{
+	char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
+	char *p;
+	int nest_brace;
+	int tn;
+	const struct cmds *cp;
+
+	assert(buf != NULL);
+	for (p = buf; *p != '\0'; p++) {
+		/* Start of line */
+		if (isspace(*p))
+			continue;
+		if (*p == '#') {
+			for (; *p != '\0' && *p != '\n'; p++)
+				;
+			if (*p == '\0')
+				break;
+			continue;
+		}
+
+		/* First content on line, collect tokens */
+		tn = 0;
+		while (*p != '\0') {
+			assert(tn < MAX_TOKENS);
+			if (*p == '\n') { /* End on NL */
+				break;
+			} else if (isspace(*p)) { /* Inter-token whitespace */
+				p++;
+			} else if (*p == '{') { /* Braces */
+				nest_brace = 0;
+				token_s[tn] = p + 1;
+				for (; *p != '\0'; p++) {
+					if (*p == '{')
+						nest_brace++;
+					else if (*p == '}') {
+						if (--nest_brace == 0)
+							break;
+					}
+				}
+				assert(*p == '}');
+				token_e[tn++] = p;
+				p++;	/* Swallow closing brace */
+			} else { /* other tokens */
+				token_s[tn] = p;
+				for (; *p != '\0' && !isspace(*p); p++)
+					;
+				token_e[tn++] = p;
+			}
+		}
+		assert(tn < MAX_TOKENS);
+		token_s[tn] = NULL;
+		for (tn = 0; token_s[tn] != NULL; tn++)
+			*token_e[tn] = '\0';
+
+		for (cp = cmd; cp->name != NULL; cp++)
+			if (!strcmp(token_s[0], cp->name))
+				break;
+		if (cp->name == NULL) {
+			for (tn = 0; token_s[tn] != NULL; tn++)
+				fprintf(stderr, "%s ", token_s[tn]);
+			fprintf(stderr, "\n");
+			errx(1, "Unknown command: \"%s\"", token_s[0]);
+		}
+	
+		assert(cp->cmd != NULL);
+		cp->cmd(token_s, priv);
+	}
+}
+
+/**********************************************************************
+ * Execute a file
+ */
+
+static void
+cmd_bogo(char **av, void *priv)
+{
+	printf("cmd_bogo(%p)\n", priv);
+	while (*av)
+		printf("\t<%s>\n", *av++);
+}
+
+static struct cmds cmds[] = {
+	{ "server", 	cmd_server },
+	{ "client", 	cmd_bogo },
+	{ "vcl", 	cmd_bogo },
+	{ "stats", 	cmd_bogo },
+	{ "varnish", 	cmd_bogo },
+	{ NULL, 	NULL }
+};
+
+static void
+exec_file(const char *fn)
+{
+	char *buf;
+
+	buf = read_file(fn);
+	parse_string(buf, cmds, NULL);
+}
+
+/**********************************************************************
+ * Main 
+ */
+
+int
+main(int argc, char **argv)
+{
+	int ch;
+
+	setbuf(stdout, NULL);
+	while ((ch = getopt(argc, argv, "")) != -1) {
+		switch (ch) {
+		case '?':
+		default:
+			errx(1, "Usage");
+		}
+	}
+	argc -= optind;
+	argv += optind;
+	for (ch = 0; ch < argc; ch++)
+		exec_file(argv[ch]);
+	return (0);
+}

Added: trunk/varnish-cache/bin/varnishtest/vtc.h
===================================================================
--- trunk/varnish-cache/bin/varnishtest/vtc.h	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/vtc.h	2008-06-15 09:50:13 UTC (rev 2666)
@@ -0,0 +1,15 @@
+
+
+
+typedef void cmd_f(char **av, void *priv);
+
+struct cmds {
+	const char	*name;
+	cmd_f		*cmd;
+};
+
+void parse_string(char *buf, const struct cmds *cmd, void *priv);
+
+/* vtc_server.c */
+void cmd_server(char **av, void *priv);
+

Added: trunk/varnish-cache/bin/varnishtest/vtc_server.c
===================================================================
--- trunk/varnish-cache/bin/varnishtest/vtc_server.c	                        (rev 0)
+++ trunk/varnish-cache/bin/varnishtest/vtc_server.c	2008-06-15 09:50:13 UTC (rev 2666)
@@ -0,0 +1,13 @@
+
+#include <stdio.h>
+
+#include "vtc.h"
+
+void
+cmd_server(char **av, void *priv)
+{
+
+	printf("cmd_server(%p)\n", priv);
+	while (*av)
+		printf("\t<%s>\n", *av++);
+}




More information about the varnish-commit mailing list