bhyvexml2argvtest.c 5.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"
R
Roman Bogorodskiy 已提交
10 11 12 13 14 15 16
# include "bhyve/bhyve_utils.h"
# include "bhyve/bhyve_command.h"

# define VIR_FROM_THIS VIR_FROM_BHYVE

static bhyveConn driver;

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

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

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

    if (!(vmdef = virDomainDefParseFile(xml, driver.caps, driver.xmlopt,
38 39 40
                                        VIR_DOMAIN_DEF_PARSE_INACTIVE))) {
        if (flags & FLAG_EXPECT_PARSE_ERROR)
            ret = 0;
R
Roman Bogorodskiy 已提交
41
        goto out;
42
    }
R
Roman Bogorodskiy 已提交
43

44
    conn->privateData = &driver;
R
Roman Bogorodskiy 已提交
45

46 47 48 49 50 51 52 53 54 55 56
    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 已提交
57
        goto out;
58
    }
R
Roman Bogorodskiy 已提交
59 60 61 62

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

63 64 65 66 67 68
    if (actualdm != NULL)
        virTrimSpaces(actualdm, NULL);

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

C
Cole Robinson 已提交
69
    if (virtTestCompareToFile(actualargv, cmdline) < 0)
R
Roman Bogorodskiy 已提交
70 71
        goto out;

C
Cole Robinson 已提交
72
    if (virtTestCompareToFile(actualld, ldcmdline) < 0)
73 74
        goto out;

C
Cole Robinson 已提交
75 76 77
    if (virFileExists(dmcmdline) || actualdm) {
        if (virtTestCompareToFile(actualdm, dmcmdline) < 0)
            goto out;
78 79
    }

R
Roman Bogorodskiy 已提交
80 81 82 83
    ret = 0;

 out:
    VIR_FREE(actualargv);
84 85
    VIR_FREE(actualld);
    VIR_FREE(actualdm);
R
Roman Bogorodskiy 已提交
86
    virCommandFree(cmd);
87
    virCommandFree(ldcmd);
R
Roman Bogorodskiy 已提交
88 89 90 91
    virDomainDefFree(vmdef);
    return ret;
}

92 93 94 95 96
struct testInfo {
    const char *name;
    unsigned int flags;
};

R
Roman Bogorodskiy 已提交
97 98 99 100
static int
testCompareXMLToArgvHelper(const void *data)
{
    int ret = -1;
101
    const struct testInfo *info = data;
R
Roman Bogorodskiy 已提交
102
    char *xml = NULL;
103
    char *args = NULL, *ldargs = NULL, *dmargs = NULL;
R
Roman Bogorodskiy 已提交
104 105

    if (virAsprintf(&xml, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.xml",
106
                    abs_srcdir, info->name) < 0 ||
R
Roman Bogorodskiy 已提交
107
        virAsprintf(&args, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.args",
108
                    abs_srcdir, info->name) < 0 ||
109
        virAsprintf(&ldargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.ldargs",
110
                    abs_srcdir, info->name) < 0 ||
111
        virAsprintf(&dmargs, "%s/bhyvexml2argvdata/bhyvexml2argv-%s.devmap",
112
                    abs_srcdir, info->name) < 0)
R
Roman Bogorodskiy 已提交
113 114
        goto cleanup;

115
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs, info->flags);
R
Roman Bogorodskiy 已提交
116 117 118 119 120 121 122 123 124 125 126 127

 cleanup:
    VIR_FREE(xml);
    VIR_FREE(args);
    return ret;
}

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

128
    if ((driver.caps = virBhyveCapsBuild()) == NULL)
R
Roman Bogorodskiy 已提交
129 130 131 132 133
        return EXIT_FAILURE;

    if ((driver.xmlopt = virDomainXMLOptionNew(NULL, NULL, NULL)) == NULL)
        return EXIT_FAILURE;

134 135 136 137 138 139 140 141
# define DO_TEST_FULL(name, flags)                             \
    do {                                                       \
        static struct testInfo info = {                        \
            name, (flags)                                      \
        };                                                     \
        if (virtTestRun("BHYVE XML-2-ARGV " name,              \
                       testCompareXMLToArgvHelper, &info) < 0) \
            ret = -1;                                          \
R
Roman Bogorodskiy 已提交
142 143
    } while (0)

144 145 146 147 148 149 150 151 152
# 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)

153
    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
R
Roman Bogorodskiy 已提交
154
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC;
R
Roman Bogorodskiy 已提交
155 156 157

    DO_TEST("base");
    DO_TEST("acpiapic");
R
Roman Bogorodskiy 已提交
158
    DO_TEST("disk-cdrom");
R
Roman Bogorodskiy 已提交
159 160
    DO_TEST("disk-virtio");
    DO_TEST("macaddr");
161 162
    DO_TEST("serial");
    DO_TEST("console");
163
    DO_TEST("grub-defaults");
164 165
    DO_TEST("grub-bootorder");
    DO_TEST("grub-bootorder2");
166 167 168 169
    DO_TEST("bhyveload-bootorder");
    DO_TEST("bhyveload-bootorder1");
    DO_TEST_FAILURE("bhyveload-bootorder2");
    DO_TEST("bhyveload-bootorder3");
170
    DO_TEST("bhyveload-explicitargs");
171 172
    DO_TEST_FAILURE("bhyveload-bootorder4");
    DO_TEST_PARSE_ERROR("bhyveload-bootorder5");
173 174
    DO_TEST("custom-loader");
    DO_TEST("disk-cdrom-grub");
175
    DO_TEST("serial-grub");
R
Roman Bogorodskiy 已提交
176
    DO_TEST("localtime");
177 178 179 180

    driver.grubcaps = 0;

    DO_TEST("serial-grub-nocons");
R
Roman Bogorodskiy 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

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