Code Coverage: vinyl-cache/bin/vinyltest/vtest_ext_vinyl_logexp.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Martin Blix Grydeland <martin@varnish-software.com>
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
/* SECTION: logexpect logexpect
31
 *
32
 * Reads the VSL and looks for records matching a given specification. It will
33
 * process records trying to match the first pattern, and when done, will
34
 * continue processing, trying to match the following pattern. If a pattern
35
 * isn't matched, the test will fail.
36
 *
37
 * logexpect threads are declared this way::
38
 *
39
 *         logexpect lNAME -v <id> [-g <grouping>] [-d 0|1] [-q query] \
40
 *                 [vsl arguments] {
41
 *                         expect <skip> <vxid> <tag> <regex>
42
 *                         expect <skip> <vxid> <tag> <regex>
43
 *                         fail add <vxid> <tag> <regex>
44
 *                         fail clear
45
 *                         abort
46
 *                         ...
47
 *                 } [-start|-wait|-run]
48
 *
49
 * And once declared, you can start them, or wait on them::
50
 *
51
 *         logexpect lNAME <-start|-wait>
52
 *
53
 * With:
54
 *
55
 * lNAME
56
 *         Name the logexpect thread, it must start with 'l'.
57
 *
58
 * \-v id
59
 *         Specify the vinyl instance to use (most of the time, id=v1).
60
 *
61
 * \-g <session|request|vxid|raw
62
 *         Decide how records are grouped, see -g in ``man vinyllog`` for more
63
 *         information.
64
 *
65
 * \-d <0|1>
66
 *         Start processing log records at the head of the log instead of the
67
 *         tail.
68
 *
69
 * \-q query
70
 *         Filter records using a query expression, see ``man vsl-query`` for
71
 *         more information. Multiple -q options are not supported.
72
 *
73
 * \-m
74
 *         Also emit log records for misses (only for debugging)
75
 *
76
 * \-err
77
 *         Invert the meaning of success. Usually called once to expect the
78
 *         logexpect to fail
79
 *
80
 * \-start
81
 *         Start the logexpect thread in the background.
82
 *
83
 * \-wait
84
 *         Wait for the logexpect thread to finish
85
 *
86
 * \-run
87
 *        Equivalent to "-start -wait".
88
 *
89
 * VSL arguments (similar to the vinyllog options):
90
 *
91
 * \-C
92
 *         Use caseless regex
93
 *
94
 * \-i <taglist>
95
 *         Include tags
96
 *
97
 * \-I <[taglist:]regex>
98
 *         Include by regex
99
 *
100
 * \-T <seconds>
101
 *         Transaction end timeout
102
 *
103
 * expect specification:
104
 *
105
 * skip: [uint|*|?]
106
 *         Max number of record to skip
107
 *
108
 * vxid: [uint|*|=]
109
 *         vxid to match
110
 *
111
 * tag:  [tagname|*|=]
112
 *         Tag to match against
113
 *
114
 * regex:
115
 *         regular expression to match against (optional)
116
 *
117
 * For skip, vxid and tag, '*' matches anything, '=' expects the value of the
118
 * previous matched record. The '?' marker is equivalent to zero, expecting a
119
 * match on the next record. The difference is that '?' can be used when the
120
 * order of individual consecutive logs is not deterministic. In other words,
121
 * lines from a block of alternatives marked by '?' can be matched in any order,
122
 * but all need to match eventually.
123
 *
124
 * fail specification:
125
 *
126
 * add: Add to the fail list
127
 *
128
 *      Arguments are equivalent to expect, except for skip missing
129
 *
130
 * clear: Clear the fail list
131
 *
132
 * Any number of fail specifications can be active during execution of
133
 * a logexpect. All active fail specifications are matched against every
134
 * log line and, if any match, the logexpect fails immediately.
135
 *
136
 * For a logexpect to end successfully, there must be no specs on the fail list,
137
 * so logexpects should always end with
138
 *
139
 *      expect <skip> <vxid> <tag> <termination-condition>
140
 *      fail clear
141
 *
142
 * .. XXX can we come up with a better solution which is still safe?
143
 *
144
 * abort specification:
145
 *
146
 * abort(3) vinyltest, intended to help debugging of the VSL client library
147
 * itself.
148
 */
