qemuxml2argvtest.c 8.7 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

17 18
#include "testutilsqemu.h"

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

#define MAX_FILE 4096

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

39
    if (virtTestLoadFile(cmd, &expectargv, MAX_FILE) < 0)
40 41
        goto fail;

42 43
    if (!(vmdef = virDomainDefParseFile(NULL, driver.caps, xml,
                                        VIR_DOMAIN_XML_INACTIVE)))
44 45
        goto fail;

46
    if (extraFlags & QEMUD_CMD_FLAG_DOMID)
47
        vmdef->id = 6;
48
    else
49
        vmdef->id = -1;
50

51 52 53
    monitor_chr.type = VIR_DOMAIN_CHR_TYPE_UNIX;
    monitor_chr.data.nix.path = (char *)"/tmp/test-monitor";
    monitor_chr.data.nix.listen = 1;
54

55 56 57
    flags = QEMUD_CMD_FLAG_VNC_COLON |
        QEMUD_CMD_FLAG_NO_REBOOT |
        extraFlags;
58

59 60 61
    if (qemudCanonicalizeMachine(&driver, vmdef) < 0)
        goto fail;

62
    if (qemudBuildCommandLine(NULL, &driver,
63 64
                              vmdef, &monitor_chr, flags,
                              &argv, &qenv,
65
                              NULL, NULL, migrateFrom) < 0)
66 67
        goto fail;

68
    len = 1; /* for trailing newline */
69 70 71 72 73 74 75
    tmp = qenv;
    while (*tmp) {
        len += strlen(*tmp) + 1;
        tmp++;
    }

    tmp = argv;
76 77 78 79
    while (*tmp) {
        len += strlen(*tmp) + 1;
        tmp++;
    }
80
    actualargv = malloc(sizeof(*actualargv)*len);
81
    actualargv[0] = '\0';
82 83 84 85 86 87 88
    tmp = qenv;
    while (*tmp) {
        if (actualargv[0])
            strcat(actualargv, " ");
        strcat(actualargv, *tmp);
        tmp++;
    }
89 90 91 92 93 94 95
    tmp = argv;
    while (*tmp) {
        if (actualargv[0])
            strcat(actualargv, " ");
        strcat(actualargv, *tmp);
        tmp++;
    }
96
    strcat(actualargv, "\n");
97

98 99
    if (STRNEQ(expectargv, actualargv)) {
        virtTestDifference(stderr, expectargv, actualargv);
100 101 102 103 104 105 106 107 108 109
        goto fail;
    }

    ret = 0;

 fail:
    free(actualargv);
    if (argv) {
        tmp = argv;
        while (*tmp) {
110
            free(*(char**)tmp);
111 112 113 114
            tmp++;
        }
        free(argv);
    }
115 116 117 118 119 120 121 122
    if (qenv) {
        tmp = qenv;
        while (*tmp) {
            free(*(char**)tmp);
            tmp++;
        }
        free(qenv);
    }
123
    virDomainDefFree(vmdef);
124 125 126 127
    return ret;
}


128 129
struct testInfo {
    const char *name;
130
    int extraFlags;
131
    const char *migrateFrom;
132 133
};

134
static int testCompareXMLToArgvHelper(const void *data) {
135
    const struct testInfo *info = data;
136 137
    char xml[PATH_MAX];
    char args[PATH_MAX];
138
    snprintf(xml, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.xml",
139
             abs_srcdir, info->name);
140
    snprintf(args, PATH_MAX, "%s/qemuxml2argvdata/qemuxml2argv-%s.args",
141
             abs_srcdir, info->name);
142
    return testCompareXMLToArgvFiles(xml, args, info->extraFlags, info->migrateFrom);
143 144 145 146
}



147 148
static int
mymain(int argc, char **argv)
149 150
{
    int ret = 0;
151
    char cwd[PATH_MAX];
152 153 154 155 156

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
157
        return (EXIT_FAILURE);
158 159
    }

160 161 162
    abs_srcdir = getenv("abs_srcdir");
    if (!abs_srcdir)
        abs_srcdir = getcwd(cwd, sizeof(cwd));
163

164 165
    if ((driver.caps = testQemuCapsInit()) == NULL)
        return EXIT_FAILURE;
166 167 168 169 170
    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)
171
        return EXIT_FAILURE;
