forked from libvirt/libvirt
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathqemublocktest.c
1401 lines (1106 loc) · 48.8 KB
/
qemublocktest.c
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 "testutilsqemuschema.h"
#include "virstoragefile.h"
#include "virstring.h"
#include "virlog.h"
#include "qemu/qemu_block.h"
#include "qemu/qemu_qapi.h"
#include "qemu/qemu_monitor_json.h"
#include "qemu/qemu_backup.h"
#include "qemu/qemu_checkpoint.h"
#include "qemu/qemu_validate.h"
#include "qemu/qemu_command.h"
#define LIBVIRT_SNAPSHOT_CONF_PRIV_H_ALLOW
#include "conf/snapshot_conf_priv.h"
#define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("tests.storagetest");
struct testBackingXMLjsonXMLdata {
int type;
const char *xml;
bool legacy;
virHashTablePtr schema;
virJSONValuePtr schemaroot;
};
static int
testBackingXMLjsonXML(const void *args)
{
const struct testBackingXMLjsonXMLdata *data = args;
g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autoptr(virJSONValue) backendprops = NULL;
g_autoptr(virJSONValue) wrapper = NULL;
g_autofree char *propsstr = NULL;
g_autofree char *protocolwrapper = NULL;
g_autofree char *actualxml = NULL;
g_autoptr(virStorageSource) xmlsrc = NULL;
g_autoptr(virStorageSource) jsonsrc = NULL;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
unsigned int backendpropsflags = 0;
if (data->legacy)
backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
if (!(xmlsrc = virStorageSourceNew()))
return -1;
xmlsrc->type = data->type;
if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
return -1;
if (virDomainStorageSourceParse(ctxt->node, ctxt, xmlsrc, 0, NULL) < 0) {
fprintf(stderr, "failed to parse disk source xml\n");
return -1;
}
if (!(backendprops = qemuBlockStorageSourceGetBackendProps(xmlsrc,
backendpropsflags))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
if (!data->legacy) {
if (testQEMUSchemaValidate(backendprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *debugprops = virJSONValueToString(backendprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
debugprops, NULLSTR(debugmsg));
return -1;
}
}
if (virJSONValueObjectCreate(&wrapper, "a:file", &backendprops, NULL) < 0)
return -1;
if (!(propsstr = virJSONValueToString(wrapper, false)))
return -1;
protocolwrapper = g_strdup_printf("json:%s", propsstr);
if (virStorageSourceNewFromBackingAbsolute(protocolwrapper,
&jsonsrc) < 0) {
fprintf(stderr, "failed to parse disk json\n");
return -1;
}
if (virDomainDiskSourceFormat(&buf, jsonsrc, "source", 0, false, 0,
false, false, NULL) < 0 ||
!(actualxml = virBufferContentAndReset(&buf))) {
fprintf(stderr, "failed to format disk source xml\n");
return -1;
}
if (STRNEQ(actualxml, data->xml)) {
fprintf(stderr, "\n expected storage source xml:\n'%s'\n"
"actual storage source xml:\n%s\n"
"intermediate json:\n%s\n",
data->xml, actualxml, protocolwrapper);
return -1;
}
return 0;
}
static const char *testJSONtoJSONPath = abs_srcdir "/qemublocktestdata/jsontojson/";
struct testJSONtoJSONData {
const char *name;
virHashTablePtr schema;
virJSONValuePtr schemaroot;
};
static int
testJSONtoJSON(const void *args)
{
const struct testJSONtoJSONData *data = args;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
g_autoptr(virJSONValue) jsonsrcout = NULL;
g_autoptr(virStorageSource) src = NULL;
g_autofree char *actual = NULL;
g_autofree char *in = NULL;
g_autofree char *infile = g_strdup_printf("%s%s-in.json", testJSONtoJSONPath,
data->name);
g_autofree char *outfile = g_strdup_printf("%s%s-out.json", testJSONtoJSONPath,
data->name);
if (virTestLoadFile(infile, &in) < 0)
return -1;
if (virStorageSourceNewFromBackingAbsolute(in, &src) < 0) {
fprintf(stderr, "failed to parse disk json\n");
return -1;
}
if (!(jsonsrcout = qemuBlockStorageSourceGetBackendProps(src,
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY))) {
fprintf(stderr, "failed to format disk source json\n");
return -1;
}
if (!(actual = virJSONValueToString(jsonsrcout, true)))
return -1;
if (testQEMUSchemaValidate(jsonsrcout, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
actual, NULLSTR(debugmsg));
return -1;
}
return virTestCompareToFile(actual, outfile);
}
struct testQemuDiskXMLToJSONImageData {
virJSONValuePtr formatprops;
virJSONValuePtr storageprops;
virJSONValuePtr storagepropssrc;
char *backingstore;
};
struct testQemuDiskXMLToJSONData {
virQEMUDriverPtr driver;
virHashTablePtr schema;
virJSONValuePtr schemaroot;
const char *name;
bool fail;
struct testQemuDiskXMLToJSONImageData *images;
size_t nimages;
virQEMUCapsPtr qemuCaps;
};
static void
testQemuDiskXMLToPropsClear(struct testQemuDiskXMLToJSONData *data)
{
size_t i;
for (i = 0; i < data->nimages; i++) {
virJSONValueFree(data->images[i].formatprops);
virJSONValueFree(data->images[i].storageprops);
virJSONValueFree(data->images[i].storagepropssrc);
g_free(data->images[i].backingstore);
}
data->nimages = 0;
VIR_FREE(data->images);
}
static int
testQemuDiskXMLToJSONFakeSecrets(virStorageSourcePtr src)
{
qemuDomainStorageSourcePrivatePtr srcpriv;
if (!src->privateData &&
!(src->privateData = qemuDomainStorageSourcePrivateNew()))
return -1;
srcpriv = QEMU_DOMAIN_STORAGE_SOURCE_PRIVATE(src);
if (src->auth) {
if (VIR_ALLOC(srcpriv->secinfo) < 0)
return -1;
srcpriv->secinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
srcpriv->secinfo->s.aes.username = g_strdup(src->auth->username);
srcpriv->secinfo->s.aes.alias = g_strdup_printf("%s-secalias",
NULLSTR(src->nodestorage));
}
if (src->encryption) {
if (VIR_ALLOC(srcpriv->encinfo) < 0)
return -1;
srcpriv->encinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_AES;
srcpriv->encinfo->s.aes.alias = g_strdup_printf("%s-encalias",
NULLSTR(src->nodeformat));
}
return 0;
}
static const char *testQemuDiskXMLToJSONPath = abs_srcdir "/qemublocktestdata/xml2json/";
static int
testQemuDiskXMLToProps(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_autoptr(virDomainDef) vmdef = NULL;
virDomainDiskDefPtr disk = NULL;
virStorageSourcePtr n;
g_autoptr(virJSONValue) formatProps = NULL;
g_autoptr(virJSONValue) storageProps = NULL;
g_autoptr(virJSONValue) storageSrcOnlyProps = NULL;
g_autofree char *xmlpath = NULL;
g_autofree char *xmlstr = NULL;
xmlpath = g_strdup_printf("%s%s.xml", testQemuDiskXMLToJSONPath, data->name);
if (virTestLoadFile(xmlpath, &xmlstr) < 0)
return -1;
/* qemu stores node names in the status XML portion */
if (!(disk = virDomainDiskDefParse(xmlstr, data->driver->xmlopt,
VIR_DOMAIN_DEF_PARSE_STATUS)))
return -1;
if (!(vmdef = virDomainDefNew()) ||
virDomainDiskInsert(vmdef, disk) < 0)
return -1;
if (qemuValidateDomainDeviceDefDisk(disk, vmdef, data->qemuCaps) < 0) {
VIR_TEST_VERBOSE("invalid configuration for disk");
return -1;
}
for (n = disk->src; virStorageSourceIsBacking(n); n = n->backingStore) {
g_autofree char *backingstore = NULL;
unsigned int backendpropsflagsnormal = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY;
unsigned int backendpropsflagstarget = QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_AUTO_READONLY |
QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_TARGET_ONLY;
if (testQemuDiskXMLToJSONFakeSecrets(n) < 0)
return -1;
if (qemuDomainValidateStorageSource(n, data->qemuCaps, false) < 0)
return -1;
qemuDomainPrepareDiskSourceData(disk, n);
if (!(formatProps = qemuBlockStorageSourceGetBlockdevProps(n, n->backingStore)) ||
!(storageSrcOnlyProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagstarget)) ||
!(storageProps = qemuBlockStorageSourceGetBackendProps(n, backendpropsflagsnormal)) ||
!(backingstore = qemuBlockGetBackingStoreString(n, true))) {
if (!data->fail) {
VIR_TEST_VERBOSE("failed to generate qemu blockdev props");
return -1;
}
} else if (data->fail) {
VIR_TEST_VERBOSE("qemu blockdev props should have failed");
return -1;
}
if (VIR_REALLOC_N(data->images, data->nimages + 1) < 0)
return -1;
data->images[data->nimages].formatprops = g_steal_pointer(&formatProps);
data->images[data->nimages].storageprops = g_steal_pointer(&storageProps);
data->images[data->nimages].storagepropssrc = g_steal_pointer(&storageSrcOnlyProps);
data->images[data->nimages].backingstore = g_steal_pointer(&backingstore);
data->nimages++;
}
return 0;
}
static int
testQemuDiskXMLToPropsValidateSchema(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
int ret = 0;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
for (i = 0; i < data->nimages; i++) {
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
if (testQEMUSchemaValidate(data->images[i].formatprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].formatprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
virBufferFreeAndReset(&debug);
if (testQEMUSchemaValidate(data->images[i].storageprops, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].storageprops, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
virBufferFreeAndReset(&debug);
if (testQEMUSchemaValidate(data->images[i].storagepropssrc, data->schemaroot,
data->schema, false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
g_autofree char *propsstr = virJSONValueToString(data->images[i].storagepropssrc, true);
VIR_TEST_VERBOSE("json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
propsstr, NULLSTR(debugmsg));
ret = -1;
}
}
return ret;
}
static int
testQemuDiskXMLToPropsValidateFile(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *jsonpath = NULL;
g_autofree char *actual = NULL;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
jsonpath = g_strdup_printf("%s%s.json", testQemuDiskXMLToJSONPath, data->name);
for (i = 0; i < data->nimages; i++) {
g_autofree char *formatprops = NULL;
g_autofree char *storageprops = NULL;
if (!(formatprops = virJSONValueToString(data->images[i].formatprops, true)))
return -1;
if (!(storageprops = virJSONValueToString(data->images[i].storageprops, true)))
return -1;
virBufferStrcat(&buf, formatprops, storageprops, NULL);
}
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, jsonpath);
}
static int
testQemuDiskXMLToPropsValidateFileSrcOnly(const void *opaque)
{
struct testQemuDiskXMLToJSONData *data = (void *) opaque;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
g_autofree char *jsonpath = NULL;
g_autofree char *actual = NULL;
size_t i;
if (data->fail)
return EXIT_AM_SKIP;
jsonpath = g_strdup_printf("%s%s-srconly.json", testQemuDiskXMLToJSONPath,
data->name);
for (i = 0; i < data->nimages; i++) {
g_autofree char *jsonstr = NULL;
virBufferAddLit(&buf, "(\n");
virBufferAdjustIndent(&buf, 2);
virBufferAddLit(&buf, "source only properties:\n");
if (!(jsonstr = virJSONValueToString(data->images[i].storagepropssrc, true)))
return -1;
virBufferAddStr(&buf, jsonstr);
virBufferAddLit(&buf, "backing store string:\n");
virBufferAddStr(&buf, data->images[i].backingstore);
virBufferTrim(&buf, "\n");
virBufferAdjustIndent(&buf, -2);
virBufferAddLit(&buf, "\n)\n");
}
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, jsonpath);
}
struct testQemuImageCreateData {
const char *name;
const char *backingname;
virHashTablePtr schema;
virJSONValuePtr schemaroot;
virQEMUDriverPtr driver;
virQEMUCapsPtr qemuCaps;
};
static const char *testQemuImageCreatePath = abs_srcdir "/qemublocktestdata/imagecreate/";
static virStorageSourcePtr
testQemuImageCreateLoadDiskXML(const char *name,
virDomainXMLOptionPtr xmlopt)
{
virDomainSnapshotDiskDefPtr diskdef = NULL;
g_autoptr(xmlDoc) doc = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL;
xmlNodePtr node;
g_autofree char *xmlpath = NULL;
virStorageSourcePtr ret = NULL;
xmlpath = g_strdup_printf("%s%s.xml", testQemuImageCreatePath, name);
if (!(doc = virXMLParseFileCtxt(xmlpath, &ctxt)))
return NULL;
if (!(node = virXPathNode("//disk", ctxt))) {
VIR_TEST_VERBOSE("failed to find <source> element\n");
return NULL;
}
if (VIR_ALLOC(diskdef) < 0)
return NULL;
if (virDomainSnapshotDiskDefParseXML(node, ctxt, diskdef,
VIR_DOMAIN_DEF_PARSE_STATUS,
xmlopt) == 0)
ret = g_steal_pointer(&diskdef->src);
virDomainSnapshotDiskDefFree(diskdef);
return ret;
}
static int
testQemuImageCreate(const void *opaque)
{
struct testQemuImageCreateData *data = (void *) opaque;
g_autoptr(virJSONValue) protocolprops = NULL;
g_autoptr(virJSONValue) formatprops = NULL;
g_autoptr(virStorageSource) src = NULL;
g_auto(virBuffer) debug = VIR_BUFFER_INITIALIZER;
g_auto(virBuffer) actualbuf = VIR_BUFFER_INITIALIZER;
g_autofree char *jsonprotocol = NULL;
g_autofree char *jsonformat = NULL;
g_autofree char *actual = NULL;
g_autofree char *jsonpath = NULL;
if (!(src = testQemuImageCreateLoadDiskXML(data->name, data->driver->xmlopt)))
return -1;
if (data->backingname &&
!(src->backingStore = testQemuImageCreateLoadDiskXML(data->backingname,
data->driver->xmlopt)))
return -1;
if (testQemuDiskXMLToJSONFakeSecrets(src) < 0)
return -1;
/* fake some sizes */
src->capacity = UINT_MAX * 2ULL;
src->physical = UINT_MAX + 1ULL;
if (qemuDomainValidateStorageSource(src, data->qemuCaps, false) < 0)
return -1;
if (qemuBlockStorageSourceCreateGetStorageProps(src, &protocolprops) < 0)
return -1;
if (qemuBlockStorageSourceCreateGetFormatProps(src, src->backingStore, &formatprops) < 0)
return -1;
if (formatprops) {
if (!(jsonformat = virJSONValueToString(formatprops, true)))
return -1;
if (testQEMUSchemaValidate(formatprops, data->schemaroot, data->schema,
false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
VIR_TEST_VERBOSE("blockdev-create format json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
jsonformat, NULLSTR(debugmsg));
return -1;
}
virBufferFreeAndReset(&debug);
}
if (protocolprops) {
if (!(jsonprotocol = virJSONValueToString(protocolprops, true)))
return -1;
if (testQEMUSchemaValidate(protocolprops, data->schemaroot, data->schema,
false, &debug) < 0) {
g_autofree char *debugmsg = virBufferContentAndReset(&debug);
VIR_TEST_VERBOSE("blockdev-create protocol json does not conform to QAPI schema");
VIR_TEST_DEBUG("json:\n%s\ndoes not match schema. Debug output:\n %s",
jsonprotocol, NULLSTR(debugmsg));
return -1;
}
virBufferFreeAndReset(&debug);
}
virBufferStrcat(&actualbuf, "protocol:\n", NULLSTR(jsonprotocol),
"\nformat:\n", NULLSTR(jsonformat), NULL);
virBufferTrim(&actualbuf, "\n");
virBufferAddLit(&actualbuf, "\n");
jsonpath = g_strdup_printf("%s%s.json", testQemuImageCreatePath, data->name);
if (!(actual = virBufferContentAndReset(&actualbuf)))
return -1;
return virTestCompareToFile(actual, jsonpath);
}
static const char *bitmapDetectPrefix = "qemublocktestdata/bitmap/";
static void
testQemuDetectBitmapsWorker(virHashTablePtr nodedata,
const char *nodename,
virBufferPtr buf)
{
qemuBlockNamedNodeDataPtr data;
size_t i;
if (!(data = virHashLookup(nodedata, nodename)))
return;
virBufferAsprintf(buf, "%s:\n", nodename);
virBufferAdjustIndent(buf, 1);
for (i = 0; i < data->nbitmaps; i++) {
qemuBlockNamedNodeDataBitmapPtr bitmap = data->bitmaps[i];
virBufferAsprintf(buf, "%8s: record:%d busy:%d persist:%d inconsist:%d gran:%llu dirty:%llu\n",
bitmap->name, bitmap->recording, bitmap->busy,
bitmap->persistent, bitmap->inconsistent,
bitmap->granularity, bitmap->dirtybytes);
}
virBufferAdjustIndent(buf, -1);
}
static int
testQemuDetectBitmaps(const void *opaque)
{
const char *name = opaque;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
g_autofree char *actual = NULL;
g_autofree char *expectpath = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
size_t i;
expectpath = g_strdup_printf("%s/%s%s.out", abs_srcdir,
bitmapDetectPrefix, name);
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, name,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON");
return -1;
}
/* we detect for the first 30 nodenames for simplicity */
for (i = 0; i < 30; i++) {
g_autofree char *nodename = g_strdup_printf("libvirt-%zu-format", i);
testQemuDetectBitmapsWorker(nodedata, nodename, &buf);
}
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, expectpath);
}
static void
testQemuBitmapListPrint(const char *title,
GSList *next,
virBufferPtr buf)
{
if (!next)
return;
virBufferAsprintf(buf, "%s\n", title);
for (; next; next = next->next) {
virStorageSourcePtr src = next->data;
virBufferAsprintf(buf, "%s\n", src->nodeformat);
}
}
static virStorageSourcePtr
testQemuBackupIncrementalBitmapCalculateGetFakeImage(size_t idx)
{
virStorageSourcePtr ret;
if (!(ret = virStorageSourceNew()))
abort();
ret->id = idx;
ret->type = VIR_STORAGE_TYPE_FILE;
ret->format = VIR_STORAGE_FILE_QCOW2;
ret->path = g_strdup_printf("/image%zu", idx);
ret->nodestorage = g_strdup_printf("libvirt-%zu-storage", idx);
ret->nodeformat = g_strdup_printf("libvirt-%zu-format", idx);
return ret;
}
static virStorageSourcePtr
testQemuBackupIncrementalBitmapCalculateGetFakeChain(void)
{
virStorageSourcePtr ret;
virStorageSourcePtr n;
size_t i;
n = ret = testQemuBackupIncrementalBitmapCalculateGetFakeImage(1);
for (i = 2; i < 6; i++) {
n->backingStore = testQemuBackupIncrementalBitmapCalculateGetFakeImage(i);
n = n->backingStore;
}
return ret;
}
static virStorageSourcePtr
testQemuBitmapGetFakeChainEntry(virStorageSourcePtr src,
size_t idx)
{
virStorageSourcePtr n;
for (n = src; n; n = n->backingStore) {
if (n->id == idx)
return n;
}
return NULL;
}
static const char *backupDataPrefix = "qemublocktestdata/backupmerge/";
struct testQemuBackupIncrementalBitmapCalculateData {
const char *name;
virStorageSourcePtr chain;
const char *incremental;
const char *nodedatafile;
};
static int
testQemuBackupIncrementalBitmapCalculate(const void *opaque)
{
const struct testQemuBackupIncrementalBitmapCalculateData *data = opaque;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
g_autoptr(virJSONValue) actions = virJSONValueNewArray();
g_autofree char *expectpath = NULL;
g_autoptr(virStorageSource) target = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
backupDataPrefix, data->name);
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
return -1;
}
if (!(target = virStorageSourceNew()))
return -1;
target->nodeformat = g_strdup_printf("target_node");
if (qemuBackupDiskPrepareOneBitmapsChain(data->chain,
target,
"target-bitmap-name",
data->incremental,
actions,
nodedata) >= 0) {
if (virJSONValueToBuffer(actions, &buf, true) < 0)
return -1;
} else {
virBufferAddLit(&buf, "NULL\n");
}
return virTestCompareToFile(virBufferCurrentContent(&buf), expectpath);
}
static const char *checkpointDeletePrefix = "qemublocktestdata/checkpointdelete/";
struct testQemuCheckpointDeleteData {
const char *name;
virStorageSourcePtr chain;
const char *deletebitmap;
const char *nodedatafile;
};
static int
testQemuCheckpointDelete(const void *opaque)
{
const struct testQemuCheckpointDeleteData *data = opaque;
g_autofree char *actual = NULL;
g_autofree char *expectpath = NULL;
g_autoptr(virJSONValue) actions = NULL;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
g_autoptr(GSList) reopenimages = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
checkpointDeletePrefix, data->name);
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
return -1;
}
actions = virJSONValueNewArray();
if (qemuCheckpointDiscardDiskBitmaps(data->chain,
nodedata,
data->deletebitmap,
actions,
"testdisk",
&reopenimages) >= 0) {
if (virJSONValueToBuffer(actions, &buf, true) < 0)
return -1;
} else {
virBufferAddLit(&buf, "NULL\n");
}
testQemuBitmapListPrint("reopen nodes:", reopenimages, &buf);
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, expectpath);
}
struct testQemuBlockBitmapValidateData {
const char *name;
const char *bitmapname;
virStorageSourcePtr chain;
bool expect;
};
static int
testQemuBlockBitmapValidate(const void *opaque)
{
const struct testQemuBlockBitmapValidateData *data = opaque;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
bool actual;
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->name,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
return -1;
}
actual = qemuBlockBitmapChainIsValid(data->chain, data->bitmapname, nodedata);
if (actual != data->expect) {
VIR_TEST_VERBOSE("expected rv:'%d' actual rv:'%d'\n", data->expect, actual);
return -1;
}
return 0;
}
static const char *blockcopyPrefix = "qemublocktestdata/bitmapblockcopy/";
struct testQemuBlockBitmapBlockcopyData {
const char *name;
bool shallow;
virStorageSourcePtr chain;
const char *nodedatafile;
};
static int
testQemuBlockBitmapBlockcopy(const void *opaque)
{
const struct testQemuBlockBitmapBlockcopyData *data = opaque;
g_autofree char *actual = NULL;
g_autofree char *expectpath = NULL;
g_autoptr(virJSONValue) actions = NULL;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
g_autoptr(virStorageSource) fakemirror = virStorageSourceNew();
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
if (!fakemirror)
return -1;
fakemirror->nodeformat = g_strdup("mirror-format-node");
expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
blockcopyPrefix, data->name);
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
return -1;
}
if (qemuBlockBitmapsHandleBlockcopy(data->chain, fakemirror, nodedata,
data->shallow, &actions) < 0)
return -1;
if (actions &&
virJSONValueToBuffer(actions, &buf, true) < 0)
return -1;
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, expectpath);
}
static const char *blockcommitPrefix = "qemublocktestdata/bitmapblockcommit/";
struct testQemuBlockBitmapBlockcommitData {
const char *name;
virStorageSourcePtr top;
virStorageSourcePtr base;
virStorageSourcePtr chain;
const char *nodedatafile;
};
static int
testQemuBlockBitmapBlockcommit(const void *opaque)
{
const struct testQemuBlockBitmapBlockcommitData *data = opaque;
g_autofree char *actual = NULL;
g_autofree char *expectpath = NULL;
g_autoptr(virJSONValue) actionsMerge = NULL;
g_autoptr(virJSONValue) nodedatajson = NULL;
g_autoptr(virHashTable) nodedata = NULL;
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
bool active = data->top == data->chain;
expectpath = g_strdup_printf("%s/%s%s", abs_srcdir,
blockcommitPrefix, data->name);
if (!(nodedatajson = virTestLoadFileJSON(bitmapDetectPrefix, data->nodedatafile,
".json", NULL)))
return -1;
if (!(nodedata = qemuMonitorJSONBlockGetNamedNodeDataJSON(nodedatajson))) {
VIR_TEST_VERBOSE("failed to load nodedata JSON\n");
return -1;
}
virBufferAddLit(&buf, "merge bitmpas:\n");
if (qemuBlockBitmapsHandleCommitFinish(data->top, data->base, active, nodedata,
&actionsMerge) < 0)
return -1;
if (actionsMerge &&
virJSONValueToBuffer(actionsMerge, &buf, true) < 0)
return -1;
actual = virBufferContentAndReset(&buf);
return virTestCompareToFile(actual, expectpath);
}
static int
mymain(void)
{
int ret = 0;
virQEMUDriver driver;
struct testBackingXMLjsonXMLdata xmljsonxmldata;
struct testQemuDiskXMLToJSONData diskxmljsondata;
struct testJSONtoJSONData jsontojsondata;
struct testQemuImageCreateData imagecreatedata;
struct testQemuBackupIncrementalBitmapCalculateData backupbitmapcalcdata;
struct testQemuCheckpointDeleteData checkpointdeletedata;
struct testQemuBlockBitmapValidateData blockbitmapvalidatedata;
struct testQemuBlockBitmapBlockcopyData blockbitmapblockcopydata;
struct testQemuBlockBitmapBlockcommitData blockbitmapblockcommitdata;
char *capslatest_x86_64 = NULL;
g_autoptr(virQEMUCaps) caps_x86_64 = NULL;
g_autoptr(virHashTable) qmp_schema_x86_64 = NULL;
virJSONValuePtr qmp_schemaroot_x86_64_blockdev_add = NULL;
g_autoptr(virStorageSource) bitmapSourceChain = NULL;
if (qemuTestDriverInit(&driver) < 0)
return EXIT_FAILURE;
bitmapSourceChain = testQemuBackupIncrementalBitmapCalculateGetFakeChain();
diskxmljsondata.driver = &driver;