bhyvexml2argvtest.c 7.3 KB
Newer Older
R
Roman Bogorodskiy 已提交
1 2 3 4 5 6 7 8
#include <config.h>

#include "testutils.h"

#ifdef WITH_BHYVE

# include "datatypes.h"

9
# include "bhyve/bhyve_capabilities.h"
10
# include "bhyve/bhyve_domain.h"
R
Roman Bogorodskiy 已提交
11 12 13 14 15 16 17
# include "bhyve/bhyve_utils.h"
# include "bhyve/bhyve_command.h"

# define VIR_FROM_THIS VIR_FROM_BHYVE

static bhyveConn driver;

18 19 20 21 22
typedef enum {
    FLAG_EXPECT_FAILURE     = 1 << 0,
    FLAG_EXPECT_PARSE_ERROR = 1 << 1,
} virBhyveXMLToArgvTestFlags;

R
Roman Bogorodskiy 已提交
23
static int testCompareXMLToArgvFiles(const char *xml,
24 25
                                     const char *cmdline,
                                     const char *ldcmdline,
26 27
                                     const char *dmcmdline,
                                     unsigned int flags)
R
Roman Bogorodskiy 已提交
28
{
29
    char *actualargv = NULL, *actualld = NULL, *actualdm = NULL;
R
Roman Bogorodskiy 已提交
30
    virDomainDefPtr vmdef = NULL;
31
    virCommandPtr cmd = NULL, ldcmd = NULL;
R
Roman Bogorodskiy 已提交
32
    virConnectPtr conn;
R
Roman Bogorodskiy 已提交
33 34
    int ret = -1;

R
Roman Bogorodskiy 已提交
35 36
    if (!(conn = virGetConnect()))
        goto out;
R
Roman Bogorodskiy 已提交
37 38

    if (!(vmdef = virDomainDefParseFile(xml, driver.caps, driver.xmlopt,
39
                                        NULL, VIR_DOMAIN_DEF_PARSE_INACTIVE))) {
40
        if (flags & FLAG_EXPECT_PARSE_ERROR) {
41
            ret = 0;
42 43 44 45 46 47 48
        } else if (flags & FLAG_EXPECT_FAILURE) {
            ret = 0;
            VIR_TEST_DEBUG("Got expected error: %s\n",
                    virGetLastErrorMessage());
            virResetLastError();
        }

R
Roman Bogorodskiy 已提交
49
        goto out;
50
    }
R
Roman Bogorodskiy 已提交
51

52
    conn->privateData = &driver;
R
Roman Bogorodskiy 已提交
53

54
    cmd = virBhyveProcessBuildBhyveCmd(conn, vmdef, false);
55 56 57 58 59
    if (vmdef->os.loader)
        ldcmd = virCommandNew("dummy");
    else
        ldcmd = virBhyveProcessBuildLoadCmd(conn, vmdef, "<device.map>",
                                            &actualdm);
60 61 62 63 64 65 66 67

    if ((cmd == NULL) || (ldcmd == NULL)) {
        if (flags & FLAG_EXPECT_FAILURE) {
            ret = 0;
            VIR_TEST_DEBUG("Got expected error: %s\n",
                    virGetLastErrorMessage());
            virResetLastError();
        }
R
Roman Bogorodskiy 已提交
68
        goto out;
69
    }
R
Roman Bogorodskiy 已提交
70

71
    if (!(actualargv = virCommandToString(cmd, false)))
R
Roman Bogorodskiy 已提交
72 73
        goto out;

74 75 76
    if (actualdm != NULL)
        virTrimSpaces(actualdm, NULL);

77
    if (!(actualld = virCommandToString(ldcmd, false)))
78 79
        goto out;

80
    if (virTestCompareToFile(actualargv, cmdline) < 0)
R
Roman Bogorodskiy 已提交
81 82
        goto out;

83
    if (virTestCompareToFile(actualld, ldcmdline) < 0)
84 85
        goto out;

C
Cole Robinson 已提交
86
    if (virFileExists(dmcmdline) || actualdm) {
87
        if (virTestCompareToFile(actualdm, dmcmdline) < 0)
C
Cole Robinson 已提交
88
            goto out;
89 90
    }

R
Roman Bogorodskiy 已提交
91 92 93
    ret = 0;

 out:
94 95
    if (vmdef &&
        vmdef->ngraphics == 1 &&
96 97
        vmdef->graphics[0]->type == VIR_DOMAIN_GRAPHICS_TYPE_VNC)
        virPortAllocatorRelease(vmdef->graphics[0]->data.vnc.port);
98

R
Roman Bogorodskiy 已提交
99
    VIR_FREE(actualargv);
100 101
    VIR_FREE(actualld);
    VIR_FREE(actualdm);
R
Roman Bogorodskiy 已提交
102
    virCommandFree(cmd);
103
    virCommandFree(ldcmd);
R
Roman Bogorodskiy 已提交
104
    virDomainDefFree(vmdef);
105
    virObjectUnref(conn);
R
Roman Bogorodskiy 已提交
106 107 108
    return ret;
}

