Code Coverage: vinyl-cache/bin/vinyltest/vtest_ext_vinyl_vsm.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2023 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.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
#include "config.h"
31
32
#include <sys/types.h>
33
#include <signal.h>
34
#include <stdint.h>
35
#include <stdlib.h>
36
#include <string.h>
37
38
#include "vapi/vsm.h"
39
40
#include "vdef.h"
41
42
#include "miniobj.h"
43
#include "vas.h"
44
#include "vav.h"
45
#include "vqueue.h"
46
#include "vsb.h"
47
48
#include <vtest_api.h>
49
#include "vtest_ext_vinyl.h"
50
51
struct vtc_vsm {
52
        unsigned                        magic;
53
#define VTC_VSM_MAGIC                   0x5ca77a36
54
        VTAILQ_ENTRY(vtc_vsm)           list;
55
56
        char                            *name;
57
        char                            *n_arg;
58
        struct vtclog                   *vl;
59
        struct vsm                      *vsm;
60
};
61
62
static VTAILQ_HEAD(, vtc_vsm) vsms = VTAILQ_HEAD_INITIALIZER(vsms);
63
64
static struct vtc_vsm *
65
vsm_new(const char *name)
66 2
{
67 2
        struct vtc_vsm *m;
68
69 2
        ALLOC_OBJ(m, VTC_VSM_MAGIC);
70 2
        AN(m);
71 2
        REPLACE(m->name, name);
72 2
        REPLACE(m->n_arg, "${v1_name}");
73 2
        m->vl = vtc_logopen("%s", name);
74
75 2
        VTAILQ_INSERT_TAIL(&vsms, m, list);
76 2
        return (m);
77 2
}
78
79
static void
80
vsm_detach(struct vtc_vsm *m)
81 2
{
82
83 2
        CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
84 2
        if (m->vsm == NULL)
85 0
                vtc_fatal(m->vl, "Cannot detach unattached VSM");
86 2
        vtc_log(m->vl, 3, "Detaching from VSM");
87 2
        VSM_Destroy(&m->vsm);
88 2
        AZ(m->vsm);
89 2
}
90
91
static void
92
vsm_attach(struct vtc_vsm *m)
93 2
{
94 2
        struct vsb *n_arg;
95
96 2
        CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
97 2
        if (m->vsm != NULL)
98 0
                vsm_detach(m);
99
100 2
        n_arg = macro_expandf(m->vl, "%s", m->n_arg);
101 2
        if (n_arg == NULL)
102 0
                vtc_fatal(m->vl, "Could not expand -n argument");
103 2
        vtc_log(m->vl, 3, "Attaching to VSM: %s", VSB_data(n_arg));
104
105 2
        m->vsm = VSM_New();
106 2
        AN(m->vsm);
107
108 2
        if (VSM_Arg(m->vsm, 'n', VSB_data(n_arg)) <= 0)
109 0
                vtc_fatal(m->vl, "-n argument error: %s", VSM_Error(m->vsm));
110 2
        if (VSM_Attach(m->vsm, -1))
111 0
                vtc_fatal(m->vl, "VSM_Attach: %s", VSM_Error(m->vsm));
112
113 2
        VSB_destroy(&n_arg);
114 2
}
115
116
static void
117
vsm_delete(struct vtc_vsm *m)
118 2
{
119
120 2
        CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
121 2
        if (m->vsm != NULL)
122 2
                vsm_detach(m);
123 2
        REPLACE(m->name, NULL);
124 2
        REPLACE(m->n_arg, NULL);
125 2
        vtc_logclose(m->vl);
126 2
        FREE_OBJ(m);
127 2
}
128
129
#define STATUS_BITS()                                   \
130 56
        STATUS_BIT(VSM_MGT_RUNNING,     mgt-running);   \
131 42
        STATUS_BIT(VSM_MGT_CHANGED,     mgt-changed);   \
132 36
        STATUS_BIT(VSM_MGT_RESTARTED,   mgt-restarted); \
133 36
        STATUS_BIT(VSM_WRK_RUNNING,     wrk-running);   \
134 28
        STATUS_BIT(VSM_WRK_CHANGED,     wrk-changed);   \
135 18
        STATUS_BIT(VSM_WRK_RESTARTED,   wrk-restarted);
