vinyl-cache/bin/vinyltest/vtc_vinyl.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_VINYL
31
32
#include "config.h"
33
34
#include <sys/socket.h>
35
36
#include <errno.h>
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 vinyl {
59
        unsigned                magic;
60
#define VINYL_MAGIC             0x208cd8e4
61
        char                    *me;
62
        char                    *name;
63
        struct vtclog           *vl;
64
        VTAILQ_ENTRY(vinyl)     list;
65
66
        struct vsb              *args;
67
        int                     fds[4];
68
        pid_t                   pid;
69
70
        double                  syntax;
71
72
        pthread_t               tp;
73
        pthread_t               tp_vsl;
74
        int                     tp_started;
75
76
        int                     expect_exit;
77
78
        int                     cli_fd;
79
        int                     vcl_nbr;
80
        char                    *workdir;
81
        char                    *jail;
82
        char                    *proto;
83
84
        struct vsm              *vsm_vsl;
85
        struct vsm              *vsm_vsc;
86
        struct vsc              *vsc;
87
        int                     has_a_arg;
88
89
        unsigned                vsl_tag_count[256];
90
91
        volatile int            vsl_rec;
92
        volatile int            vsl_idle;
93
};
94
95
#define NONSENSE        "%XJEIFLH|)Xspa8P"
96
97
static VTAILQ_HEAD(, vinyl)     vinyles =
98
    VTAILQ_HEAD_INITIALIZER(vinyles);
99
100
/**********************************************************************
101
 * Fatal condition cleanup
102
 * Invalid to call in any code path not followed by vtc_fatal().
103
 */
104
105
static void
106 3
vinyl_fatal_cleanup(const struct vinyl *v)
107
{
108
        struct pollfd fd[1];
109
        int n;
110
111 3
        if (!pthread_equal(pthread_self(), vtc_thread))
112 0
                return;
113
114 3
        if (!v->tp_started)
115 3
                return;
116
117 0
        memset(fd, 0, sizeof(fd));
118 0
        fd[0].fd = v->fds[0];
119 0
        fd[0].events = POLLIN;
120
121 0
        do {
122 0
                n = poll(fd, sizeof(fd)/sizeof(fd[0]), 10);
123 0
                if (n == 1 && (fd[0].revents & (POLLHUP|POLLERR)) != 0) {
124 0
                        PTOK(pthread_join(v->tp, NULL));
125 0
                        break;
126
                }
127
128 0
                if (n == 1)
129 0
                        VTIM_sleep(0.01);
130 0
        } while (n > 0);
131 3
}
132
133
#define vinyl_fatal(v, ...)                                             \
134
    do {                                                                \
135
            vinyl_fatal_cleanup((v));                                   \
136
            vtc_fatal((v)->vl, __VA_ARGS__);                            \
137
    } while (0)
138
139
/**********************************************************************
140
 * Ask a question over CLI
141
 */
142
143
static enum VCLI_status_e
144 45222
vinyl_ask_cli(const struct vinyl *v, const char *cmd, char **repl)
145
{
146
        int i;
147
        unsigned retval;
148
        char *r;
149
150 45222
        if (cmd != NULL) {
151 42240
                vtc_dump(v->vl, 4, "CLI TX", cmd, -1);
152 42240
                i = write(v->cli_fd, cmd, vstrlen(cmd));
153 42240
                if (i != vstrlen(cmd) && !vtc_stop)
154 0
                        vinyl_fatal(v, "CLI write failed (%s) = %u %s",
155
                            cmd, errno, strerror(errno));
156 0
                i = write(v->cli_fd, "\n", 1);
157 0
                if (i != 1 && !vtc_stop)
158 0
                        vinyl_fatal(v, "CLI write failed (%s) = %u %s",
159
                            cmd, errno, strerror(errno));
160 42240
        }
161 84480
        i = VCLI_ReadResult(v->cli_fd, &retval, &r, vtc_maxdur);
162 84480
        if (i != 0 && !vtc_stop)
163 0
                vinyl_fatal(v, "CLI failed (%s) = %d %u %s",
164
                    cmd != NULL ? cmd : "NULL", i, retval, r);
165 45222
        vtc_log(v->vl, 3, "CLI RX  %u", retval);
166 45222
        vtc_dump(v->vl, 4, "CLI RX", r, -1);
167 45222
        if (repl != NULL)
168 28899
                *repl = r;
169
        else
170 16323
                free(r);
171 45222
        return ((enum VCLI_status_e)retval);
172
}
173
174
/**********************************************************************
175
 *
176
 */
177
178
static void
179 3156
wait_stopped(const struct vinyl *v)
180
{
181 3156
        char *r = NULL;
182
        enum VCLI_status_e st;
183
184 3156
        vtc_log(v->vl, 3, "wait-stopped");
185 3156
        while (1) {
186 3156
                st = vinyl_ask_cli(v, "status", &r);
187 3156
                if (st != CLIS_OK)
188 0
                        vinyl_fatal(v,
189
                            "CLI status command failed: %u %s", st, r);
190 3156
                if (!vstrcmp(r, "Child in state stopped")) {
191 3156
                        free(r);
192 3156
                        break;
193
                }
194 0
                free(r);
195 0
                r = NULL;
196 0
                VTIM_sleep(0.2);
197
        }
198 3156
}
199
/**********************************************************************
200
 *
201
 */
202
203
static void
204 2949
wait_running(const struct vinyl *v)
205
{
206 2949
        char *r = NULL;
207
        enum VCLI_status_e st;
208
209 2949
        while (1) {
210 2949
                vtc_log(v->vl, 3, "wait-running");
211 2949
                st = vinyl_ask_cli(v, "status", &r);
212 2949
                if (st != CLIS_OK)
213 0
                        vinyl_fatal(v,
214
                            "CLI status command failed: %u %s", st, r);
215 0
                if (!vstrcmp(r, "Child in state stopped"))
216 0
                        vinyl_fatal(v,
217
                            "Child stopped before running: %u %s", st, r);
218 2949
                if (!vstrcmp(r, "Child in state running")) {
219 2949
                        free(r);
220 2949
                        r = NULL;
221 2949
                        st = vinyl_ask_cli(v, "debug.listen_address", &r);
222 2949
                        if (st != CLIS_OK)
223 0
                                vinyl_fatal(v,
224
                                    "CLI status command failed: %u %s", st, r);
225 2949
                        free(r);
226 2949
                        break;
227
                }
228 0
                free(r);
229 0
                r = NULL;
230 0
                VTIM_sleep(0.2);
231
        }
232 2949
}
233
234
/**********************************************************************
235
 * Vinyllog gatherer thread
236
 */