149
150
#include "config.h"
151
152
#include <pthread.h>
153
#include <signal.h>
154
#include <stdint.h>
155
#include <stdio.h>
156
#include <stdlib.h>
157
#include <string.h>
158
159
#include "vapi/vsm.h"
160
#include "vapi/vsl.h"
161
162
#include "vdef.h"
163
164
#include "vas.h"
165
#include "miniobj.h"
166
#include "vqueue.h"
167
#include "vre.h"
168
#include "vsb.h"
169
#include "vtim.h"
170
171
#include <vtest_api.h>
172
#include "vtest_ext_vinyl.h"
173
174 67411
#define LE_ANY   (-1)
175 71308
#define LE_LAST  (-2)
176 37264
#define LE_ALT   (-3)
177 2146
#define LE_SEEN  (-4)
178 35345
#define LE_FAIL  (-5)
179 198
#define LE_CLEAR (-6)   // clear fail list
180 0
#define LE_ABORT (-7)
181
182
struct logexp_test {
183
        unsigned                        magic;
184
#define LOGEXP_TEST_MAGIC               0x6F62B350
185
        VTAILQ_ENTRY(logexp_test)       list;
186
        VTAILQ_ENTRY(logexp_test)       faillist;
187
188
        struct vsb                      *str;
189
        int64_t                         vxid;
190
        int                             tag;
191
        vre_t                           *vre;
192
        int                             skip_max;
193
};
194
195
VTAILQ_HEAD(tests_head,logexp_test);
196
197
struct logexp {
198
        unsigned                        magic;
199
#define LOGEXP_MAGIC                    0xE81D9F1B
200
        VTAILQ_ENTRY(logexp)            list;
201
202
        char                            *name;
203
        char                            *vname;
204
        struct vtclog                   *vl;
205
        char                            run;
206
        struct tests_head               tests;
207
208
        struct logexp_test              *test;
209
        int                             skip_cnt;
210
        int64_t                         vxid_last;
211
        int                             tag_last;
212
213
        struct tests_head               fail;
214
215
        int                             m_arg;
216
        int                             err_arg;
217
        int                             d_arg;
218
        enum VSL_grouping_e             g_arg;
219
        char                            *query;
220
221
        struct vsm                      *vsm;
222
        struct VSL_data                 *vsl;
223
        struct VSLQ                     *vslq;
224
        pthread_t                       tp;
225
};
226
227
static VTAILQ_HEAD(, logexp)            logexps =
228
        VTAILQ_HEAD_INITIALIZER(logexps);
