qemuxml2argvtest.c 8.2 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 15 16
#include "testutils.h"
#include "qemu_conf.h"

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
    if (qemudBuildCommandLine(NULL, &driver,
60 61
                              vmdef, &monitor_chr, flags,
                              &argv, &qenv,
62
                              NULL, NULL, migrateFrom) < 0)
63 64
        goto fail;

65
    len = 1; /* for trailing newline */
66 67 68 69 70 71 72
    tmp = qenv;
    while (*tmp) {
        len += strlen(*tmp) + 1;
        tmp++;
    }

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

95 96
    if (STRNEQ(expectargv, actualargv)) {
        virtTestDifference(stderr, expectargv, actualargv);
97 98 99 100 101 102 103 104 105 106
        goto fail;
    }

    ret = 0;

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


125 126
struct testInfo {
    const char *name;
127
    int extraFlags;
128
    const char *migrateFrom;
129 130
};

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



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

    progname = argv[0];

    if (argc > 1) {
        fprintf(stderr, "Usage: %s\n", progname);
154
        return (EXIT_FAILURE);
155 156
    }

157 158 159
    abs_srcdir = getenv("abs_srcdir");
    if (!abs_srcdir)
        abs_srcdir = getcwd(cwd, sizeof(cwd));
160

161 162
    if ((driver.caps = testQemuCapsInit()) == NULL)
        return EXIT_FAILURE;
163 164
    if((driver.stateDir = strdup("/nowhere")) == NULL)
        return EXIT_FAILURE;
165

166
#define DO_TEST_FULL(name, extraFlags, migrateFrom)                     \
167
    do {                                                                \
168
        const struct testInfo info = { name, extraFlags, migrateFrom }; \
169 170 171 172 173
        if (virtTestRun("QEMU XML-2-ARGV " name,                        \
                        1, testCompareXMLToArgvHelper, &info) < 0)      \
            ret = -1;                                                   \
    } while (0)

174 175 176
#define DO_TEST(name, extraFlags)                       \
        DO_TEST_FULL(name, extraFlags, NULL)

177 178 179 180 181 182 183 184
    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");

185
    DO_TEST("minimal", QEMUD_CMD_FLAG_NAME);
186 187 188
    DO_TEST("boot-cdrom", 0);
    DO_TEST("boot-network", 0);
    DO_TEST("boot-floppy", 0);
D
Daniel P. Berrange 已提交
189
    DO_TEST("bootloader", 0);
190 191 192
    DO_TEST("clock-utc", 0);
    DO_TEST("clock-localtime", 0);
    DO_TEST("disk-cdrom", 0);
193
    DO_TEST("disk-cdrom-empty", QEMUD_CMD_FLAG_DRIVE);
194 195
    DO_TEST("disk-floppy", 0);
    DO_TEST("disk-many", 0);
196 197 198 199
    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);
200 201 202 203
    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);
204
    DO_TEST("disk-drive-fmt-qcow", QEMUD_CMD_FLAG_DRIVE |
205 206 207 208 209 210 211 212 213
            QEMUD_CMD_FLAG_DRIVE_BOOT | QEMUD_CMD_FLAG_DRIVE_FORMAT);
    DO_TEST("disk-drive-shared", QEMUD_CMD_FLAG_DRIVE |
            QEMUD_CMD_FLAG_DRIVE_FORMAT);
    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);
214
    DO_TEST("disk-drive-cache-v2-wt", QEMUD_CMD_FLAG_DRIVE |
215
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
216
    DO_TEST("disk-drive-cache-v2-wb", QEMUD_CMD_FLAG_DRIVE |
217
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
218
    DO_TEST("disk-drive-cache-v2-none", QEMUD_CMD_FLAG_DRIVE |
219
            QEMUD_CMD_FLAG_DRIVE_CACHE_V2 | QEMUD_CMD_FLAG_DRIVE_FORMAT);
220
    DO_TEST("disk-usb", 0);
221
    DO_TEST("graphics-vnc", 0);
222 223 224

    driver.vncSASL = 1;
    driver.vncSASLdir = strdup("/root/.sasl2");
225
    DO_TEST("graphics-vnc-sasl", QEMUD_CMD_FLAG_VGA);
226 227 228 229 230 231 232 233 234
    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;

235
    DO_TEST("graphics-sdl", 0);
236
    DO_TEST("graphics-sdl-fullscreen", 0);
237 238
    DO_TEST("input-usbmouse", 0);
    DO_TEST("input-usbtablet", 0);
239
    DO_TEST("input-xen", 0);
240 241
    DO_TEST("misc-acpi", 0);
    DO_TEST("misc-no-reboot", 0);
242 243
    DO_TEST("misc-uuid", QEMUD_CMD_FLAG_NAME |
        QEMUD_CMD_FLAG_UUID | QEMUD_CMD_FLAG_DOMID);
244 245
    DO_TEST("net-user", 0);
    DO_TEST("net-virtio", 0);
246 247
    DO_TEST("net-eth", 0);
    DO_TEST("net-eth-ifname", 0);
248
    DO_TEST("net-eth-names", QEMUD_CMD_FLAG_NET_NAME);
249 250 251 252 253 254 255 256 257 258 259 260 261

    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);
262

263 264 265
    DO_TEST("hostdev-usb-product", 0);
    DO_TEST("hostdev-usb-address", 0);

266
    DO_TEST("hostdev-pci-address", QEMUD_CMD_FLAG_PCIDEVICE);
267

268 269 270 271 272
    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");

273 274
    virCapabilitiesFree(driver.caps);

275
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
276 277
}

278 279
VIRT_TEST_MAIN(mymain)

280 281
#else

282
int main (void) { return (77); /* means 'test skipped' for automake */ }
283 284

#endif /* WITH_QEMU */