virnetdevbandwidthtest.c 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright (C) 2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Author: Michal Privoznik <mprivozn@redhat.com>
 */

#include <config.h>

#include "testutils.h"
24 25
#define __VIR_COMMAND_PRIV_H_ALLOW__
#include "vircommandpriv.h"
26 27 28 29 30 31 32 33 34 35 36
#include "virnetdevbandwidth.h"
#include "netdev_bandwidth_conf.c"

#define VIR_FROM_THIS VIR_FROM_NONE

struct testMinimalStruct {
    const char *expected_result;
    const char *band1;
    const char *band2;
};

37 38 39 40 41 42 43
struct testSetStruct {
    const char *band;
    const char *exp_cmd;
    const char *iface;
    const bool hierarchical_class;
};

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#define PARSE(xml, var)                                                 \
    do {                                                                \
        xmlDocPtr doc;                                                  \
        xmlXPathContextPtr ctxt = NULL;                                 \
                                                                        \
        if (!xml)                                                       \
            break;                                                      \
                                                                        \
        if (!(doc = virXMLParseStringCtxt((xml),                        \
                                          "bandwidth definition",       \
                                          &ctxt)))                      \
            goto cleanup;                                               \
                                                                        \
        (var) = virNetDevBandwidthParse(ctxt->node,                     \
                                        VIR_DOMAIN_NET_TYPE_NETWORK);   \
        xmlFreeDoc(doc);                                                \
        xmlXPathFreeContext(ctxt);                                      \
        if (!(var))                                                     \
            goto cleanup;                                               \
    } while (0)

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static int
testVirNetDevBandwidthSet(const void *data)
{
    int ret = -1;
    const struct testSetStruct *info = data;
    const char *iface = info->iface;
    virNetDevBandwidthPtr band = NULL;
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    char *actual_cmd = NULL;

    PARSE(info->band, band);

    if (!iface)
        iface = "eth0";

80
    virCommandSetDryRun(&buf, NULL, NULL);
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    if (virNetDevBandwidthSet(iface, band, info->hierarchical_class) < 0)
        goto cleanup;

    if (!(actual_cmd = virBufferContentAndReset(&buf))) {
        int err = virBufferError(&buf);
        if (err) {
            fprintf(stderr, "buffer's in error state: %d", err);
            goto cleanup;
        }
        /* This is interesting, no command has been executed.
         * Maybe that's expected, actually. */
    }

    if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
96 97 98
        virtTestDifference(stderr,
                           NULLSTR(info->exp_cmd),
                           NULLSTR(actual_cmd));
99 100 101 102 103
        goto cleanup;
    }

    ret = 0;
cleanup:
104
    virCommandSetDryRun(NULL, NULL, NULL);
105 106 107 108 109 110
    virNetDevBandwidthFree(band);
    virBufferFreeAndReset(&buf);
    VIR_FREE(actual_cmd);
    return ret;
}

111 112 113 114 115
static int
mymain(void)
{
    int ret = 0;

116 117 118 119 120 121 122 123 124 125 126
#define DO_TEST_SET(Band, Exp_cmd, ...)                     \
    do {                                                    \
        struct testSetStruct data = {.band = Band,          \
                                     .exp_cmd = Exp_cmd,    \
                                     __VA_ARGS__};          \
        if (virtTestRun("virNetDevBandwidthSet",            \
                        testVirNetDevBandwidthSet,          \
                        &data) < 0)                         \
            ret = -1;                                       \
    } while (0)

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
    DO_TEST_SET(NULL, NULL);

    DO_TEST_SET(("<bandwidth/>"),
                (TC " qdisc del dev eth0 root\n"
                 TC " qdisc del dev eth0 ingress\n"));

    DO_TEST_SET(("<bandwidth>"
                 "  <inbound average='1024'/>"
                 "</bandwidth>"),
                (TC " qdisc del dev eth0 root\n"
                 TC " qdisc del dev eth0 ingress\n"
                 TC " qdisc add dev eth0 root handle 1: htb default 1\n"
                 TC " class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps\n"
                 TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
                 TC " filter add dev eth0 parent 1:0 protocol ip handle 1 fw flowid 1\n"));

    DO_TEST_SET(("<bandwidth>"
                 "  <outbound average='1024'/>"
                 "</bandwidth>"),
                (TC " qdisc del dev eth0 root\n"
                 TC " qdisc del dev eth0 ingress\n"
                 TC " qdisc add dev eth0 ingress\n"
                 TC " filter add dev eth0 parent ffff: protocol ip u32 match ip src 0.0.0.0/0 "
                 "police rate 1024kbps burst 1024kb mtu 64kb drop flowid :1\n"));

153 154 155 156
    DO_TEST_SET(("<bandwidth>"
                 "  <inbound average='1' peak='2' floor='3' burst='4'/>"
                 "  <outbound average='5' peak='6' burst='7'/>"
                 "</bandwidth>"),
157 158 159 160 161 162 163 164
                (TC " qdisc del dev eth0 root\n"
                 TC " qdisc del dev eth0 ingress\n"
                 TC " qdisc add dev eth0 root handle 1: htb default 1\n"
                 TC " class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb\n"
                 TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
                 TC " filter add dev eth0 parent 1:0 protocol ip handle 1 fw flowid 1\n"
                 TC " qdisc add dev eth0 ingress\n"
                 TC " filter add dev eth0 parent ffff: protocol ip u32 match ip src 0.0.0.0/0 "
165 166
                 "police rate 5kbps burst 7kb mtu 64kb drop flowid :1\n"));

167 168 169 170
    return ret;
}

VIRT_TEST_MAIN(mymain);