172

173
#define DO_TEST_FULL(name, extraFlags, migrateFrom)                     \
174
    do {                                                                \
175
        const struct testInfo info = { name, extraFlags, migrateFrom }; \
176 177 178 179 180
        if (virtTestRun("QEMU XML-2-ARGV " name,                        \
                        1, testCompareXMLToArgvHelper, &info) < 0)      \
            ret = -1;                                                   \
    } while (0)

181 182 183
#define DO_TEST(name, extraFlags)                       \
        DO_TEST_FULL(name, extraFlags, NULL)

184 185 186 187 188 189 190 191
    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");

192
    DO_TEST("minimal", QEMUD_CMD_FLAG_NAME);
M
Mark McLoughlin 已提交
193
    DO_TEST("machine-aliases1", 0);
194
    DO_TEST("machine-aliases2", 0);
195 196 197
    DO_TEST("boot-cdrom", 0);
    DO_TEST("boot-network", 0);
    DO_TEST("boot-floppy", 0);
198
    DO_TEST("bootloader", QEMUD_CMD_FLAG_DOMID);
199 200
    DO_TEST("clock-utc", 0);
    DO_TEST("clock-localtime", 0);
201
    DO_TEST("hugepages", QEMUD_CMD_FLAG_MEM_PATH);
202
    DO_TEST("disk-cdrom", 0);
203
    DO_TEST("disk-cdrom-empty", QEMUD_CMD_FLAG_DRIVE);
204 205
    DO_TEST("disk-floppy", 0);
    DO_TEST("disk-many", 0);
206 207 208 209
    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);
210 211 212 213
    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);
214
    DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE |
215 216
            QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-shared", QEMUD_CMD_FLAG_DRIVE |
217
            QEMUD_CMD_FLAG_DRIVE_FORMAT | QEMUD_CMD_FLAG_DRIVE_SERIAL);
218 219 220 221 222 223
    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);
224
    DO_TEST("disk-drive-cache-v2-wt", QEMUD_CMD_FLAG_DRIVE |
225
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
226
    DO_TEST("disk-drive-cache-v2-wb", QEMUD_CMD_FLAG_DRIVE |
227
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
228
    DO_TEST("disk-drive-cache-v2-none", QEMUD_CMD_FLAG_DRIVE |
229
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
230
    DO_TEST("disk-usb", 0);
231
    DO_TEST("graphics-vnc", 0);
232 233 234

    driver.vncSASL = 1;
    driver.vncSASLdir = strdup("/root/.sasl2");
235
    DO_TEST("graphics-vnc-sasl", QEMUD_CMD_FLAG_VGA);
236 237 238 239 240 241 242 243 244
    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;

245
    DO_TEST("graphics-sdl", 0);
246
    DO_TEST("graphics-sdl-fullscreen", 0);
247 248
    DO_TEST("input-usbmouse", 0);
    DO_TEST("input-usbtablet", 0);
249
    DO_TEST("input-xen", QEMUD_CMD_FLAG_DOMID);
250 251
    DO_TEST("misc-acpi", 0);
    DO_TEST("misc-no-reboot", 0);
252
    DO_TEST("misc-uuid", QEMUD_CMD_FLAG_NAME |
253
            QEMUD_CMD_FLAG_UUID);
254 255
    DO_TEST("net-user", 0);
    DO_TEST("net-virtio", 0);
256 257
    DO_TEST("net-eth", 0);
    DO_TEST("net-eth-ifname", 0);
258
    DO_TEST("net-eth-names", QEMUD_CMD_FLAG_NET_NAME);
259 260 261 262 263 264 265 266 267 268 269 270 271

    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);
    DO_TEST("sound", 0);
272

273 274 275
    DO_TEST("hostdev-usb-product", 0);
    DO_TEST("hostdev-usb-address", 0);

276
    DO_TEST("hostdev-pci-address", QEMUD_CMD_FLAG_PCIDEVICE);
277

278 279 280 281 282
    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 已提交
283
    free(driver.stateDir);
284 285
    virCapabilitiesFree(driver.caps);

286
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
287 288
}

289 290
VIRT_TEST_MAIN(mymain)

291 292
#else

293
int main (void) { return (77); /* means 'test skipped' for automake */ }
294 295

#endif /* WITH_QEMU */