229
230
static cmd_f cmd_logexp_expect;
231
static cmd_f cmd_logexp_fail;
232
static cmd_f cmd_logexp_abort;
233
234
static const struct cmds logexp_cmds[] = {
235
        { CMDS_MAGIC, "expect",         cmd_logexp_expect },
236
        { CMDS_MAGIC, "fail",           cmd_logexp_fail },
237
        { CMDS_MAGIC, "abort",          cmd_logexp_abort },
238
        { CMDS_MAGIC, NULL,             NULL },
239
};
240
241
static void
242
logexp_delete_tests(struct logexp *le)
243 653
{
244 653
        struct logexp_test *test;
245
246 653
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
247 2765
        while (!VTAILQ_EMPTY(&le->tests)) {
248 2112
                test = VTAILQ_FIRST(&le->tests);
249 2112
                CHECK_OBJ_NOTNULL(test, LOGEXP_TEST_MAGIC);
250 2112
                VTAILQ_REMOVE(&le->tests, test, list);
251 2112
                VSB_destroy(&test->str);
252 2112
                if (test->vre)
253 1751
                        VRE_free(&test->vre);
254 2112
                FREE_OBJ(test);
255 2112
        }
256 653
}
257
258
static void
259
logexp_delete(struct logexp *le)
260 298
{
261 298
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
262 298
        AZ(le->run);
263 298
        AN(le->vsl);
264 298
        VSL_Delete(le->vsl);
265 298
        AZ(le->vslq);
266 298
        logexp_delete_tests(le);
267 298
        free(le->name);
268 298
        free(le->vname);
269 298
        free(le->query);
270 298
        VSM_Destroy(&le->vsm);
271 298
        vtc_logclose(le->vl);
272 298
        FREE_OBJ(le);
273 298
}
274
275
static struct logexp *
276
logexp_new(const char *name, const char *varg)
277 298
{
278 298
        struct logexp *le;
279 298
        struct vsb *n_arg;
280
281 298
        ALLOC_OBJ(le, LOGEXP_MAGIC);
282 298
        AN(le);
283 298
        REPLACE(le->name, name);
284 298
        le->vl = vtc_logopen("%s", name);
285 298
        vtc_log_set_cmd(le->vl, logexp_cmds);
286 298
        VTAILQ_INIT(&le->tests);
287
288 298
        le->d_arg = 0;
289 298
        le->g_arg = VSL_g_vxid;
290 298
        le->vsm = VSM_New();
291 298
        le->vsl = VSL_New();
292 298
        AN(le->vsm);
293 298
        AN(le->vsl);
294
295 298
        VTAILQ_INSERT_TAIL(&logexps, le, list);
296
297 298
        REPLACE(le->vname, varg);
298
299 298
        n_arg = macro_expandf(le->vl, "${tmpdir}/%s", varg);
300 298
        if (n_arg == NULL)
301 0
                vtc_fatal(le->vl, "-v argument problems");
302 298
        if (VSM_Arg(le->vsm, 'n', VSB_data(n_arg)) <= 0)
303 0
                vtc_fatal(le->vl, "-v argument error: %s",
304 0
                    VSM_Error(le->vsm));
305 298
        VSB_destroy(&n_arg);
306 298
        if (VSM_Attach(le->vsm, -1))
307 0
                vtc_fatal(le->vl, "VSM_Attach: %s", VSM_Error(le->vsm));
308 298
        return (le);
309 298
}
310
311
static void
312
logexp_clean(const struct tests_head *head)
313 358
{
314 358
        struct logexp_test *test;
315
316 358
        VTAILQ_FOREACH(test, head, list)
317 2115
                if (test->skip_max == LE_SEEN)
318 0
                        test->skip_max = LE_ALT;
319 358
}
320
321
static struct logexp_test *
322
logexp_alt(struct logexp_test *test)
323 5
{
324 5
        assert(test->skip_max == LE_ALT);
325
326 5
        do
327 6
                test = VTAILQ_NEXT(test, list);
328 6
        while (test != NULL && test->skip_max == LE_SEEN);
329
330 5
        if (test == NULL || test->skip_max != LE_ALT)
331 0
                return (NULL);
332
333 5
        return (test);
334 5
}
335
336
static void
337
logexp_next(struct logexp *le)
338 2476
{
339 2476
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
340
341 2476
        if (le->test && le->test->skip_max == LE_ALT) {
342
                /*
343
                 * if an alternative was not seen, continue at this expection
344
                 * with the next vsl
345
                 */
346 3
                (void)0;
347 2473
        } else if (le->test) {
348 2115
                CHECK_OBJ_NOTNULL(le->test, LOGEXP_TEST_MAGIC);
349 2115
                le->test = VTAILQ_NEXT(le->test, list);
350 2115
        } else {
351 358
                logexp_clean(&le->tests);
352 358
                VTAILQ_INIT(&le->fail);
353 358
                le->test = VTAILQ_FIRST(&le->tests);
354 358
        }
355
356 2476
        if (le->test == NULL)
357 358
                return;
358
359 2118
        CHECK_OBJ(le->test, LOGEXP_TEST_MAGIC);
360
361 2118
        switch (le->test->skip_max) {
362 3
        case LE_SEEN:
363 3
                logexp_next(le);
364 3
                return;
365 99
        case LE_CLEAR:
366 99
                vtc_log(le->vl, 3, "cond | fail clear");
367 99
                VTAILQ_INIT(&le->fail);
368 99
                logexp_next(le);
369 99
                return;
370 113
        case LE_FAIL:
371 113
                vtc_log(le->vl, 3, "cond | %s", VSB_data(le->test->str));
372 113
                VTAILQ_INSERT_TAIL(&le->fail, le->test, faillist);
373 113
                logexp_next(le);
374 113
                return;
375 0
        case LE_ABORT:
376 0
                abort();
377 0
                NEEDLESS(return);
378 1903
        default:
379 1903
                vtc_log(le->vl, 3, "test | %s", VSB_data(le->test->str));
380 2118
        }
381 2118
}
382
383
enum le_match_e {
384
        LEM_OK,
385
        LEM_SKIP,
386
        LEM_FAIL
387
};
388
389
static enum le_match_e
390
logexp_match(const struct logexp *le, struct logexp_test *test,
391
    const char *data, int64_t vxid, int tag, int type, int len)
