xmconfigtest.c 6.7 KB
Newer Older
1 2 3
/*
 * xmconfigtest.c: Test backend for xm_internal config file handling
 *
4
 * Copyright (C) 2007, 2010-2011, 2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library.  If not, see
O
Osier Yang 已提交
18
 * <http://www.gnu.org/licenses/>.
19 20 21 22 23
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 *
 */

24
#include <config.h>
25

26 27
#include <stdio.h>
#include <string.h>
28
#include <unistd.h>
29

30
#include "internal.h"
31
#include "datatypes.h"
32 33
#include "xen/xen_driver.h"
#include "xen/xm_internal.h"
J
Jim Fehlig 已提交
34
#include "xenconfig/xen_xm.h"
35
#include "testutils.h"
36
#include "testutilsxen.h"
37
#include "viralloc.h"
38
#include "virstring.h"
39

40 41
#define VIR_FROM_THIS VIR_FROM_NONE

42
static virCapsPtr caps;
43
static virDomainXMLOptionPtr xmlopt;
44

45
static int
46
testCompareParseXML(const char *xmcfg, const char *xml)
47 48
{
    char *gotxmcfgData = NULL;
49 50
    virConfPtr conf = NULL;
    int ret = -1;
E
Eric Blake 已提交
51
    virConnectPtr conn = NULL;
52
    int wrote = 4096;
53
    struct _xenUnifiedPrivate priv;
54
    virDomainDefPtr def = NULL;
55

56 57 58
    if (VIR_ALLOC_N(gotxmcfgData, wrote) < 0)
        goto fail;

59
    conn = virGetConnect();
60
    if (!conn) goto fail;
61

62
    /* Many puppies died to bring you this code. */
63
    priv.caps = caps;
64
    conn->privateData = &priv;
65

C
Cole Robinson 已提交
66 67
    if (!(def = virDomainDefParseFile(xml, caps, xmlopt,
                                      VIR_DOMAIN_DEF_PARSE_INACTIVE)))
68 69
        goto fail;

70 71 72 73 74
    if (!virDomainDefCheckABIStability(def, def)) {
        fprintf(stderr, "ABI stability check failed on %s", xml);
        goto fail;
    }

75
    if (!(conf = xenFormatXM(conn, def)))
76 77
        goto fail;

78
    if (virConfWriteMem(gotxmcfgData, &wrote, conf) < 0)
79
        goto fail;
80
    gotxmcfgData[wrote] = '\0';
81

C
Cole Robinson 已提交
82
    if (virtTestCompareToFile(gotxmcfgData, xmcfg) < 0)
83 84 85 86 87
        goto fail;

    ret = 0;

 fail:
88
    VIR_FREE(gotxmcfgData);
89 90
    if (conf)
        virConfFree(conf);
91
    virDomainDefFree(def);
92
    virObjectUnref(conn);
93

94 95 96
    return ret;
}

97
static int
98
testCompareFormatXML(const char *xmcfg, const char *xml)
99 100
{
    char *xmcfgData = NULL;
101 102 103 104
    char *gotxml = NULL;
    virConfPtr conf = NULL;
    int ret = -1;
    virConnectPtr conn;
105
    struct _xenUnifiedPrivate priv;
106
    virDomainDefPtr def = NULL;
107

108
    conn = virGetConnect();
109
    if (!conn) goto fail;
110

111
    if (virtTestLoadFile(xmcfg, &xmcfgData) < 0)
112 113
        goto fail;

114
    /* Many puppies died to bring you this code. */
115
    priv.caps = caps;
116
    conn->privateData = &priv;
117

118
    if (!(conf = virConfReadMem(xmcfgData, strlen(xmcfgData), 0)))
119 120
        goto fail;

121
    if (!(def = xenParseXM(conf, caps, xmlopt)))
122 123
        goto fail;

124
    if (!(gotxml = virDomainDefFormat(def, VIR_DOMAIN_DEF_FORMAT_SECURE)))
125 126
        goto fail;

C
Cole Robinson 已提交
127
    if (virtTestCompareToFile(gotxml, xml) < 0)
128 129 130 131 132 133 134
        goto fail;

    ret = 0;

 fail:
    if (conf)
        virConfFree(conf);
135
    VIR_FREE(xmcfgData);
136 137
    VIR_FREE(gotxml);
    virDomainDefFree(def);
138
    virObjectUnref(conn);
139

140 141 142
    return ret;
}

