varnish-cache/bin/varnishtest/vtc_varnish.c
0
/*-
1
 * Copyright (c) 2008-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#ifdef VTEST_WITH_VTC_VARNISH
31
32
#include "config.h"
33
34
#include <sys/types.h>
35
#include <sys/socket.h>
36
37
#include <fcntl.h>
38
#include <fnmatch.h>
39
#include <inttypes.h>
40
#include <poll.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <unistd.h>
45
46
#include "vtc.h"
47
48
#include "vapi/vsc.h"
49
#include "vapi/vsl.h"
50
#include "vapi/vsm.h"
51
#include "vcli.h"
52
#include "vjsn.h"
53
#include "vre.h"
54
#include "vsub.h"
55
#include "vtcp.h"
56
#include "vtim.h"
57
58
struct varnish {
59
        unsigned                magic;
60
#define VARNISH_MAGIC           0x208cd8e3
61
        char                    *name;
62
        struct vtclog           *vl;
63
        VTAILQ_ENTRY(varnish)   list;
64
65
        struct vsb              *args;
66
        int                     fds[4];
67
        pid_t                   pid;
68
69
        double                  syntax;
70
71
        pthread_t               tp;
72
        pthread_t               tp_vsl;
73
74
        int                     expect_exit;
75
76
        int                     cli_fd;
77
        int                     vcl_nbr;
78
        char                    *workdir;
79
        char                    *jail;
80
        char                    *proto;
81
82
        struct vsm              *vsm_vsl;
83
        struct vsm              *vsm_vsc;
84
        struct vsc              *vsc;
85
        int                     has_a_arg;
86
87
        unsigned                vsl_tag_count[256];
88
89
        volatile int            vsl_rec;
90
        volatile int            vsl_idle;
91
};
92
93
#define NONSENSE        "%XJEIFLH|)Xspa8P"
94
95
static VTAILQ_HEAD(, varnish)   varnishes =
96
    VTAILQ_HEAD_INITIALIZER(varnishes);
97
98
/**********************************************************************
99
 * Ask a question over CLI
100
 */
101
102
static enum VCLI_status_e
103 341300
varnish_ask_cli(const struct varnish *v, const char *cmd, char **repl)
104
{
105
        int i;
106
        unsigned retval;
107
        char *r;
108
109 341300
        if (cmd != NULL) {
110 318675
                vtc_dump(v->vl, 4, "CLI TX", cmd, -1);
111 318675
                i = write(v->cli_fd, cmd, strlen(cmd));
112 318675
                if (i != strlen(cmd) && !vtc_stop)
113 0
                        vtc_fatal(v->vl, "CLI write failed (%s) = %u %s",
114 0
                            cmd, errno, strerror(errno));
115 0
                i = write(v->cli_fd, "\n", 1);
116 0
                if (i != 1 && !vtc_stop)
117 0
                        vtc_fatal(v->vl, "CLI write failed (%s) = %u %s",
118 0
                            cmd, errno, strerror(errno));
119 318675
        }
120 637350
        i = VCLI_ReadResult(v->cli_fd, &retval, &r, vtc_maxdur);
121 637350
        if (i != 0 && !vtc_stop)
122 0
                vtc_fatal(v->vl, "CLI failed (%s) = %d %u %s",
123 0
                    cmd != NULL ? cmd : "NULL", i, retval, r);
124 341300
        vtc_log(v->vl, 3, "CLI RX  %u", retval);
125 341300
        vtc_dump(v->vl, 4, "CLI RX", r, -1);
126 341300
        if (repl != NULL)
127 218300
                *repl = r;
128
        else
129 123000
                free(r);
130 341300
        return ((enum VCLI_status_e)retval);
131
}
132
133
/**********************************************************************
134
 *
135
 */
136
137
static void
138 23675
wait_stopped(const struct varnish *v)
139
{
140 23675
        char *r = NULL;
141
        enum VCLI_status_e st;
142
143 23675
        vtc_log(v->vl, 3, "wait-stopped");
144 23675
        while (1) {
145 23675
                st = varnish_ask_cli(v, "status", &r);
146 23675
                if (st != CLIS_OK)
147 0
                        vtc_fatal(v->vl,
148 0
                            "CLI status command failed: %u %s", st, r);
149 23675
                if (!strcmp(r, "Child in state stopped")) {
150 23675
                        free(r);
151 23675
                        break;
152
                }
153 0
                free(r);
154 0
                r = NULL;
155 0
                (void)usleep(200000);
156
        }
157 23675
}
158
/**********************************************************************
159
 *
160
 */
161
162
static void
163 22275
wait_running(const struct varnish *v)
164
{
165 22275
        char *r = NULL;
166
        enum VCLI_status_e st;
167
168 22275
        while (1) {
169 22275
                vtc_log(v->vl, 3, "wait-running");
170 22275
                st = varnish_ask_cli(v, "status", &r);
171 22275
                if (st != CLIS_OK)
172 0
                        vtc_fatal(v->vl,
173 0
                            "CLI status command failed: %u %s", st, r);
174 22275
                if (!strcmp(r, "Child in state stopped"))
175 0
                        vtc_fatal(v->vl,
176 0
                            "Child stopped before running: %u %s", st, r);
177 22275
                if (!strcmp(r, "Child in state running")) {
178 22275
                        free(r);
179 22275
                        r = NULL;
180 22275
                        st = varnish_ask_cli(v, "debug.listen_address", &r);
181 22275
                        if (st != CLIS_OK)
182 0
                                vtc_fatal(v->vl,
183 0
                                    "CLI status command failed: %u %s", st, r);
184 22275
                        free(r);
185 22275
                        break;
186
                }
187 0
                free(r);
188 0
                r = NULL;
189 0
                (void)usleep(200000);
190
        }
191 22275
}
192
193
/**********************************************************************
194
 * Varnishlog gatherer thread
195
 */
