qemuxml2argvtest.c 11.4 KB
Newer Older
1
#include <config.h>
2

3
#include <stdio.h>
4 5
#include <stdlib.h>
#include <unistd.h>
6 7 8 9 10
#include <string.h>

#include <sys/types.h>
#include <fcntl.h>

11 12
#ifdef WITH_QEMU

13
#include "internal.h"
14
#include "testutils.h"
15
#include "qemu/qemu_conf.h"
16
#include "datatypes.h"
17

18 19
#include "testutilsqemu.h"

20
static char *progname;
21
static char *abs_srcdir;
22
static struct qemud_driver driver;
23 24 25

#define MAX_FILE 4096

26 27
static int testCompareXMLToArgvFiles(const char *xml,
                                     const char *cmd,
28
                                     unsigned long long extraFlags,
29
                                     const char *migrateFrom) {
30 31 32
    char argvData[MAX_FILE];
    char *expectargv = &(argvData[0]);
    char *actualargv = NULL;
33
    const char **argv = NULL;
34
    const char **qenv = NULL;
35
    const char **tmp = NULL;
36 37
    int ret = -1, len;
    unsigned long long flags;
38
    virDomainDefPtr vmdef = NULL;
39
    virDomainChrDef monitor_chr;
40 41 42 43
    virConnectPtr conn;

    if (!(conn = virGetConnect()))
        goto fail;
44

45
    if (virtTestLoadFile(cmd, &expectargv, MAX_FILE) < 0)
46 47
        goto fail;

48
    if (!(vmdef = virDomainDefParseFile(driver.caps, xml,
49
                                        VIR_DOMAIN_XML_INACTIVE)))
50 51
        goto fail;

52
    if (extraFlags & QEMUD_CMD_FLAG_DOMID)
53
        vmdef->id = 6;
54
    else
55
        vmdef->id = -1;
56

57
    memset(&monitor_chr, 0, sizeof(monitor_chr));
58 59 60
    monitor_chr.type = VIR_DOMAIN_CHR_TYPE_UNIX;
    monitor_chr.data.nix.path = (char *)"/tmp/test-monitor";
    monitor_chr.data.nix.listen = 1;
61 62
    if (!(monitor_chr.info.alias = strdup("monitor")))
        goto fail;
63

64 65 66
    flags = QEMUD_CMD_FLAG_VNC_COLON |
        QEMUD_CMD_FLAG_NO_REBOOT |
        extraFlags;
67

68 69 70
    if (qemudCanonicalizeMachine(&driver, vmdef) < 0)
        goto fail;

71 72 73 74 75 76 77 78 79 80 81 82
    if (flags & QEMUD_CMD_FLAG_DEVICE) {
        qemuDomainPCIAddressSetPtr pciaddrs;
        if (!(pciaddrs = qemuDomainPCIAddressSetCreate(vmdef)))
            goto fail;

        if (qemuAssignDevicePCISlots(vmdef, pciaddrs) < 0)
            goto fail;

        qemuDomainPCIAddressSetFree(pciaddrs);
    }


83
    if (qemudBuildCommandLine(conn, &driver,
D
Daniel P. Berrange 已提交
84
                              vmdef, &monitor_chr, 0, flags,
85
                              &argv, &qenv,
86
                              NULL, NULL, migrateFrom) < 0)
87 88
        goto fail;

89
    len = 1; /* for trailing newline */
90 91 92 93 94 95 96
    tmp = qenv;
    while (*tmp) {
        len += strlen(*tmp) + 1;
        tmp++;
    }

    tmp = argv;
97 98 99 100
    while (*tmp) {
        len += strlen(*tmp) + 1;
        tmp++;
    }
101 102
    if ((actualargv = malloc(sizeof(*actualargv)*len)) == NULL)
        goto fail;
103
    actualargv[0] = '\0';
104 105 106 107 108 109 110
    tmp = qenv;
    while (*tmp) {
        if (actualargv[0])
            strcat(actualargv, " ");
        strcat(actualargv, *tmp);
        tmp++;
    }
111 112 113 114 115 116 117
    tmp = argv;
    while (*tmp) {
        if (actualargv[0])
            strcat(actualargv, " ");
        strcat(actualargv, *tmp);
        tmp++;
    }
118
    strcat(actualargv, "\n");
119

120 121
    if (STRNEQ(expectargv, actualargv)) {
        virtTestDifference(stderr, expectargv, actualargv);
122 123 124 125 126 127 128 129 130 131
        goto fail;
    }

    ret = 0;

 fail:
    free(actualargv);
    if (argv) {
        tmp = argv;
        while (*tmp) {
132
            free(*(char**)tmp);
133 134 135 136
            tmp++;
        }
        free(argv);
    }
137 138 139 140 141 142 143 144
    if (qenv) {
        tmp = qenv;
        while (*tmp) {
            free(*(char**)tmp);
            tmp++;
        }
        free(qenv);
    }
145
    virDomainDefFree(vmdef);
146
    virUnrefConnect(conn);
147 148 149 150
    return ret;
}


