networkxml2xmltest.c 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include <config.h>

#include <unistd.h>

#include <sys/types.h>
#include <fcntl.h>

#include "internal.h"
#include "testutils.h"
#include "network_conf.h"
#include "testutilsqemu.h"
12
#include "virstring.h"
13
#include "network/bridge_driver.h"
14

15 16
#define VIR_FROM_THIS VIR_FROM_NONE

17 18 19 20 21 22 23
typedef enum {
    TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT,
    TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE,
} testCompareNetXML2XMLResult;

24
static int
25
testCompareXMLToXMLFiles(const char *inxml, const char *outxml,
26 27
                         unsigned int flags,
                         testCompareNetXML2XMLResult expectResult)
28
{
29
    char *actual = NULL;
30
    int ret;
A
Andrea Bolognani 已提交
31
    testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS;
32
    virNetworkDefPtr dev = NULL;
33
    virNetworkXMLOptionPtr xmlopt = NULL;
34

35 36 37 38
    if (!(xmlopt = networkDnsmasqCreateXMLConf()))
        goto cleanup;

    if (!(dev = virNetworkDefParseFile(inxml, xmlopt))) {
39 40 41 42 43
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE)
        goto cleanup;
44

45
    if (!(actual = virNetworkDefFormat(dev, xmlopt, flags))) {
46 47 48 49 50
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_FORMAT)
        goto cleanup;
51

52
    if (virTestCompareToFile(actual, outxml) < 0) {
53 54 55 56 57
        result = TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE;
        goto cleanup;
    }
    if (expectResult == TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE)
        goto cleanup;
58

59 60 61 62 63 64 65 66 67 68 69 70 71
 cleanup:
    if (result == expectResult) {
        ret = 0;
        if (expectResult != TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS) {
            VIR_TEST_DEBUG("Got expected failure code=%d msg=%s",
                           result, virGetLastErrorMessage());
        }
    } else {
        ret = -1;
        VIR_TEST_DEBUG("Expected result code=%d but received code=%d",
                       expectResult, result);
    }
    virResetLastError();
72

73
    VIR_FREE(actual);
74
    virNetworkDefFree(dev);
75
    virObjectUnref(xmlopt);
76 77 78
    return ret;
}

79 80 81
struct testInfo {
    const char *name;
    unsigned int flags;
82
    testCompareNetXML2XMLResult expectResult;
83 84
};

85 86 87
static int
testCompareXMLToXMLHelper(const void *data)
{
88
    const struct testInfo *info = data;
89 90 91 92 93
    int result = -1;
    char *inxml = NULL;
    char *outxml = NULL;

    if (virAsprintf(&inxml, "%s/networkxml2xmlin/%s.xml",
94
                    abs_srcdir, info->name) < 0 ||
95
        virAsprintf(&outxml, "%s/networkxml2xmlout/%s.xml",
96
                    abs_srcdir, info->name) < 0) {
97 98 99
        goto cleanup;
    }

100 101
    result = testCompareXMLToXMLFiles(inxml, outxml, info->flags,
                                      info->expectResult);
102

103
 cleanup:
104 105
    VIR_FREE(inxml);
    VIR_FREE(outxml);
106 107 108

    return result;
}
109 110

static int
E
Eric Blake 已提交
111
mymain(void)
112 113 114
{
    int ret = 0;

115 116 117 118 119 120
#define DO_TEST_FULL(name, flags, expectResult) \
    do { \
        const struct testInfo info = {name, flags, expectResult}; \
        if (virTestRun("Network XML-2-XML " name, \
                       testCompareXMLToXMLHelper, &info) < 0) \
            ret = -1; \
121
    } while (0)
122 123 124 125 126 127
#define DO_TEST(name) \
    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
#define DO_TEST_FLAGS(name, flags) \
    DO_TEST_FULL(name, flags, TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS)
#define DO_TEST_PARSE_ERROR(name) \
    DO_TEST_FULL(name, 0, TEST_COMPARE_NET_XML2XML_RESULT_FAIL_PARSE)
128

129
    DO_TEST("dhcp6host-routed-network");
130
    DO_TEST("empty-allow-ipv6");
131 132
    DO_TEST("isolated-network");
    DO_TEST("routed-network");
133 134
    DO_TEST("routed-network-no-dns");
    DO_TEST_PARSE_ERROR("routed-network-no-dns-extra-elements");
135 136
    DO_TEST("open-network");
    DO_TEST_PARSE_ERROR("open-network-with-forward-dev");
137 138
    DO_TEST("nat-network");
    DO_TEST("netboot-network");
139
    DO_TEST("netboot-proxy-network");
140
    DO_TEST("nat-network-dns-txt-record");
141
    DO_TEST("nat-network-dns-srv-record");
J
Ján Tomko 已提交
142
    DO_TEST("nat-network-dns-srv-records");
143
    DO_TEST("nat-network-dns-srv-record-minimal");
144
    DO_TEST("nat-network-dns-hosts");
145
    DO_TEST("nat-network-dns-forward-plain");
146
    DO_TEST("nat-network-dns-forwarders");
147
    DO_TEST("nat-network-dns-forwarder-no-resolv");
148
    DO_TEST("nat-network-forward-nat-address");
149
    DO_TEST("nat-network-forward-nat-no-address");
150
    DO_TEST("nat-network-mtu");
151 152 153 154
    DO_TEST("8021Qbh-net");
    DO_TEST("direct-net");
    DO_TEST("host-bridge-net");
    DO_TEST("vepa-net");
155
    DO_TEST("bandwidth-network");
156
    DO_TEST("openvswitch-net");
157
    DO_TEST_FLAGS("passthrough-pf", VIR_NETWORK_XML_INACTIVE);
158
    DO_TEST("hostdev");
159
    DO_TEST_FLAGS("hostdev-pf", VIR_NETWORK_XML_INACTIVE);
160
    DO_TEST("passthrough-address-crash");
161 162
    DO_TEST("nat-network-explicit-flood");
    DO_TEST("host-bridge-no-flood");
163 164
    DO_TEST_PARSE_ERROR("hostdev-duplicate");
    DO_TEST_PARSE_ERROR("passthrough-duplicate");
165
    DO_TEST("metadata");
166
    DO_TEST("set-mtu");
167
    DO_TEST("dnsmasq-options");
168

169
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
170 171
}

172
VIR_TEST_MAIN(mymain)