1 #include <config.h>
2 
3 #include <unistd.h>
4 
5 #include <sys/types.h>
6 #include <fcntl.h>
7 
8 #include "testutils.h"
9 
10 #include "internal.h"
11 #include "testutilsqemu.h"
12 #include "configmake.h"
13 
14 #define VIR_FROM_THIS VIR_FROM_NONE
15 
16 static virQEMUDriver driver;
17 
18 static int
testCompareStatusXMLToXMLFiles(const void * opaque)19 testCompareStatusXMLToXMLFiles(const void *opaque)
20 {
21     const struct testQemuInfo *data = opaque;
22     virDomainObj *obj = NULL;
23     g_autofree char *actual = NULL;
24     int ret = -1;
25 
26     if (testQemuInfoInitArgs((struct testQemuInfo *) data) < 0)
27         return -1;
28 
29     if (qemuTestCapsCacheInsert(driver.qemuCapsCache, data->qemuCaps) < 0)
30         return -1;
31 
32     if (!(obj = virDomainObjParseFile(data->infile, driver.xmlopt,
33                                       VIR_DOMAIN_DEF_PARSE_STATUS |
34                                       VIR_DOMAIN_DEF_PARSE_ACTUAL_NET |
35                                       VIR_DOMAIN_DEF_PARSE_PCI_ORIG_STATES |
36                                       VIR_DOMAIN_DEF_PARSE_SKIP_VALIDATE |
37                                       VIR_DOMAIN_DEF_PARSE_ALLOW_POST_PARSE_FAIL))) {
38         VIR_TEST_DEBUG("\nfailed to parse '%s'", data->infile);
39         goto cleanup;
40     }
41 
42     if (!(actual = virDomainObjFormat(obj, driver.xmlopt,
43                                       VIR_DOMAIN_DEF_FORMAT_SECURE |
44                                       VIR_DOMAIN_DEF_FORMAT_STATUS |
45                                       VIR_DOMAIN_DEF_FORMAT_ACTUAL_NET |
46                                       VIR_DOMAIN_DEF_FORMAT_PCI_ORIG_STATES |
47                                       VIR_DOMAIN_DEF_FORMAT_CLOCK_ADJUST))) {
48         VIR_TEST_DEBUG("\nfailed to format back '%s'", data->infile);
49         goto cleanup;
50     }
51 
52     if (virTestCompareToFile(actual, data->outfile) < 0)
53         goto cleanup;
54 
55     ret = 0;
56 
57  cleanup:
58     virDomainObjEndAPI(&obj);
59     return ret;
60 }
61 
62 
63 static const char *statusPath = abs_srcdir "/qemustatusxml2xmldata/";
64 
65 static void
testInfoSetStatusPaths(struct testQemuInfo * info)66 testInfoSetStatusPaths(struct testQemuInfo *info)
67 {
68     info->infile = g_strdup_printf("%s%s-in.xml", statusPath, info->name);
69     info->outfile = g_strdup_printf("%s%s-out.xml", statusPath, info->name);
70 }
71 
72 
73 #define FAKEROOTDIRTEMPLATE abs_builddir "/fakerootdir-XXXXXX"
74 
75 static int
mymain(void)76 mymain(void)
77 {
78     int ret = 0;
79     g_autofree char *fakerootdir = NULL;
80     g_autoptr(GHashTable) capslatest = testQemuGetLatestCaps();
81     g_autoptr(GHashTable) capscache = virHashNew(virObjectFreeHashData);
82     g_autoptr(virConnect) conn = NULL;
83     struct testQemuConf testConf = { .capslatest = capslatest,
84                                      .capscache = capscache,
85                                      .qapiSchemaCache = NULL };
86 
87     if (!capslatest)
88         return EXIT_FAILURE;
89 
90     fakerootdir = g_strdup(FAKEROOTDIRTEMPLATE);
91 
92     if (!g_mkdtemp(fakerootdir)) {
93         fprintf(stderr, "Cannot create fakerootdir");
94         abort();
95     }
96 
97     g_setenv("LIBVIRT_FAKE_ROOT_DIR", fakerootdir, TRUE);
98 
99     if (qemuTestDriverInit(&driver) < 0)
100         return EXIT_FAILURE;
101 
102     driver.privileged = true;
103 
104     if (!(conn = virGetConnect()))
105         goto cleanup;
106 
107     virSetConnectInterface(conn);
108     virSetConnectNetwork(conn);
109     virSetConnectNWFilter(conn);
110     virSetConnectNodeDev(conn);
111     virSetConnectSecret(conn);
112     virSetConnectStorage(conn);
113 
114 #define DO_TEST_STATUS(_name) \
115     do { \
116         static struct testQemuInfo info = { \
117             .name = _name, \
118         }; \
119         testQemuInfoSetArgs(&info, &testConf, ARG_END); \
120         testInfoSetStatusPaths(&info); \
121 \
122         if (virTestRun("QEMU status XML-2-XML " _name, \
123                        testCompareStatusXMLToXMLFiles, &info) < 0) \
124             ret = -1; \
125 \
126         testQemuInfoClear(&info); \
127     } while (0)
128 
129 
130     DO_TEST_STATUS("blockjob-mirror");
131     DO_TEST_STATUS("vcpus-multi");
132     DO_TEST_STATUS("modern");
133     DO_TEST_STATUS("migration-out-nbd");
134     DO_TEST_STATUS("migration-in-params");
135     DO_TEST_STATUS("migration-out-params");
136     DO_TEST_STATUS("migration-out-nbd-tls");
137     DO_TEST_STATUS("migration-out-nbd-bitmaps");
138     DO_TEST_STATUS("upgrade");
139 
140     DO_TEST_STATUS("blockjob-blockdev");
141 
142     DO_TEST_STATUS("backup-pull");
143 
144  cleanup:
145     if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
146         virFileDeleteTree(fakerootdir);
147 
148     qemuTestDriverFree(&driver);
149 
150     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
151 }
152 
153 VIR_TEST_MAIN_PRELOAD(mymain,
154                       VIR_TEST_MOCK("virpci"),
155                       VIR_TEST_MOCK("virrandom"),
156                       VIR_TEST_MOCK("domaincaps"))
157