Add missing libxml2-tools dependency
[archive/platform/upstream/libvirt.git] / tests / virnetdevbandwidthtest.c
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
17  *
18  * Author: Michal Privoznik <mprivozn@redhat.com>
19  */
20
21 #include <config.h>
22
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"
28
29 #define VIR_FROM_THIS VIR_FROM_NONE
30
31 struct testMinimalStruct {
32     const char *expected_result;
33     const char *band1;
34     const char *band2;
35 };
36
37 struct testSetStruct {
38     const char *band;
39     const char *exp_cmd;
40     const char *iface;
41     const bool hierarchical_class;
42 };
43
44 #define PARSE(xml, var)                                                 \
45     do {                                                                \
46         xmlDocPtr doc;                                                  \
47         xmlXPathContextPtr ctxt = NULL;                                 \
48                                                                         \
49         if (!xml)                                                       \
50             break;                                                      \
51                                                                         \
52         if (!(doc = virXMLParseStringCtxt((xml),                        \
53                                           "bandwidth definition",       \
54                                           &ctxt)))                      \
55             goto cleanup;                                               \
56                                                                         \
57         (var) = virNetDevBandwidthParse(ctxt->node,                     \
58                                         VIR_DOMAIN_NET_TYPE_NETWORK);   \
59         xmlFreeDoc(doc);                                                \
60         xmlXPathFreeContext(ctxt);                                      \
61         if (!(var))                                                     \
62             goto cleanup;                                               \
63     } while (0)
64
65 static int
66 testVirNetDevBandwidthSet(const void *data)
67 {
68     int ret = -1;
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;
74
75     PARSE(info->band, band);
76
77     if (!iface)
78         iface = "eth0";
79
80     virCommandSetDryRun(&buf, NULL, NULL);
81
82     if (virNetDevBandwidthSet(iface, band, info->hierarchical_class) < 0)
83         goto cleanup;
84
85     if (!(actual_cmd = virBufferContentAndReset(&buf))) {
86         int err = virBufferError(&buf);
87         if (err) {
88             fprintf(stderr, "buffer's in error state: %d", err);
89             goto cleanup;
90         }
91         /* This is interesting, no command has been executed.
92          * Maybe that's expected, actually. */
93     }
94
95     if (STRNEQ_NULLABLE(info->exp_cmd, actual_cmd)) {
96         virtTestDifference(stderr,
97                            NULLSTR(info->exp_cmd),
98                            NULLSTR(actual_cmd));
99         goto cleanup;
100     }
101
102     ret = 0;
103  cleanup:
104     virCommandSetDryRun(NULL, NULL, NULL);
105     virNetDevBandwidthFree(band);
106     virBufferFreeAndReset(&buf);
107     VIR_FREE(actual_cmd);
108     return ret;
109 }
110
111 static int
112 mymain(void)
113 {
114     int ret = 0;
115
116 #define DO_TEST_SET(Band, Exp_cmd, ...)                     \
117     do {                                                    \
118         struct testSetStruct data = {.band = Band,          \
119                                      .exp_cmd = Exp_cmd,    \
120                                      __VA_ARGS__};          \
121         if (virtTestRun("virNetDevBandwidthSet",            \
122                         testVirNetDevBandwidthSet,          \
123                         &data) < 0)                         \
124             ret = -1;                                       \
125     } while (0)
126
127
128     DO_TEST_SET(NULL, NULL);
129
130     DO_TEST_SET(("<bandwidth/>"),
131                 (TC " qdisc del dev eth0 root\n"
132                  TC " qdisc del dev eth0 ingress\n"));
133
134     DO_TEST_SET(("<bandwidth>"
135                  "  <inbound average='1024'/>"
136                  "</bandwidth>"),
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"));
143
144     DO_TEST_SET(("<bandwidth>"
145                  "  <outbound average='1024'/>"
146                  "</bandwidth>"),
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"));
152
153     DO_TEST_SET(("<bandwidth>"
154                  "  <inbound average='1' peak='2' floor='3' burst='4'/>"
155                  "  <outbound average='5' peak='6' burst='7'/>"
156                  "</bandwidth>"),
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"));
166
167     return ret;
168 }
169
170 VIRT_TEST_MAIN(mymain);