qemu: Validate spapr-vio addresses
[libvirt/ericb.git] / tests / storagevolxml2argvtest.c
blob20d986b52440833628a9b5d9b004f084c8d3db16
1 #include <config.h>
3 #include "internal.h"
4 #include "testutils.h"
5 #include "datatypes.h"
6 #include "storage/storage_util.h"
7 #include "testutilsqemu.h"
8 #include "virstring.h"
10 #define VIR_FROM_THIS VIR_FROM_NONE
12 const char create_tool[] = "qemu-img";
14 /* createVol sets this on volume creation */
15 static void
16 testSetVolumeType(virStorageVolDefPtr vol,
17 virStoragePoolDefPtr pool)
19 if (!vol || !pool)
20 return;
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;
29 case VIR_STORAGE_POOL_LOGICAL:
30 vol->type = VIR_STORAGE_VOL_BLOCK;
31 return;
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 unsigned long parse_flags)
45 virStorageVolEncryptConvertStep convertStep = VIR_STORAGE_VOL_ENCRYPT_NONE;
46 int ret = -1;
47 virStoragePoolDefPtr def = NULL;
48 virStoragePoolObjPtr obj = NULL;
49 VIR_AUTOFREE(char *) actualCmdline = NULL;
50 VIR_AUTOPTR(virStorageVolDef) vol = NULL;
51 VIR_AUTOPTR(virStorageVolDef) inputvol = NULL;
52 VIR_AUTOPTR(virStoragePoolDef) inputpool = NULL;
53 VIR_AUTOPTR(virCommand) cmd = NULL;
55 if (!(def = virStoragePoolDefParseFile(poolxml)))
56 goto cleanup;
58 if (!(obj = virStoragePoolObjNew())) {
59 virStoragePoolDefFree(def);
60 goto cleanup;
62 virStoragePoolObjSetDef(obj, def);
64 if (inputpoolxml) {
65 if (!(inputpool = virStoragePoolDefParseFile(inputpoolxml)))
66 goto cleanup;
69 if (inputvolxml)
70 parse_flags |= VIR_VOL_XML_PARSE_NO_CAPACITY;
72 if (!(vol = virStorageVolDefParseFile(def, volxml, parse_flags)))
73 goto cleanup;
75 if (inputvolxml &&
76 !(inputvol = virStorageVolDefParseFile(inputpool, inputvolxml, 0)))
77 goto cleanup;
79 testSetVolumeType(vol, def);
80 testSetVolumeType(inputvol, inputpool);
82 /* Using an input file for encryption requires a multi-step process
83 * to create an image of the same size as the inputvol and then to
84 * convert the inputvol afterwards. Since we only care about the
85 * command line we have to copy code from storageBackendCreateQemuImg
86 * and adjust it for the test needs. */
87 if (inputvol && (vol->target.encryption || inputvol->target.encryption))
88 convertStep = VIR_STORAGE_VOL_ENCRYPT_CREATE;
90 do {
91 virCommandFree(cmd);
92 cmd = virStorageBackendCreateQemuImgCmdFromVol(obj, vol,
93 inputvol, flags,
94 create_tool,
95 "/path/to/secretFile",
96 "/path/to/inputSecretFile",
97 convertStep);
98 if (!cmd) {
99 if (shouldFail) {
100 virResetLastError();
101 ret = 0;
103 goto cleanup;
106 if (convertStep != VIR_STORAGE_VOL_ENCRYPT_CONVERT) {
107 if (!(actualCmdline = virCommandToString(cmd, false)))
108 goto cleanup;
109 } else {
110 char *createCmdline = actualCmdline;
111 VIR_AUTOFREE(char *) cvtCmdline = NULL;
112 int rc;
114 if (!(cvtCmdline = virCommandToString(cmd, false)))
115 goto cleanup;
117 rc = virAsprintf(&actualCmdline, "%s\n%s",
118 createCmdline, cvtCmdline);
120 VIR_FREE(createCmdline);
121 if (rc < 0)
122 goto cleanup;
125 if (convertStep == VIR_STORAGE_VOL_ENCRYPT_NONE)
126 convertStep = VIR_STORAGE_VOL_ENCRYPT_DONE;
127 else if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CREATE)
128 convertStep = VIR_STORAGE_VOL_ENCRYPT_CONVERT;
129 else if (convertStep == VIR_STORAGE_VOL_ENCRYPT_CONVERT)
130 convertStep = VIR_STORAGE_VOL_ENCRYPT_DONE;
132 } while (convertStep != VIR_STORAGE_VOL_ENCRYPT_DONE);
134 if (virTestCompareToFile(actualCmdline, cmdline) < 0)
135 goto cleanup;
137 ret = 0;
139 cleanup:
140 virStoragePoolObjEndAPI(&obj);
141 return ret;
144 struct testInfo {
145 bool shouldFail;
146 const char *pool;
147 const char *vol;
148 const char *inputpool;
149 const char *inputvol;
150 const char *cmdline;
151 unsigned int flags;
152 unsigned long parseflags;
155 static int
156 testCompareXMLToArgvHelper(const void *data)
158 const struct testInfo *info = data;
159 VIR_AUTOFREE(char *) poolxml = NULL;
160 VIR_AUTOFREE(char *) inputpoolxml = NULL;
161 VIR_AUTOFREE(char *) volxml = NULL;
162 VIR_AUTOFREE(char *) inputvolxml = NULL;
163 VIR_AUTOFREE(char *) cmdline = NULL;
165 if (info->inputvol &&
166 virAsprintf(&inputvolxml, "%s/storagevolxml2xmlin/%s.xml",
167 abs_srcdir, info->inputvol) < 0)
168 return -1;
169 if (info->inputpool &&
170 virAsprintf(&inputpoolxml, "%s/storagepoolxml2xmlin/%s.xml",
171 abs_srcdir, info->inputpool) < 0)
172 return -1;
173 if (virAsprintf(&poolxml, "%s/storagepoolxml2xmlin/%s.xml",
174 abs_srcdir, info->pool) < 0 ||
175 virAsprintf(&volxml, "%s/storagevolxml2xmlin/%s.xml",
176 abs_srcdir, info->vol) < 0) {
177 return -1;
179 if (virAsprintf(&cmdline, "%s/storagevolxml2argvdata/%s.argv",
180 abs_srcdir, info->cmdline) < 0 && !info->shouldFail)
181 return -1;
183 return testCompareXMLToArgvFiles(info->shouldFail, poolxml, volxml,
184 inputpoolxml, inputvolxml,
185 cmdline, info->flags,
186 info->parseflags);
190 static int
191 mymain(void)
193 int ret = 0;
194 unsigned int flags = VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA;
196 #define DO_TEST_FULL(shouldFail, parseflags, pool, vol, inputpool, inputvol, \
197 cmdline, flags) \
198 do { \
199 struct testInfo info = { shouldFail, pool, vol, inputpool, inputvol, \
200 cmdline, flags, parseflags }; \
201 if (virTestRun("Storage Vol XML-2-argv " cmdline, \
202 testCompareXMLToArgvHelper, &info) < 0) \
203 ret = -1; \
205 while (0);
207 #define DO_TEST(pool, ...) \
208 DO_TEST_FULL(false, 0, pool, __VA_ARGS__)
210 #define DO_TEST_FAIL(pool, ...) \
211 DO_TEST_FULL(true, 0, pool, __VA_ARGS__)
213 DO_TEST("pool-dir", "vol-qcow2",
214 NULL, NULL,
215 "qcow2-compat", 0);
216 DO_TEST("pool-dir", "vol-qcow2-nobacking",
217 NULL, NULL,
218 "qcow2-nobacking-prealloc-compat", flags);
219 DO_TEST("pool-dir", "vol-qcow2-nobacking",
220 "pool-dir", "vol-file",
221 "qcow2-nobacking-convert-prealloc-compat", flags);
222 DO_TEST("pool-dir", "vol-qcow2-lazy",
223 NULL, NULL,
224 "qcow2-lazy", 0);
225 DO_TEST("pool-dir", "vol-qcow2-1.1",
226 NULL, NULL,
227 "qcow2-1.1", 0);
228 DO_TEST_FAIL("pool-dir", "vol-qcow2-0.10-lazy",
229 NULL, NULL,
230 "qcow2-0.10-lazy", 0);
231 DO_TEST("pool-dir", "vol-qcow2-nobacking",
232 "pool-logical", "vol-logical",
233 "qcow2-from-logical-compat", 0);
234 DO_TEST("pool-logical", "vol-logical",
235 "pool-dir", "vol-qcow2-nobacking",
236 "logical-from-qcow2", 0);
237 DO_TEST("pool-dir", "vol-qcow2-nocow",
238 NULL, NULL,
239 "qcow2-nocow-compat", 0);
240 DO_TEST("pool-dir", "vol-qcow2-nocapacity",
241 "pool-dir", "vol-file",
242 "qcow2-nocapacity-convert-prealloc", flags);
243 DO_TEST("pool-dir", "vol-qcow2-zerocapacity",
244 NULL, NULL,
245 "qcow2-zerocapacity", 0);
246 DO_TEST_FULL(false, VIR_VOL_XML_PARSE_OPT_CAPACITY,
247 "pool-dir", "vol-qcow2-nocapacity-backing", NULL, NULL,
248 "qcow2-nocapacity", 0);
250 DO_TEST("pool-dir", "vol-file-iso",
251 NULL, NULL,
252 "iso", 0);
253 DO_TEST("pool-dir", "vol-file",
254 "pool-dir", "vol-file-iso",
255 "iso-input", 0);
257 DO_TEST_FAIL("pool-dir", "vol-qcow2-encryption",
258 NULL, NULL,
259 "qcow2-encryption", 0);
261 DO_TEST("pool-dir", "vol-luks",
262 NULL, NULL,
263 "luks", 0);
264 DO_TEST("pool-dir", "vol-luks-cipher",
265 NULL, NULL,
266 "luks-cipher", 0);
268 DO_TEST("pool-dir", "vol-luks-convert",
269 "pool-dir", "vol-file",
270 "luks-convert", 0);
272 DO_TEST("pool-dir", "vol-luks-convert",
273 "pool-dir", "vol-file-qcow2",
274 "luks-convert-qcow2", 0);
276 DO_TEST("pool-dir", "vol-encrypt2",
277 "pool-dir", "vol-encrypt1",
278 "luks-convert-encrypt", 0);
280 DO_TEST("pool-dir", "vol-file",
281 "pool-dir", "vol-encrypt2",
282 "luks-convert-encrypt2fileraw", 0);
284 DO_TEST("pool-dir", "vol-file-qcow2",
285 "pool-dir", "vol-encrypt2",
286 "luks-convert-encrypt2fileqcow2", 0);
288 return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
291 VIR_TEST_MAIN(mymain)