bhyvexml2argvtest.c 4.6 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 17
# include "bhyve/bhyve_utils.h"
# include "bhyve/bhyve_command.h"

# define VIR_FROM_THIS VIR_FROM_BHYVE

static bhyveConn driver;

static int testCompareXMLToArgvFiles(const char *xml,
18 19 20
                                     const char *cmdline,
                                     const char *ldcmdline,
                                     const char *dmcmdline)
R
Roman Bogorodskiy 已提交
21
{
22
    char *expectargv = NULL, *expectld = NULL, *expectdm = NULL;
R
Roman Bogorodskiy 已提交
23
    int len;
24
    char *actualargv = NULL, *actualld = NULL, *actualdm = NULL;
R
Roman Bogorodskiy 已提交
25 26
    virDomainDefPtr vmdef = NULL;
    virDomainObj vm;
27
    virCommandPtr cmd = NULL, ldcmd = NULL;
R
Roman Bogorodskiy 已提交
28
    virConnectPtr conn;
R
Roman Bogorodskiy 已提交
29 30
    int ret = -1;

R
Roman Bogorodskiy 已提交
31 32
    if (!(conn = virGetConnect()))
        goto out;
R
Roman Bogorodskiy 已提交
33 34 35 36 37 38 39 40

    if (!(vmdef = virDomainDefParseFile(xml, driver.caps, driver.xmlopt,
                                        1 << VIR_DOMAIN_VIRT_BHYVE,
                                        VIR_DOMAIN_XML_INACTIVE)))
        goto out;

    vm.def = vmdef;

R
Roman Bogorodskiy 已提交
41
    if (!(cmd = virBhyveProcessBuildBhyveCmd(conn, vmdef, false)))
R
Roman Bogorodskiy 已提交
42 43 44 45 46
        goto out;

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

47 48 49 50 51 52 53 54 55 56
    if (!(ldcmd = virBhyveProcessBuildLoadCmd(conn, vmdef, "<device.map>",
                                              &actualdm)))
        goto out;

    if (actualdm != NULL)
        virTrimSpaces(actualdm, NULL);

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

R
Roman Bogorodskiy 已提交
57 58 59 60 61 62 63
    len = virtTestLoadFile(cmdline, &expectargv);
    if (len < 0)
        goto out;

    if (len && expectargv[len - 1] == '\n')
        expectargv[len - 1] = '\0';

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    len = virtTestLoadFile(ldcmdline, &expectld);
    if (len < 0)
        goto out;

    if (len && expectld[len - 1] == '\n')
        expectld[len - 1] = '\0';

    len = virFileReadAllQuiet(dmcmdline, 1000, &expectdm);
    if (len < 0) {
        if (actualdm != NULL) {
            virtTestDifference(stderr, "", actualdm);
            goto out;
        }
    } else if (len && expectdm[len - 1] == '\n') {
        expectdm[len - 1] = '\0';
    }

R
Roman Bogorodskiy 已提交
81 82 83 84 85
    if (STRNEQ(expectargv, actualargv)) {
        virtTestDifference(stderr, expectargv, actualargv);
        goto out;
    }

86 87 88 89 90 91 92 93 94 95
    if (STRNEQ(expectld, actualld)) {
        virtTestDifference(stderr, expectld, actualld);
        goto out;
    }

    if (expectdm && STRNEQ(expectdm, actualdm)) {
        virtTestDifference(stderr, expectdm, actualdm);
        goto out;
    }

R
Roman Bogorodskiy 已提交
96 97 98 99
    ret = 0;

 out:
    VIR_FREE(expectargv);
100 101
    VIR_FREE(expectld);
    VIR_FREE(expectdm);
R
Roman Bogorodskiy 已提交
102
    VIR_FREE(actualargv);
103 104
    VIR_FREE(actualld);
    VIR_FREE(actualdm);
R
Roman Bogorodskiy 已提交
105
    virCommandFree(cmd);
106
    virCommandFree(ldcmd);
R
Roman Bogorodskiy 已提交
107 108 109 110 111 112 113 114 115 116
    virDomainDefFree(vmdef);
    return ret;
}

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

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

129
    ret = testCompareXMLToArgvFiles(xml, args, ldargs, dmargs);
R
Roman Bogorodskiy 已提交
130 131 132 133 134 135 136 137 138 139 140 141

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

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

142
    if ((driver.caps = virBhyveCapsBuild()) == NULL)
R
Roman Bogorodskiy 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        return EXIT_FAILURE;

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

# define DO_TEST(name)                                        \
    do {                                                      \
        if (virtTestRun("BHYVE XML-2-ARGV " name,             \
                       testCompareXMLToArgvHelper, name) < 0) \
            ret = -1;                                         \
    } while (0)


    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 164 165 166
    DO_TEST("grub-defaults");
    DO_TEST("bhyveload-explicitargs");
    DO_TEST("custom-loader");
    DO_TEST("disk-cdrom-grub");
R
Roman Bogorodskiy 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

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