237
238
static void *
239 2982
vinyllog_thread(void *priv)
240
{
241
        struct vinyl *v;
242
        struct VSL_data *vsl;
243
        struct vsm *vsm;
244
        struct VSL_cursor *c;
245
        enum VSL_tag_e tag;
246
        uint64_t vxid;
247
        unsigned len, vs;
248
        const char *tagname, *data;
249
        int type, i, opt;
250 2982
        struct vsb *vsb = NULL;
251
252 2982
        CAST_OBJ_NOTNULL(v, priv, VINYL_MAGIC);
253
254 2982
        vsl = VSL_New();
255 2982
        AN(vsl);
256 2982
        vsm = v->vsm_vsl;
257
258 2982
        c = NULL;
259 2982
        opt = 0;
260 94062
        while (v->fds[1] > 0 || c != NULL) {    //lint !e845 bug in flint
261 91081
                if (c == NULL) {
262 27585
                        if (vtc_error)
263 0
                                break;
264 27585
                        VTIM_sleep(0.1);
265 27585
                        (void)VSM_Status(vsm);
266 27585
                        c = VSL_CursorVSM(vsl, vsm, opt);
267 27585
                        if (c == NULL) {
268 19735
                                vtc_log(v->vl, 3, "vsl|%s", VSL_Error(vsl));
269 19735
                                VSL_ResetError(vsl);
270 19735
                                continue;
271
                        }
272 7849
                }
273 71345
                AN(c);
274
275 71345
                opt = VSL_COPT_TAIL;
276
277 1012634
                while (1) {
278 1012634
                        i = VSL_Next(c);
279 1012634
                        if (i != 1)
280 71345
                                break;
281
282 941289
                        v->vsl_rec = 1;
283
284 941289
                        tag = VSL_TAG(c->rec.ptr);
285 941289
                        vxid = VSL_ID(c->rec.ptr);
286 941289
                        if (tag == SLT__Batch)
287 0
                                continue;
288 941289
                        tagname = VSL_tags[tag];
289 941289
                        len = VSL_LEN(c->rec.ptr);
290 941289
                        type = VSL_CLIENT(c->rec.ptr) ?
291 374904
                            'c' : VSL_BACKEND(c->rec.ptr) ?
292
                            'b' : '-';
293 941289
                        data = VSL_CDATA(c->rec.ptr);
294 941289
                        v->vsl_tag_count[tag]++;
295 941289
                        if (VSL_tagflags[tag] & SLT_F_BINARY) {
296 4497
                                if (vsb == NULL)
297 165
                                        vsb = VSB_new_auto();
298 4497
                                VSB_clear(vsb);
299 4497
                                VSB_quote(vsb, data, len, VSB_QUOTE_HEX);
300 4497
                                AZ(VSB_finish(vsb));
301
                                /* +2 to skip "0x" */
302 8994
                                vtc_log(v->vl, 4, "vsl| %10ju %-15s %c [%s]",
303 4497
                                    (uintmax_t)vxid, tagname, type,
304 4497
                                    VSB_data(vsb) + 2);
305 4497
                        } else {
306 1873584
                                vtc_log(v->vl, 4, "vsl| %10ju %-15s %c %.*s",
307 936792
                                    (uintmax_t)vxid, tagname, type, (int)len,
308 936792
                                    data);
309
                        }
310
                }
311 71345
                if (i == 0) {
312
                        /* Nothing to do but wait */
313 71345
                        v->vsl_idle++;
314 71345
                        vs = VSM_Status(vsm) & VSM_WRK_MASK;
315 71345
                        if ((vs & ~VSM_WRK_CHANGED) != VSM_WRK_RUNNING) {
316
                                /* Abandoned - try reconnect */
317 7849
                                VSL_DeleteCursor(c);
318 7849
                                c = NULL;
319 7849
                        } else {
320 63496
                                VTIM_sleep(0.1);
321
                        }
322 71345
                } else if (i == -2) {
323
                        /* Abandoned - try reconnect */
324 0
                        VSL_DeleteCursor(c);
325 0
                        c = NULL;
326 0
                } else
327 0
                        break;
328
        }
329
330 0
        if (c)
331 0
                VSL_DeleteCursor(c);
332 165
        VSL_Delete(vsl);
333 165
        if (vsb != NULL)
334 165
                VSB_destroy(&vsb);
335
336 2982
        return (NULL);
337
}
338
339
/**********************************************************************
340
 * Allocate and initialize a vinyl
341
 */
342
343
static struct vinyl *
344 2985
vinyl_new(const char *name)
345
{
346
        struct vinyl *v;
347
        struct vsb *vsb;
348
        char buf[1024];
349
350 2985
        ALLOC_OBJ(v, VINYL_MAGIC);
351 2985
        AN(v);
352 2985
        REPLACE(v->name, name);
353
354 2985
        REPLACE(v->jail, "");
355
356 2985
        v->vl = vtc_logopen("%s", name);
357 2985
        AN(v->vl);
358
359 2985
        vsb = macro_expandf(v->vl, "${tmpdir}/%s", name);
360 2985
        AN(vsb);
361 2985
        v->workdir = strdup(VSB_data(vsb));
362 2985
        AN(v->workdir);
363 2985
        VSB_destroy(&vsb);
364
365 2985
        bprintf(buf, "rm -rf %s ; mkdir -p %s", v->workdir, v->workdir);
366 2985
        AZ(system(buf));
367
368 2985
        v->args = VSB_new_auto();
369
370 2985
        v->cli_fd = -1;
371 2985
        VTAILQ_INSERT_TAIL(&vinyles, v, list);
372
373 2985
        return (v);
374
}
375
376
/**********************************************************************
377
 * Delete a vinyl instance
378
 */
379
380
static void
381 2985
vinyl_delete(struct vinyl *v)
382
{
383
384 2985
        CHECK_OBJ_NOTNULL(v, VINYL_MAGIC);
385 2985
        vtc_logclose(v->vl);
386 2985
        free(v->name);
387 2985
        free(v->jail);
388 2985
        free(v->workdir);
389 2985
        VSB_destroy(&v->args);
390 2985
        if (v->vsc != NULL)
391 2982
                VSC_Destroy(&v->vsc, v->vsm_vsc);
392 2982
        if (v->vsm_vsc != NULL)
393 2982
                VSM_Destroy(&v->vsm_vsc);
394 2982
        if (v->vsm_vsl != NULL)
395 2982
                VSM_Destroy(&v->vsm_vsl);
396
397
        /*
398
         * We do not delete the workdir, it may contain stuff people
399
         * want (coredumps, shmlog/stats etc), and trying to divine
400
         * "may want" is just too much trouble.  Leave it around and
401
         * nuke it at the start of the next test-run.
402
         */
403
404
        /* XXX: MEMLEAK (?) */
405 2985
        FREE_OBJ(v);
406 2985
}
407
408
/**********************************************************************
409
 * Vinyl listener
410
 */
411
412
static void *
413 2982
vinyl_thread(void *priv)
414
{
415
        struct vinyl *v;
416
417 2982
        CAST_OBJ_NOTNULL(v, priv, VINYL_MAGIC);
418 2982
        return (vtc_record(v->vl, v->fds[0], NULL));
419
}
420
421
/**********************************************************************
422
 * Launch a Vinyl
423
 */