196
197
static void *
198 22625
varnishlog_thread(void *priv)
199
{
200
        struct varnish *v;
201
        struct VSL_data *vsl;
202
        struct vsm *vsm;
203
        struct VSL_cursor *c;
204
        enum VSL_tag_e tag;
205
        uint64_t vxid;
206
        unsigned len;
207
        const char *tagname, *data;
208
        int type, i, opt;
209 22625
        struct vsb *vsb = NULL;
210
211 22625
        CAST_OBJ_NOTNULL(v, priv, VARNISH_MAGIC);
212
213 22625
        vsl = VSL_New();
214 22625
        AN(vsl);
215 22625
        vsm = v->vsm_vsl;
216
217 22625
        c = NULL;
218 22625
        opt = 0;
219 506622
        while (v->fds[1] > 0 || c != NULL) {    //lint !e845 bug in flint
220 483993
                if (c == NULL) {
221 127333
                        if (vtc_error)
222 0
                                break;
223 127333
                        VTIM_sleep(0.1);
224 127333
                        (void)VSM_Status(vsm);
225 127333
                        c = VSL_CursorVSM(vsl, vsm, opt);
226 127333
                        if (c == NULL) {
227 104761
                                vtc_log(v->vl, 3, "vsl|%s", VSL_Error(vsl));
228 104761
                                VSL_ResetError(vsl);
229 104761
                                continue;
230
                        }
231 22572
                }
232 379232
                AN(c);
233
234 379232
                opt = VSL_COPT_TAIL;
235
236 7274294
                while (1) {
237 7274299
                        i = VSL_Next(c);
238 7274299
                        if (i != 1)
239 379232
                                break;
240
241 6895064
                        v->vsl_rec = 1;
242
243 6895064
                        tag = VSL_TAG(c->rec.ptr);
244 6895064
                        vxid = VSL_ID(c->rec.ptr);
245 6895064
                        if (tag == SLT__Batch)
246 0
                                continue;
247 6895063
                        tagname = VSL_tags[tag];
248 6895063
                        len = VSL_LEN(c->rec.ptr);
249 6895063
                        type = VSL_CLIENT(c->rec.ptr) ?
250 2763276
                            'c' : VSL_BACKEND(c->rec.ptr) ?
251
                            'b' : '-';
252 6895065
                        data = VSL_CDATA(c->rec.ptr);
253 6895065
                        v->vsl_tag_count[tag]++;
254 6895065
                        if (VSL_tagflags[tag] & SLT_F_BINARY) {
255 33382
                                if (vsb == NULL)
256 1172
                                        vsb = VSB_new_auto();
257 33382
                                VSB_clear(vsb);
258 33382
                                VSB_quote(vsb, data, len, VSB_QUOTE_HEX);
259 33382
                                AZ(VSB_finish(vsb));
260
                                /* +2 to skip "0x" */
261 66764
                                vtc_log(v->vl, 4, "vsl| %10ju %-15s %c [%s]",
262 33382
                                    (uintmax_t)vxid, tagname, type,
263 33382
                                    VSB_data(vsb) + 2);
264 33382
                        } else {
265 13723360
                                vtc_log(v->vl, 4, "vsl| %10ju %-15s %c %.*s",
266 6861680
                                    (uintmax_t)vxid, tagname, type, (int)len,
267 6861680
                                    data);
268
                        }
269
                }
270 379233
                if (i == 0) {
271
                        /* Nothing to do but wait */
272 378358
                        v->vsl_idle++;
273 378358
                        if (!(VSM_Status(vsm) & VSM_WRK_RUNNING)) {
274
                                /* Abandoned - try reconnect */
275 21698
                                VSL_DeleteCursor(c);
276 21698
                                c = NULL;
277 21698
                        } else {
278 356661
                                VTIM_sleep(0.1);
279
                        }
280 379236
                } else if (i == -2) {
281
                        /* Abandoned - try reconnect */
282 874
                        VSL_DeleteCursor(c);
283 874
                        c = NULL;
284 874
                } else
285 0
                        break;
286
        }
287
288 0
        if (c)
289 0
                VSL_DeleteCursor(c);
290 1172
        VSL_Delete(vsl);
291 1172
        if (vsb != NULL)
292 1172
                VSB_destroy(&vsb);
293
294 22625
        return (NULL);
295
}
296
297
/**********************************************************************
298
 * Allocate and initialize a varnish
299
 */
300
301
static struct varnish *
302 22625
varnish_new(const char *name)
303
{
304
        struct varnish *v;
305
        struct vsb *vsb;
306
        char buf[1024];
307
308 22625
        ALLOC_OBJ(v, VARNISH_MAGIC);
309 22625
        AN(v);
310 22625
        REPLACE(v->name, name);
311
312 22625
        REPLACE(v->jail, "");
313
314 22625
        v->vl = vtc_logopen("%s", name);
315 22625
        AN(v->vl);
316
317 22625
        vsb = macro_expandf(v->vl, "${tmpdir}/%s", name);
318 22625
        AN(vsb);
319 22625
        v->workdir = strdup(VSB_data(vsb));
320 22625
        AN(v->workdir);
321 22625
        VSB_destroy(&vsb);
322
323 22625
        bprintf(buf, "rm -rf %s ; mkdir -p %s", v->workdir, v->workdir);
324 22625
        AZ(system(buf));
325
326 22625
        v->args = VSB_new_auto();
327
328 22625
        v->cli_fd = -1;
329 22625
        VTAILQ_INSERT_TAIL(&varnishes, v, list);
330
331 22625
        return (v);
332
}
333
334
/**********************************************************************
335
 * Delete a varnish instance
336
 */
337
338
static void
339 22625
varnish_delete(struct varnish *v)
340
{
341
342 22625
        CHECK_OBJ_NOTNULL(v, VARNISH_MAGIC);
343 22625
        vtc_logclose(v->vl);
344 22625
        free(v->name);
345 22625
        free(v->jail);
346 22625
        free(v->workdir);
347 22625
        VSB_destroy(&v->args);
348 22625
        if (v->vsc != NULL)
349 22625
                VSC_Destroy(&v->vsc, v->vsm_vsc);
350 22625
        if (v->vsm_vsc != NULL)
351 22625
                VSM_Destroy(&v->vsm_vsc);
352 22625
        if (v->vsm_vsl != NULL)
353 22625
                VSM_Destroy(&v->vsm_vsl);
354
355
        /*
356
         * We do not delete the workdir, it may contain stuff people
357
         * want (coredumps, shmlog/stats etc), and trying to divine
358
         * "may want" is just too much trouble.  Leave it around and
359
         * nuke it at the start of the next test-run.
360
         */
361
362
        /* XXX: MEMLEAK (?) */
363 22625
        FREE_OBJ(v);
364 22625
}
365
366
/**********************************************************************
367
 * Varnish listener
368
 */
369
370
static void *
371 22625
varnish_thread(void *priv)
372
{
373
        struct varnish *v;
374
375 22625
        CAST_OBJ_NOTNULL(v, priv, VARNISH_MAGIC);
376 22625
        return (vtc_record(v->vl, v->fds[0], NULL));
377
}
378
379
/**********************************************************************
380
 * Launch a Varnish
381
 */
