2 * Copyright (C) 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
18 * Author: Michal Privoznik <mprivozn@redhat.com>
23 #include "testutils.h"
24 #define __VIR_COMMAND_PRIV_H_ALLOW__
25 #include "vircommandpriv.h"
26 #include "virnetdevbandwidth.h"
27 #include "netdev_bandwidth_conf.c"
29 #define VIR_FROM_THIS VIR_FROM_NONE
31 struct testMinimalStruct {
32 const char *expected_result;
37 struct testSetStruct {
41 const bool hierarchical_class;
44 #define PARSE(xml, var) \
47 xmlXPathContextPtr ctxt = NULL; \
52 if (!(doc = virXMLParseStringCtxt((xml), \
53 "bandwidth definition", \
57 (var) = virNetDevBandwidthParse(ctxt->node, \
58 VIR_DOMAIN_NET_TYPE_NETWORK); \
60 xmlXPathFreeContext(ctxt); \
66 testVirNetDevBandwidthSet(const void *data)
69 const struct testSetStruct *info = data;
70 const char *iface = info->iface;
71 virNetDevBandwidthPtr band = NULL;
72 virBuffer buf = VIR_BUFFER_INITIALIZER;
73 char *actual_cmd = NULL;
75 PARSE(info->band, band);
80 virCommandSetDryRun(&buf, NULL, NULL);
82 if (virNetDevBandwidthSet(iface, band, info->hierarchical_class) < 0)
85 if (!(actual_cmd = virBufferContentAndReset(&buf))) {
86 int err = virBufferError(&buf);
88 fprintf(stderr, "buffer's in error state: %d", err);
91 /* This is interesting, no command has been executed.
92 * Maybe that's expected, actually. */
95 if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
96 virtTestDifference(stderr,
97 NULLSTR(info->exp_cmd),
104 virCommandSetDryRun(NULL, NULL, NULL);
105 virNetDevBandwidthFree(band);
106 virBufferFreeAndReset(&buf);
107 VIR_FREE(actual_cmd);
116 #define DO_TEST_SET(Band, Exp_cmd, ...) \
118 struct testSetStruct data = {.band = Band, \
119 .exp_cmd = Exp_cmd, \
121 if (virtTestRun("virNetDevBandwidthSet", \
122 testVirNetDevBandwidthSet, \
128 DO_TEST_SET(NULL, NULL);
130 DO_TEST_SET(("<bandwidth/>"),
131 (TC " qdisc del dev eth0 root\n"
132 TC " qdisc del dev eth0 ingress\n"));
134 DO_TEST_SET(("<bandwidth>"
135 " <inbound average='1024'/>"
137 (TC " qdisc del dev eth0 root\n"
138 TC " qdisc del dev eth0 ingress\n"
139 TC " qdisc add dev eth0 root handle 1: htb default 1\n"
140 TC " class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps\n"
141 TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
142 TC " filter add dev eth0 parent 1:0 protocol all handle 1 fw flowid 1\n"));
144 DO_TEST_SET(("<bandwidth>"
145 " <outbound average='1024'/>"
147 (TC " qdisc del dev eth0 root\n"
148 TC " qdisc del dev eth0 ingress\n"
149 TC " qdisc add dev eth0 ingress\n"
150 TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
151 "police rate 1024kbps burst 1024kb mtu 64kb drop flowid :1\n"));
153 DO_TEST_SET(("<bandwidth>"
154 " <inbound average='1' peak='2' floor='3' burst='4'/>"
155 " <outbound average='5' peak='6' burst='7'/>"
157 (TC " qdisc del dev eth0 root\n"
158 TC " qdisc del dev eth0 ingress\n"
159 TC " qdisc add dev eth0 root handle 1: htb default 1\n"
160 TC " class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb\n"
161 TC " qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
162 TC " filter add dev eth0 parent 1:0 protocol all handle 1 fw flowid 1\n"
163 TC " qdisc add dev eth0 ingress\n"
164 TC " filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
165 "police rate 5kbps burst 7kb mtu 64kb drop flowid :1\n"));
170 VIRT_TEST_MAIN(mymain);