151 152
struct testInfo {
    const char *name;
153
    unsigned long long extraFlags;
154
    const char *migrateFrom;
155 156
};

157
static int testCompareXMLToArgvHelper(const void *data) {
158
    const struct testInfo *info = data;
159 160
    char xml[PATH_MAX];
    char args[PATH_MAX];
161
    snprintf(xml, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
162
             abs_srcdir, info->name);
163
    snprintf(args, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
164
             abs_srcdir, info->name);
165
    return testCompareXMLToArgvFiles(xml, args, info->extraFlags, info->migrateFrom);
166 167 168 169
}



170 171
static int
mymain(int argc, char **argv)
172 173
{
    int ret = 0;
174
    char cwd[PATH_MAX];
175 176 177 178 179

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
180
        return (EXIT_FAILURE);
181 182
    }

183 184 185
    abs_srcdir = getenv("abs_srcdir");
    if (!abs_srcdir)
        abs_srcdir = getcwd(cwd, sizeof(cwd));
186

187 188
    if ((driver.caps = testQemuCapsInit()) == NULL)
        return EXIT_FAILURE;
189 190 191 192 193
    if ((driver.stateDir = strdup("/nowhere")) == NULL)
        return EXIT_FAILURE;
    if ((driver.hugetlbfs_mount = strdup("/dev/hugepages")) == NULL)
        return EXIT_FAILURE;
    if ((driver.hugepage_path = strdup("/dev/hugepages/libvirt/qemu")) == NULL)
194
        return EXIT_FAILURE;
195

196
#define DO_TEST_FULL(name, extraFlags, migrateFrom)                     \
197
    do {                                                                \
198
        const struct testInfo info = { name, extraFlags, migrateFrom }; \
199 200 201 202 203
        if (virtTestRun("QEMU XML-2-ARGV " name,                        \
                        1, testCompareXMLToArgvHelper, &info) < 0)      \
            ret = -1;                                                   \
    } while (0)

204 205 206
#define DO_TEST(name, extraFlags)                       \
        DO_TEST_FULL(name, extraFlags, NULL)

207 208 209
    /* Unset or set all envvars here that are copied in qemudBuildCommandLine
     * using ADD_ENV_COPY, otherwise these tests may fail due to unexpected
     * values for these envvars */
210 211 212 213 214 215 216
    setenv("PATH", "/bin", 1);
    setenv("USER", "test", 1);
    setenv("LOGNAME", "test", 1);
    setenv("HOME", "/home/test", 1);
    unsetenv("TMPDIR");
    unsetenv("LD_PRELOAD");
    unsetenv("LD_LIBRARY_PATH");
217 218
    unsetenv("QEMU_AUDIO_DRV");
    unsetenv("SDL_AUDIODRIVER");
219

220
    DO_TEST("minimal", QEMUD_CMD_FLAG_NAME);
M
Mark McLoughlin 已提交
221
    DO_TEST("machine-aliases1", 0);
222
    DO_TEST("machine-aliases2", 0);
223 224 225
    DO_TEST("boot-cdrom", 0);
    DO_TEST("boot-network", 0);
    DO_TEST("boot-floppy", 0);
226
    DO_TEST("bootloader", QEMUD_CMD_FLAG_DOMID);
227 228
    DO_TEST("clock-utc", 0);
    DO_TEST("clock-localtime", 0);
229
    DO_TEST("hugepages", QEMUD_CMD_FLAG_MEM_PATH);
230
    DO_TEST("disk-cdrom", 0);
231
    DO_TEST("disk-cdrom-empty", QEMUD_CMD_FLAG_DRIVE);
232 233
    DO_TEST("disk-floppy", 0);
    DO_TEST("disk-many", 0);
234 235 236 237
    DO_TEST("disk-virtio", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT);
    DO_TEST("disk-xenvbd", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT);
238 239 240 241
    DO_TEST("disk-drive-boot-disk", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT);
    DO_TEST("disk-drive-boot-cdrom", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT);
242 243 244 245
    DO_TEST("floppy-drive-fat", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-fat", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
246
    DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE |
247 248
            QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-shared", QEMUD_CMD_FLAG_DRIVE |
249
            QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_SERIAL);
250 251 252 253 254 255
    DO_TEST("disk-drive-cache-v1-wt", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-cache-v1-wb", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-cache-v1-none", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_FORMAT);