382
383
static void
384 22625
varnish_launch(struct varnish *v)
385
{
386
        struct vsb *vsb, *vsb1;
387
        int i, nfd, asock;
388
        char abuf[128], pbuf[128];
389
        struct pollfd fd[3];
390
        enum VCLI_status_e u;
391
        const char *err;
392 22625
        char *r = NULL;
393
394
        /* Create listener socket */
395 22625
        asock = VTCP_listen_on(default_listen_addr, NULL, 1, &err);
396 22625
        if (err != NULL)
397 0
                vtc_fatal(v->vl, "Create CLI listen socket failed: %s", err);
398 22625
        assert(asock > 0);
399 22625
        VTCP_myname(asock, abuf, sizeof abuf, pbuf, sizeof pbuf);
400
401 22625
        AZ(VSB_finish(v->args));
402 22625
        vtc_log(v->vl, 2, "Launch");
403 22625
        vsb = VSB_new_auto();
404 22625
        AN(vsb);
405 22625
        VSB_cat(vsb, "cd ${pwd} &&");
406 45250
        VSB_printf(vsb, " exec varnishd %s -d -n %s -i %s",
407 22625
            v->jail, v->workdir, v->name);
408 22625
        if (macro_isdef(NULL, "varnishd_args_prepend")) {
409 0
                VSB_putc(vsb, ' ');
410 0
                macro_cat(v->vl, vsb, "varnishd_args_prepend", NULL);
411 0
        }
412 0
        VSB_cat(vsb, VSB_data(params_vsb));
413 0
        if (leave_temp) {
414 0
                VSB_cat(vsb, " -p debug=+vcl_keep");
415 0
                VSB_cat(vsb, " -p debug=+vmod_so_keep");
416 0
                VSB_cat(vsb, " -p debug=+vsm_keep");
417 0
        }
418 21950
        VSB_cat(vsb, " -l 2m");
419 21950
        VSB_cat(vsb, " -p auto_restart=off");
420 21950
        VSB_cat(vsb, " -p syslog_cli_traffic=off");
421 21950
        VSB_cat(vsb, " -p thread_pool_min=10");
422 21950
        VSB_cat(vsb, " -p debug=+vtc_mode");
423 21950
        VSB_cat(vsb, " -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody");
424 21950
        VSB_cat(vsb, " -p h2_initial_window_size=1m");
425 21950
        VSB_cat(vsb, " -p h2_rx_window_low_water=64k");
426 21950
        if (!v->has_a_arg) {
427 21950
                VSB_printf(vsb, " -a '%s'", default_listen_addr);
428 21950
                if (v->proto != NULL)
429 350
                        VSB_printf(vsb, ",%s", v->proto);
430 21950
        }
431 22600
        VSB_printf(vsb, " -M '%s %s'", abuf, pbuf);
432 22600
        VSB_printf(vsb, " -P %s/varnishd.pid", v->workdir);
433 22600
        if (vmod_path != NULL)
434 22600
                VSB_printf(vsb, " -p vmod_path=%s", vmod_path);
435 45200
        VSB_printf(vsb, " %s", VSB_data(v->args));
436 45200
        if (macro_isdef(NULL, "varnishd_args_append")) {
437 0
                VSB_putc(vsb, ' ');
438 0
                macro_cat(v->vl, vsb, "varnishd_args_append", NULL);
439 0
        }
440 22625
        AZ(VSB_finish(vsb));
441 22625
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
442 22625
        vsb1 = macro_expand(v->vl, VSB_data(vsb));
443 22625
        AN(vsb1);
444 22625
        VSB_destroy(&vsb);
445 22625
        vsb = vsb1;
446 22625
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
447 22625
        AZ(pipe(&v->fds[0]));
448 22625
        AZ(pipe(&v->fds[2]));
449 22625
        v->pid = fork();
450 22625
        assert(v->pid >= 0);
451 45250
        if (v->pid == 0) {
452 22625
                AZ(dup2(v->fds[0], 0));
453 22625
                assert(dup2(v->fds[3], 1) == 1);
454 22625
                assert(dup2(1, 2) == 2);
455 22625
                closefd(&v->fds[0]);
456 22625
                closefd(&v->fds[1]);
457 22625
                closefd(&v->fds[2]);
458 22625
                closefd(&v->fds[3]);
459 22625
                VSUB_closefrom(STDERR_FILENO + 1);
460 22625
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0));
461 0
                exit(1);
462
        } else {
463 22625
                vtc_log(v->vl, 3, "PID: %ld", (long)v->pid);
464 22625
                macro_def(v->vl, v->name, "pid", "%ld", (long)v->pid);
465 22625
                macro_def(v->vl, v->name, "name", "%s", v->workdir);
466
        }
467 22625
        closefd(&v->fds[0]);
468 22625
        closefd(&v->fds[3]);
469 22625
        v->fds[0] = v->fds[2];
470 22625
        v->fds[2] = v->fds[3] = -1;
471 22625
        VSB_destroy(&vsb);
472 22625
        PTOK(pthread_create(&v->tp, NULL, varnish_thread, v));
473
474
        /* Wait for the varnish to call home */
475 22625
        memset(fd, 0, sizeof fd);
476 22625
        fd[0].fd = asock;
477 22625
        fd[0].events = POLLIN;
478 22625
        fd[1].fd = v->fds[1];
479 22625
        fd[1].events = POLLIN;
480 22625
        fd[2].fd = v->fds[2];
481 22625
        fd[2].events = POLLIN;
482 22625
        i = poll(fd, 2, (int)(vtc_maxdur * 1000 / 3));
483 45250
        vtc_log(v->vl, 4, "CLIPOLL %d 0x%x 0x%x 0x%x",
484 22625
            i, fd[0].revents, fd[1].revents, fd[2].revents);
485 22625
        if (i == 0)
486 0
                vtc_fatal(v->vl, "FAIL timeout waiting for CLI connection");
487 22625
        if (fd[1].revents & POLLHUP)
488 0
                vtc_fatal(v->vl, "FAIL debug pipe closed");
489 22625
        if (!(fd[0].revents & POLLIN))
490 0
                vtc_fatal(v->vl, "FAIL CLI connection wait failure");
491 22625
        nfd = accept(asock, NULL, NULL);
492 22625
        closefd(&asock);
493 22625
        if (nfd < 0)
494 0
                vtc_fatal(v->vl, "FAIL no CLI connection accepted");
495
496 22625
        v->cli_fd = nfd;
497
498 22625
        vtc_log(v->vl, 3, "CLI connection fd = %d", v->cli_fd);
499 22625
        assert(v->cli_fd >= 0);
500
501
        /* Receive the banner or auth response */
502 22625
        u = varnish_ask_cli(v, NULL, &r);
503 22625
        if (vtc_error)
504 0
                return;
505 22625
        if (u != CLIS_AUTH)
506 0
                vtc_fatal(v->vl, "CLI auth demand expected: %u %s", u, r);
507
508 22625
        bprintf(abuf, "%s/_.secret", v->workdir);
509 22625
        nfd = open(abuf, O_RDONLY);
510 22625
        assert(nfd >= 0);
511
512 22625
        assert(sizeof abuf >= CLI_AUTH_RESPONSE_LEN + 7);
513 22625
        bstrcpy(abuf, "auth ");