109 110 111 112 113
struct testInfo {
    const char *name;
    unsigned int flags;
};

R
Roman Bogorodskiy 已提交
114 115 116 117
static int
testCompareXMLToArgvHelper(const void *data)
{
    int ret = -1;
118
    const struct testInfo *info = data;
R
Roman Bogorodskiy 已提交
119
    char *xml = NULL;
120
    char *args = NULL, *ldargs = NULL, *dmargs = NULL;
R
Roman Bogorodskiy 已提交
121 122

    if (virAsprintf(&xml, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.xml",
123
                    abs_srcdir, info->name) < 0 ||
R
Roman Bogorodskiy 已提交
124
        virAsprintf(&args, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.args",
125
                    abs_srcdir, info->name) < 0 ||
126
        virAsprintf(&ldargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.ldargs",
127
                    abs_srcdir, info->name) < 0 ||
128
        virAsprintf(&dmargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.devmap",
129
                    abs_srcdir, info->name) < 0)
R
Roman Bogorodskiy 已提交
130 131
        goto cleanup;

132
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs, info->flags);
R
Roman Bogorodskiy 已提交
133 134 135 136

 cleanup:
    VIR_FREE(xml);
    VIR_FREE(args);
137 138
    VIR_FREE(ldargs);
    VIR_FREE(dmargs);
R
Roman Bogorodskiy 已提交
139 140 141 142 143 144 145 146
    return ret;
}

static int
mymain(void)
{
    int ret = 0;

147
    if ((driver.caps = virBhyveCapsBuild()) == NULL)
R
Roman Bogorodskiy 已提交
148 149
        return EXIT_FAILURE;

150
    if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL)
R
Roman Bogorodskiy 已提交
151 152
        return EXIT_FAILURE;

153
    if (!(driver.remotePorts = virPortAllocatorRangeNew("display", 5900, 65535)))
154 155 156
        return EXIT_FAILURE;


157 158 159 160 161 162
# define DO_TEST_FULL(name, flags) \
    do { \
        static struct testInfo info = { \
            name, (flags) \
        }; \
        if (virTestRun("BHYVE XML-2-ARGV " name, \
163
                       testCompareXMLToArgvHelper, &info) < 0) \
164
            ret = -1; \
R
Roman Bogorodskiy 已提交
165 166
    } while (0)

167
# define DO_TEST(name) \
168 169
    DO_TEST_FULL(name, 0)

170
# define DO_TEST_FAILURE(name) \
171 172
    DO_TEST_FULL(name, FLAG_EXPECT_FAILURE)

173
# define DO_TEST_PARSE_ERROR(name) \
174 175
    DO_TEST_FULL(name, FLAG_EXPECT_PARSE_ERROR)

176
    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
177
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC | BHYVE_CAP_AHCI32SLOT | \
R
Roman Bogorodskiy 已提交
178
                       BHYVE_CAP_NET_E1000 | BHYVE_CAP_LPC_BOOTROM | \
179 180
                       BHYVE_CAP_FBUF | BHYVE_CAP_XHCI | \
                       BHYVE_CAP_CPUTOPOLOGY;
R
Roman Bogorodskiy 已提交
181 182

    DO_TEST("base");
183
    DO_TEST("wired");
R
Roman Bogorodskiy 已提交
184
    DO_TEST("acpiapic");
