bhyvexml2argvtest.c 6.1 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 55 56 57 58 59 60 61 62 63 64
    cmd = virBhyveProcessBuildBhyveCmd(conn, vmdef, false);
    ldcmd = virBhyveProcessBuildLoadCmd(conn, vmdef, "<device.map>",
                                        &actualdm);

    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 已提交
65
        goto out;
66
    }
R
Roman Bogorodskiy 已提交
67 68 69 70

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

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

    if (!(actualld = virCommandToString(ldcmd)))
        goto out;

77
    if (virTestCompareToFile(actualargv, cmdline) < 0)
R
Roman Bogorodskiy 已提交
78 79
        goto out;

80
    if (virTestCompareToFile(actualld, ldcmdline) < 0)
81 82
        goto out;

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

R
Roman Bogorodskiy 已提交
88 89 90 91
    ret = 0;

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

101 102 103 104 105
struct testInfo {
    const char *name;
    unsigned int flags;
};

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

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

124
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs, info->flags);
R
Roman Bogorodskiy 已提交
125 126 127 128

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

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

139
    if ((driver.caps = virBhyveCapsBuild()) == NULL)
R
Roman Bogorodskiy 已提交
140 141
        return EXIT_FAILURE;

142
    if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL)
R
Roman Bogorodskiy 已提交
143 144
        return EXIT_FAILURE;

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

155 156 157 158 159 160 161 162 163
# 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)

164
    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
165
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC | BHYVE_CAP_AHCI32SLOT;
R
Roman Bogorodskiy 已提交
166 167 168

    DO_TEST("base");
    DO_TEST("acpiapic");
R
Roman Bogorodskiy 已提交
169
    DO_TEST("disk-cdrom");
R
Roman Bogorodskiy 已提交
170 171
    DO_TEST("disk-virtio");
    DO_TEST("macaddr");
172 173
    DO_TEST("serial");
    DO_TEST("console");
174
    DO_TEST("grub-defaults");
175 176
    DO_TEST("grub-bootorder");
    DO_TEST("grub-bootorder2");
177 178 179 180
    DO_TEST("bhyveload-bootorder");
    DO_TEST("bhyveload-bootorder1");
    DO_TEST_FAILURE("bhyveload-bootorder2");
    DO_TEST("bhyveload-bootorder3");
181
    DO_TEST("bhyveload-explicitargs");
182 183
    DO_TEST_FAILURE("bhyveload-bootorder4");
    DO_TEST_PARSE_ERROR("bhyveload-bootorder5");
184 185
    DO_TEST("custom-loader");
    DO_TEST("disk-cdrom-grub");
186
    DO_TEST("serial-grub");
R
Roman Bogorodskiy 已提交
187
    DO_TEST("localtime");
188

189 190 191 192
    /* Address allocation tests */
    DO_TEST("addr-single-sata-disk");
    DO_TEST("addr-multiple-sata-disks");
    DO_TEST("addr-more-than-32-sata-disks");
193 194
    DO_TEST("addr-single-virtio-disk");
    DO_TEST("addr-multiple-virtio-disks");
195 196 197 198 199 200 201

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

202 203 204
    driver.grubcaps = 0;

    DO_TEST("serial-grub-nocons");
R
Roman Bogorodskiy 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

    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 */