143

144 145 146 147
struct testInfo {
    const char *name;
    int mode;
};
148

149 150 151 152
static int
testCompareHelper(const void *data)
{
    int result = -1;
153
    const struct testInfo *info = data;
154 155
    char *xml = NULL;
    char *cfg = NULL;
C
Chunyan Liu 已提交
156
    char *cfgout = NULL;
157 158 159 160 161 162 163

    if (virAsprintf(&xml, "%s/xmconfigdata/test-%s.xml",
                    abs_srcdir, info->name) < 0 ||
        virAsprintf(&cfg, "%s/xmconfigdata/test-%s.cfg",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

164
    if (info->mode == 0)
165
        result = testCompareParseXML(cfg, xml);
166
    else
167
        result = testCompareFormatXML(cfg, xml);
168

169
 cleanup:
170 171
    VIR_FREE(xml);
    VIR_FREE(cfg);
C
Chunyan Liu 已提交
172
    VIR_FREE(cfgout);
173 174

    return result;
175 176
}

177

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

183
    if (!(caps = testXenCapsInit()))
184
        return EXIT_FAILURE;
185

186
    if (!(xmlopt = xenDomainXMLConfInit()))
187 188
        return EXIT_FAILURE;

189
#define DO_TEST_PARSE(name)                                             \
190
    do {                                                                \
191
        struct testInfo info0 = { name, 0 };                            \
192
        if (virtTestRun("Xen XM-2-XML Parse  " name,                    \
193
                        testCompareHelper, &info0) < 0)                 \
194
            ret = -1;                                                   \
C
Chunyan Liu 已提交
195 196 197
    } while (0)


198
#define DO_TEST_FORMAT(name)                                            \
C
Chunyan Liu 已提交
199
    do {                                                                \
200
        struct testInfo info1 = { name, 1 };                            \
201
        if (virtTestRun("Xen XM-2-XML Format " name,                    \
202
                        testCompareHelper, &info1) < 0)                 \
203 204 205
            ret = -1;                                                   \
    } while (0)

C
Chunyan Liu 已提交
206

207
#define DO_TEST(name)                                                   \
C
Chunyan Liu 已提交
208
    do {                                                                \
209 210
        DO_TEST_PARSE(name);                                            \
        DO_TEST_FORMAT(name);                                           \
C
Chunyan Liu 已提交
211 212
    } while (0)

213 214 215 216 217
    DO_TEST("paravirt-new-pvfb");
    DO_TEST("paravirt-new-pvfb-vncdisplay");
    DO_TEST("paravirt-net-e1000");
    DO_TEST("paravirt-net-vifname");
    DO_TEST("paravirt-vcpu");
218
    DO_TEST("paravirt-maxvcpus");
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    DO_TEST("fullvirt-new-cdrom");
    DO_TEST("fullvirt-utc");
    DO_TEST("fullvirt-localtime");
    DO_TEST("fullvirt-usbtablet");
    DO_TEST("fullvirt-usbmouse");
    DO_TEST("fullvirt-serial-file");
    DO_TEST("fullvirt-serial-dev-2-ports");
    DO_TEST("fullvirt-serial-dev-2nd-port");
    DO_TEST("fullvirt-serial-null");
    DO_TEST("fullvirt-serial-pipe");
    DO_TEST("fullvirt-serial-pty");
    DO_TEST("fullvirt-serial-stdio");
    DO_TEST("fullvirt-serial-tcp");
    DO_TEST("fullvirt-serial-tcp-telnet");
    DO_TEST("fullvirt-serial-udp");
    DO_TEST("fullvirt-serial-unix");

    DO_TEST("fullvirt-force-hpet");
    DO_TEST("fullvirt-force-nohpet");

    DO_TEST("fullvirt-parallel-tcp");

    DO_TEST("fullvirt-sound");

    DO_TEST("fullvirt-net-netfront");

    DO_TEST_FORMAT("fullvirt-default-feature");

    DO_TEST("escape-paths");
    DO_TEST("no-source-cdrom");
    DO_TEST("pci-devs");
250

251
    virObjectUnref(caps);
252
    virObjectUnref(xmlopt);
253

254
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
255
}
256 257

VIRT_TEST_MAIN(mymain)