514 22625
        VCLI_AuthResponse(nfd, r, abuf + 5);
515 22625
        closefd(&nfd);
516 22625
        free(r);
517 22625
        r = NULL;
518 22625
        strcat(abuf, "\n");
519
520 22625
        u = varnish_ask_cli(v, abuf, &r);
521 22625
        if (vtc_error)
522 0
                return;
523 22625
        if (u != CLIS_OK)
524 0
                vtc_fatal(v->vl, "CLI auth command failed: %u %s", u, r);
525 22625
        free(r);
526
527 22625
        v->vsm_vsc = VSM_New();
528 22625
        AN(v->vsm_vsc);
529 22625
        v->vsc = VSC_New();
530 22625
        AN(v->vsc);
531 22625
        assert(VSM_Arg(v->vsm_vsc, 'n', v->workdir) > 0);
532 22625
        AZ(VSM_Attach(v->vsm_vsc, -1));
533
534 22625
        v->vsm_vsl = VSM_New();
535 22625
        assert(VSM_Arg(v->vsm_vsl, 'n', v->workdir) > 0);
536 22625
        AZ(VSM_Attach(v->vsm_vsl, -1));
537
538 22625
        PTOK(pthread_create(&v->tp_vsl, NULL, varnishlog_thread, v));
539 22625
}
540
541
#define VARNISH_LAUNCH(v)                               \
542
        do {                                            \
543
                CHECK_OBJ_NOTNULL(v, VARNISH_MAGIC);    \
544
                if (v->cli_fd < 0)                      \
545
                        varnish_launch(v);              \
546
                if (vtc_error)                          \
547
                        return;                         \
548
        } while (0)
549
550
/**********************************************************************
551
 * Start a Varnish
552
 */
553
554
static void
555 22200
varnish_listen(const struct varnish *v, char *la)
556
{
557
        const char *a, *p, *n, *n2;
558
        char m[64], s[256];
559
        unsigned first;
560
561 22200
        n2 = "";
562 22200
        first = 1;
563
564 44550
        while (*la != '\0') {
565 22350
                n = la;
566 22350
                la = strchr(la, ' ');
567 22350
                AN(la);
568 22350
                *la = '\0';
569 22350
                a = ++la;
570 22350
                la = strchr(la, ' ');
571 22350
                AN(la);
572 22350
                *la = '\0';
573 22350
                p = ++la;
574 22350
                la = strchr(la, '\n');
575 22350
                AN(la);
576 22350
                *la = '\0';
577 22350
                la++;
578
579 22350
                AN(*n);
580 22350
                AN(*a);
581 22350
                AN(*p);
582
583 22350
                if (*p == '-') {
584 675
                        bprintf(s, "%s", a);
585 675
                        a = "0.0.0.0";
586 675
                        p = "0";
587 22350
                } else if (strchr(a, ':')) {
588 25
                        bprintf(s, "[%s]:%s", a, p);
589 25
                } else {
590 21650
                        bprintf(s, "%s:%s", a, p);
591
                }
592
593 22350
                if (first) {
594 22200
                        vtc_log(v->vl, 2, "Listen on %s %s", a, p);
595 22200
                        macro_def(v->vl, v->name, "addr", "%s", a);
596 22200
                        macro_def(v->vl, v->name, "port", "%s", p);
597 22200
                        macro_def(v->vl, v->name, "sock", "%s", s);
598 22200
                        first = 0;
599 22200
                }
600
601 22350
                if (!strcmp(n, n2))
602 25
                        continue;
603
604 22325
                bprintf(m, "%s_addr", n);
605 22325
                macro_def(v->vl, v->name, m, "%s", a);
606 22325
                bprintf(m, "%s_port", n);
607 22325
                macro_def(v->vl, v->name, m, "%s", p);
608 22325
                bprintf(m, "%s_sock", n);
609 22325
                macro_def(v->vl, v->name, m, "%s", s);
610 22325
                n2 = n;
611
        }
612 22200
}
613
614
static void
615 22200
varnish_start(struct varnish *v)
616
{
617
        enum VCLI_status_e u;
618 22200
        char *resp = NULL;
619
620 22200
        VARNISH_LAUNCH(v);
621 22200
        vtc_log(v->vl, 2, "Start");
622 22200
        u = varnish_ask_cli(v, "start", &resp);
623 22200
        if (vtc_error)
624 0
                return;
625 22200
        if (u != CLIS_OK)
626 0
                vtc_fatal(v->vl, "CLI start command failed: %u %s", u, resp);
627 22200
        wait_running(v);
628 22200
        free(resp);
629 22200
        resp = NULL;
630 22200
        u = varnish_ask_cli(v, "debug.xid 1000", &resp);
631 22200
        if (vtc_error)
632 0
                return;
633 22200
        if (u != CLIS_OK)
634 0
                vtc_fatal(v->vl, "CLI debug.xid command failed: %u %s",
635 0
                    u, resp);
636 22200
        free(resp);
637 22200
        resp = NULL;
638 22200
        u = varnish_ask_cli(v, "debug.listen_address", &resp);
639 22200
        if (vtc_error)
640 0
                return;
641 22200
        if (u != CLIS_OK)
642 0
                vtc_fatal(v->vl,
643 0
                    "CLI debug.listen_address command failed: %u %s", u, resp);
644 22200
        varnish_listen(v, resp);
645 22200
        free(resp);
646
        /* Wait for vsl logging to get underway */
647 22200
        while (v->vsl_rec == 0)
648 0
                VTIM_sleep(.1);
649 22200
}
650
651
/**********************************************************************
652
 * Stop a Varnish
653
 */
654
655
static void
656 23600
varnish_stop(struct varnish *v)
657
{
658
659 23600
        VARNISH_LAUNCH(v);
660 23600
        vtc_log(v->vl, 2, "Stop");
661 23600
        (void)varnish_ask_cli(v, "stop", NULL);
662 23600
        wait_stopped(v);
663 23600
}
664
665
/**********************************************************************
666
 * Cleanup
667
 */
668
669
static void
670 22625
varnish_cleanup(struct varnish *v)
671
{
672
        void *p;
673
674
        /* Close the CLI connection */
675 22625
        closefd(&v->cli_fd);
676
677
        /* Close the STDIN connection. */
678 22625
        closefd(&v->fds[1]);
679
680
        /* Wait until STDOUT+STDERR closes */
681 22625
        PTOK(pthread_join(v->tp, &p));
682 22625
        closefd(&v->fds[0]);
683
684
        /* Pick up the VSL thread */
685 22625
        PTOK(pthread_join(v->tp_vsl, &p));
686
687 22625
        vtc_wait4(v->vl, v->pid, v->expect_exit, 0, 0);
688 22625
        v->pid = 0;
689 22625
}
690
691
/**********************************************************************
692
 * Wait for a Varnish
693
 */