424
425
static void
426 2982
vinyl_launch(struct vinyl *v)
427
{
428
        struct vsb *vsb, *vsb1;
429
        int i, nfd, asock;
430
        char abuf[VTCP_ADDRBUFSIZE];
431
        char pbuf[VTCP_PORTBUFSIZE];
432
        char lbuf[PATH_MAX];
433
        struct pollfd fd[3];
434
        enum VCLI_status_e u;
435
        const char *err;
436 2982
        char *r = NULL;
437
438
        /* Create listener socket */
439 2982
        asock = VTCP_listen_on(default_listen_addr, NULL, 1, &err);
440 2982
        if (err != NULL)
441 0
                vinyl_fatal(v, "Create CLI listen socket failed: %s", err);
442 2982
        assert(asock > 0);
443 2982
        VTCP_myname(asock, abuf, sizeof abuf, pbuf, sizeof pbuf);
444
445 2982
        AZ(VSB_finish(v->args));
446 2982
        vtc_log(v->vl, 2, "Launch");
447 2982
        vsb = VSB_new_auto();
448 2982
        AN(vsb);
449 2982
        VSB_cat(vsb, "cd ${pwd} &&");
450 5964
        VSB_printf(vsb, " exec %sd %s -d -n %s -i %s",
451 2982
            v->me, v->jail, v->workdir, v->name);
452 2982
        if (macro_isdef(NULL, "vinyld_args_prepend")) {
453 0
                VSB_putc(vsb, ' ');
454 0
                macro_cat(v->vl, vsb, "vinyld_args_prepend", NULL);
455 0
        }
456 0
        VSB_cat(vsb, VSB_data(params_vsb));
457 0
        if (leave_temp) {
458 0
                VSB_cat(vsb, " -p debug=+vcl_keep");
459 0
                VSB_cat(vsb, " -p debug=+vmod_so_keep");
460 0
                VSB_cat(vsb, " -p debug=+vsm_keep");
461 0
        }
462 2877
        VSB_cat(vsb, " -l 2m");
463 2877
        VSB_cat(vsb, " -p auto_restart=off");
464 2877
        VSB_cat(vsb, " -p syslog_cli_traffic=off");
465 2877
        VSB_cat(vsb, " -p thread_pool_min=10");
466 2877
        VSB_cat(vsb, " -p debug=+vtc_mode");
467 2877
        VSB_cat(vsb, " -p vsl_mask=+Debug,+H2RxHdr,+H2RxBody");
468 2877
        VSB_cat(vsb, " -p h2_initial_window_size=1m");
469 2877
        VSB_cat(vsb, " -p h2_rx_window_low_water=64k");
470 2877
        if (!v->has_a_arg) {
471 2877
                VSB_printf(vsb, " -a '%s'", default_listen_addr);
472 2877
                if (v->proto != NULL)
473 48
                        VSB_printf(vsb, ",%s", v->proto);
474 2877
        }
475 2979
        VSB_printf(vsb, " -M '%s %s'", abuf, pbuf);
476 2979
        VSB_printf(vsb, " -P %s/%sd.pid", v->workdir, v->me);
477 2979
        if (vmod_path != NULL)
478 2979
                VSB_printf(vsb, " -p vmod_path=%s", vmod_path);
479 5958
        VSB_printf(vsb, " %s", VSB_data(v->args));
480 5958
        if (macro_isdef(NULL, "vinyld_args_append")) {
481 0
                VSB_putc(vsb, ' ');
482 0
                macro_cat(v->vl, vsb, "vinyld_args_append", NULL);
483 0
        }
484 2982
        AZ(VSB_finish(vsb));
485 2982
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
486 2982
        vsb1 = macro_expand(v->vl, VSB_data(vsb));
487 2982
        AN(vsb1);
488 2982
        VSB_destroy(&vsb);
489 2982
        vsb = vsb1;
490 2982
        vtc_log(v->vl, 3, "CMD: %s", VSB_data(vsb));
491 2982
        AZ(pipe(&v->fds[0]));
492 2982
        AZ(pipe(&v->fds[2]));
493 2982
        v->pid = fork();
494 2982
        assert(v->pid >= 0);
495 5964
        if (v->pid == 0) {
496 2982
                AZ(dup2(v->fds[0], 0));
497 2982
                assert(dup2(v->fds[3], 1) == 1);
498 2982
                assert(dup2(1, 2) == 2);
499 2982
                closefd(&v->fds[0]);
500 2982
                closefd(&v->fds[1]);
501 2982
                closefd(&v->fds[2]);
502 2982
                closefd(&v->fds[3]);
503 2982
                VSUB_closefrom(STDERR_FILENO + 1);
504 2982
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0));
505 0
                exit(1);
506
        } else {
507 2982
                vtc_log(v->vl, 3, "PID: %ld", (long)v->pid);
508 2982
                macro_def(v->vl, v->name, "pid", "%ld", (long)v->pid);
509 2982
                macro_def(v->vl, v->name, "name", "%s", v->workdir);
510
        }
511 2982
        closefd(&v->fds[0]);
512 2982
        closefd(&v->fds[3]);
513 2982
        v->fds[0] = v->fds[2];
514 2982
        v->fds[2] = v->fds[3] = -1;
515 2982
        VSB_destroy(&vsb);
516 2982
        AZ(v->tp_started);
517 2982
        v->tp_started = 1;
518 2982
        PTOK(pthread_create(&v->tp, NULL, vinyl_thread, v));
519
520
        /* Wait for the vinyl to call home */
521 2982
        memset(fd, 0, sizeof fd);
522 2982
        fd[0].fd = asock;
523 2982
        fd[0].events = POLLIN;
524 2982
        fd[1].fd = v->fds[1];
525 2982
        fd[1].events = POLLIN;
526 2982
        fd[2].fd = v->fds[2];
527 2982
        fd[2].events = POLLIN;
528 2982
        i = poll(fd, 2, (int)(vtc_maxdur * 1000 / 3));
529 5964
        vtc_log(v->vl, 4, "CLIPOLL %d 0x%x 0x%x 0x%x",
530 2982
            i, fd[0].revents, fd[1].revents, fd[2].revents);
531 2982
        if (i == 0)
532 0
                vinyl_fatal(v, "FAIL timeout waiting for CLI connection");
533 0
        if (fd[1].revents & POLLHUP)
534 0
                vinyl_fatal(v, "FAIL debug pipe closed");
535 0
        if (!(fd[0].revents & POLLIN))
536 0
                vinyl_fatal(v, "FAIL CLI connection wait failure");
537 2982
        nfd = accept(asock, NULL, NULL);
538 2982
        closefd(&asock);
539 2982
        if (nfd < 0)
540 0
                vinyl_fatal(v, "FAIL no CLI connection accepted");
541
542 2982
        v->cli_fd = nfd;
543
544 2982
        vtc_log(v->vl, 3, "CLI connection fd = %d", v->cli_fd);
545 2982
        assert(v->cli_fd >= 0);
546
547
        /* Receive the banner or auth response */
548 2982
        u = vinyl_ask_cli(v, NULL, &r);
549 2982
        if (vtc_error)
550 0
                return;
551 2982
        if (u != CLIS_AUTH)
552 0
                vinyl_fatal(v, "CLI auth demand expected: %u %s", u, r);
553
554 2982
        bprintf(lbuf, "%s/_.secret", v->workdir);
555 2982
        nfd = open(lbuf, O_RDONLY);
556 2982
        assert(nfd >= 0);
557
558 2982
        assert(sizeof lbuf >= CLI_AUTH_RESPONSE_LEN + 7);
559 2982
        bstrcpy(lbuf, "auth ");
560 2982
        VCLI_AuthResponse(nfd, r, lbuf + 5);
