summaryrefslogtreecommitdiff
path: root/tests/bhyveargv2xmltest.c
blob: d108366682e9addc18b4228dd0c4beefb6aba623 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <config.h>

#include "testutils.h"

#ifdef WITH_BHYVE

# include "datatypes.h"

# include "util/viruuid.h"
# include "bhyve/bhyve_driver.h"
# include "bhyve/bhyve_capabilities.h"
# include "bhyve/bhyve_utils.h"
# include "bhyve/bhyve_parse_command.h"

# define VIR_FROM_THIS VIR_FROM_BHYVE

static bhyveConn driver;

typedef enum {
    FLAG_EXPECT_FAILURE     = 1,
    FLAG_EXPECT_PARSE_ERROR = 2,
    FLAG_EXPECT_WARNING     = 4,
} virBhyveArgv2XMLTestFlags;

static int
testCompareXMLToArgvFiles(const char *xmlfile,
                          const char *cmdfile,
                          unsigned int flags)

{
    char *actualxml = NULL;
    char *cmd = NULL;
    char *log = NULL;
    int ret = -1;
    virDomainDefPtr vmdef = NULL;

    if (virTestLoadFile(cmdfile, &cmd) < 0)
        goto fail;

    if (!(vmdef = bhyveParseCommandLineString(cmd, driver.bhyvecaps,
                                              driver.xmlopt))) {
        if ((flags & FLAG_EXPECT_FAILURE)) {
            VIR_TEST_DEBUG("Got expected failure from "
                           "bhyveParseCommandLineString.");
        } else {
            goto fail;
        }
    } else if ((flags & FLAG_EXPECT_FAILURE)) {
        VIR_TEST_DEBUG("Did not get expected failure from "
                       "bhyveParseCommandLineString.");
        goto fail;
    }

    if ((log = virTestLogContentAndReset()) == NULL)
        goto fail;
    if (flags & FLAG_EXPECT_WARNING) {
        if (*log) {
            VIR_TEST_DEBUG("Got expected warning from "
                           "bhyveParseCommandLineString:\n%s",
                           log);
        } else {
            VIR_TEST_DEBUG("bhyveParseCommandLineString "
                           "should have logged a warning");
            goto fail;
        }
    } else { /* didn't expect a warning */
        if (*log) {
            VIR_TEST_DEBUG("Got unexpected warning from "
                           "bhyveParseCommandLineString:\n%s",
                           log);
            goto fail;
        }
    }

    if (vmdef && !virDomainDefCheckABIStability(vmdef, vmdef, driver.xmlopt)) {
        VIR_TEST_DEBUG("ABI stability check failed on %s", xmlfile);
        goto fail;
    }

    if (vmdef && !(actualxml = virDomainDefFormat(vmdef, driver.caps, 0)))
        goto fail;

    if (vmdef && virTestCompareToFile(actualxml, xmlfile) < 0)
        goto fail;

    ret = 0;

 fail:
    VIR_FREE(actualxml);
    VIR_FREE(cmd);
    VIR_FREE(log);
    virDomainDefFree(vmdef);
    return ret;
}

struct testInfo {
    const char *name;
    unsigned int flags;
};

static int
testCompareXMLToArgvHelper(const void *data)
{
    int result = -1;
    const struct testInfo *info = data;
    char *xml = NULL;
    char *args = NULL;

    if (virAsprintf(&xml, "%s/bhyveargv2xmldata/bhyveargv2xml-%s.xml",
                    abs_srcdir, info->name) < 0 ||
        virAsprintf(&args, "%s/bhyveargv2xmldata/bhyveargv2xml-%s.args",
                    abs_srcdir, info->name) < 0)
        goto cleanup;

    result = testCompareXMLToArgvFiles(xml, args, info->flags);

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

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

    if ((driver.caps = virBhyveCapsBuild()) == NULL)
        return EXIT_FAILURE;

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

# define DO_TEST_FULL(name, flags) \
    do { \
        static struct testInfo info = { \
            name, (flags) \
        }; \
        if (virTestRun("BHYVE ARGV-2-XML " name, \
                       testCompareXMLToArgvHelper, &info) < 0) \
            ret = -1; \
    } while (0)

# define DO_TEST(name) \
    DO_TEST_FULL(name, 0)

# define DO_TEST_FAIL(name) \
    DO_TEST_FULL(name, 5)

# define DO_TEST_WARN(name) \
    DO_TEST_FULL(name, 4)

# define DO_TEST_FAIL_SILENT(name) \
    DO_TEST_FULL(name, 1)

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

    driver.grubcaps = BHYVE_GRUB_CAP_CONSDEV;
    driver.bhyvecaps = BHYVE_CAP_RTC_UTC;

    DO_TEST("base");
    DO_TEST("wired");
    DO_TEST("oneline");
    DO_TEST("name");
    DO_TEST("console");
    DO_TEST_FAIL("console2");
    DO_TEST_FAIL("console3");
    DO_TEST_FAIL("console4");
    DO_TEST("acpiapic");
    DO_TEST("utc");
    DO_TEST("vcpus");
    DO_TEST("cdrom");
    DO_TEST("ahci-hd");
    DO_TEST("virtio-blk");
    DO_TEST("virtio-net");
    DO_TEST("e1000");
    DO_TEST_WARN("virtio-net2");
    DO_TEST_WARN("virtio-net3");
    DO_TEST_WARN("virtio-net4");
    DO_TEST_WARN("disk-toomany");
    DO_TEST("uuid");
    DO_TEST_FAIL("uuid2");
    DO_TEST("memsize-large");
    DO_TEST("memsize-human");
    DO_TEST_FAIL("memsize-fail");
    DO_TEST("custom-loader");
    DO_TEST("bhyveload-custom");
    DO_TEST("bhyveload-vda");
    DO_TEST_FAIL("bhyveload-name-mismatch");
    DO_TEST_FAIL("bhyverun-name-mismatch");
    DO_TEST_FAIL("bhyveload-mem-mismatch");
    DO_TEST_FAIL("bhyverun-mem-mismatch");
    DO_TEST_FAIL("bhyveload-mem-mismatch");
    DO_TEST_FAIL("bhyveload-memsize-fail");
    DO_TEST("bhyveload-bootorder");
    DO_TEST_FAIL("extraargs");

    virObjectUnref(driver.caps);
    virObjectUnref(driver.xmlopt);

    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}

VIR_TEST_MAIN_PRELOAD(mymain, VIR_TEST_MOCK("bhyveargv2xml"))

#else

int main(void)
{
    return EXIT_AM_SKIP;
}

#endif /* WITH_BHYVE */