392 35119
{
393 35119
        const char *legend;
394 35119
        int ok = 1, skip = 0, alt, fail, vxid_ok = 0;
395
396 35119
        AN(le);
397 35119
        AN(test);
398 35119
        assert(test->skip_max != LE_SEEN);
399 35119
        assert(test->skip_max != LE_CLEAR);
400
401 35119
        if (test->vxid == LE_LAST) {
402 5035
                if (le->vxid_last != vxid)
403 351
                        ok = 0;
404 5035
                vxid_ok = ok;
405 30084
        } else if (test->vxid >= 0) {
406 9354
                if (test->vxid != vxid)
407 6433
                        ok = 0;
408 9354
                vxid_ok = ok;
409 9354
        }
410 35119
        if (test->tag == LE_LAST) {
411 0
                if (le->tag_last != tag)
412 0
                        ok = 0;
413 35119
        } else if (test->tag >= 0) {
414 35119
                if (test->tag != tag)
415 31643
                        ok = 0;
416 35119
        }
417 35119
        if (test->vre &&
418 35119
            test->tag >= 0 &&
419 35119
            test->tag == tag &&
420 35119
            VRE_ERROR_NOMATCH == VRE_match(test->vre, data, len, 0, NULL))
421 1562
                ok = 0;
422
423 35119
        alt = (test->skip_max == LE_ALT);
424 35119
        fail = (test->skip_max == LE_FAIL);
425
426 35119
        if (!ok && !alt && (test->skip_max == LE_ANY ||
427 33212
                    test->skip_max > le->skip_cnt))
428 27289
                skip = 1;
429
430 35119
        if (skip && vxid_ok && tag == SLT_End)
431 0
                fail = 1;
432
433 35119
        if (fail) {
434 5924
                if (ok) {
435 1
                        legend = "fail";
436 5923
                } else if (skip) {
437 0
                        legend = "end";
438 0
                        skip = 0;
439 5923
                } else if (le->m_arg) {
440 0
                        legend = "fmiss";
441 5923
                } else {
442 5923
                        legend = NULL;
443 5923
                }
444 5924
        }
445 29195
        else if (ok)
446 1903
                legend = "match";
447 27292
        else if (skip && le->m_arg)
448 23
                legend = "miss";
449 27269
        else if (skip || alt)
450 27273
                legend = NULL;
451 18446744073709551612
        else
452 18446744073709551612
                legend = "err";
453
454 35119
        if (legend != NULL)
455 1927
                vtc_log(le->vl, 4, "%-5s| %10ju %-15s %c %.*s",
456 1927
                    legend, (intmax_t)vxid, VSL_tags[tag], type, len,
457 1927
                    data);
458
459 35119
        if (ok) {
460 1904
                if (alt)
461 22
                        test->skip_max = LE_SEEN;
462 1904
                return (LEM_OK);
463 1904
        }
464 33215
        if (alt) {
465 5
                test = logexp_alt(test);
466 5
                if (test == NULL)
467 0
                        return (LEM_FAIL);
468 5
                vtc_log(le->vl, 3, "alt  | %s", VSB_data(test->str));
469 5
                return (logexp_match(le, test, data, vxid, tag, type, len));
470 5
        }
471 33210
        if (skip)
472 27291
                return (LEM_SKIP);
473 5919
        return (LEM_FAIL);
474 33210
}
475
476
static enum le_match_e
477
logexp_failchk(const struct logexp *le,
478
    const char *data, int64_t vxid, int tag, int type, int len)
479 29200
{
480 29200
        struct logexp_test *test;
481 29200
        static enum le_match_e r;
482
483 29200
        if (VTAILQ_FIRST(&le->fail) == NULL)
484 25311
                return (LEM_SKIP);
485
486 5924
        VTAILQ_FOREACH(test, &le->fail, faillist) {
487 5924
                r = logexp_match(le, test, data, vxid, tag, type, len);
488 5924
                if (r == LEM_OK)
489 1
                        return (LEM_FAIL);
490 5923
                assert (r == LEM_FAIL);
491 5923
        }
492 3888
        return (LEM_OK);
493 3889
}
494
495
static int
496
logexp_done(const struct logexp *le)
497 52578
{
498 52578
        return ((VTAILQ_FIRST(&le->fail) == NULL) && le->test == NULL);
499 52578
}
500
501
static int v_matchproto_(VSLQ_dispatch_f)
502
logexp_dispatch(struct VSL_data *vsl, struct VSL_transaction * const pt[],
503
    void *priv)