561 2982
        closefd(&nfd);
562 2982
        free(r);
563 2982
        r = NULL;
564 2982
        strcat(lbuf, "\n");
565
566 2982
        u = vinyl_ask_cli(v, lbuf, &r);
567 2982
        if (vtc_error)
568 0
                return;
569 2982
        if (u != CLIS_OK)
570 0
                vinyl_fatal(v, "CLI auth command failed: %u %s", u, r);
571 2982
        free(r);
572
573 2982
        v->vsm_vsc = VSM_New();
574 2982
        AN(v->vsm_vsc);
575 2982
        v->vsc = VSC_New();
576 2982
        AN(v->vsc);
577 2982
        assert(VSM_Arg(v->vsm_vsc, 'n', v->workdir) > 0);
578 2982
        AZ(VSM_Attach(v->vsm_vsc, -1));
579
580 2982
        v->vsm_vsl = VSM_New();
581 2982
        assert(VSM_Arg(v->vsm_vsl, 'n', v->workdir) > 0);
582 2982
        AZ(VSM_Attach(v->vsm_vsl, -1));
583
584 2982
        PTOK(pthread_create(&v->tp_vsl, NULL, vinyllog_thread, v));
585 2982
}
586
587
#define VINYL_LAUNCH(v)                         \
588
        do {                                            \
589
                CHECK_OBJ_NOTNULL(v, VINYL_MAGIC);      \
590
                if (v->cli_fd < 0)                      \
591
                        vinyl_launch(v);                \
592
                if (vtc_error)                          \
593
                        return;                         \
594
        } while (0)
595
596
/**********************************************************************
597
 * Start a Vinyl
598
 */
599
600
static void
601 2940
vinyl_listen(const struct vinyl *v, char *la)
602
{
603
        const char *a, *p, *n, *n2;
604
        char m[64], s[256];
605
        unsigned first;
606
607 2940
        n2 = "";
608 2940
        first = 1;
609
610 5895
        while (*la != '\0') {
611 2955
                n = la;
612 2955
                la = strchr(la, ' ');
613 2955
                AN(la);
614 2955
                *la = '\0';
615 2955
                a = ++la;
616 2955
                la = strchr(la, ' ');
617 2955
                AN(la);
618 2955
                *la = '\0';
619 2955
                p = ++la;
620 2955
                la = strchr(la, '\n');
621 2955
                AN(la);
622 2955
                *la = '\0';
623 2955
                la++;
624
625 2955
                AN(*n);
626 2955
                AN(*a);
627 2955
                AN(*p);
628
629 2955
                if (*p == '-') {
630 84
                        bprintf(s, "%s", a);
631 84
                        a = "0.0.0.0";
632 84
                        p = "0";
633 2955
                } else if (strchr(a, ':')) {
634 0
                        bprintf(s, "[%s]:%s", a, p);
635 0
                } else {
636 2871
                        bprintf(s, "%s:%s", a, p);
637
                }
638
639 2955
                if (first) {
640 2940
                        vtc_log(v->vl, 2, "Listen on %s %s", a, p);
641 2940
                        macro_def(v->vl, v->name, "addr", "%s", a);
642 2940
                        macro_def(v->vl, v->name, "port", "%s", p);
643 2940
                        macro_def(v->vl, v->name, "sock", "%s", s);
644 2940
                        first = 0;
645 2940
                }
646
647 2955
                if (!vstrcmp(n, n2))
648 0
                        continue;
649
650 2955
                bprintf(m, "%s_addr", n);
651 2955
                macro_def(v->vl, v->name, m, "%s", a);
652 2955
                bprintf(m, "%s_port", n);
653 2955
                macro_def(v->vl, v->name, m, "%s", p);
654 2955
                bprintf(m, "%s_sock", n);
655 2955
                macro_def(v->vl, v->name, m, "%s", s);
656 2955
                n2 = n;
657
        }
658 2940
}
659
660
static void
661 2940
vinyl_start(struct vinyl *v)
662
{
663
        enum VCLI_status_e u;
664 2940
        char *resp = NULL;
665
666 2940
        VINYL_LAUNCH(v);
667 2940
        vtc_log(v->vl, 2, "Start");
668 2940
        u = vinyl_ask_cli(v, "start", &resp);
669 2940
        if (vtc_error)
670 0
                return;
671 2940
        if (u != CLIS_OK)
672 0
                vinyl_fatal(v, "CLI start command failed: %u %s", u, resp);
673 2940
        wait_running(v);
674 2940
        free(resp);
675 2940
        resp = NULL;
676 2940
        u = vinyl_ask_cli(v, "debug.xid 1000", &resp);
677 2940
        if (vtc_error)
678 0
                return;
679 2940
        if (u != CLIS_OK)
680 0
                vinyl_fatal(v, "CLI debug.xid command failed: %u %s",
681
                    u, resp);
682 2940
        free(resp);
683 2940
        resp = NULL;
684 2940
        u = vinyl_ask_cli(v, "debug.listen_address", &resp);
685 2940
        if (vtc_error)
686 0
                return;
687 2940
        if (u != CLIS_OK)
688 0
                vinyl_fatal(v,
689
                    "CLI debug.listen_address command failed: %u %s", u, resp);
690 2940
        vinyl_listen(v, resp);
691 2940
        free(resp);
692
        /* Wait for vsl logging to get underway */
693 2940
        while (v->vsl_rec == 0)
694 0
                VTIM_sleep(.1);
695 2940
}
696
697
/**********************************************************************
698
 * Stop a Vinyl
699
 */
700
701
static void
702 3147
vinyl_stop(struct vinyl *v)
703
{
704
705 3147
        VINYL_LAUNCH(v);
706 3147
        vtc_log(v->vl, 2, "Stop");
707 3147
        (void)vinyl_ask_cli(v, "stop", NULL);
708 3147
        wait_stopped(v);
709 3147
}
710
711
/**********************************************************************
712
 * Cleanup
713
 */
714
715
static void
716 2982
vinyl_cleanup(struct vinyl *v)
717
{
718
        void *p;
719
720
        /* Close the CLI connection */
721 2982
        closefd(&v->cli_fd);
722
723
        /* Close the STDIN connection. */
724 2982
        closefd(&v->fds[1]);
725
726
        /* Wait until STDOUT+STDERR closes */
727 2982
        AN(v->tp_started);
728 2982
        PTOK(pthread_join(v->tp, &p));
729 2982
        closefd(&v->fds[0]);
730 2982
        v->tp_started = 0;
731
732
        /* Pick up the VSL thread */
733 2982
        PTOK(pthread_join(v->tp_vsl, &p));
734
735 2982
        vtc_wait4(v->vl, v->pid, v->expect_exit, 0, 0);
736 2982
        v->pid = 0;
737 2982
}
738
739
/**********************************************************************
740
 * Wait for a Vinyl
741
 */
