[PATCH v2 3/3] virnetdevbandwidthtest: Introduce testVirNetDevBandwidthSet

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The test tries to set some QoS limits and check if the commands
that are actually executed are the expected ones.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tests/virnetdevbandwidthtest.c | 98 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)

diff --git a/tests/virnetdevbandwidthtest.c b/tests/virnetdevbandwidthtest.c
index 989018e..a8156cd 100644
--- a/tests/virnetdevbandwidthtest.c
+++ b/tests/virnetdevbandwidthtest.c
@@ -20,7 +20,10 @@
 
 #include <config.h>
 
+#include <fcntl.h>
+#include <stdlib.h>
 #include "testutils.h"
+#include "vircommand.h"
 #include "virnetdevbandwidth.h"
 #include "netdev_bandwidth_conf.c"
 
@@ -32,6 +35,14 @@ struct testMinimalStruct {
     const char *band2;
 };
 
+struct testSetStruct {
+    const char *outfile;
+    const char *band;
+    const char *exp_cmd;
+    const char *iface;
+    const bool hierarchical_class;
+};
+
 #define PARSE(xml, var)                                                 \
     do {                                                                \
         xmlDocPtr doc;                                                  \
@@ -104,9 +115,64 @@ cleanup:
 }
 
 static int
+testVirNetDevBandwidthSet(const void *data)
+{
+    int ret = -1;
+    const struct testSetStruct *info = data;
+    const char *iface = info->iface;
+    virNetDevBandwidthPtr band = NULL;
+    char *actual_cmd = NULL;
+
+    PARSE(info->band, band);
+
+    if (!iface)
+        iface = "eth0";
+
+    /* Clear the temporary file between runs */
+    if (truncate(info->outfile, 0) < 0) {
+        fprintf(stderr, "Unable to truncate %s %d\n", info->outfile, errno);
+        goto cleanup;
+    }
+
+    virCommandSetDryRun(info->outfile);
+
+    if (virNetDevBandwidthSet(iface, band, info->hierarchical_class) < 0)
+        goto cleanup;
+
+    if (virFileReadAll(info->outfile, 2048, &actual_cmd) < 0)
+        goto cleanup;
+
+    if (STRNEQ(info->exp_cmd, actual_cmd)) {
+        virtTestDifference(stderr, info->exp_cmd, actual_cmd);
+        goto cleanup;
+    }
+
+    ret = 0;
+cleanup:
+    virNetDevBandwidthFree(band);
+    VIR_FREE(actual_cmd);
+    return ret;
+}
+
+
+#define OUTFILETEMPLATE abs_builddir "/virnetdevbandwidthtest-XXXXXX"
+
+static int
 mymain(void)
 {
     int ret = 0;
+    int outfile_fd = -1;
+    char *outfile;
+
+    if (VIR_STRDUP_QUIET(outfile, OUTFILETEMPLATE) < 0) {
+        fprintf(stderr, "Out of memory\n");
+        abort();
+    }
+
+    if ((outfile_fd = mkostemp(outfile, O_CLOEXEC)) < 0) {
+        fprintf(stderr, "Cannot create outfile");
+        abort();
+    }
 
 #define DO_TEST_MINIMAL(r, ...)                             \
     do {                                                    \
@@ -117,6 +183,18 @@ mymain(void)
             ret = -1;                                       \
     } while (0)
 
+#define DO_TEST_SET(Band, Exp_cmd, ...)                     \
+    do {                                                    \
+        struct testSetStruct data = {.outfile = outfile,    \
+                                     .band = Band,          \
+                                     .exp_cmd = Exp_cmd,    \
+                                     __VA_ARGS__};          \
+        if (virtTestRun("virNetDevBandwidthSet",            \
+                        testVirNetDevBandwidthSet,          \
+                        &data) < 0)                         \
+            ret = -1;                                       \
+    } while (0)
+
 
     DO_TEST_MINIMAL(NULL, NULL, NULL);
 
@@ -149,6 +227,26 @@ mymain(void)
                     "  <inbound average='1' peak='2' floor='3'/>"
                     "  <outbound average='5' peak='6'/>"
                     "</bandwidth>");
+
+    DO_TEST_SET(("<bandwidth>"
+                 "  <inbound average='1' peak='2' floor='3' burst='4'/>"
+                 "  <outbound average='5' peak='6' burst='7'/>"
+                 "</bandwidth>"),
+                ("/sbin/tc qdisc del dev eth0 root\n"
+                 "/sbin/tc qdisc del dev eth0 ingress\n"
+                 "/sbin/tc qdisc add dev eth0 root handle 1: htb default 1\n"
+                 "/sbin/tc class add dev eth0 parent 1: classid 1:1 htb rate 1kbps ceil 2kbps burst 4kb\n"
+                 "/sbin/tc qdisc add dev eth0 parent 1:1 handle 2: sfq perturb 10\n"
+                 "/sbin/tc filter add dev eth0 parent 1:0 protocol ip handle 1 fw flowid 1\n"
+                 "/sbin/tc qdisc add dev eth0 ingress\n"
+                 "/sbin/tc filter add dev eth0 parent ffff: protocol ip u32 match ip src 0.0.0.0/0 "
+                 "police rate 5kbps burst 7kb mtu 64kb drop flowid :1\n"));
+
+    VIR_FORCE_CLOSE(outfile_fd);
+
+    if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
+        unlink(outfile);
+
     return ret;
 }
 
-- 
1.8.5.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]