504 12756
{
505 12756
        struct logexp *le;
506 12756
        struct VSL_transaction *t;
507 12756
        int i;
508 12756
        enum le_match_e r;
509 12756
        int64_t vxid;
510 12756
        int tag, type, len;
511 12756
        const char *data;
512
513 12756
        CAST_OBJ_NOTNULL(le, priv, LOGEXP_MAGIC);
514
515 25221
        for (i = 0; (t = pt[i]) != NULL; i++) {
516 41961
                while (1 == VSL_Next(t->c)) {
517 29496
                        if (!VSL_Match(vsl, t->c))
518 307
                                continue;
519
520 29189
                        AN(t->c->rec.ptr);
521 29189
                        tag = VSL_TAG(t->c->rec.ptr);
522
523 29189
                        if (tag == SLT__Batch || tag == SLT_Witness)
524 0
                                continue;
525
526 29189
                        vxid = VSL_ID(t->c->rec.ptr);
527 29189
                        data = VSL_CDATA(t->c->rec.ptr);
528 29189
                        len = VSL_LEN(t->c->rec.ptr) - 1;
529 29189
                        type = VSL_CLIENT(t->c->rec.ptr) ? 'c' :
530 29189
                            VSL_BACKEND(t->c->rec.ptr) ? 'b' : '-';
531
532 29189
                        r = logexp_failchk(le, data, vxid, tag, type, len);
533 29189
                        if (r == LEM_FAIL)
534 1
                                return (r);
535 29188
                        if (le->test == NULL) {
536 15
                                assert (r == LEM_OK);
537 15
                                continue;
538 15
                        }
539
540 29173
                        CHECK_OBJ_NOTNULL(le->test, LOGEXP_TEST_MAGIC);
541
542 29173
                        r = logexp_match(le, le->test,
543 29173
                            data, vxid, tag, type, len);
544 29173
                        if (r == LEM_FAIL)
545 0
                                return (r);
546 29173
                        if (r == LEM_SKIP) {
547 27291
                                le->skip_cnt++;
548 27291
                                continue;
549 27291
                        }
550 1882
                        assert(r == LEM_OK);
551 1882
                        le->vxid_last = vxid;
552 1882
                        le->tag_last = tag;
553 1882
                        le->skip_cnt = 0;
554 1882
                        logexp_next(le);
555 1882
                        if (logexp_done(le))
556 355
                                return (1);
557 1882
                }
558 12821
        }
559 12400
        return (0);
560 12756
}
561
562
static void *
563
logexp_thread(void *priv)
564 358
{
565 358
        struct logexp *le;
566 358
        int i;
567
568 358
        CAST_OBJ_NOTNULL(le, priv, LOGEXP_MAGIC);
569 358
        AN(le->run);
570 358
        AN(le->vsm);
571 358
        AN(le->vslq);
572
573 358
        AZ(le->test);
574 358
        vtc_log(le->vl, 4, "begin|");
575 358
        if (le->query != NULL)
576 203
                vtc_log(le->vl, 4, "qry  | %s", le->query);
577 358
        logexp_next(le);
578 37476
        while (!logexp_done(le) && !vtc_stop && !vtc_error) {
579 37119
                i = VSLQ_Dispatch(le->vslq, logexp_dispatch, le);
580 37119
                if (i == 2 && le->err_arg) {
581 1
                        vtc_log(le->vl, 4, "done | failed as expected");
582 1
                        return (NULL);
583 1
                }
584 37118
                if (i == 2)
585 0
                        vtc_fatal(le->vl, "bad  | expectation failed");
586 37118
                else if (i < 0)
587 0
                        vtc_fatal(le->vl, "bad  | dispatch failed (%d)", i);
588 37118
                else if (i == 0 && ! logexp_done(le))
589 12858
                        VTIM_sleep(0.01);
590 37118
        }
591 357
        if (!logexp_done(le))
592 0
                vtc_fatal(le->vl, "bad  | outstanding expectations");
593 357
        vtc_log(le->vl, 4, "done |");
594
595 357
        return (NULL);
596 357
}
597
598
static void
599
logexp_close(struct logexp *le)
600 358
{
601
602 358
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
603 358
        AN(le->vsm);
604 358
        if (le->vslq)
605 358
                VSLQ_Delete(&le->vslq);
606 358
        AZ(le->vslq);
607 358
}
608
609
static void
610
logexp_start(struct logexp *le)
611 358
{
612 358
        struct VSL_cursor *c;
613
614 358
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
615 358
        AN(le->vsl);
616 358
        AZ(le->vslq);
617
618 358
        AN(le->vsl);
619 358
        (void)VSM_Status(le->vsm);
620 358
        c = VSL_CursorVSM(le->vsl, le->vsm,
621 358
            (le->d_arg ? 0 : VSL_COPT_TAIL) | VSL_COPT_BATCH);
622 358
        if (c == NULL)
623 0
                vtc_fatal(le->vl, "VSL_CursorVSM: %s", VSL_Error(le->vsl));
624 358
        le->vslq = VSLQ_New(le->vsl, &c, le->g_arg, le->query);
625 358
        if (le->vslq == NULL) {
626 0
                VSL_DeleteCursor(c);
627 0
                vtc_fatal(le->vl, "VSLQ_New: %s", VSL_Error(le->vsl));
628 0
        }
629 358
        AZ(c);
630
631 358
        le->test = NULL;
632 358
        le->skip_cnt = 0;
633 358
        le->vxid_last = le->tag_last = -1;
634 358
        le->run = 1;
635 358
        PTOK(pthread_create(&le->tp, NULL, logexp_thread, le));
636 358
}
637
638
static void
639
logexp_wait(struct logexp *le)
640 358
{
641 358
        void *res;
642
643 358
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
644 358
        vtc_log(le->vl, 2, "Waiting for logexp");
645 358
        PTOK(pthread_join(le->tp, &res));
646 358
        logexp_close(le);
647 358
        if (res != NULL && !vtc_stop)
648 0
                vtc_fatal(le->vl, "logexp returned \"%p\"", (char *)res);
649 358
        le->run = 0;
650 358
}
651
652
/* shared by expect and fail: parse from av[2] (vxid) onwards */
653
654
static void
655
cmd_logexp_common(struct logexp *le, struct vtclog *vl,
656
    int skip_max, char * const *av)