742
743
static void
744 2973
vinyl_wait(struct vinyl *v)
745
{
746 2973
        if (v->cli_fd < 0)
747 0
                return;
748
749 2973
        vtc_log(v->vl, 2, "Wait");
750
751 2973
        if (!vtc_error) {
752
                /* Do a backend.list to log if child is still running */
753 2973
                (void)vinyl_ask_cli(v, "backend.list", NULL);
754 2973
        }
755
756
        /* Then stop it */
757 5946
        vinyl_stop(v);
758
759 5946
        if (vinyl_ask_cli(v, "panic.show", NULL) != CLIS_CANT)
760 0
                vinyl_fatal(v, "Unexpected panic");
761
762 2973
        vinyl_cleanup(v);
763 2973
}
764
765
766
/**********************************************************************
767
 * Ask a CLI JSON question
768
 */
769
770
static void
771 114
vinyl_cli_json(struct vinyl *v, const char *cli)
772
{
773
        enum VCLI_status_e u;
774 114
        char *resp = NULL;
775
        const char *errptr;
776
        struct vjsn *vj;
777
778 114
        VINYL_LAUNCH(v);
779 114
        u = vinyl_ask_cli(v, cli, &resp);
780 114
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
781 114
        if (u != CLIS_OK)
782 0
                vinyl_fatal(v,
783
                    "FAIL CLI response %u expected %u", u, CLIS_OK);
784 0
        vj = vjsn_parse(resp, &errptr);
785 0
        if (vj == NULL)
786 0
                vinyl_fatal(v, "FAIL CLI, not good JSON: %s", errptr);
787 114
        vjsn_delete(&vj);
788 114
        free(resp);
789 114
}
790
791
/**********************************************************************
792
 * Ask a CLI question
793
 */
794
795
static void
796 3930
vinyl_cli(struct vinyl *v, const char *cli, unsigned exp, const char *re,
797
    int neg)
798
{
799
        enum VCLI_status_e u;
800
        struct vsb vsb[1];
801 3930
        vre_t *vre = NULL;
802 3930
        char *resp = NULL, errbuf[VRE_ERROR_LEN];
803
        int err, erroff;
804
805 3930
        VINYL_LAUNCH(v);
806 3930
        if (re != NULL) {
807 318
                vre = VRE_compile(re, 0, &err, &erroff, 1);
808 318
                if (vre == NULL) {
809 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
810 0
                        AZ(VRE_error(vsb, err));
811 0
                        AZ(VSB_finish(vsb));
812 0
                        VSB_fini(vsb);
813 0
                        vinyl_fatal(v, "Illegal regexp: %s (@%d)",
814
                            errbuf, erroff);
815 0
                }
816 318
        }
817 3468
        u = vinyl_ask_cli(v, cli, &resp);
818 3468
        vtc_log(v->vl, 2, "CLI %03u <%s>", u, cli);
819 3468
        if (exp != 0 && exp != (unsigned)u)
820 0
                vinyl_fatal(v, "FAIL CLI response %u expected %u", u, exp);
821 318
        if (vre != NULL) {
822 318
                err = VRE_match(vre, resp, 0, 0, NULL);
823 318
                if (err < VRE_ERROR_NOMATCH) {
824 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
825 0
                        AZ(VRE_error(vsb, err));
826 0
                        AZ(VSB_finish(vsb));
827 0
                        VSB_fini(vsb);
828 0
                        vinyl_fatal(v, "Regexp failed: %s with %s (%d)",
829
                            re, errbuf, err);
830 318
                } else if ((err == VRE_ERROR_NOMATCH) == (neg == 0)) { /*lint !e731 */
831 0
                        vinyl_fatal(v, "Expect failed: regexp \"%s\" did%s "
832
                            "match response \"%s\"", re, neg ? "" : " not",
833
                            resp);
834 0
                }
835 318
                VRE_free(&vre);
836 318
        }
837 3930
        free(resp);
838 3930
}
839
840
static const char *
841 4632
vcl_prepend(void)
842
{
843
        const char *vcl_pfx;
844
845 4632
        vcl_pfx = getenv("VTEST_VINYL_VCL_PREPEND");
846 4632
        return vcl_pfx ? vcl_pfx : "";
847
}
848
849
/**********************************************************************
850
 * Load a VCL program
851
 */
852
853
static void
854 1863
vinyl_vcl(struct vinyl *v, const char *vcl, int fail, char **resp)
855
{
856
        struct vsb *vsb;
857
        enum VCLI_status_e u;
858
859 1863
        VINYL_LAUNCH(v);
860 1863
        vsb = VSB_new_auto();
861 1863
        AN(vsb);
862
863 3726
        VSB_printf(vsb, "vcl.inline vcl%d << %s\nvcl %.1f;\n%s%s\n%s\n",
864 1863
            ++v->vcl_nbr, NONSENSE, v->syntax,
865 1863
            vcl_prepend(), vcl, NONSENSE);
866 1863
        AZ(VSB_finish(vsb));
867
868 1863
        u = vinyl_ask_cli(v, VSB_data(vsb), resp);
869 1863
        if (u == CLIS_OK) {
870 846
                VSB_clear(vsb);
871 846
                VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
872 846
                AZ(VSB_finish(vsb));
873 846
                u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
874 846
        }
875 846
        if (u == CLIS_OK && fail) {
876 0
                VSB_destroy(&vsb);
877 0
                vinyl_fatal(v, "VCL compilation succeeded expected failure");
878 1017
        } else if (u != CLIS_OK && !fail) {
879 0
                VSB_destroy(&vsb);
880 0
                vinyl_fatal(v, "VCL compilation failed expected success");
881 1017
        } else if (fail)
882 1017
                vtc_log(v->vl, 2, "VCL compilation failed (as expected)");
883 1863
        VSB_destroy(&vsb);
884 1863
}
885
886
/**********************************************************************
887
 * Load a VCL program prefixed by backend decls for our servers
888
 */
889
890
static void
891 2769
vinyl_vclbackend(struct vinyl *v, const char *vcl)
892
{
893
        struct vsb *vsb, *vsb2;
894
        enum VCLI_status_e u;
895
896 2769
        VINYL_LAUNCH(v);
897 2769
        vsb = VSB_new_auto();
898 2769
        AN(vsb);
899
900 2769
        vsb2 = VSB_new_auto();
901 2769
        AN(vsb2);
902
903 2769
        VSB_printf(vsb2, "vcl %.1f;\n", v->syntax);
904
905 2769
        cmd_server_gen_vcl(vsb2);
906
907 2769
        AZ(VSB_finish(vsb2));
908
909 5538
        VSB_printf(vsb, "vcl.inline vcl%d << %s\n%s\n%s%s\n%s\n",
910 2769
            ++v->vcl_nbr, NONSENSE, VSB_data(vsb2), vcl_prepend(),
911 2769
            vcl, NONSENSE);
912 2769
        AZ(VSB_finish(vsb));
913
914 2769
        u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
915 2769
        if (u != CLIS_OK) {
916 0
                VSB_destroy(&vsb);
917 0
                VSB_destroy(&vsb2);
918 0
                vinyl_fatal(v, "FAIL VCL does not compile");
919 0
        }
920 2769
        VSB_clear(vsb);
921 2769
        VSB_printf(vsb, "vcl.use vcl%d", v->vcl_nbr);
922 2769
        AZ(VSB_finish(vsb));
923 2769
        u = vinyl_ask_cli(v, VSB_data(vsb), NULL);
924 2769
        assert(u == CLIS_OK);
925 2769
        VSB_destroy(&vsb);
926 2769
        VSB_destroy(&vsb2);
927 2769
}
928
929
/**********************************************************************
930
 */
