qemuxmlnstest.c 8.3 KB
Newer Older
P
Philipp Hahn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

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

#ifdef WITH_QEMU

# include "internal.h"
# include "testutils.h"
# include "qemu/qemu_capabilities.h"
# include "qemu/qemu_command.h"
# include "qemu/qemu_domain.h"
# include "datatypes.h"
# include "cpu/cpu_map.h"

# include "testutilsqemu.h"

static const char *abs_top_srcdir;
24
static virQEMUDriver driver;
P
Philipp Hahn 已提交
25 26 27

static int testCompareXMLToArgvFiles(const char *xml,
                                     const char *cmdline,
28
                                     virQEMUCapsPtr extraFlags,
P
Philipp Hahn 已提交
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
                                     const char *migrateFrom,
                                     int migrateFd,
                                     bool json,
                                     bool expectError)
{
    char *expectargv = NULL;
    int len;
    char *actualargv = NULL;
    int ret = -1;
    virDomainDefPtr vmdef = NULL;
    virDomainChrSourceDef monitor_chr;
    virConnectPtr conn;
    char *log = NULL;
    char *emulator = NULL;
    virCommandPtr cmd = NULL;

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

    len = virtTestLoadFile(cmdline, &expectargv);
    if (len < 0)
        goto fail;
    if (len && expectargv[len - 1] == '\n')
        expectargv[len - 1] = '\0';

54
    if (!(vmdef = virDomainDefParseFile(driver.caps, driver.xmlopt, xml,
P
Philipp Hahn 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                                        QEMU_EXPECTED_VIRT_TYPES,
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto fail;

    /*
     * For test purposes, we may want to fake emulator's output by providing
     * our own script instead of a real emulator. For this to work we need to
     * specify a relative path in <emulator/> element, which, however, is not
     * allowed by RelaxNG schema for domain XML. To work around it we add an
     * extra '/' at the beginning of relative emulator path so that it looks
     * like, e.g., "/./qemu.sh" or "/../emulator/qemu.sh" instead of
     * "./qemu.sh" or "../emulator/qemu.sh" respectively. The following code
     * detects such paths, strips the extra '/' and makes the path absolute.
     */
    if (vmdef->emulator && STRPREFIX(vmdef->emulator, "/.")) {
        if (!(emulator = strdup(vmdef->emulator + 1)))
            goto fail;
72
        VIR_FREE(vmdef->emulator);
P
Philipp Hahn 已提交
73 74 75 76 77 78
        vmdef->emulator = NULL;
        if (virAsprintf(&vmdef->emulator, "%s/qemuxml2argvdata/%s",
                        abs_srcdir, emulator) < 0)
            goto fail;
    }

79
    if (virQEMUCapsGet(extraFlags, QEMU_CAPS_DOMID))
P
Philipp Hahn 已提交
80 81 82 83 84 85 86 87 88
        vmdef->id = 6;
    else
        vmdef->id = -1;

    memset(&monitor_chr, 0, sizeof(monitor_chr));
    monitor_chr.type = VIR_DOMAIN_CHR_TYPE_UNIX;
    monitor_chr.data.nix.path = (char *)"/tmp/test-monitor";
    monitor_chr.data.nix.listen = true;

89 90 91 92 93
    virQEMUCapsSetList(extraFlags,
                       QEMU_CAPS_VNC_COLON,
                       QEMU_CAPS_NO_REBOOT,
                       QEMU_CAPS_NO_ACPI,
                       QEMU_CAPS_LAST);
P
Philipp Hahn 已提交
94

95
    if (virQEMUCapsGet(extraFlags, QEMU_CAPS_DEVICE))
96
        qemuDomainAssignAddresses(vmdef, extraFlags, NULL);
P
Philipp Hahn 已提交
97

98 99
    log = virtTestLogContentAndReset();
    VIR_FREE(log);
P
Philipp Hahn 已提交
100 101
    virResetLastError();

102 103
    if (vmdef->os.arch == VIR_ARCH_X86_64 ||
        vmdef->os.arch == VIR_ARCH_I686) {
104
        virQEMUCapsSet(extraFlags, QEMU_CAPS_PCI_MULTIBUS);
P
Philipp Hahn 已提交
105 106
    }

107 108 109
    if (qemuAssignDeviceAliases(vmdef, extraFlags) < 0)
        goto fail;

P
Philipp Hahn 已提交
110 111 112
    if (!(cmd = qemuBuildCommandLine(conn, &driver,
                                     vmdef, &monitor_chr, json, extraFlags,
                                     migrateFrom, migrateFd, NULL,
113
                                     VIR_NETDEV_VPORT_PROFILE_OP_NO_OP)))
P
Philipp Hahn 已提交
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
        goto fail;

    if (!!virGetLastError() != expectError) {
        if (virTestGetDebug() && (log = virtTestLogContentAndReset()))
            fprintf(stderr, "\n%s", log);
        goto fail;
    }

    if (expectError) {
        /* need to suppress the errors */
        virResetLastError();
    }

    if (!(actualargv = virCommandToString(cmd)))
        goto fail;

    if (emulator) {
        /* Skip the abs_srcdir portion of replacement emulator.  */
        char *start_skip = strstr(actualargv, abs_srcdir);
        char *end_skip = strstr(actualargv, emulator);
        if (!start_skip || !end_skip)
            goto fail;
        memmove(start_skip, end_skip, strlen(end_skip) + 1);
    }

    if (STRNEQ(expectargv, actualargv)) {
        virtTestDifference(stderr, expectargv, actualargv);
        goto fail;
    }

    ret = 0;

 fail:
147 148 149 150
    VIR_FREE(log);
    VIR_FREE(emulator);
    VIR_FREE(expectargv);
    VIR_FREE(actualargv);
P
Philipp Hahn 已提交
151 152
    virCommandFree(cmd);
    virDomainDefFree(vmdef);
153
    virObjectUnref(conn);
P
Philipp Hahn 已提交
154 155 156 157 158 159
    return ret;
}


struct testInfo {
    const char *name;
160
    virQEMUCapsPtr extraFlags;
P
Philipp Hahn 已提交
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
    const char *migrateFrom;
    int migrateFd;
    bool json;
    bool expectError;
};

static int
testCompareXMLToArgvHelper(const void *data)
{
    int result = -1;
    const struct testInfo *info = data;
    char *xml = NULL;
    char *args = NULL;

    if (virAsprintf(&xml, "%s/qemuxmlnsdata/qemuxmlns-%s.xml",
                    abs_srcdir, info->name) < 0 ||
        virAsprintf(&args, "%s/qemuxmlnsdata/qemuxmlns-%s.args",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

    result = testCompareXMLToArgvFiles(xml, args, info->extraFlags,
                                       info->migrateFrom, info->migrateFd,
                                       info->json, info->expectError);

cleanup:
186 187
    VIR_FREE(xml);
    VIR_FREE(args);
P
Philipp Hahn 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    return result;
}



static int
mymain(void)
{
    int ret = 0;
    char *map = NULL;
    bool json = false;

    abs_top_srcdir = getenv("abs_top_srcdir");
    if (!abs_top_srcdir)
        abs_top_srcdir = "..";

204
    driver.config = virQEMUDriverConfigNew(false);
P
Philipp Hahn 已提交
205 206
    if ((driver.caps = testQemuCapsInit()) == NULL)
        return EXIT_FAILURE;
207
    if (!(driver.xmlopt = virQEMUDriverCreateXMLConf()))
208
        return EXIT_FAILURE;
P
Philipp Hahn 已提交
209 210
    if (virAsprintf(&map, "%s/src/cpu/cpu_map.xml", abs_top_srcdir) < 0 ||
        cpuMapOverride(map) < 0) {
211
        VIR_FREE(map);
P
Philipp Hahn 已提交
212 213 214 215 216 217 218 219
        return EXIT_FAILURE;
    }

# define DO_TEST_FULL(name, migrateFrom, migrateFd, expectError, ...)   \
    do {                                                                \
        struct testInfo info = {                                        \
            name, NULL, migrateFrom, migrateFd, json, expectError       \
        };                                                              \
220
        if (!(info.extraFlags = virQEMUCapsNew()))                      \
P
Philipp Hahn 已提交
221
            return EXIT_FAILURE;                                        \
222
        virQEMUCapsSetList(info.extraFlags, __VA_ARGS__, QEMU_CAPS_LAST);\
P
Philipp Hahn 已提交
223 224 225
        if (virtTestRun("QEMU XML-2-ARGV " name,                        \
                        1, testCompareXMLToArgvHelper, &info) < 0)      \
            ret = -1;                                                   \
226
        virObjectUnref(info.extraFlags);                                \
P
Philipp Hahn 已提交
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
    } while (0)

# define DO_TEST(name, expectError, ...)                                \
    DO_TEST_FULL(name, NULL, -1, expectError, __VA_ARGS__)

# define NONE QEMU_CAPS_LAST

    /* 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 */
    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");
    unsetenv("QEMU_AUDIO_DRV");
    unsetenv("SDL_AUDIODRIVER");

    DO_TEST("qemu-ns-domain", false, NONE);
    DO_TEST("qemu-ns-domain-ns0", false, NONE);
    DO_TEST("qemu-ns-domain-commandline", false, NONE);
    DO_TEST("qemu-ns-domain-commandline-ns0", false, NONE);
    DO_TEST("qemu-ns-commandline", false, NONE);
    DO_TEST("qemu-ns-commandline-ns0", false, NONE);
    DO_TEST("qemu-ns-commandline-ns1", false, NONE);

255
    virObjectUnref(driver.config);
256
    virObjectUnref(driver.caps);
257
    virObjectUnref(driver.xmlopt);
258
    VIR_FREE(map);
P
Philipp Hahn 已提交
259

260
    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
P
Philipp Hahn 已提交
261 262 263 264 265
}

VIRT_TEST_MAIN(mymain)

#else
E
Eric Blake 已提交
266
# include "testutils.h"
P
Philipp Hahn 已提交
267 268 269 270 271 272 273

int main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_QEMU */