136
137
static void
138
vsm_expect_status(struct vtc_vsm *m, const char *exp)
139 14
{
140 14
        struct vsb *stat;
141 14
        const char *sep;
142 14
        char **av;
143 14
        unsigned bstat, bexp, bfound;
144 14
        int argc, i;
145
146 14
        CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
147 14
        if (exp == NULL)
148 0
                vtc_fatal(m->vl, "Missing expected status");
149
150 14
        if (m->vsm == NULL)
151 2
                vsm_attach(m);
152
153 14
        av = VAV_Parse(exp, &argc, ARGV_COMMA|ARGV_NOESC);
154 14
        AN(av);
155
156 14
        bexp = 0;
157 56
        for (i = 1; i < argc; i++) {
158 42
#define STATUS_BIT(b, s)                                \
159 132
                if (!strcasecmp(#s, av[i])) {           \
160 42
                        bexp |= b;                      \
161 42
                        continue;                       \
162 42
                }
163 42
                STATUS_BITS()
164 0
#undef STATUS_BIT
165 0
                vtc_fatal(m->vl, "Unknown status bit: %s", av[i]);
166 90
        }
167 14
        VAV_Free(av);
168
169 14
        bfound = 0;
170 14
        bstat = VSM_Status(m->vsm);
171 14
        stat = VSB_new_auto();
172 14
        AN(stat);
173
174
        //lint -esym(438, sep) last value not used
175 14
        sep = "";
176 14
#define STATUS_BIT(b, s)                                \
177 84
        if (bstat & b) {                                \
178 42
                VSB_cat(stat, sep);                     \
179 42
                VSB_cat(stat, #s);                      \
180 42
                bfound |= b;                            \
181 42
                sep = ",";                              \
182 42
        }
183 14
        STATUS_BITS();
184 14
#undef STATUS_BIT
185
186 14
        if (bstat != bfound) {
187 0
                vtc_fatal(m->vl, "VSM status bits not handled: %x",
188 0
                    bstat & ~bfound);
189 0
        }
190
191 14
        if (bstat != bexp) {
192 0
                AZ(VSB_finish(stat));
193 0
                vtc_fatal(m->vl, "Expected VSM status '%s' got '%s'",
194 0
                    exp, VSB_data(stat));
195 0
        }
196
197 14
        VSB_destroy(&stat);
198 14
        vtc_log(m->vl, 4, "Found expected VSM status");
199 14
}
200
201
/* SECTION: vsm vsm
202
 *
203
 * Interact with the shared memory of a vinyl instance.
204
 *
205
 * To define a VSM consumer, use this syntax::
206
 *
207
 *     vsm mNAME [-n STRING]
208
 *
209
 * Arguments:
210
 *
211
 * mNAME
212
 *         Identify the VSM consumer, it must starts with 'm'.
213
 *
214
 * \-n STRING
215
 *         Choose the working directory of the vinyl instance. By default
216
 *         a VSM consumer connects to ``${v1_name}``.
217
 *
218
 * \-attach
219
 *         Attach to a new vinyl instance. Implicitly detach from the
220
 *         current vinyl instance if applicable.
221
 *
222
 * \-detach
223
 *         Detach from the current vinyl instance.
224
 *
225
 * \-expect-status STRING
226
 *         Check that the status of VSM matches the list of status flags from
227
 *         STRING. The expected status is represented as a comma-separated
228
 *         list of flags. The list of flags in STRING is not sensitive to the
229
 *         order of flags.
230
 *
231
 *         The available flags are:
232
 *
233
 *         - ``mgt-running``
234
 *         - ``mgt-changed``
235
 *         - ``mgt-restarted``
236
 *         - ``wrk-running``
237
 *         - ``wrk-changed``
238
 *         - ``wrk-restarted``
239
 *
240
 *         Expecting a status automatically attaches to the vinyl instance
241
 *         if that was not already the case.
242
 */
243
244
void
245
cmd_vsm(CMD_ARGS)
246 1136
{
247 1136
        struct vtc_vsm *m, *m2;
248
249 1136
        (void)priv;
250
251 1136
        if (av == NULL) {
252
                /* Reset and free */
253 1122
                VTAILQ_FOREACH_SAFE(m, &vsms, list, m2) {
254 2
                        CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
255 2
                        VTAILQ_REMOVE(&vsms, m, list);
256 2
                        vsm_delete(m);
257 2
                }
258 1122
                return;
259 1122
        }
260
261 14
        AZ(vstrcmp(av[0], "vsm"));
262 14
        av++;
263
264 14
        VTC_CHECK_NAME(vl, av[0], "VSM", 'm');
265 14
        VTAILQ_FOREACH(m, &vsms, list) {
266 12
                if (!vstrcmp(m->name, av[0]))
267 12
                        break;
268 12
        }
269
270 14
        if (m == NULL) {
271 2
                m = vsm_new(*av);
272 2
                AN(m);
273 2
        }
274 14
        av++;
275
276 28
        for (; *av != NULL; av++) {
277 14
                if (vtc_error)
278 0
                        break;
279 14
                if (!vstrcmp(*av, "-attach")) {
280 0
                        vsm_attach(m);
281 0
                        continue;
282 0
                }
283 14
                if (!vstrcmp(*av, "-detach")) {
284 0
                        vsm_detach(m);
285 0
                        continue;
286 0
                }
287 14
                if (!vstrcmp(*av, "-expect-status")) {
288 14
                        vsm_expect_status(m, av[1]);
289 14
                        av++;
290 14
                        continue;
291 14
                }
292 0
                if (!vstrcmp(*av, "-n")) {
293 0
                        if (av[1] == NULL)
294 0
                                vtc_fatal(m->vl, "Missing -n argument");
295 0
                        REPLACE(m->n_arg, av[1]);
296 0
                        av++;
297 0
                        continue;
298 0
                }
299 0
                vtc_fatal(vl, "Unknown VSM argument: %s", *av);
300 0
        }
301 14
}