From: phk Date: Sun, 15 Jun 2008 09:50:13 +0000 (+0000) Subject: Add Skelton "varnishtest" program. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=460c71ad5ccadc756145de40d0d534cbd3aa91db;p=varnish 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. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2666 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishtest/Makefile b/varnish-cache/bin/varnishtest/Makefile new file mode 100644 index 00000000..cf8b40dd --- /dev/null +++ b/varnish-cache/bin/varnishtest/Makefile @@ -0,0 +1,15 @@ +PROG = vtc + +SRCS += vtc.c +SRCS += vtc_server.c + +CFLAGS += -g + +NO_MAN = sorry + +WARNS ?= 6 + +.include + +test: ${PROG} + ./${PROG} ${.CURDIR}/t000.vtc diff --git a/varnish-cache/bin/varnishtest/t000.vtc b/varnish-cache/bin/varnishtest/t000.vtc new file mode 100644 index 00000000..3bf14e7c --- /dev/null +++ b/varnish-cache/bin/varnishtest/t000.vtc @@ -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 +} diff --git a/varnish-cache/bin/varnishtest/vtc.c b/varnish-cache/bin/varnishtest/vtc.c new file mode 100644 index 00000000..ae9ae16e --- /dev/null +++ b/varnish-cache/bin/varnishtest/vtc.c @@ -0,0 +1,170 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#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); +} diff --git a/varnish-cache/bin/varnishtest/vtc.h b/varnish-cache/bin/varnishtest/vtc.h new file mode 100644 index 00000000..b73f2d5d --- /dev/null +++ b/varnish-cache/bin/varnishtest/vtc.h @@ -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); + diff --git a/varnish-cache/bin/varnishtest/vtc_server.c b/varnish-cache/bin/varnishtest/vtc_server.c new file mode 100644 index 00000000..b2a5c46b --- /dev/null +++ b/varnish-cache/bin/varnishtest/vtc_server.c @@ -0,0 +1,13 @@ + +#include + +#include "vtc.h" + +void +cmd_server(char **av, void *priv) +{ + + printf("cmd_server(%p)\n", priv); + while (*av) + printf("\t<%s>\n", *av++); +}