vmx2xmltest.c 8.3 KB
Newer Older
1 2
#include <config.h>

3 4
#include "testutils.h"

5
#ifdef WITH_VMX
6

7 8 9
# include <stdio.h>
# include <string.h>
# include <unistd.h>
10

11
# include "internal.h"
12
# include "viralloc.h"
13
# include "vmx/vmx.h"
14
# include "virstring.h"
15

16 17
# define VIR_FROM_THIS VIR_FROM_VMWARE

18
static virCapsPtr caps;
19
static virDomainXMLOptionPtr xmlopt;
20
static virVMXContext ctx;
21

22

23 24 25 26 27
static void
testCapsInit(void)
{
    virCapsGuestPtr guest = NULL;

28
    caps = virCapabilitiesNew(VIR_ARCH_I686, true, true);
29

30
    if (caps == NULL)
31 32 33 34 35 36
        return;

    virCapabilitiesAddHostMigrateTransport(caps, "esx");

    /* i686 guest */
    guest =
37
      virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
38 39
                              VIR_ARCH_I686,
                              NULL, NULL, 0, NULL);
40

41
    if (guest == NULL)
42 43
        goto failure;

44
    if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0,
45 46 47 48 49 50
                                      NULL) == NULL) {
        goto failure;
    }

    /* x86_64 guest */
    guest =
51
      virCapabilitiesAddGuest(caps, VIR_DOMAIN_OSTYPE_HVM,
52 53
                              VIR_ARCH_X86_64,
                              NULL, NULL, 0, NULL);
54

55
    if (guest == NULL)
56 57
        goto failure;

58
    if (virCapabilitiesAddGuestDomain(guest, VIR_DOMAIN_VIRT_VMWARE, NULL, NULL, 0,
59 60 61 62 63 64
                                      NULL) == NULL) {
        goto failure;
    }

    return;

65
 failure:
66
    virObjectUnref(caps);
67 68 69
    caps = NULL;
}

70
static int
71
testCompareFiles(const char *vmx, const char *xml)
72
{
73
    int ret = -1;
74
    char *vmxData = NULL;
75 76 77
    char *formatted = NULL;
    virDomainDefPtr def = NULL;

78 79
    if (virtTestLoadFile(vmx, &vmxData) < 0)
        goto cleanup;
80

81 82
    if (!(def = virVMXParseConfig(&ctx, xmlopt, vmxData)))
        goto cleanup;
83

84 85 86 87 88
    if (!virDomainDefCheckABIStability(def, def)) {
        fprintf(stderr, "ABI stability check failed on %s", vmx);
        goto cleanup;
    }

89
    if (!(formatted = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_SECURE)))
90
        goto cleanup;
91

C
Cole Robinson 已提交
92
    if (virtTestCompareToFile(formatted, xml) < 0)
93
        goto cleanup;
94

95
    ret = 0;
96

97
 cleanup:
98
    VIR_FREE(vmxData);
99 100 101
    VIR_FREE(formatted);
    virDomainDefFree(def);

102
    return ret;
103 104 105 106 107 108 109 110 111 112
}

struct testInfo {
    const char *input;
    const char *output;
};

static int
testCompareHelper(const void *data)
{
113
    int ret = -1;
114
    const struct testInfo *info = data;
115 116 117 118 119 120 121 122 123
    char *vmx = NULL;
    char *xml = NULL;

    if (virAsprintf(&vmx, "%s/vmx2xmldata/vmx2xml-%s.vmx", abs_srcdir,
                    info->input) < 0 ||
        virAsprintf(&xml, "%s/vmx2xmldata/vmx2xml-%s.xml", abs_srcdir,
                    info->output) < 0) {
        goto cleanup;
    }
124

125
    ret = testCompareFiles(vmx, xml);
126

127
 cleanup:
128 129 130
    VIR_FREE(vmx);
    VIR_FREE(xml);

131
    return ret;
132 133
}

