summaryrefslogtreecommitdiff
path: root/tests/qemucapabilitiesnumbering.c
blob: 6f33321e17234fbbe19e215f8f2bc80166091f49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
 * qemucapabilitiesnumbering.c: swiss-army knife for qemu capability data manipulation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <config.h>

#include "testutils.h"
#include "testutilsqemu.h"
#include "qemumonitortestutils.h"

struct qmpTuple {
    virJSONValue *command;
    virJSONValue *reply;
};

struct qmpCommandList {
    struct qmpTuple *items;
    size_t nitems;
};

typedef struct qmpCommandList qmpCommandList;


static int
modify(struct qmpCommandList *list G_GNUC_UNUSED)
{
    /* in case you want to programmatically modify the replies file enable the
     * following block and modify it to your needs. After compiling run this
     * with:
     *
     * VIR_TEST_REGENERATE_OUTPUT=1 tests/qemucapabilitiesnumbering
     *
     * This applies the modification along with updating the files. Use git to
     * your advantage to roll back mistakes.
     */
#if 0
    struct qmpTuple tmptuple = { NULL, NULL };
    size_t found = 0;
    size_t i;

    for (i = 0; i < list->nitems; i++) {
        struct qmpTuple *item = list->items + i;
        const char *cmdname = virJSONValueObjectGetString(item->command, "execute");

        if (STREQ_NULLABLE(cmdname, "qom-list-properties")) {
            found = i;
            // break; /* uncomment if you want to find the first occurrence */
        }
    }

    if (found == 0) {
        fprintf(stderr, "entry not found!!!\n");
        return -1;
    }

    tmptuple.command = virJSONValueFromString("{\"execute\":\"dummy\"}");
    tmptuple.reply = virJSONValueFromString("{\"return\":{}}");
    // tmptuple.reply = virTestLoadFileJSON("path/", "to/", "file.json");

    ignore_value(VIR_INSERT_ELEMENT(list->items, found + 1, list->nitems, tmptuple));
#endif

    return 0;
}


static void
qmpCommandListFree(qmpCommandList* list)
{
    size_t i;

    if (!list)
        return;

    for (i = 0; i < list->nitems; i++) {
        struct qmpTuple *item = list->items + i;

        virJSONValueFree(item->command);
        virJSONValueFree(item->reply);
    }

    g_free(list->items);
    g_free(list);
}


G_DEFINE_AUTOPTR_CLEANUP_FUNC(qmpCommandList, qmpCommandListFree);


static qmpCommandList *
loadReplies(const char *filename)
{
    g_autofree char *replies = NULL;
    g_autofree struct qemuMonitorTestCommandReplyTuple *items = NULL;
    size_t nitems = 0;
    g_autoptr(qmpCommandList) list = NULL;
    size_t i;

    if (virTestLoadFile(filename, &replies) < 0) {
        fprintf(stderr, "Failed to load '%s'\n", filename);
        return NULL;
    }

    if (qemuMonitorTestProcessFileEntries(replies, filename, &items, &nitems) < 0)
        return NULL;

    list = g_new0(qmpCommandList, 1);
    list->items = g_new0(struct qmpTuple, nitems);

    for (i = 0; i < nitems; i++) {
        struct qemuMonitorTestCommandReplyTuple *item = items + i;

        if (!(list->items[list->nitems].command = virJSONValueFromString(item->command)) ||
            !(list->items[list->nitems++].reply = virJSONValueFromString(item->reply)))
            return NULL;
    }

    return g_steal_pointer(&list);
}

/* see printLineSkipEmpty in tests/qemucapsprobemock.c */
static void
printLineSkipEmpty(const char *p,
                   virBuffer *buf)
{
    for (; *p; p++) {
        if (p[0] == '\n' && p[1] == '\n')
            continue;

        virBufferAddChar(buf, *p);
    }
}


static void
renumberItem(virJSONValue *val,
             size_t num)
{
    g_autoptr(virJSONValue) label = virJSONValueNewString(g_strdup_printf("libvirt-%zu", num));

    virJSONValueObjectReplaceValue(val, "id", &label);
}


static int
output(virBuffer *buf,
       qmpCommandList *list)
{
    size_t commandindex = 1;
    size_t i;

    for (i = 0; i < list->nitems; i++) {
        struct qmpTuple *item = list->items + i;
        g_autofree char *jsoncommand = NULL;
        g_autofree char *jsonreply = NULL;

        if (STREQ_NULLABLE(virJSONValueObjectGetString(item->command, "execute"), "qmp_capabilities"))
            commandindex = 1;

        /* fix numbering */
        renumberItem(item->command, commandindex);
        renumberItem(item->reply, commandindex);
        commandindex++;

        /* output formatting */
        if (!(jsoncommand = virJSONValueToString(item->command, true)) ||
            !(jsonreply = virJSONValueToString(item->reply, true)))
            return -1;

        printLineSkipEmpty(jsoncommand, buf);
        virBufferAddLit(buf, "\n");
        printLineSkipEmpty(jsonreply, buf);
        virBufferAddLit(buf, "\n");
    }

    virBufferTrim(buf, "\n");

    return 0;
}


static int
testCapsFile(const void *opaque)
{
    const char *repliesFile = opaque;
    g_autoptr(qmpCommandList) list = NULL;
    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;

    if (!(list = loadReplies(repliesFile)))
        return -1;

    if (virTestGetRegenerate() > 0) {
        if (modify(list) < 0)
            return -1;
    }

    output(&buf, list);

    if (virTestCompareToFile(virBufferCurrentContent(&buf), repliesFile) < 0)
        return -1;

    return 0;
}


static int
iterateCapsFile(const char *inputDir,
                const char *prefix,
                const char *version,
                const char *archName,
                const char *variant,
                const char *suffix,
                void *opaque G_GNUC_UNUSED)
{
    g_autofree char *repliesFile = g_strdup_printf("%s/%s_%s_%s%s.%s",
                                                   inputDir, prefix, version,
                                                   archName, variant, suffix);

    return virTestRun(repliesFile, testCapsFile, repliesFile);
}


static int
testmain(void)
{
    if (testQemuCapsIterate(".replies", iterateCapsFile, NULL) < 0)
        return EXIT_FAILURE;

    return EXIT_SUCCESS;
}

VIR_TEST_MAIN(testmain)