backup: Wire up qemu full pull backup commands over QMP
[libvirt/ericb.git] / tests / virnetdevbandwidthtest.c
blob2c0b6a6713c3f090cf75a4751b4670420cc448ca
1 /*
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/>.
19 #include <config.h>
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;
31 const char *band1;
32 const char *band2;
35 struct testSetStruct {
36 const char *band;
37 const char *exp_cmd;
38 const char *iface;
39 const bool hierarchical_class;
42 #define PARSE(xml, var) \
43 do { \
44 int rc; \
45 xmlDocPtr doc; \
46 xmlXPathContextPtr ctxt = NULL; \
48 if (!xml) \
49 break; \
51 if (!(doc = virXMLParseStringCtxt((xml), \
52 "bandwidth definition", \
53 &ctxt))) \
54 goto cleanup; \
56 rc = virNetDevBandwidthParse(&(var), \
57 NULL, \
58 ctxt->node, \
59 true); \
60 xmlFreeDoc(doc); \
61 xmlXPathFreeContext(ctxt); \
62 if (rc < 0) \
63 goto cleanup; \
64 } while (0)
66 static int
67 testVirNetDevBandwidthSet(const void *data)
69 int ret = -1;
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);
78 if (!iface)
79 iface = "eth0";
81 virCommandSetDryRun(&buf, NULL, NULL);
83 if (virNetDevBandwidthSet(iface, band, info->hierarchical_class, true) < 0)
84 goto cleanup;
86 if (!(actual_cmd = virBufferContentAndReset(&buf))) {
87 int err = virBufferError(&buf);
88 if (err) {
89 fprintf(stderr, "buffer's in error state: %d", err);
90 goto cleanup;
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),
99 NULLSTR(actual_cmd));
100 goto cleanup;
103 ret = 0;
104 cleanup:
105 virCommandSetDryRun(NULL, NULL, NULL);
106 virNetDevBandwidthFree(band);
107 virBufferFreeAndReset(&buf);
108 VIR_FREE(actual_cmd);
109 return ret;
112 static int
113 mymain(void)
115 int ret = 0;
117 #define DO_TEST_SET(Band, Exp_cmd, ...) \
118 do { \
119 struct testSetStruct data = {.band = Band, \
120 .exp_cmd = Exp_cmd, \
121 __VA_ARGS__}; \
122 if (virTestRun("virNetDevBandwidthSet", \
123 testVirNetDevBandwidthSet, \
124 &data) < 0) \
125 ret = -1; \
126 } while (0)
129 DO_TEST_SET(NULL, NULL);
131 DO_TEST_SET("<bandwidth/>", NULL);
133 DO_TEST_SET(("<bandwidth>"
134 " <inbound average='1024'/>"
135 "</bandwidth>"),
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'/>"
145 "</bandwidth>"),
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'/>"
155 "</bandwidth>"),
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"));
166 return ret;
169 VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virnetdevbandwidthmock.so")