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/>.
21 #include "testutils.h"
22 #define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
23 #include "vircommandpriv.h"
24 #include "virnetdevbandwidth.h"
25 #include "netdev_bandwidth_conf.c"
27 #define VIR_FROM_THIS VIR_FROM_NONE
29 struct testMinimalStruct
{
30 const char *expected_result
;
35 struct testSetStruct
{
39 const bool hierarchical_class
;
42 #define PARSE(xml, var) \
46 xmlXPathContextPtr ctxt = NULL; \
51 if (!(doc = virXMLParseStringCtxt((xml), \
52 "bandwidth definition", \
56 rc = virNetDevBandwidthParse(&(var), \
61 xmlXPathFreeContext(ctxt); \
67 testVirNetDevBandwidthSet(const void *data
)
70 const struct testSetStruct
*info
= data
;
71 const char *iface
= info
->iface
;
72 virNetDevBandwidthPtr band
= NULL
;
73 virBuffer buf
= VIR_BUFFER_INITIALIZER
;
74 char *actual_cmd
= NULL
;
76 PARSE(info
->band
, band
);
81 virCommandSetDryRun(&buf
, NULL
, NULL
);
83 if (virNetDevBandwidthSet(iface
, band
, info
->hierarchical_class
, true) < 0)
86 if (!(actual_cmd
= virBufferContentAndReset(&buf
))) {
87 int err
= virBufferError(&buf
);
89 fprintf(stderr
, "buffer's in error state: %d", err
);
92 /* This is interesting, no command has been executed.
93 * Maybe that's expected, actually. */
96 if (STRNEQ_NULLABLE(info
->exp_cmd
, actual_cmd
)) {
97 virTestDifference(stderr
,
98 NULLSTR(info
->exp_cmd
),
105 virCommandSetDryRun(NULL
, NULL
, NULL
);
106 virNetDevBandwidthFree(band
);
107 virBufferFreeAndReset(&buf
);
108 VIR_FREE(actual_cmd
);
117 #define DO_TEST_SET(Band, Exp_cmd, ...) \
119 struct testSetStruct data = {.band = Band, \
120 .exp_cmd = Exp_cmd, \
122 if (virTestRun("virNetDevBandwidthSet", \
123 testVirNetDevBandwidthSet, \
129 DO_TEST_SET(NULL
, NULL
);
131 DO_TEST_SET("<bandwidth/>", NULL
);
133 DO_TEST_SET(("<bandwidth>"
134 " <inbound average='1024'/>"
136 (TC
" qdisc del dev eth0 root\n"
137 TC
" qdisc del dev eth0 ingress\n"
138 TC
" qdisc add dev eth0 root handle 1: htb default 1\n"
139 TC
" class add dev eth0 parent 1: classid 1:1 htb rate 1024kbps quantum 87\n"
140 TC
" qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
141 TC
" filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1\n"));
143 DO_TEST_SET(("<bandwidth>"
144 " <outbound average='1024'/>"
146 (TC
" qdisc del dev eth0 root\n"
147 TC
" qdisc del dev eth0 ingress\n"
148 TC
" qdisc add dev eth0 ingress\n"
149 TC
" filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
150 "police rate 1024kbps burst 1024kb mtu 64kb drop flowid :1\n"));
152 DO_TEST_SET(("<bandwidth>"
153 " <inbound average='1' peak='2' floor='3' burst='4'/>"
154 " <outbound average='5' peak='6' burst='7'/>"
156 (TC
" qdisc del dev eth0 root\n"
157 TC
" qdisc del dev eth0 ingress\n"
158 TC
" qdisc add dev eth0 root handle 1: htb default 1\n"
159 TC
" class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb quantum 1\n"
160 TC
" qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
161 TC
" filter add dev eth0 parent 1:0 protocol all prio 1 handle 1 fw flowid 1\n"
162 TC
" qdisc add dev eth0 ingress\n"
163 TC
" filter add dev eth0 parent ffff: protocol all u32 match u32 0 0 "
164 "police rate 5kbps burst 7kb mtu 64kb drop flowid :1\n"));
169 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/virnetdevbandwidthmock.so")