694
695
static void
696 22550
varnish_wait(struct varnish *v)
697
{
698 22550
        if (v->cli_fd < 0)
699 0
                return;
700
701 22550
        vtc_log(v->vl, 2, "Wait");
702
703 22550
        if (!vtc_error) {
704
                /* Do a backend.list to log if child is still running */
705 22550
                (void)varnish_ask_cli(v, "backend.list", NULL);
706 22550
        }
707
708
        /* Then stop it */
709 22550
        varnish_stop(v);
710
711 22550
        if (varnish_ask_cli(v, "panic.show", NULL) != CLIS_CANT)
712 0
                vtc_fatal(v->vl, "Unexpected panic");
713
714 22550
        varnish_cleanup(v);
715 22550
}
716
717
718
/**********************************************************************
719
 * Ask a CLI JSON question
720
 */
721
722
static void
723 950
varnish_cli_json(struct varnish *v, const char *cli)
724
{
725
        enum VCLI_status_e u;
726 950
        char *resp = NULL;
727
        const char *errptr;
728
        struct vjsn *vj;
729
730 950
        VARNISH_LAUNCH(v);
731 950
        u = varnish_ask_cli(v, cli, &resp);
732 950
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
733 950
        if (u != CLIS_OK)
734 0
                vtc_fatal(v->vl,
735 0
                    "FAIL CLI response %u expected %u", u, CLIS_OK);
736 950
        vj = vjsn_parse(resp, &errptr);
737 950
        if (vj == NULL)
738 0
                vtc_fatal(v->vl, "FAIL CLI, not good JSON: %s", errptr);
739 950
        vjsn_delete(&vj);
740 950
        free(resp);
741 950
}
742
743
/**********************************************************************
744
 * Ask a CLI question
745
 */
746
747
static void
748 29100
varnish_cli(struct varnish *v, const char *cli, unsigned exp, const char *re)
749
{
750
        enum VCLI_status_e u;
751
        struct vsb vsb[1];
752 29100
        vre_t *vre = NULL;
753 29100
        char *resp = NULL, errbuf[VRE_ERROR_LEN];
754
        int err, erroff;
755
756 29100
        VARNISH_LAUNCH(v);
757 29100
        if (re != NULL) {
758 2500
                vre = VRE_compile(re, 0, &err, &erroff, 1);
759 2500
                if (vre == NULL) {
760 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
761 0
                        AZ(VRE_error(vsb, err));
762 0
                        AZ(VSB_finish(vsb));
763 0
                        VSB_fini(vsb);
764 0
                        vtc_fatal(v->vl, "Illegal regexp: %s (@%d)",
765 0
                            errbuf, erroff);
766
                }
767 2500
        }
768 25450
        u = varnish_ask_cli(v, cli, &resp);
769 25450
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
770 25450
        if (exp != 0 && exp != (unsigned)u)
771 0
                vtc_fatal(v->vl, "FAIL CLI response %u expected %u", u, exp);
772 2500
        if (vre != NULL) {
773 2500
                err = VRE_match(vre, resp, 0, 0, NULL);
774 2500
                if (err < 1) {
775 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
776 0
                        AZ(VRE_error(vsb, err));
777 0
                        AZ(VSB_finish(vsb));
778 0
                        VSB_fini(vsb);
779 0
                        vtc_fatal(v->vl, "Expect failed (%s)", errbuf);
780
                }
781 2500
                VRE_free(&vre);
782 2500
        }
783 29100
        free(resp);
784 29100
}
785
786
/**********************************************************************
787
 * Load a VCL program
788
 */
789
790
static void
791 14175
varnish_vcl(struct varnish *v, const char *vcl, int fail, char **resp)
792
{
793
        struct vsb *vsb;
794
        enum VCLI_status_e u;
795
796 14175
        VARNISH_LAUNCH(v);
797 14175
        vsb = VSB_new_auto();
798 14175
        AN(vsb);
799
800 28350
        VSB_printf(vsb, "vcl.inline vcl%d << %s\nvcl %.1f;\n%s\n%s\n",
801 14175
            ++v->vcl_nbr, NONSENSE, v->syntax, vcl, NONSENSE);
802 14175
        AZ(VSB_finish(vsb));
803
804 14175
        u = varnish_ask_cli(v, VSB_data(vsb), resp);
805 14175
        if (u == CLIS_OK) {
806 6000
                VSB_clear(vsb);
807 6000
                VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
808 6000
                AZ(VSB_finish(vsb));
809 6000
                u = varnish_ask_cli(v, VSB_data(vsb), NULL);
810 6000
        }
811 6000
        if (u == CLIS_OK && fail) {
812 0
                VSB_destroy(&vsb);
813 0
                vtc_fatal(v->vl, "VCL compilation succeeded expected failure");
814 8175
        } else if (u != CLIS_OK && !fail) {
815 0
                VSB_destroy(&vsb);
816 0
                vtc_fatal(v->vl, "VCL compilation failed expected success");
817 8175
        } else if (fail)
818 8175
                vtc_log(v->vl, 2, "VCL compilation failed (as expected)");
819 14175
        VSB_destroy(&vsb);
820 14175
}
821
822
/**********************************************************************
823
 * Load a VCL program prefixed by backend decls for our servers
824
 */
825
826
static void
827 21150
varnish_vclbackend(struct varnish *v, const char *vcl)
828
{
829
        struct vsb *vsb, *vsb2;
830
        enum VCLI_status_e u;
831
832 21150
        VARNISH_LAUNCH(v);
833 21150
        vsb = VSB_new_auto();
834 21150
        AN(vsb);
835
836 21150
        vsb2 = VSB_new_auto();
837 21150
        AN(vsb2);
838
839 21150
        VSB_printf(vsb2, "vcl %.1f;\n", v->syntax);
840
841 21150
        cmd_server_gen_vcl(vsb2);
842
843 21150
        AZ(VSB_finish(vsb2));
844
845 42300
        VSB_printf(vsb, "vcl.inline vcl%d << %s\n%s\n%s\n%s\n",
846 21150
            ++v->vcl_nbr, NONSENSE, VSB_data(vsb2), vcl, NONSENSE);
847 21150
        AZ(VSB_finish(vsb));
848
849 21150
        u = varnish_ask_cli(v, VSB_data(vsb), NULL);
850 21150
        if (u != CLIS_OK) {
851 0
                VSB_destroy(&vsb);
852 0
                VSB_destroy(&vsb2);
853 0
                vtc_fatal(v->vl, "FAIL VCL does not compile");
854
        }
855 21150
        VSB_clear(vsb);
856 21150
        VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
857 21150
        AZ(VSB_finish(vsb));
858 21150
        u = varnish_ask_cli(v, VSB_data(vsb), NULL);
859 21150
        assert(u == CLIS_OK);
860 21150
        VSB_destroy(&vsb);
861 21150
        VSB_destroy(&vsb2);
862 21150
}
863
864
/**********************************************************************
865
 */