931
932
struct dump_priv {
933
        const char *arg;
934
        const struct vinyl *v;
935
};
936
937
static int
938 12594
do_stat_dump_cb(void *priv, const struct VSC_point * const pt)
939
{
940
        const struct vinyl *v;
941
        struct dump_priv *dp;
942
        uint64_t u;
943
944 12594
        if (pt == NULL)
945 0
                return (0);
946 12594
        dp = priv;
947 12594
        v = dp->v;
948
949 12594
        if (vstrcmp(pt->ctype, "uint64_t"))
950 0
                return (0);
951 12594
        u = VSC_Value(pt);
952
953 12594
        if (vstrcmp(dp->arg, "*")) {
954 12594
                if (fnmatch(dp->arg, pt->name, 0))
955 12171
                        return (0);
956 423
        }
957
958 423
        vtc_log(v->vl, 4, "VSC %s %ju",  pt->name, (uintmax_t)u);
959 423
        return (0);
960 12594
}
961
962
static void
963 30
vinyl_vsc(struct vinyl *v, const char *arg)
964
{
965
        struct dump_priv dp;
966
967 30
        VINYL_LAUNCH(v);
968 30
        memset(&dp, 0, sizeof dp);
969 30
        dp.v = v;
970 30
        dp.arg = arg;
971 30
        (void)VSM_Status(v->vsm_vsc);
972 30
        (void)VSC_Iter(v->vsc, v->vsm_vsc, do_stat_dump_cb, &dp);
973 30
}
974
975
/**********************************************************************
976
 * Check statistics
977
 */
978
979
struct stat_arg {
980
        const char      *pattern;
981
        uintmax_t       val;
982
        unsigned        good;
983
};
984
985
struct stat_priv {
986
        struct stat_arg lhs;
987
        struct stat_arg rhs;
988
};
989
990
static int
991 904683
stat_match(const char *pattern, const char *name)
992
{
993
994 904683
        if (strchr(pattern, '.') == NULL) {
995 194536
                if (fnmatch("MAIN.*", name, 0))
996 13293
                        return (FNM_NOMATCH);
997 181243
                name += 5;
998 181243
        }
999 891390
        return (fnmatch(pattern, name, 0));
1000 904683
}
1001
1002
static int
1003 904200
do_expect_cb(void *priv, const struct VSC_point * const pt)
1004
{
1005 904200
        struct stat_priv *sp = priv;
1006
1007 904200
        if (pt == NULL)
1008 0
                return (0);
1009
1010 904200
        if (!sp->lhs.good && stat_match(sp->lhs.pattern, pt->name) == 0) {
1011 3262
                AZ(vstrcmp(pt->ctype, "uint64_t"));
1012 3262
                AN(pt->ptr);
1013 3262
                sp->lhs.val = VSC_Value(pt);
1014 3262
                sp->lhs.good = 1;
1015 3262
        }
1016
1017 904200
        if (sp->rhs.pattern == NULL) {
1018 903705
                sp->rhs.good = 1;
1019 904200
        } else if (!sp->rhs.good &&
1020 495
            stat_match(sp->rhs.pattern, pt->name) == 0) {
1021 12
                AZ(vstrcmp(pt->ctype, "uint64_t"));
1022 12
                AN(pt->ptr);
1023 12
                sp->rhs.val = VSC_Value(pt);
1024 12
                sp->rhs.good = 1;
1025 12
        }
1026
1027 904200
        return (sp->lhs.good && sp->rhs.good);
1028 904200
}
1029
1030
/**********************************************************************
1031
 */