134 135 136 137 138 139 140 141 142 143 144 145
static char *
testParseVMXFileName(const char *fileName, void *opaque ATTRIBUTE_UNUSED)
{
    char *copyOfFileName = NULL;
    char *tmp = NULL;
    char *saveptr = NULL;
    char *datastoreName = NULL;
    char *directoryAndFileName = NULL;
    char *src = NULL;

    if (STRPREFIX(fileName, "/vmfs/volumes/")) {
        /* Found absolute path referencing a file inside a datastore */
146
        if (VIR_STRDUP(copyOfFileName, fileName) < 0)
147 148 149 150 151 152 153 154 155
            goto cleanup;

        /* Expected format: '/vmfs/volumes/<datastore>/<path>' */
        if ((tmp = STRSKIP(copyOfFileName, "/vmfs/volumes/")) == NULL ||
            (datastoreName = strtok_r(tmp, "/", &saveptr)) == NULL ||
            (directoryAndFileName = strtok_r(NULL, "", &saveptr)) == NULL) {
            goto cleanup;
        }

156 157 158
        if (virAsprintf(&src, "[%s] %s", datastoreName,
                        directoryAndFileName) < 0)
            goto cleanup;
159 160
    } else if (STRPREFIX(fileName, "/")) {
        /* Found absolute path referencing a file outside a datastore */
161
        ignore_value(VIR_STRDUP(src, fileName));
162 163 164 165 166
    } else if (strchr(fileName, '/') != NULL) {
        /* Found relative path, this is not supported */
        src = NULL;
    } else {
        /* Found single file name referencing a file inside a datastore */
167 168
        if (virAsprintf(&src, "[datastore] directory/%s", fileName) < 0)
            goto cleanup;
169 170
    }

171
 cleanup:
172 173 174 175 176
    VIR_FREE(copyOfFileName);

    return src;
}