657 2013
{
658 2013
        vre_t *vre;
659 2013
        struct vsb vsb[1];
660 2013
        int64_t vxid;
661 2013
        int err, pos, tag;
662 2013
        struct logexp_test *test;
663 2013
        char *end, errbuf[VRE_ERROR_LEN];
664
665 2013
        if (!vstrcmp(av[2], "*"))
666 373
                vxid = LE_ANY;
667 1640
        else if (!vstrcmp(av[2], "="))
668 1070
                vxid = LE_LAST;
669 570
        else {
670 570
                vxid = strtoll(av[2], &end, 10);
671 570
                if (*end != '\0' || vxid < 0)
672 0
                        vtc_fatal(vl, "Not a positive integer: '%s'", av[2]);
673 570
        }
674 2013
        if (!vstrcmp(av[3], "*"))
675 0
                tag = LE_ANY;
676 2013
        else if (!vstrcmp(av[3], "="))
677 0
                tag = LE_LAST;
678 2013
        else {
679 2013
                tag = VSL_Name2Tag(av[3], vstrlen(av[3]));
680 2013
                if (tag < 0)
681 0
                        vtc_fatal(vl, "Unknown tag name: '%s'", av[3]);
682 2013
        }
683 2013
        vre = NULL;
684 2013
        if (av[4]) {
685 1751
                vre = VRE_compile(av[4], 0, &err, &pos, 1);
686 1751
                if (vre == NULL) {
687 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
688 0
                        AZ(VRE_error(vsb, err));
689 0
                        AZ(VSB_finish(vsb));
690 0
                        VSB_fini(vsb);
691 0
                        vtc_fatal(vl, "Regex error (%s): '%s' pos %d",
692 0
                            errbuf, av[4], pos);
693 0
                }
694 1751
        }
695
696 2013
        ALLOC_OBJ(test, LOGEXP_TEST_MAGIC);
697 2013
        AN(test);
698 2013
        test->str = VSB_new_auto();
699 2013
        AN(test->str);
700 2013
        AZ(VSB_printf(test->str, "%s %s %s %s ", av[0], av[1], av[2], av[3]));
701 2013
        if (av[4])
702 1751
                VSB_quote(test->str, av[4], -1, 0);
703 2013
        AZ(VSB_finish(test->str));
704 2013
        test->skip_max = skip_max;
705 2013
        test->vxid = vxid;
706 2013
        test->tag = tag;
707 2013
        test->vre = vre;
708 2013
        VTAILQ_INSERT_TAIL(&le->tests, test, list);
709 2013
}
710
711
static void
712
cmd_logexp_expect(CMD_ARGS)
713 1900
{
714 1900
        struct logexp *le;
715 1900
        int skip_max;
716 1900
        char *end;
717
718 1900
        CAST_OBJ_NOTNULL(le, priv, LOGEXP_MAGIC);
719 1900
        if (av[1] == NULL || av[2] == NULL || av[3] == NULL)
720 0
                vtc_fatal(vl, "Syntax error");
721
722 1900
        if (av[4] != NULL && av[5] != NULL)
723 0
                vtc_fatal(vl, "Syntax error");
724
725 1900
        if (!vstrcmp(av[1], "*"))
726 614
                skip_max = LE_ANY;
727 1286
        else if (!vstrcmp(av[1], "?"))
728 22
                skip_max = LE_ALT;
729 1264
        else {
730 1264
                skip_max = (int)strtol(av[1], &end, 10);
731 1264
                if (*end != '\0' || skip_max < 0)
732 0
                        vtc_fatal(vl, "Not a positive integer: '%s'", av[1]);
733 1264
        }
734 1900
        cmd_logexp_common(le, vl, skip_max, av);
735 1900
}
736
737
static void
738
cmd_logexp_fail(CMD_ARGS)
739 212
{
740 212
        struct logexp *le;
741 212
        struct logexp_test *test;
742
743 212
        CAST_OBJ_NOTNULL(le, priv, LOGEXP_MAGIC);
744
745 212
        if (av[1] == NULL)
746 0
                vtc_fatal(vl, "Syntax error");
747
748 212
        if (!vstrcmp(av[1], "clear")) {
749 99
                ALLOC_OBJ(test, LOGEXP_TEST_MAGIC);
750 99
                AN(test);
751 99
                test->skip_max = LE_CLEAR;
752 99
                test->str = VSB_new_auto();
753 99
                AN(test->str);
754 99
                AZ(VSB_printf(test->str, "%s %s",
755 99
                    av[0], av[1]));
756 99
                AZ(VSB_finish(test->str));
757
758 99
                VTAILQ_INSERT_TAIL(&le->tests, test, list);
759 99
                return;
760 99
        }
761
762 113
        if (vstrcmp(av[1], "add"))
763 0
                vtc_fatal(vl, "Unknown fail argument '%s'", av[1]);
764
765 113
        if (av[2] == NULL || av[3] == NULL)
766 0
                vtc_fatal(vl, "Syntax error");
767
768 113
        cmd_logexp_common(le, vl, LE_FAIL, av);
769 113
}
770
771
/* aid vsl debugging */
772
static void
773
cmd_logexp_abort(CMD_ARGS)
774 0
{
775
776 0
        struct logexp *le;
777
778 0
        CAST_OBJ_NOTNULL(le, priv, LOGEXP_MAGIC);
779
780 0
        cmd_logexp_common(le, vl, LE_ABORT, av);
781 0
}
782
783
static void
784
logexp_spec(struct logexp *le, const char *spec)
785 355
{
786 355
        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
787
788 355
        logexp_delete_tests(le);
789
790 355
        parse_string(le->vl, le, spec);
791 355
}
792
793
void
794
cmd_logexpect(CMD_ARGS)
795 1788
{
796 1788
        struct logexp *le, *le2;
797 1788
        int i;
798
799 1788
        (void)priv;
800
801 1788
        if (av == NULL) {
802
                /* Reset and free */
803 1122
                VTAILQ_FOREACH_SAFE(le, &logexps, list, le2) {
804 298
                        CHECK_OBJ_NOTNULL(le, LOGEXP_MAGIC);
805 298
                        VTAILQ_REMOVE(&logexps, le, list);
806 298
                        if (le->run) {
807 0
                                (void)pthread_cancel(le->tp);
808 0
                                logexp_wait(le);
809 0
                        }
810 298
                        logexp_delete(le);
811 298
                }
812 1122
                return;
813 1122
        }
814
815 666
        AZ(vstrcmp(av[0], "logexpect"));
816 666
        av++;
817
818 666
        VTC_CHECK_NAME(vl, av[0], "Logexpect", 'l');
819 1319
        VTAILQ_FOREACH(le, &logexps, list) {
820 1319
                if (!vstrcmp(le->name, av[0]))
821 368
                        break;
822 1319
        }
823 666
        if (le == NULL) {
824 298
                if (vstrcmp(av[1], "-v") || av[2] == NULL)
825 0
                        vtc_fatal(vl, "new logexp lacks -v");
826 298
                le = logexp_new(av[0], av[2]);
827 298
                av += 2;
828 298
        }
829 666
        av++;
830
831 2275
        for (; *av != NULL; av++) {
832 1609
                if (vtc_error)
833 0
                        break;
834 1609
                if (!vstrcmp(*av, "-wait")) {
835 302
                        if (!le->run)
836 0
                                vtc_fatal(le->vl, "logexp not -started '%s'",
837 0
                                        *av);
838 302
                        logexp_wait(le);
839 302
                        continue;
840 302
                }
841
842
                /*
843
                 * We do an implicit -wait if people muck about with a
844
                 * running logexp.
845
                 */
846 1307
                if (le->run)
847 0
                        logexp_wait(le);
848 1307
                AZ(le->run);
849
850 1307
                if (!vstrcmp(*av, "-v")) {
851 20
                        if (av[1] == NULL || vstrcmp(av[1], le->vname))
852 0
                                vtc_fatal(le->vl, "-v argument cannot change");
853 20
                        av++;
854 20
                        continue;
855 20
                }
856 1287
                if (!vstrcmp(*av, "-d")) {
857 78
                        if (av[1] == NULL)
858 0
                                vtc_fatal(le->vl, "Missing -d argument");
859 78
                        le->d_arg = atoi(av[1]);
860 78
                        av++;
861 78
                        continue;
862 78
                }
863 1209
                if (!vstrcmp(*av, "-g")) {
864 277
                        if (av[1] == NULL)
865 0
                                vtc_fatal(le->vl, "Missing -g argument");
866 277
                        i = VSLQ_Name2Grouping(av[1], vstrlen(av[1]));
867 277
                        if (i < 0)
868 0
                                vtc_fatal(le->vl, "Unknown grouping '%s'",
869 0
                                    av[1]);
870 277
                        le->g_arg = (enum VSL_grouping_e)i;
871 277
                        av++;
872 277
                        continue;
873 277
                }
874 932
                if (!vstrcmp(*av, "-q")) {
875 192
                        if (av[1] == NULL)
876 0
                                vtc_fatal(le->vl, "Missing -q argument");
877 192
                        REPLACE(le->query, av[1]);
878 192
                        av++;
879 192
                        continue;
880 192
                }
881 740
                if (!vstrcmp(*av, "-m")) {
882 1
                        le->m_arg = !le->m_arg;
883 1
                        continue;
884 1
                }
885 739
                if (!vstrcmp(*av, "-err")) {
886 1
                        le->err_arg = !le->err_arg;
887 1
                        continue;
888 1
                }
889 738
                if (!vstrcmp(*av, "-start")) {
890 302
                        logexp_start(le);
891 302
                        continue;
892 302
                }
893 436
                if (!vstrcmp(*av, "-run")) {
894 56
                        logexp_start(le);
895 56
                        logexp_wait(le);
896 56
                        continue;
897 56
                }
898 380
                if (**av == '-') {
899 25
                        if (av[1] != NULL) {
900 25
                                if (VSL_Arg(le->vsl, av[0][1], av[1])) {
901 25
                                        av++;
902 25
                                        continue;
903 25
                                }
904 0
                                vtc_fatal(le->vl, "%s", VSL_Error(le->vsl));
905 25
                        }
906 0
                        vtc_fatal(le->vl, "Unknown logexp argument: %s", *av);
907 25
                }
908 355
                logexp_spec(le, *av);
909 355
        }
910 666
}