866
867
struct dump_priv {
868
        const char *arg;
869
        const struct varnish *v;
870
};
871
872
static int
873 87542
do_stat_dump_cb(void *priv, const struct VSC_point * const pt)
874
{
875
        const struct varnish *v;
876
        struct dump_priv *dp;
877
        uint64_t u;
878
879 87542
        if (pt == NULL)
880 0
                return (0);
881 87542
        dp = priv;
882 87542
        v = dp->v;
883
884 87542
        if (strcmp(pt->ctype, "uint64_t"))
885 0
                return (0);
886 87542
        u = VSC_Value(pt);
887
888 87542
        if (strcmp(dp->arg, "*")) {
889 87542
                if (fnmatch(dp->arg, pt->name, 0))
890 85106
                        return (0);
891 2436
        }
892
893 2436
        vtc_log(v->vl, 4, "VSC %s %ju",  pt->name, (uintmax_t)u);
894 2436
        return (0);
895 87542
}
896
897
static void
898 250
varnish_vsc(struct varnish *v, const char *arg)
899
{
900
        struct dump_priv dp;
901
902 250
        VARNISH_LAUNCH(v);
903 250
        memset(&dp, 0, sizeof dp);
904 250
        dp.v = v;
905 250
        dp.arg = arg;
906 250
        (void)VSM_Status(v->vsm_vsc);
907 250
        (void)VSC_Iter(v->vsc, v->vsm_vsc, do_stat_dump_cb, &dp);
908 250
}
909
910
/**********************************************************************
911
 * Check statistics
912
 */
913
914
struct stat_arg {
915
        const char      *pattern;
916
        uintmax_t       val;
917
        unsigned        good;
918
};
919
920
struct stat_priv {
921
        struct stat_arg lhs;
922
        struct stat_arg rhs;
923
};
924
925
static int
926 5478918
stat_match(const char *pattern, const char *name)
927
{
928
929 5478918
        if (strchr(pattern, '.') == NULL) {
930 1123729
                if (fnmatch("MAIN.*", name, 0))
931 84119
                        return (FNM_NOMATCH);
932 1039610
                name += 5;
933 1039610
        }
934 5394799
        return (fnmatch(pattern, name, 0));
935 5478918
}
936
937
static int
938 5475368
do_expect_cb(void *priv, const struct VSC_point * const pt)
939
{
940 5475368
        struct stat_priv *sp = priv;
941
942 5475368
        if (pt == NULL)
943 0
                return (0);
944
945 5475368
        if (!sp->lhs.good && stat_match(sp->lhs.pattern, pt->name) == 0) {
946 20703
                AZ(strcmp(pt->ctype, "uint64_t"));
947 20703
                AN(pt->ptr);
948 20703
                sp->lhs.val = VSC_Value(pt);
949 20703
                sp->lhs.good = 1;
950 20703
        }
951
952 5475368
        if (sp->rhs.pattern == NULL) {
953 5471718
                sp->rhs.good = 1;
954 5475368
        } else if (!sp->rhs.good &&
955 3650
            stat_match(sp->rhs.pattern, pt->name) == 0) {
956 100
                AZ(strcmp(pt->ctype, "uint64_t"));
957 100
                AN(pt->ptr);
958 100
                sp->rhs.val = VSC_Value(pt);
959 100
                sp->rhs.good = 1;
960 100
        }
961
962 5475368
        return (sp->lhs.good && sp->rhs.good);
963 5475368
}
964
965
/**********************************************************************
966
 */
