You need to sign in or sign up before continuing.
bhyvexml2argvtest.c 5.4 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 41
        if (flags & FLAG_EXPECT_PARSE_ERROR)
            ret = 0;
R
Roman Bogorodskiy 已提交
42
        goto out;
43
    }
R
Roman Bogorodskiy 已提交
44

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

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

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

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

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

70
    if (virTestCompareToFile(actualargv, cmdline) < 0)
R
Roman Bogorodskiy 已提交
71 72
        goto out;

73
    if (virTestCompareToFile(actualld, ldcmdline) < 0)
74 75
        goto out;

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

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

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

94 95 96 97 98
struct testInfo {
    const char *name;
    unsigned int flags;
};

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

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

117
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs, info->flags);
R
Roman Bogorodskiy 已提交
118 119 120 121

 cleanup:
    VIR_FREE(xml);
    VIR_FREE(args);
122 123
    VIR_FREE(ldargs);
    VIR_FREE(dmargs);
R
Roman Bogorodskiy 已提交
124 125 126 127 128 129 130 131
    return ret;
}

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

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

135
    if ((driver.xmlopt = virBhyveDriverCreateXMLConf(&driver)) == NULL)
R
Roman Bogorodskiy 已提交
136 137
        return EXIT_FAILURE;

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

148 149 150 151 152 153 154 155 156
# 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)

157
    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
158
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC | BHYVE_CAP_AHCI32SLOT;
R
Roman Bogorodskiy 已提交
159 160 161

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

    driver.grubcaps = 0;

    DO_TEST("serial-grub-nocons");
R
Roman Bogorodskiy 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

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