R
Roman Bogorodskiy 已提交
185
    DO_TEST("disk-cdrom");
R
Roman Bogorodskiy 已提交
186 187
    DO_TEST("disk-virtio");
    DO_TEST("macaddr");
188 189
    DO_TEST("serial");
    DO_TEST("console");
190
    DO_TEST("grub-defaults");
191 192
    DO_TEST("grub-bootorder");
    DO_TEST("grub-bootorder2");
193 194 195 196
    DO_TEST("bhyveload-bootorder");
    DO_TEST("bhyveload-bootorder1");
    DO_TEST_FAILURE("bhyveload-bootorder2");
    DO_TEST("bhyveload-bootorder3");
197
    DO_TEST("bhyveload-explicitargs");
198 199
    DO_TEST_FAILURE("bhyveload-bootorder4");
    DO_TEST_PARSE_ERROR("bhyveload-bootorder5");
200 201
    DO_TEST("custom-loader");
    DO_TEST("disk-cdrom-grub");
202
    DO_TEST("serial-grub");
R
Roman Bogorodskiy 已提交
203
    DO_TEST("localtime");
R
Roman Bogorodskiy 已提交
204
    DO_TEST("net-e1000");
205
    DO_TEST("uefi");
R
Roman Bogorodskiy 已提交
206
    DO_TEST("vnc");
207 208 209
    DO_TEST("vnc-vgaconf-on");
    DO_TEST("vnc-vgaconf-off");
    DO_TEST("vnc-vgaconf-io");
210
    DO_TEST("vnc-autoport");
211 212
    DO_TEST("cputopology");
    DO_TEST_FAILURE("cputopology-nvcpu-mismatch");
213
    DO_TEST("commandline");
214
    DO_TEST("msrs");
215

216 217 218 219
    /* Address allocation tests */
    DO_TEST("addr-single-sata-disk");
    DO_TEST("addr-multiple-sata-disks");
    DO_TEST("addr-more-than-32-sata-disks");
220 221
    DO_TEST("addr-single-virtio-disk");
    DO_TEST("addr-multiple-virtio-disks");
222 223 224 225 226 227 228

    /* The same without 32 devs per controller support */
    driver.bhyvecaps ^= BHYVE_CAP_AHCI32SLOT;
    DO_TEST("addr-no32devs-single-sata-disk");
    DO_TEST("addr-no32devs-multiple-sata-disks");
    DO_TEST_FAILURE("addr-no32devs-more-than-32-sata-disks");

229 230 231 232 233 234 235 236
    /* USB xhci tablet */
    DO_TEST("input-xhci-tablet");
    DO_TEST_FAILURE("xhci-multiple-controllers");
    DO_TEST_FAILURE("xhci-no-devs");
    DO_TEST_FAILURE("xhci-multiple-devs");
    driver.bhyvecaps ^= BHYVE_CAP_XHCI;
    DO_TEST_FAILURE("input-xhci-tablet");

237 238 239
    driver.grubcaps = 0;

    DO_TEST("serial-grub-nocons");
R
Roman Bogorodskiy 已提交
240

R
Roman Bogorodskiy 已提交
241 242 243 244
    driver.bhyvecaps &= ~BHYVE_CAP_NET_E1000;

    DO_TEST_FAILURE("net-e1000");

245 246 247
    driver.bhyvecaps &= ~BHYVE_CAP_LPC_BOOTROM;
    DO_TEST_FAILURE("uefi");

R
Roman Bogorodskiy 已提交
248 249 250
    driver.bhyvecaps &= ~BHYVE_CAP_FBUF;
    DO_TEST_FAILURE("vnc");

251 252 253
    driver.bhyvecaps &= ~BHYVE_CAP_CPUTOPOLOGY;
    DO_TEST_FAILURE("cputopology");

R
Roman Bogorodskiy 已提交
254 255
    virObjectUnref(driver.caps);
    virObjectUnref(driver.xmlopt);
256
    virPortAllocatorRangeFree(driver.remotePorts);
R
Roman Bogorodskiy 已提交
257 258 259 260

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

261
VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("bhyvexml2argv"))
R
Roman Bogorodskiy 已提交
262 263 264 265 266 267 268 269 270

#else

int main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_BHYVE */