967
968
static void
969 19325
varnish_expect(struct varnish *v, char * const *av)
970
{
971
        struct stat_priv sp;
972
        int good, i, not;
973
        uintmax_t u;
974
        char *l, *p;
975
976 19325
        VARNISH_LAUNCH(v);
977 19325
        ZERO_OBJ(&sp, sizeof sp);
978 19325
        l = av[0];
979 19325
        not = (*l == '!');
980 19325
        if (not) {
981 125
                l++;
982 125
                AZ(av[1]);
983 125
        } else {
984 19200
                AN(av[1]);
985 19200
                AN(av[2]);
986 19200
                u = strtoumax(av[2], &p, 0);
987 19200
                if (u != UINTMAX_MAX && *p == '\0')
988 19100
                        sp.rhs.val = u;
989
                else
990 100
                        sp.rhs.pattern = av[2];
991
        }
992
993 19325
        sp.lhs.pattern = l;
994
995 27078
        for (i = 0; i < 50; i++, (void)usleep(100000)) {
996 26953
                (void)VSM_Status(v->vsm_vsc);
997 26953
                sp.lhs.good = sp.rhs.good = 0;
998 26953
                good = VSC_Iter(v->vsc, v->vsm_vsc, do_expect_cb, &sp);
999 26953
                if (!good)
1000 6250
                        good = -2;
1001 26953
                if (good < 0)
1002 6250
                        continue;
1003
1004 20703
                if (not)
1005 0
                        vtc_fatal(v->vl, "Found (not expected): %s", l);
1006
1007 20703
                good = -1;
1008 20703
                if (!strcmp(av[1], "==")) good = (sp.lhs.val == sp.rhs.val);
1009 35384
                if (!strcmp(av[1], "!=")) good = (sp.lhs.val != sp.rhs.val);
1010 1300
                if (!strcmp(av[1], ">" )) good = (sp.lhs.val >  sp.rhs.val);
1011 1689
                if (!strcmp(av[1], "<" )) good = (sp.lhs.val <  sp.rhs.val);
1012 1097
                if (!strcmp(av[1], ">=")) good = (sp.lhs.val >= sp.rhs.val);
1013 1450
                if (!strcmp(av[1], "<=")) good = (sp.lhs.val <= sp.rhs.val);
1014 20703
                if (good == -1)
1015 0
                        vtc_fatal(v->vl, "comparison %s unknown", av[1]);
1016 20703
                if (good)
1017 19200
                        break;
1018 1503
        }
1019 19325
        if (good == -1) {
1020 0
                vtc_fatal(v->vl, "VSM error: %s", VSM_Error(v->vsm_vsc));
1021
        }
1022 19325
        if (good == -2) {
1023 125
                if (not) {
1024 125
                        vtc_log(v->vl, 2, "not found (as expected): %s", l);
1025 125
                        return;
1026
                }
1027 0
                vtc_fatal(v->vl, "stats field %s unknown",
1028 0
                    sp.lhs.good ? sp.rhs.pattern : sp.lhs.pattern);
1029
        }
1030
1031 19200
        if (good == 1) {
1032 38400
                vtc_log(v->vl, 2, "as expected: %s (%ju) %s %s (%ju)",
1033 19200
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1034 19200
        } else {
1035 0
                vtc_fatal(v->vl, "Not true: %s (%ju) %s %s (%ju)",
1036 0
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1037
        }
1038 19325
}
1039
1040
static void
1041 3300
vsl_catchup(struct varnish *v)
1042
{
1043
        int vsl_idle;
1044
1045 3300
        VARNISH_LAUNCH(v);
1046 3300
        vsl_idle = v->vsl_idle;
1047 6644
        while (!vtc_error && vsl_idle == v->vsl_idle)
1048 3344
                VTIM_sleep(0.1);
1049 3300
}
1050
1051
/* SECTION: varnish varnish
1052
 *
1053
 * Define and interact with varnish instances.
1054
 *
1055
 * To define a Varnish server, you'll use this syntax::
1056
 *
1057
 *      varnish vNAME [-arg STRING] [-vcl STRING] [-vcl+backend STRING]
1058
 *              [-errvcl STRING STRING] [-jail STRING] [-proto PROXY]
1059
 *
1060
 * The first ``varnish vNAME`` invocation will start the varnishd master
1061
 * process in the background, waiting for the ``-start`` switch to actually
1062
 * start the child.
1063
 *
1064
 * Types used in the description below:
1065
 *
1066
 * PATTERN
1067
 *         is a 'glob' style pattern (ie: fnmatch(3)) as used in shell filename
1068
 *         expansion.
1069
 *
1070
 * Arguments:
1071
 *
1072
 * vNAME
1073
 *         Identify the Varnish server with a string, it must starts with 'v'.
1074
 *
1075
 * \-arg STRING
1076
 *         Pass an argument to varnishd, for example "-h simple_list".
1077
 *
1078
 *         If the ${varnishd_args_prepend} or ${varnishd_args_append} macros are
1079
 *         defined, they are expanded and inserted before / appended to the
1080
 *         varnishd command line as constructed by varnishtest, before the
1081
 *         command line itself is expanded. This enables tweaks to the varnishd
1082
 *         command line without editing test cases. This macros can be defined
1083
 *         using the ``-D`` option for varnishtest.
1084
 *
1085
 * \-vcl STRING
1086
 *         Specify the VCL to load on this Varnish instance. You'll probably
1087
 *         want to use multi-lines strings for this ({...}).
1088
 *
1089
 * \-vcl+backend STRING
1090
 *         Do the exact same thing as -vcl, but adds the definition block of
1091
 *         known backends (ie. already defined).
1092
 *
1093
 * \-errvcl STRING1 STRING2
1094
 *         Load STRING2 as VCL, expecting it to fail, and Varnish to send an
1095
 *         error string matching STRING1
1096
 *
1097
 * \-jail STRING
1098
 *         Look at ``man varnishd`` (-j) for more information.
1099
 *
1100
 * \-proto PROXY
1101
 *         Have Varnish use the proxy protocol. Note that PROXY here is the
1102
 *         actual string.
1103
 *
1104
 * You can decide to start the Varnish instance and/or wait for several events::
1105
 *
1106
 *         varnish vNAME [-start] [-wait] [-wait-running] [-wait-stopped]
1107
 *
1108
 * \-start
1109
 *         Start the child process.
1110
 *
1111
 *         Once successfully started, the following macros are available for
1112
 *         the default listen address: ``${vNAME_addr}``, ``${vNAME_port}``
1113
 *         and ``${vNAME_sock}``. Additional macros are available, including
1114
 *         the listen address name for each address vNAME listens to, like for
1115
 *         example: ``${vNAME_a0_addr}``.
1116
 *
1117
 * \-stop
1118
 *         Stop the child process.
1119
 *
1120
 * \-syntax
1121
 *         Set the VCL syntax level for this command (default: 4.1)
1122
 *
1123
 * \-wait
1124
 *         Wait for that instance to terminate.
1125
 *
1126
 * \-wait-running
1127
 *         Wait for the Varnish child process to be started.
1128
 *
1129
 * \-wait-stopped
1130
 *         Wait for the Varnish child process to stop.
1131
 *
1132
 * \-cleanup
1133
 *         Once Varnish is stopped, clean everything after it. This is only used
1134
 *         in very few tests and you should never need it.
1135
 *
1136
 * \-expectexit NUMBER
1137
 *         Expect varnishd to exit(3) with this value
1138
 *
1139
 * Once Varnish is started, you can talk to it (as you would through
1140
 * ``varnishadm``) with these additional switches::
1141
 *
1142
 *         varnish vNAME [-cli STRING] [-cliok STRING] [-clierr STRING]
1143
 *                       [-clijson STRING]
1144
 *
1145
 * \-cli STRING|-cliok STRING|-clierr STATUS STRING|-cliexpect REGEXP STRING
1146
 *         All four of these will send STRING to the CLI, the only difference
1147
 *         is what they expect the result to be. -cli doesn't expect
1148
 *         anything, -cliok expects 200, -clierr expects STATUS, and
1149
 *         -cliexpect expects the REGEXP to match the returned response.
1150
 *
1151
 * \-clijson STRING
1152
 *         Send STRING to the CLI, expect success (CLIS_OK/200) and check
1153
 *         that the response is parsable JSON.
1154
 *
1155
 * It is also possible to interact with its shared memory (as you would
1156
 * through tools like ``varnishstat``) with additional switches:
1157
 *
1158
 * \-expect \!PATTERN|PATTERN OP NUMBER|PATTERN OP PATTERN
1159
 *         Look into the VSM and make sure the first VSC counter identified by
1160
 *         PATTERN has a correct value. OP can be ==, >, >=, <, <=. For
1161
 *         example::
1162
 *
1163
 *                 varnish v1 -expect SM?.s1.g_space > 1000000
1164
 *                 varnish v1 -expect cache_hit >= cache_hit_grace
1165
 *
1166
 *         In the \! form the test fails if a counter matches PATTERN.
1167
 *
1168
 *         The ``MAIN.`` namespace can be omitted from PATTERN.
1169
 *
1170
 *         The test takes up to 5 seconds before timing out.
1171
 *
1172
 * \-vsc PATTERN
1173
 *         Dump VSC counters matching PATTERN.
1174
 *
1175
 * \-vsl_catchup
1176
 *         Wait until the logging thread has idled to make sure that all
1177
 *         the generated log is flushed
1178
 */
1179
1180
void
1181 114750
cmd_varnish(CMD_ARGS)
1182
{
1183
        struct varnish *v, *v2;
1184
1185 114750
        (void)priv;
1186
1187 114750
        if (av == NULL) {
1188
                /* Reset and free */
1189 47050
                VTAILQ_FOREACH_SAFE(v, &varnishes, list, v2) {
1190 22625
                        if (v->cli_fd >= 0)
1191 22400
                                varnish_wait(v);
1192 22625
                        VTAILQ_REMOVE(&varnishes, v, list);
1193 22625
                        varnish_delete(v);
1194 22625
                }
1195 24425
                return;
1196
        }
1197
1198 90325
        AZ(strcmp(av[0], "varnish"));
1199 90325
        av++;
1200
1201 90325
        VTC_CHECK_NAME(vl, av[0], "Varnish", 'v');
1202 92050
        VTAILQ_FOREACH(v, &varnishes, list)
1203 69425
                if (!strcmp(v->name, av[0]))
1204 67700
                        break;
1205 112775
        if (v == NULL)
1206 22625
                v = varnish_new(av[0]);
1207 90325
        av++;
1208 90325
        v->syntax = 4.1;
1209
1210 211675
        for (; *av != NULL; av++) {
1211 121350
                if (vtc_error)
1212 0
                        break;
1213 121350
                if (!strcmp(*av, "-arg")) {
1214 7425
                        AN(av[1]);
1215 7425
                        AZ(v->pid);
1216 7425
                        VSB_cat(v->args, " ");
1217 7425
                        VSB_cat(v->args, av[1]);
1218 7425
                        if (av[1][0] == '-' && av[1][1] == 'a')
1219 725
                                v->has_a_arg = 1;
1220 7425
                        av++;
1221 7425
                        continue;
1222
                }
1223 113925
                if (!strcmp(*av, "-cleanup")) {
1224 75
                        AZ(av[1]);
1225 75
                        varnish_cleanup(v);
1226 75
                        continue;
1227
                }
1228 113850
                if (!strcmp(*av, "-cli")) {
1229 1150
                        AN(av[1]);
1230 1150
                        varnish_cli(v, av[1], 0, NULL);
1231 1150
                        av++;
1232 1150
                        continue;
1233
                }
1234 112700
                if (!strcmp(*av, "-clierr")) {
1235 2975
                        AN(av[1]);
1236 2975
                        AN(av[2]);
1237 2975
                        varnish_cli(v, av[2], atoi(av[1]), NULL);
1238 2975
                        av += 2;
1239 2975
                        continue;
1240
                }
1241 109725
                if (!strcmp(*av, "-cliexpect")) {
1242 2500
                        AN(av[1]);
1243 2500
                        AN(av[2]);
1244 2500
                        varnish_cli(v, av[2], 0, av[1]);
1245 2500
                        av += 2;
1246 2500
                        continue;
1247
                }
1248 107225
                if (!strcmp(*av, "-clijson")) {
1249 950
                        AN(av[1]);
1250 950
                        varnish_cli_json(v, av[1]);
1251 950
                        av++;
1252 950
                        continue;
1253
                }
1254 106275
                if (!strcmp(*av, "-cliok")) {
1255 22475
                        AN(av[1]);
1256 22475
                        varnish_cli(v, av[1], (unsigned)CLIS_OK, NULL);
1257 22475
                        av++;
1258 22475
                        continue;
1259
                }
1260 83800
                if (!strcmp(*av, "-errvcl")) {
1261 8175
                        char *r = NULL;
1262 8175
                        AN(av[1]);
1263 8175
                        AN(av[2]);
1264 8175
                        varnish_vcl(v, av[2], 1, &r);
1265 8175
                        if (strstr(r, av[1]) == NULL)
1266 0
                                vtc_fatal(v->vl,
1267
                                    "Did not find expected string: (\"%s\")",
1268 0
                                    av[1]);
1269
                        else
1270 16350
                                vtc_log(v->vl, 3,
1271
                                    "Found expected string: (\"%s\")",
1272 8175
                                    av[1]);
1273 8175
                        free(r);
1274 8175
                        av += 2;
1275 8175
                        continue;
1276
                }
1277 75625
                if (!strcmp(*av, "-expect")) {
1278 19325
                        av++;
1279 19325
                        varnish_expect(v, av);
1280 19325
                        av += 2;
1281 19325
                        continue;
1282
                }
1283 56300
                if (!strcmp(*av, "-expectexit")) {
1284 225
                        v->expect_exit = strtoul(av[1], NULL, 0);
1285 225
                        av++;
1286 225
                        continue;
1287
                }
1288 56075
                if (!strcmp(*av, "-jail")) {
1289 100
                        AN(av[1]);
1290 100
                        AZ(v->pid);
1291 100
                        REPLACE(v->jail, av[1]);
1292 100
                        av++;
1293 100
                        continue;
1294
                }
1295 55975
                if (!strcmp(*av, "-proto")) {
1296 350
                        AN(av[1]);
1297 350
                        AZ(v->pid);
1298 350
                        REPLACE(v->proto, av[1]);
1299 350
                        av++;
1300 350
                        continue;
1301
                }
1302 55625
                if (!strcmp(*av, "-start")) {
1303 22200
                        varnish_start(v);
1304 22200
                        continue;
1305
                }
1306 33425
                if (!strcmp(*av, "-stop")) {
1307 1050
                        varnish_stop(v);
1308 1050
                        continue;
1309
                }
1310 32375
                if (!strcmp(*av, "-syntax")) {
1311 1375
                        AN(av[1]);
1312 1375
                        v->syntax = strtod(av[1], NULL);
1313 1375
                        av++;
1314 1375
                        continue;
1315
                }
1316 31000
                if (!strcmp(*av, "-vcl")) {
1317 6000
                        AN(av[1]);
1318 6000
                        varnish_vcl(v, av[1], 0, NULL);
1319 6000
                        av++;
1320 6000
                        continue;
1321
                }
1322 25000
                if (!strcmp(*av, "-vcl+backend")) {
1323 21150
                        AN(av[1]);
1324 21150
                        varnish_vclbackend(v, av[1]);
1325 21150
                        av++;
1326 21150
                        continue;
1327
                }
1328 3850
                if (!strcmp(*av, "-vsc")) {
1329 250
                        AN(av[1]);
1330 250
                        varnish_vsc(v, av[1]);
1331 250
                        av++;
1332 250
                        continue;
1333
                }
1334 3600
                if (!strcmp(*av, "-wait-stopped")) {
1335 75
                        wait_stopped(v);
1336 75
                        continue;
1337
                }
1338 3525
                if (!strcmp(*av, "-wait-running")) {
1339 75
                        wait_running(v);
1340 75
                        continue;
1341
                }
1342 3450
                if (!strcmp(*av, "-wait")) {
1343 150
                        varnish_wait(v);
1344 150
                        continue;
1345
                }
1346 3300
                if (!strcmp(*av, "-vsl_catchup")) {
1347 3300
                        vsl_catchup(v);
1348 3300
                        continue;
1349
                }
1350 0
                vtc_fatal(v->vl, "Unknown varnish argument: %s", *av);
1351
        }
1352 114750
}
1353
1354
#endif /* VTEST_WITH_VTC_VARNISH */