Add missing libxml2-tools dependency
[archive/platform/upstream/libvirt.git] / tests / storagevolxml2argvtest.c
1 #include <config.h>
2
3 #include "internal.h"
4 #include "testutils.h"
5 #include "datatypes.h"
6 #include "storage/storage_backend.h"
7 #include "testutilsqemu.h"
8 #include "virstring.h"
9
10 #define VIR_FROM_THIS VIR_FROM_NONE
11
12 const char create_tool[] = "qemu-img";
13
14 /* createVol sets this on volume creation */
15 static void
16 testSetVolumeType(virStorageVolDefPtr vol,
17                   virStoragePoolDefPtr pool)
18 {
19     if (!vol || !pool)
20         return;
21
22     switch (pool->type) {
23     case VIR_STORAGE_POOL_DIR:
24     case VIR_STORAGE_POOL_FS:
25     case VIR_STORAGE_POOL_NETFS:
26         vol->type = VIR_STORAGE_VOL_FILE;
27         return;
28
29     case VIR_STORAGE_POOL_LOGICAL:
30         vol->type = VIR_STORAGE_VOL_BLOCK;
31         return;
32     }
33 }
34
35 static int
36 testCompareXMLToArgvFiles(bool shouldFail,
37                           const char *poolxml,
38                           const char *volxml,
39                           const char *inputpoolxml,
40                           const char *inputvolxml,
41                           const char *cmdline,
42                           unsigned int flags,
43                           int imgformat)
44 {
45     char *volXmlData = NULL;
46     char *poolXmlData = NULL;
47     char *inputpoolXmlData = NULL;
48     char *inputvolXmlData = NULL;
49     char *expectedCmdline = NULL;
50     char *actualCmdline = NULL;
51     int ret = -1;
52
53     int len;
54
55     virCommandPtr cmd = NULL;
56     virConnectPtr conn;
57
58     virStorageVolDefPtr vol = NULL, inputvol = NULL;
59     virStoragePoolDefPtr pool = NULL;
60     virStoragePoolDefPtr inputpool = NULL;
61     virStoragePoolObj poolobj = {.def = NULL };
62
63
64     if (!(conn = virGetConnect()))
65         goto cleanup;
66
67     if (virtTestLoadFile(poolxml, &poolXmlData) < 0)
68         goto cleanup;
69     if (virtTestLoadFile(volxml, &volXmlData) < 0)
70         goto cleanup;
71     if (inputvolxml &&
72         virtTestLoadFile(inputvolxml, &inputvolXmlData) < 0)
73         goto cleanup;
74
75     if (!(pool = virStoragePoolDefParseString(poolXmlData)))
76         goto cleanup;
77
78     poolobj.def = pool;
79
80     if (inputpoolxml) {
81         if (virtTestLoadFile(inputpoolxml, &inputpoolXmlData) < 0)
82             goto cleanup;
83         if (!(inputpool = virStoragePoolDefParseString(inputpoolXmlData)))
84             goto cleanup;
85     }
86
87     if (!(vol = virStorageVolDefParseString(pool, volXmlData)))
88         goto cleanup;
89
90     if (inputvolxml &&
91         !(inputvol = virStorageVolDefParseString(inputpool, inputvolXmlData)))
92         goto cleanup;
93
94     testSetVolumeType(vol, pool);
95     testSetVolumeType(inputvol, inputpool);
96
97     cmd = virStorageBackendCreateQemuImgCmd(conn, &poolobj, vol, inputvol,
98                                             flags, create_tool, imgformat);
99     if (!cmd) {
100         if (shouldFail) {
101             virResetLastError();
102             ret = 0;
103         }
104         goto cleanup;
105     }
106
107     if (!(actualCmdline = virCommandToString(cmd)))
108         goto cleanup;
109
110     len = virtTestLoadFile(cmdline, &expectedCmdline);
111     if (len < 0)
112         goto cleanup;
113     if (len && expectedCmdline[len-1] == '\n')
114         expectedCmdline[len-1] = '\0';
115
116     if (STRNEQ_NULLABLE(expectedCmdline, actualCmdline)) {
117         virtTestDifference(stderr, expectedCmdline, actualCmdline);
118         goto cleanup;
119     }
120
121     ret = 0;
122
123  cleanup:
124     virStoragePoolDefFree(pool);
125     virStoragePoolDefFree(inputpool);
126     virStorageVolDefFree(vol);
127     virStorageVolDefFree(inputvol);
128     virCommandFree(cmd);
129     VIR_FREE(actualCmdline);
130     VIR_FREE(expectedCmdline);
131     VIR_FREE(inputpoolXmlData);
132     VIR_FREE(poolXmlData);
133     VIR_FREE(volXmlData);
134     VIR_FREE(inputvolXmlData);
135     virObjectUnref(conn);
136     return ret;
137 }
138
139 struct testInfo {
140     bool shouldFail;
141     const char *pool;
142     const char *vol;
143     const char *inputpool;
144     const char *inputvol;
145     const char *cmdline;
146     unsigned int flags;
147     int imgformat;
148 };
149
150 static int
151 testCompareXMLToArgvHelper(const void *data)
152 {
153     int result = -1;
154     const struct testInfo *info = data;
155     char *poolxml = NULL;
156     char *inputpoolxml = NULL;
157     char *volxml = NULL;
158     char *inputvolxml = NULL;
159     char *cmdline = NULL;
160
161     if (info->inputvol &&
162         virAsprintf(&inputvolxml, "%s/storagevolxml2xmlin/%s.xml",
163                     abs_srcdir, info->inputvol) < 0)
164         goto cleanup;
165     if (info->inputpool &&
166         virAsprintf(&inputpoolxml, "%s/storagepoolxml2xmlin/%s.xml",
167                     abs_srcdir, info->inputpool) < 0)
168         goto cleanup;
169     if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
170                     abs_srcdir, info->pool) < 0 ||
171         virAsprintf(&volxml, "%s/storagevolxml2xmlin/%s.xml",
172                     abs_srcdir, info->vol) < 0) {
173         goto cleanup;
174     }
175     if (virAsprintf(&cmdline, "%s/storagevolxml2argvdata/%s.argv",
176                     abs_srcdir, info->cmdline) < 0 && !info->shouldFail)
177         goto cleanup;
178
179     result = testCompareXMLToArgvFiles(info->shouldFail, poolxml, volxml,
180                                        inputpoolxml, inputvolxml,
181                                        cmdline, info->flags,
182                                        info->imgformat);
183
184  cleanup:
185     VIR_FREE(poolxml);
186     VIR_FREE(volxml);
187     VIR_FREE(inputvolxml);
188     VIR_FREE(inputpoolxml);
189     VIR_FREE(cmdline);
190
191     return result;
192 }
193
194 enum {
195     FMT_NONE = 0,
196     FMT_FLAG,
197     FMT_OPTIONS,
198     FMT_COMPAT,
199 };
200
201
202
203 static int
204 mymain(void)
205 {
206     int ret = 0;
207     unsigned int flags = VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
208
209 #define DO_TEST_FULL(shouldFail, pool, vol, inputpool, inputvol, cmdline,    \
210                      flags, imgformat)                                       \
211     do {                                                                     \
212         struct testInfo info = { shouldFail, pool, vol, inputpool, inputvol, \
213                                  cmdline, flags, imgformat };                \
214         if (virtTestRun("Storage Vol XML-2-argv " cmdline,                   \
215                         testCompareXMLToArgvHelper, &info) < 0)              \
216             ret = -1;                                                        \
217        }                                                                     \
218     while (0);
219
220 #define DO_TEST(pool, ...)                                                 \
221     DO_TEST_FULL(false, pool, __VA_ARGS__)
222
223 #define DO_TEST_FAIL(pool, ...)                                            \
224     DO_TEST_FULL(true, pool, __VA_ARGS__)
225
226     DO_TEST("pool-dir", "vol-qcow2",
227             NULL, NULL,
228             "qcow2", 0, FMT_OPTIONS);
229     DO_TEST_FAIL("pool-dir", "vol-qcow2",
230                  NULL, NULL,
231                  "qcow2-prealloc", flags, FMT_OPTIONS);
232     DO_TEST("pool-dir", "vol-qcow2-nobacking",
233             NULL, NULL,
234             "qcow2-nobacking-prealloc", flags, FMT_OPTIONS);
235     DO_TEST("pool-dir", "vol-qcow2-nobacking",
236             "pool-dir", "vol-file",
237             "qcow2-nobacking-convert-prealloc", flags, FMT_OPTIONS);
238     DO_TEST_FAIL("pool-dir", "vol-qcow2",
239                  "pool-dir", "vol-file",
240                  "qcow2-convert-prealloc", flags, FMT_OPTIONS);
241     DO_TEST("pool-dir", "vol-qcow2",
242             NULL, NULL,
243             "qcow2-flag", 0, FMT_FLAG);
244     DO_TEST("pool-dir", "vol-qcow2-nobacking",
245             NULL, NULL,
246             "qcow2-nobacking-flag", 0, FMT_FLAG);
247     DO_TEST("pool-dir", "vol-qcow2-nobacking",
248             "pool-dir", "vol-file",
249             "qcow2-nobacking-convert-flag", 0, FMT_FLAG);
250     DO_TEST("pool-dir", "vol-qcow2",
251             NULL, NULL,
252             "qcow2-none", 0, FMT_NONE);
253     DO_TEST("pool-dir", "vol-qcow2-nobacking",
254             NULL, NULL,
255             "qcow2-nobacking-none", 0, FMT_NONE);
256     DO_TEST("pool-dir", "vol-qcow2-nobacking",
257             "pool-dir", "vol-file",
258             "qcow2-nobacking-convert-none", 0, FMT_NONE);
259     DO_TEST("pool-dir", "vol-qcow2-lazy",
260             NULL, NULL,
261             "qcow2-lazy", 0, FMT_OPTIONS);
262     DO_TEST("pool-dir", "vol-qcow2-1.1",
263             NULL, NULL,
264             "qcow2-1.1", 0, FMT_OPTIONS);
265     DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
266                  NULL, NULL,
267                  "qcow2-0.10-lazy", 0, FMT_OPTIONS);
268     DO_TEST("pool-dir", "vol-qcow2-nobacking",
269             "pool-logical", "vol-logical",
270             "qcow2-from-logical", 0, FMT_OPTIONS);
271     DO_TEST("pool-logical", "vol-logical",
272             "pool-dir", "vol-qcow2-nobacking",
273             "logical-from-qcow2", 0, FMT_OPTIONS);
274
275     DO_TEST("pool-dir", "vol-qcow2",
276             NULL, NULL,
277             "qcow2-compat", 0, FMT_COMPAT);
278     DO_TEST("pool-dir", "vol-qcow2-nobacking",
279             NULL, NULL,
280             "qcow2-nobacking-prealloc-compat", flags, FMT_COMPAT);
281     DO_TEST("pool-dir", "vol-qcow2-nobacking",
282             "pool-dir", "vol-file",
283             "qcow2-nobacking-convert-prealloc-compat", flags, FMT_COMPAT);
284     DO_TEST("pool-dir", "vol-qcow2-lazy",
285             NULL, NULL,
286             "qcow2-lazy", 0, FMT_COMPAT);
287     DO_TEST("pool-dir", "vol-qcow2-1.1",
288             NULL, NULL,
289             "qcow2-1.1", 0, FMT_COMPAT);
290     DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
291                  NULL, NULL,
292                  "qcow2-0.10-lazy", 0, FMT_COMPAT);
293     DO_TEST("pool-dir", "vol-qcow2-nobacking",
294             "pool-logical", "vol-logical",
295             "qcow2-from-logical-compat", 0, FMT_COMPAT);
296     DO_TEST("pool-logical", "vol-logical",
297             "pool-dir", "vol-qcow2-nobacking",
298             "logical-from-qcow2", 0, FMT_COMPAT);
299
300     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
301 }
302
303 VIRT_TEST_MAIN(mymain)