1032
1033
static void
1034 2889
vinyl_expect(struct vinyl *v, char * const *av)
1035
{
1036
        struct stat_priv sp;
1037
        int good, i, not;
1038
        uintmax_t u;
1039
        char *l, *p;
1040
1041 2889
        VINYL_LAUNCH(v);
1042 2889
        ZERO_OBJ(&sp, sizeof sp);
1043 2889
        l = av[0];
1044 2889
        not = (*l == '!');
1045 2889
        if (not) {
1046 15
                l++;
1047 15
                AZ(av[1]);
1048 15
        } else {
1049 2874
                AN(av[1]);
1050 2874
                AN(av[2]);
1051 2874
                errno = 0;
1052 2874
                u = strtoumax(av[2], &p, 0);
1053 2874
                if (errno != ERANGE && *p == '\0')
1054 2862
                        sp.rhs.val = u;
1055
                else
1056 12
                        sp.rhs.pattern = av[2];
1057
        }
1058
1059 2889
        sp.lhs.pattern = l;
1060
1061 4027
        for (i = 0; i < 50; i++, (void)usleep(100000)) {
1062 4012
                (void)VSM_Status(v->vsm_vsc);
1063 4012
                sp.lhs.good = sp.rhs.good = 0;
1064 4012
                good = VSC_Iter(v->vsc, v->vsm_vsc, do_expect_cb, &sp);
1065 4012
                if (!good)
1066 750
                        good = -2;
1067 4012
                if (good < 0)
1068 750
                        continue;
1069
1070 3262
                if (not)
1071 0
                        vinyl_fatal(v, "Found (not expected): %s", l);
1072
1073 2893
                good = -1;
1074 2893
                if (!strcmp(av[1], "==")) good = (sp.lhs.val == sp.rhs.val);
1075 5780
                if (!strcmp(av[1], "!=")) good = (sp.lhs.val != sp.rhs.val);
1076 162
                if (!strcmp(av[1], ">" )) good = (sp.lhs.val >  sp.rhs.val);
1077 225
                if (!strcmp(av[1], "<" )) good = (sp.lhs.val <  sp.rhs.val);
1078 108
                if (!strcmp(av[1], ">=")) good = (sp.lhs.val >= sp.rhs.val);
1079 168
                if (!strcmp(av[1], "<=")) good = (sp.lhs.val <= sp.rhs.val);
1080 24
                if (good == -1)
1081 0
                        vinyl_fatal(v, "comparison %s unknown", av[1]);
1082 3262
                if (good)
1083 2874
                        break;
1084 388
        }
1085 5748
        if (good == -1) {
1086 0
                vinyl_fatal(v, "VSM error: %s", VSM_Error(v->vsm_vsc));
1087 0
        }
1088 15
        if (good == -2) {
1089 15
                if (not) {
1090 15
                        vtc_log(v->vl, 2, "not found (as expected): %s", l);
1091 15
                        return;
1092
                }
1093 0
                vinyl_fatal(v, "stats field %s unknown",
1094
                    sp.lhs.good ? sp.rhs.pattern : sp.lhs.pattern);
1095 0
        }
1096
1097 2874
        if (good == 1) {
1098 5748
                vtc_log(v->vl, 2, "as expected: %s (%ju) %s %s (%ju)",
1099 2874
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1100 2874
        } else {
1101 0
                vinyl_fatal(v, "Not true: %s (%ju) %s %s (%ju)",
1102
                    av[0], sp.lhs.val, av[1], av[2], sp.rhs.val);
1103
        }
1104 2889
}
1105
1106
static void
1107 429
vsl_catchup(struct vinyl *v)
1108
{
1109
        int vsl_idle;
1110
1111 429
        VINYL_LAUNCH(v);
1112 429
        vsl_idle = v->vsl_idle;
1113 858
        while (!vtc_error && vsl_idle == v->vsl_idle)
1114 429
                VTIM_sleep(0.1);
1115 429
}
1116
1117
/* SECTION: vinyl vinyl
1118
 *
1119
 * Define and interact with vinyl instances.
1120
 *
1121
 * To define a ``vinyld``, you'll use this syntax::
1122
 *
1123
 *      vinyl vNAME [-arg STRING] [-vcl STRING] [-vcl+backend STRING]
1124
 *              [-errvcl STRING STRING] [-jail STRING] [-proto PROXY]
1125
 *
1126
 * The first ``vinyl vNAME`` invocation will start the vinyld master
1127
 * process in the background, waiting for the ``-start`` switch to actually
1128
 * start the child.
1129
 *
1130
 * Types used in the description below:
1131
 *
1132
 * PATTERN
1133
 *         is a 'glob' style pattern (ie: fnmatch(3)) as used in shell filename
1134
 *         expansion.
1135
 *
1136
 * Arguments:
1137
 *
1138
 * vNAME
1139
 *         Identify the ``vinyld`` server with a string, it must starts with 'v'.
1140
 *
1141
 * \-arg STRING
1142
 *         Pass an argument to vinyld, for example "-h simple_list".
1143
 *
1144
 *         If the ${vinyld_args_prepend} or ${vinyld_args_append} macros are
1145
 *         defined, they are expanded and inserted before / appended to the
1146
 *         vinyld command line as constructed by vinyltest, before the
1147
 *         command line itself is expanded. This enables tweaks to the vinyld
1148
 *         command line without editing test cases. This macros can be defined
1149
 *         using the ``-D`` option for vinyltest.
1150
 *
1151
 * \-vcl STRING
1152
 *         Specify the VCL to load on this ``vinyld`` instance. You'll probably
1153
 *         want to use multi-lines strings for this ({...}).
1154
 *
1155
 * \-vcl+backend STRING
1156
 *         Do the exact same thing as -vcl, but adds the definition block of
1157
 *         known backends (ie. already defined).
1158
 *
1159
 * \-errvcl STRING1 STRING2
1160
 *         Load STRING2 as VCL, expecting it to fail, and ``vinyld`` to send an
1161
 *         error string matching STRING1
1162
 *
1163
 * \-jail STRING
1164
 *         Look at ``man vinyld`` (-j) for more information.
1165
 *
1166
 * \-proto PROXY
1167
 *         Have ``vinyld`` use the proxy protocol. Note that PROXY here is the
1168
 *         actual string.
1169
 *
1170
 * You can decide to start the ``vinyld`` instance and/or wait for several events::
1171
 *
1172
 *         vinyl vNAME [-start] [-wait] [-wait-running] [-wait-stopped]
1173
 *
1174
 * \-start
1175
 *         Start the child process.
1176
 *
1177
 *         Once successfully started, the following macros are available for
1178
 *         the default listen address: ``${vNAME_addr}``, ``${vNAME_port}``
1179
 *         and ``${vNAME_sock}``. Additional macros are available, including
1180
 *         the listen address name for each address vNAME listens to, like for
1181
 *         example: ``${vNAME_a0_addr}``.
1182
 *
1183
 * \-stop
1184
 *         Stop the child process.
1185
 *
1186
 * \-syntax
1187
 *         Set the VCL syntax level for this command (default: 4.1)
1188
 *
1189
 * \-wait
1190
 *         Wait for that instance to terminate.
1191
 *
1192
 * \-wait-running
1193
 *         Wait for the ``vinyld`` child process to be started.
1194
 *
1195
 * \-wait-stopped
1196
 *         Wait for the ``vinyld`` child process to stop.
1197
 *
1198
 * \-cleanup
1199
 *         Once ``vinyld`` is stopped, clean everything after it. This is only used
1200
 *         in very few tests and you should never need it.
1201
 *
1202
 * \-expectexit NUMBER
1203
 *         Expect vinyld to exit(3) with this value
1204
 *
1205
 * Once ``vinyld`` is started, you can talk to it (as you would through
1206
 * ``vinyladm``) with these additional switches::
1207
 *
1208
 *         vinyl vNAME [-cli STRING] [-cliok STRING] [-clierr STRING]
1209
 *                       [-clijson STRING]
1210
 *
1211
 * \-cli STRING|-cliok STRING|-clierr STATUS STRING|-cliexpect [!] REGEXP STRING
1212
 *         All four of these will send STRING to the CLI, the only difference
1213
 *         is what they expect the result to be. -cli doesn't expect
1214
 *         anything, -cliok expects 200, -clierr expects STATUS, and
1215
 *         -cliexpect expects the REGEXP to match the returned response.
1216
 *         -cliexpect ! ... negates the match
1217
 *
1218
 * \-clijson STRING
1219
 *         Send STRING to the CLI, expect success (CLIS_OK/200) and check
1220
 *         that the response is parsable JSON.
1221
 *
1222
 * It is also possible to interact with its shared memory (as you would
1223
 * through tools like ``vinylstat``) with additional switches:
1224
 *
1225
 * \-expect \!PATTERN|PATTERN OP NUMBER|PATTERN OP PATTERN
1226
 *         Look into the VSM and make sure the first VSC counter identified by
1227
 *         PATTERN has a correct value. OP can be ==, >, >=, <, <=. For
1228
 *         example::
1229
 *
1230
 *                 vinyl v1 -expect SM?.s1.g_space > 1000000
1231
 *                 vinyl v1 -expect cache_hit >= cache_hit_grace
1232
 *
1233
 *         In the \! form the test fails if a counter matches PATTERN.
1234
 *
1235
 *         The ``MAIN.`` namespace can be omitted from PATTERN.
1236
 *
1237
 *         The test takes up to 5 seconds before timing out.
1238
 *
1239
 * \-vsc PATTERN
1240
 *         Dump VSC counters matching PATTERN.
1241
 *
1242
 * \-vsl_catchup
1243
 *         Wait until the logging thread has idled to make sure that all
1244
 *         the generated log is flushed
1245
 *
1246
 * Environment variables:
1247
 *
1248
 * VTEST_VINYL_VCL_PREPEND can be used to inject VCL code into all VCL created
1249
 * by the vinyl command before VCL provided by the caller using the -vcl or
1250
 * -vcl+backend argument. That is, VTEST_VINYL_VCL_PREPEND is injected after the
1251
 * implicit "vcl x.y;" statement and backend definitions generated by the vinyl
1252
 * command.
1253
 */
1254
1255
void
1256 15630
cmd_vinyl(CMD_ARGS)
1257
{
1258
        struct vinyl *v, *v2;
1259
        const char *me;
1260
1261 15630
        (void)priv;
1262
1263 15630
        if (av == NULL) {
1264
                /* Reset and free */
1265 6258
                VTAILQ_FOREACH_SAFE(v, &vinyles, list, v2) {
1266 2985
                        if (v->cli_fd >= 0)
1267 2952
                                vinyl_wait(v);
1268 2985
                        VTAILQ_REMOVE(&vinyles, v, list);
1269 2985
                        vinyl_delete(v);
1270 2985
                }
1271 3273
                return;
1272
        }
1273
1274 12357
        me = av[0];
1275
        // AZ(strcmp(av[0], "vinyl"));
1276 12357
        av++;
1277
1278 12357
        VTC_CHECK_NAME(vl, av[0], "Vinyl", 'v');
1279 12696
        VTAILQ_FOREACH(v, &vinyles, list)
1280 9711
                if (!vstrcmp(v->name, av[0]))
1281 9372
                        break;
1282 15759
        if (v == NULL) {
1283 2985
                v = vinyl_new(av[0]);
1284 2985
                v->me =strdup(me);
1285 2985
                AN(v->me);
1286 2985
        }
1287 12357
        av++;
1288 12357
        v->syntax = 4.1;
1289
1290 28749
        for (; *av != NULL; av++) {
1291 16395
                if (vtc_error)
1292 0
                        break;
1293 16395
                if (!vstrcmp(*av, "-arg")) {
1294 951
                        AN(av[1]);
1295 951
                        AZ(v->pid);
1296 951
                        VSB_cat(v->args, " ");
1297 951
                        VSB_cat(v->args, av[1]);
1298 951
                        if (av[1][0] == '-' && av[1][1] == 'a')
1299 111
                                v->has_a_arg = 1;
1300 951
                        av++;
1301 951
                        continue;
1302
                }
1303 15444
                if (!vstrcmp(*av, "-cleanup")) {
1304 9
                        AZ(av[1]);
1305 9
                        vinyl_cleanup(v);
1306 9
                        continue;
1307
                }
1308 15435
                if (!vstrcmp(*av, "-cli")) {
1309 144
                        AN(av[1]);
1310 144
                        vinyl_cli(v, av[1], 0, NULL, 0);
1311 144
                        av++;
1312 144
                        continue;
1313
                }
1314 15291
                if (!vstrcmp(*av, "-clierr")) {
1315 402
                        AN(av[1]);
1316 402
                        AN(av[2]);
1317 402
                        vinyl_cli(v, av[2], atoi(av[1]), NULL, 0);
1318 402
                        av += 2;
1319 402
                        continue;
1320
                }
1321 14889
                if (!vstrcmp(*av, "-cliexpect")) {
1322 318
                        int neg = 0;
1323
1324 318
                        AN(av[1]);
1325 318
                        AN(av[2]);
1326 318
                        if (*av[1] == '!') {
1327 3
                                neg = 1;
1328 3
                                av++;
1329 3
                                AN(av[2]);
1330 3
                        }
1331 318
                        vinyl_cli(v, av[2], 0, av[1], neg);
1332 318
                        av += 2;
1333 318
                        continue;
1334
                }
1335 14571
                if (!vstrcmp(*av, "-clijson")) {
1336 114
                        AN(av[1]);
1337 114
                        vinyl_cli_json(v, av[1]);
1338 114
                        av++;
1339 114
                        continue;
1340
                }
1341 14457
                if (!vstrcmp(*av, "-cliok")) {
1342 3066
                        AN(av[1]);
1343 3066
                        vinyl_cli(v, av[1], (unsigned)CLIS_OK, NULL, 0);
1344 3066
                        av++;
1345 3066
                        continue;
1346
                }
1347 11391
                if (!vstrcmp(*av, "-errvcl")) {
1348 1017
                        char *r = NULL;
1349 1017
                        AN(av[1]);
1350 1017
                        AN(av[2]);
1351 1017
                        vinyl_vcl(v, av[2], 1, &r);
1352 1017
                        if (strstr(r, av[1]) == NULL)
1353 0
                                vinyl_fatal(v,
1354
                                    "Did not find expected string: (\"%s\")",
1355
                                    av[1]);
1356
                        else
1357 2034
                                vtc_log(v->vl, 3,
1358
                                    "Found expected string: (\"%s\")",
1359 1017
                                    av[1]);
1360 1017
                        free(r);
1361 1017
                        av += 2;
1362 1017
                        continue;
1363
                }
1364 10374
                if (!vstrcmp(*av, "-expect")) {
1365 2889
                        av++;
1366 2889
                        vinyl_expect(v, av);
1367 2889
                        av += 2;
1368 2889
                        continue;
1369
                }
1370 7485
                if (!vstrcmp(*av, "-expectexit")) {
1371 30
                        v->expect_exit = strtoul(av[1], NULL, 0);
1372 30
                        av++;
1373 30
                        continue;
1374
                }
1375 7455
                if (!vstrcmp(*av, "-jail")) {
1376 15
                        AN(av[1]);
1377 15
                        AZ(v->pid);
1378 15
                        REPLACE(v->jail, av[1]);
1379 15
                        av++;
1380 15
                        continue;
1381
                }
1382 7440
                if (!vstrcmp(*av, "-proto")) {
1383 48
                        AN(av[1]);
1384 48
                        AZ(v->pid);
1385 48
                        REPLACE(v->proto, av[1]);
1386 48
                        av++;
1387 48
                        continue;
1388
                }
1389 7392
                if (!vstrcmp(*av, "-start")) {
1390 2940
                        vinyl_start(v);
1391 2940
                        continue;
1392
                }
1393 4452
                if (!vstrcmp(*av, "-stop")) {
1394 174
                        vinyl_stop(v);
1395 174
                        continue;
1396
                }
1397 4278
                if (!vstrcmp(*av, "-syntax")) {
1398 162
                        AN(av[1]);
1399 162
                        v->syntax = strtod(av[1], NULL);
1400 162
                        av++;
1401 162
                        continue;
1402
                }
1403 4116
                if (!vstrcmp(*av, "-vcl")) {
1404 846
                        AN(av[1]);
1405 846
                        vinyl_vcl(v, av[1], 0, NULL);
1406 846
                        av++;
1407 846
                        continue;
1408
                }
1409 3270
                if (!vstrcmp(*av, "-vcl+backend")) {
1410 2769
                        AN(av[1]);
1411 2769
                        vinyl_vclbackend(v, av[1]);
1412 2769
                        av++;
1413 2769
                        continue;
1414
                }
1415 501
                if (!vstrcmp(*av, "-vsc")) {
1416 30
                        AN(av[1]);
1417 30
                        vinyl_vsc(v, av[1]);
1418 30
                        av++;
1419 30
                        continue;
1420
                }
1421 471
                if (!vstrcmp(*av, "-wait-stopped")) {
1422 9
                        wait_stopped(v);
1423 9
                        continue;
1424
                }
1425 462
                if (!vstrcmp(*av, "-wait-running")) {
1426 9
                        wait_running(v);
1427 9
                        continue;
1428
                }
1429 453
                if (!vstrcmp(*av, "-wait")) {
1430 21
                        vinyl_wait(v);
1431 21
                        continue;
1432
                }
1433 432
                if (!vstrcmp(*av, "-vsl_catchup")) {
1434 429
                        vsl_catchup(v);
1435 429
                        continue;
1436
                }
1437 3
                vinyl_fatal(v, "Unknown %s argument: %s", me, *av);
1438 0
        }
1439 15627
}
1440
1441
#endif /* VTEST_WITH_VTC_VINYL */