256
    DO_TEST("disk-drive-cache-v2-wt", QEMUD_CMD_FLAG_DRIVE |
257
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
258
    DO_TEST("disk-drive-cache-v2-wb", QEMUD_CMD_FLAG_DRIVE |
259
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
260
    DO_TEST("disk-drive-cache-v2-none", QEMUD_CMD_FLAG_DRIVE |
261
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
262
    DO_TEST("disk-usb", 0);
263
    DO_TEST("disk-usb-device", QEMUD_CMD_FLAG_DRIVE | QEMUD_CMD_FLAG_DEVICE);
264
    DO_TEST("graphics-vnc", 0);
265 266 267

    driver.vncSASL = 1;
    driver.vncSASLdir = strdup("/root/.sasl2");
268
    DO_TEST("graphics-vnc-sasl", QEMUD_CMD_FLAG_VGA);
269 270 271 272 273 274 275 276 277
    driver.vncTLS = 1;
    driver.vncTLSx509verify = 1;
    driver.vncTLSx509certdir = strdup("/etc/pki/tls/qemu");
    DO_TEST("graphics-vnc-tls", 0);
    driver.vncSASL = driver.vncTLSx509verify = driver.vncTLS = 0;
    free(driver.vncSASLdir);
    free(driver.vncTLSx509certdir);
    driver.vncSASLdir = driver.vncTLSx509certdir = NULL;

278
    DO_TEST("graphics-sdl", 0);
279
    DO_TEST("graphics-sdl-fullscreen", 0);
280
    DO_TEST("nographics-vga", QEMUD_CMD_FLAG_VGA);
281 282
    DO_TEST("input-usbmouse", 0);
    DO_TEST("input-usbtablet", 0);
283
    DO_TEST("input-xen", QEMUD_CMD_FLAG_DOMID);
284 285
    DO_TEST("misc-acpi", 0);
    DO_TEST("misc-no-reboot", 0);
286
    DO_TEST("misc-uuid", QEMUD_CMD_FLAG_NAME |
287
            QEMUD_CMD_FLAG_UUID);
288 289
    DO_TEST("net-user", 0);
    DO_TEST("net-virtio", 0);
290
    DO_TEST("net-virtio-device", QEMUD_CMD_FLAG_DEVICE);
291
    DO_TEST("net-virtio-netdev", QEMUD_CMD_FLAG_DEVICE | QEMUD_CMD_FLAG_NETDEV);
292 293
    DO_TEST("net-eth", 0);
    DO_TEST("net-eth-ifname", 0);
294
    DO_TEST("net-eth-names", QEMUD_CMD_FLAG_NET_NAME);
295 296 297 298 299 300 301 302 303 304 305 306

    DO_TEST("serial-vc", 0);
    DO_TEST("serial-pty", 0);
    DO_TEST("serial-dev", 0);
    DO_TEST("serial-file", 0);
    DO_TEST("serial-unix", 0);
    DO_TEST("serial-tcp", 0);
    DO_TEST("serial-udp", 0);
    DO_TEST("serial-tcp-telnet", 0);
    DO_TEST("serial-many", 0);
    DO_TEST("parallel-tcp", 0);
    DO_TEST("console-compat", 0);
307

308 309 310 311 312 313 314 315 316 317 318 319 320
    DO_TEST("serial-vc-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-pty-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-dev-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-file-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-unix-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-tcp-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-udp-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-tcp-telnet-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("serial-many-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("parallel-tcp-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
    DO_TEST("console-compat-chardev", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);

    DO_TEST("channel-guestfwd", QEMUD_CMD_FLAG_CHARDEV|QEMUD_CMD_FLAG_DEVICE);
321
    DO_TEST("channel-virtio", QEMUD_CMD_FLAG_DEVICE);
322

D
Daniel P. Berrange 已提交
323 324
    DO_TEST("watchdog", 0);
    DO_TEST("watchdog-device", QEMUD_CMD_FLAG_DEVICE);
325
    DO_TEST("sound", 0);
326
    DO_TEST("sound-device", QEMUD_CMD_FLAG_DEVICE);
327

328
    DO_TEST("hostdev-usb-address", 0);
329
    DO_TEST("hostdev-usb-address-device", QEMUD_CMD_FLAG_DEVICE);
330
    DO_TEST("hostdev-pci-address", QEMUD_CMD_FLAG_PCIDEVICE);
331
    DO_TEST("hostdev-pci-address-device", QEMUD_CMD_FLAG_PCIDEVICE|QEMUD_CMD_FLAG_DEVICE);
332

333 334 335 336 337
    DO_TEST_FULL("restore-v1", QEMUD_CMD_FLAG_MIGRATE_KVM_STDIO, "stdio");
    DO_TEST_FULL("restore-v2", QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC, "stdio");
    DO_TEST_FULL("restore-v2", QEMUD_CMD_FLAG_MIGRATE_QEMU_EXEC, "exec:cat");
    DO_TEST_FULL("migrate", QEMUD_CMD_FLAG_MIGRATE_QEMU_TCP, "tcp:10.0.0.1:5000");

D
Daniel P. Berrange 已提交
338
    free(driver.stateDir);
339 340
    virCapabilitiesFree(driver.caps);

341
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
342 343
}

344 345
VIRT_TEST_MAIN(mymain)

346 347
#else

348
int main (void) { return (77); /* means 'test skipped' for automake */ }
349 350

#endif /* WITH_QEMU */