bhyvexml2argvtest.c 6.9 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 72 73

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

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

    if (!(actualld = virCommandToString(ldcmd)))
        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 94
    ret = 0;

 out:
    VIR_FREE(actualargv);
95 96
    VIR_FREE(actualld);
    VIR_FREE(actualdm);
R
Roman Bogorodskiy 已提交
97
    virCommandFree(cmd);
98
    virCommandFree(ldcmd);
R
Roman Bogorodskiy 已提交
99
    virDomainDefFree(vmdef);
100
    virObjectUnref(conn);
R
Roman Bogorodskiy 已提交
101 102 103
    return ret;
}

104 105 106 107 108
struct testInfo {
    const char *name;
    unsigned int flags;
};

R
Roman Bogorodskiy 已提交
109 110 111 112
static int
testCompareXMLToArgvHelper(const void *data)
{
    int ret = -1;
113
    const struct testInfo *info = data;
R
Roman Bogorodskiy 已提交
114
    char *xml = NULL;
115
    char *args = NULL, *ldargs = NULL, *dmargs = NULL;
R
Roman Bogorodskiy 已提交
116 117

    if (virAsprintf(&xml, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.xml",
118
                    abs_srcdir, info->name) < 0 ||
R
Roman Bogorodskiy 已提交
119
        virAsprintf(&args, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.args",
120
                    abs_srcdir, info->name) < 0 ||
121
        virAsprintf(&ldargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.ldargs",
122
                    abs_srcdir, info->name) < 0 ||
123
        virAsprintf(&dmargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.devmap",
124
                    abs_srcdir, info->name) < 0)
R
Roman Bogorodskiy 已提交
125 126
        goto cleanup;

127
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs, info->flags);
R
Roman Bogorodskiy 已提交
128 129 130 131

 cleanup:
    VIR_FREE(xml);
    VIR_FREE(args);
132 133
    VIR_FREE(ldargs);
    VIR_FREE(dmargs);
R
Roman Bogorodskiy 已提交
134 135 136 137 138 139 140 141
    return ret;
}

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

142
    if ((driver.caps = virBhyveCapsBuild()) == NULL)
R
Roman Bogorodskiy 已提交
143 144
        return EXIT_FAILURE;

145
    if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL)
R
Roman Bogorodskiy 已提交
146 147
        return EXIT_FAILURE;

148 149 150 151 152
# define DO_TEST_FULL(name, flags)                             \
    do {                                                       \
        static struct testInfo info = {                        \
            name, (flags)                                      \
        };                                                     \
153
        if (virTestRun("BHYVE XML-2-ARGV " name,               \
154 155
                       testCompareXMLToArgvHelper, &info) < 0) \
            ret = -1;                                          \
R
Roman Bogorodskiy 已提交
156 157
    } while (0)

158 159 160 161 162 163 164 165 166
# define DO_TEST(name)                                         \
    DO_TEST_FULL(name, 0)

# define DO_TEST_FAILURE(name)                                 \
    DO_TEST_FULL(name, FLAG_EXPECT_FAILURE)

# define DO_TEST_PARSE_ERROR(name)                             \
    DO_TEST_FULL(name, FLAG_EXPECT_PARSE_ERROR)

167
    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
168
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC | BHYVE_CAP_AHCI32SLOT | \
R
Roman Bogorodskiy 已提交
169
                       BHYVE_CAP_NET_E1000 | BHYVE_CAP_LPC_BOOTROM | \
170
                       BHYVE_CAP_FBUF | BHYVE_CAP_XHCI;
R
Roman Bogorodskiy 已提交
171 172 173

    DO_TEST("base");
    DO_TEST("acpiapic");
R
Roman Bogorodskiy 已提交
174
    DO_TEST("disk-cdrom");
R
Roman Bogorodskiy 已提交
175 176
    DO_TEST("disk-virtio");
    DO_TEST("macaddr");
177 178
    DO_TEST("serial");
    DO_TEST("console");
179
    DO_TEST("grub-defaults");
180 181
    DO_TEST("grub-bootorder");
    DO_TEST("grub-bootorder2");
182 183 184 185
    DO_TEST("bhyveload-bootorder");
    DO_TEST("bhyveload-bootorder1");
    DO_TEST_FAILURE("bhyveload-bootorder2");
    DO_TEST("bhyveload-bootorder3");
186
    DO_TEST("bhyveload-explicitargs");
187 188
    DO_TEST_FAILURE("bhyveload-bootorder4");
    DO_TEST_PARSE_ERROR("bhyveload-bootorder5");
189 190
    DO_TEST("custom-loader");
    DO_TEST("disk-cdrom-grub");
191
    DO_TEST("serial-grub");
R
Roman Bogorodskiy 已提交
192
    DO_TEST("localtime");
R
Roman Bogorodskiy 已提交
193
    DO_TEST("net-e1000");
194
    DO_TEST("uefi");
R
Roman Bogorodskiy 已提交
195
    DO_TEST("vnc");
196

197 198 199 200
    /* Address allocation tests */
    DO_TEST("addr-single-sata-disk");
    DO_TEST("addr-multiple-sata-disks");
    DO_TEST("addr-more-than-32-sata-disks");
201 202
    DO_TEST("addr-single-virtio-disk");
    DO_TEST("addr-multiple-virtio-disks");
203 204 205 206 207 208 209

    /* 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");

210 211 212 213 214 215 216 217
    /* 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");

218 219 220
    driver.grubcaps = 0;

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

R
Roman Bogorodskiy 已提交
222 223 224 225
    driver.bhyvecaps &= ~BHYVE_CAP_NET_E1000;

    DO_TEST_FAILURE("net-e1000");

226 227 228
    driver.bhyvecaps &= ~BHYVE_CAP_LPC_BOOTROM;
    DO_TEST_FAILURE("uefi");

R
Roman Bogorodskiy 已提交
229 230 231
    driver.bhyvecaps &= ~BHYVE_CAP_FBUF;
    DO_TEST_FAILURE("vnc");

R
Roman Bogorodskiy 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    virObjectUnref(driver.caps);
    virObjectUnref(driver.xmlopt);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/bhyvexml2argvmock.so")

#else

int main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_BHYVE */