177
static int
E
Eric Blake 已提交
178
mymain(void)
179
{
180
    int ret = 0;
181

182
# define DO_TEST(_in, _out)                                                   \
183
        do {                                                                  \
184
            struct testInfo info = { _in, _out };                             \
185
            virResetLastError();                                              \
186
            if (virtTestRun("VMware VMX-2-XML "_in" -> "_out,                 \
187
                            testCompareHelper, &info) < 0) {                  \
188
                ret = -1;                                                     \
189 190 191
            }                                                                 \
        } while (0)

192 193
    testCapsInit();

194
    if (caps == NULL)
195 196
        return EXIT_FAILURE;

197 198 199
    if (!(xmlopt = virVMXDomainXMLConfInit()))
        return EXIT_FAILURE;

200 201 202 203 204
    ctx.opaque = NULL;
    ctx.parseFileName = testParseVMXFileName;
    ctx.formatFileName = NULL;
    ctx.autodetectSCSIControllerModel = NULL;

205 206
    DO_TEST("case-insensitive-1", "case-insensitive-1");
    DO_TEST("case-insensitive-2", "case-insensitive-2");
207

208 209
    DO_TEST("minimal", "minimal");
    DO_TEST("minimal-64bit", "minimal-64bit");
210

211
    DO_TEST("graphics-vnc", "graphics-vnc");
M
Matthias Bolte 已提交
212

213 214
    DO_TEST("scsi-driver", "scsi-driver");
    DO_TEST("scsi-writethrough", "scsi-writethrough");
215

216 217
    DO_TEST("harddisk-scsi-file", "harddisk-scsi-file");
    DO_TEST("harddisk-ide-file", "harddisk-ide-file");
218
    DO_TEST("harddisk-transient", "harddisk-transient");
219

220 221
    DO_TEST("cdrom-scsi-file", "cdrom-scsi-file");
    DO_TEST("cdrom-scsi-device", "cdrom-scsi-device");
222
    DO_TEST("cdrom-scsi-raw-device", "cdrom-scsi-raw-device");
223
    DO_TEST("cdrom-scsi-raw-auto-detect", "cdrom-scsi-raw-auto-detect");
224
    DO_TEST("cdrom-scsi-passthru", "cdrom-scsi-passthru");
225 226
    DO_TEST("cdrom-ide-file", "cdrom-ide-file");
    DO_TEST("cdrom-ide-device", "cdrom-ide-device");
227
    DO_TEST("cdrom-ide-raw-device", "cdrom-ide-raw-device");
228
    DO_TEST("cdrom-ide-raw-auto-detect", "cdrom-ide-raw-auto-detect");
229

230 231
    DO_TEST("floppy-file", "floppy-file");
    DO_TEST("floppy-device", "floppy-device");
232

233 234
    DO_TEST("sharedfolder", "sharedfolder");

235 236
    DO_TEST("ethernet-e1000", "ethernet-e1000");
    DO_TEST("ethernet-vmxnet2", "ethernet-vmxnet2");
237

238 239
    DO_TEST("ethernet-custom", "ethernet-custom");
    DO_TEST("ethernet-bridged", "ethernet-bridged");
240
    DO_TEST("ethernet-nat", "ethernet-nat");
241

242 243 244 245
    DO_TEST("ethernet-generated", "ethernet-generated");
    DO_TEST("ethernet-static", "ethernet-static");
    DO_TEST("ethernet-vpx", "ethernet-vpx");
    DO_TEST("ethernet-other", "ethernet-other");
246

247 248 249 250 251 252 253 254
    DO_TEST("serial-file", "serial-file");
    DO_TEST("serial-device", "serial-device");
    DO_TEST("serial-pipe-client-app", "serial-pipe");
    DO_TEST("serial-pipe-server-vm", "serial-pipe");
    DO_TEST("serial-pipe-client-app", "serial-pipe");
    DO_TEST("serial-pipe-server-vm", "serial-pipe");
    DO_TEST("serial-network-server", "serial-network-server");
    DO_TEST("serial-network-client", "serial-network-client");
255

256 257
    DO_TEST("parallel-file", "parallel-file");
    DO_TEST("parallel-device", "parallel-device");
258

259 260 261 262 263
    DO_TEST("esx-in-the-wild-1", "esx-in-the-wild-1");
    DO_TEST("esx-in-the-wild-2", "esx-in-the-wild-2");
    DO_TEST("esx-in-the-wild-3", "esx-in-the-wild-3");
    DO_TEST("esx-in-the-wild-4", "esx-in-the-wild-4");
    DO_TEST("esx-in-the-wild-5", "esx-in-the-wild-5");
264
    DO_TEST("esx-in-the-wild-6", "esx-in-the-wild-6");
265
    DO_TEST("esx-in-the-wild-7", "esx-in-the-wild-7");
266

267 268 269 270
    DO_TEST("gsx-in-the-wild-1", "gsx-in-the-wild-1");
    DO_TEST("gsx-in-the-wild-2", "gsx-in-the-wild-2");
    DO_TEST("gsx-in-the-wild-3", "gsx-in-the-wild-3");
    DO_TEST("gsx-in-the-wild-4", "gsx-in-the-wild-4");
271

272 273 274
    DO_TEST("ws-in-the-wild-1", "ws-in-the-wild-1");
    DO_TEST("ws-in-the-wild-2", "ws-in-the-wild-2");

275 276
    DO_TEST("fusion-in-the-wild-1", "fusion-in-the-wild-1");

277
    DO_TEST("annotation", "annotation");
278

279
    DO_TEST("smbios", "smbios");
M
Matthias Bolte 已提交
280

281 282
    DO_TEST("svga", "svga");

283
    virObjectUnref(caps);
284
    virObjectUnref(xmlopt);
285

286
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
287 288 289 290 291 292
}

VIRT_TEST_MAIN(mymain)

#else

293
int main(void)
294
{
295
    return EXIT_AM_SKIP;
296 297
}

298
#endif /* WITH_VMX */