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

#include <unistd.h>

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

#include "internal.h"
#include "testutils.h"
#include "network_conf.h"
11
#include "vircommand.h"
12
#include "viralloc.h"
13
#include "network/bridge_driver.h"
14
#include "virstring.h"
15

16 17
#define VIR_FROM_THIS VIR_FROM_NONE

18
static int
19
testCompareXMLToConfFiles(const char *inxml, const char *outconf, dnsmasqCapsPtr caps)
20
{
21 22
    char *actual = NULL;
    int ret = -1;
23
    virNetworkDefPtr def = NULL;
24 25 26
    virNetworkObjPtr obj = NULL;
    virCommandPtr cmd = NULL;
    char *pidfile = NULL;
27
    dnsmasqContext *dctx = NULL;
28
    virNetworkXMLOptionPtr xmlopt = NULL;
29

30 31 32 33
    if (!(xmlopt = networkDnsmasqCreateXMLConf()))
        goto fail;

    if (!(def = virNetworkDefParseFile(inxml, xmlopt)))
34 35
        goto fail;

36
    if (!(obj = virNetworkObjNew()))
37 38
        goto fail;

39 40 41
    virNetworkObjSetDef(obj, def);

    dctx = dnsmasqContextNew(def->name, "/var/lib/libvirt/dnsmasq");
42

43 44 45
    if (dctx == NULL)
        goto fail;

46
    if (networkDnsmasqConfContents(obj, pidfile, &actual, dctx, caps) < 0)
47 48
        goto fail;

49 50 51 52 53 54 55 56 57
    /* Any changes to this function ^^ should be reflected here too. */
#ifndef __linux__
    char * tmp;

    if (!(tmp = virStringReplace(actual,
                                 "except-interface=lo0\n",
                                 "except-interface=lo\n")))
        goto fail;
    VIR_FREE(actual);
58
    VIR_STEAL_PTR(actual, tmp);
59 60
#endif

61
    if (virTestCompareToFile(actual, outconf) < 0)
62 63 64 65 66
        goto fail;

    ret = 0;

 fail:
67
    VIR_FREE(actual);
68 69
    VIR_FREE(pidfile);
    virCommandFree(cmd);
70
    virObjectUnref(xmlopt);
71
    virNetworkObjEndAPI(&obj);
72
    dnsmasqContextFree(dctx);
73 74 75
    return ret;
}

76 77 78 79 80
typedef struct {
    const char *name;
    dnsmasqCapsPtr caps;
} testInfo;

81
static int
82
testCompareXMLToConfHelper(const void *data)
83 84
{
    int result = -1;
85
    const testInfo *info = data;
86
    char *inxml = NULL;
87
    char *outconf = NULL;
88

89
    if (virAsprintf(&inxml, "%s/networkxml2confdata/%s.xml",
90
                    abs_srcdir, info->name) < 0 ||
91
        virAsprintf(&outconf, "%s/networkxml2confdata/%s.conf",
92
                    abs_srcdir, info->name) < 0) {
93 94 95
        goto cleanup;
    }

96
    result = testCompareXMLToConfFiles(inxml, outconf, info->caps);
97

98
 cleanup:
99
    VIR_FREE(inxml);
100
    VIR_FREE(outconf);
101 102 103 104 105 106 107 108

    return result;
}

static int
mymain(void)
{
    int ret = 0;
109 110 111 112
    dnsmasqCapsPtr restricted
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.48", DNSMASQ);
    dnsmasqCapsPtr full
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.63\n--bind-dynamic", DNSMASQ);
G
Gene Czarcinski 已提交
113 114
    dnsmasqCapsPtr dhcpv6
        = dnsmasqCapsNewFromBuffer("Dnsmasq version 2.64\n--bind-dynamic", DNSMASQ);
115

116 117 118 119 120 121 122 123 124 125
#define DO_TEST(xname, xcaps) \
    do { \
        static testInfo info; \
 \
        info.name = xname; \
        info.caps = xcaps; \
        if (virTestRun("Network XML-2-Conf " xname, \
                       testCompareXMLToConfHelper, &info) < 0) { \
            ret = -1; \
        } \
126 127 128 129 130 131
    } while (0)

    DO_TEST("isolated-network", restricted);
    DO_TEST("netboot-network", restricted);
    DO_TEST("netboot-proxy-network", restricted);
    DO_TEST("nat-network-dns-srv-record-minimal", restricted);
132
    DO_TEST("nat-network-name-with-quotes", restricted);
133
    DO_TEST("routed-network", full);
134
    DO_TEST("routed-network-no-dns", full);
135
    DO_TEST("open-network", full);
G
Gene Czarcinski 已提交
136
    DO_TEST("nat-network", dhcpv6);
137 138 139
    DO_TEST("nat-network-dns-txt-record", full);
    DO_TEST("nat-network-dns-srv-record", full);
    DO_TEST("nat-network-dns-hosts", full);
140
    DO_TEST("nat-network-dns-forward-plain", full);
141
    DO_TEST("nat-network-dns-forwarders", full);
142
    DO_TEST("nat-network-dns-forwarder-no-resolv", full);
143
    DO_TEST("nat-network-dns-local-domain", full);
144
    DO_TEST("nat-network-mtu", dhcpv6);
G
Gene Czarcinski 已提交
145 146 147
    DO_TEST("dhcp6-network", dhcpv6);
    DO_TEST("dhcp6-nat-network", dhcpv6);
    DO_TEST("dhcp6host-routed-network", dhcpv6);
148
    DO_TEST("ptr-domains-auto", dhcpv6);
149
    DO_TEST("dnsmasq-options", dhcpv6);
150

151 152 153 154
    virObjectUnref(dhcpv6);
    virObjectUnref(full);
    virObjectUnref(restricted);

155
    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
156 157
}

158
VIR_TEST_MAIN(mymain)