render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From e1699c1bb0cbf20ea24db8607492a8f418ee8f95 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <e1699c1bb0cbf20ea24db8607492a8f418ee8f95@dist-git>
c480ed
From: Michal Privoznik <mprivozn@redhat.com>
c480ed
Date: Tue, 30 Jul 2019 15:30:52 +0200
c480ed
Subject: [PATCH] test: Introduce virnetdevopenvswitchtest
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Test if our parsing of interface stats as returned by ovs-vsctl
c480ed
works as expected. To achieve this without having to mock
c480ed
virCommand* I'm separating parsing of stats into a separate
c480ed
function.
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
(cherry picked from commit cc34260f5a8715d208ee45a6ebaa79e5264cbe68)
c480ed
c480ed
Conflicts:
c480ed
- src/libvirt_private.syms: Context, no
c480ed
    virNetDevOpenvswitchInterfaceGetMaster()
c480ed
- src/util/virnetdevopenvswitch.c: Unknown, conflict in a
c480ed
    comment, the diff looks the same to me.
c480ed
- tests/Makefile.am: Context, no virnetworkportxml2xmldata in
c480ed
    EXTRA_DIST
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1721434
c480ed
c480ed
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c480ed
Message-Id: <a46322e55761cff95f64c7ee023e95abed9a375a.1564493409.git.mprivozn@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/libvirt_private.syms                   |   1 +
c480ed
 src/util/virnetdevopenvswitch.c            |  93 ++++++++++++-------
c480ed
 src/util/virnetdevopenvswitch.h            |   4 +
c480ed
 tests/Makefile.am                          |  13 ++-
c480ed
 tests/virnetdevopenvswitchdata/stats1.json |   1 +
c480ed
 tests/virnetdevopenvswitchdata/stats2.json |   1 +
c480ed
 tests/virnetdevopenvswitchtest.c           | 101 +++++++++++++++++++++
c480ed
 7 files changed, 177 insertions(+), 37 deletions(-)
c480ed
 create mode 100644 tests/virnetdevopenvswitchdata/stats1.json
c480ed
 create mode 100644 tests/virnetdevopenvswitchdata/stats2.json
c480ed
 create mode 100644 tests/virnetdevopenvswitchtest.c
c480ed
c480ed
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
c480ed
index 8132f9664b..f4b54cee0b 100644
c480ed
--- a/src/libvirt_private.syms
c480ed
+++ b/src/libvirt_private.syms
c480ed
@@ -2422,6 +2422,7 @@ virNetDevMidonetUnbindPort;
c480ed
 virNetDevOpenvswitchAddPort;
c480ed
 virNetDevOpenvswitchGetMigrateData;
c480ed
 virNetDevOpenvswitchGetVhostuserIfname;
c480ed
+virNetDevOpenvswitchInterfaceParseStats;
c480ed
 virNetDevOpenvswitchInterfaceStats;
c480ed
 virNetDevOpenvswitchRemovePort;
c480ed
 virNetDevOpenvswitchSetMigrateData;
c480ed
diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
c480ed
index dbd832053b..ac8ec09b65 100644
c480ed
--- a/src/util/virnetdevopenvswitch.c
c480ed
+++ b/src/util/virnetdevopenvswitch.c
c480ed
@@ -306,49 +306,30 @@ int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
c480ed
     return 0;
c480ed
 }
c480ed
 
c480ed
+
c480ed
 /**
c480ed
- * virNetDevOpenvswitchInterfaceStats:
c480ed
- * @ifname: the name of the interface
c480ed
- * @stats: the retreived domain interface stat
c480ed
+ * virNetDevOpenvswitchInterfaceParseStats:
c480ed
+ * @json: Input string in JSON format
c480ed
+ * @stats: parsed stats
c480ed
  *
c480ed
- * Retrieves the OVS interfaces stats
c480ed
+ * For given input string @json parse interface statistics and store them into
c480ed
+ * @stats.
c480ed
  *
c480ed
- * Returns 0 in case of success or -1 in case of failure
c480ed
+ * Returns: 0 on success,
c480ed
+ *         -1 otherwise (with error reported).
c480ed
  */
c480ed
 int
c480ed
-virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
-                                   virDomainInterfaceStatsPtr stats)
c480ed
+virNetDevOpenvswitchInterfaceParseStats(const char *json,
c480ed
+                                        virDomainInterfaceStatsPtr stats)
c480ed
 {
c480ed
-    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
-    VIR_AUTOFREE(char *) output = NULL;
c480ed
     VIR_AUTOPTR(virJSONValue) jsonStats = NULL;
c480ed
     virJSONValuePtr jsonMap = NULL;
c480ed
     size_t i;
c480ed
 
c480ed
-    cmd = virCommandNew(OVSVSCTL);
c480ed
-    virNetDevOpenvswitchAddTimeout(cmd);
c480ed
-    virCommandAddArgList(cmd, "--if-exists", "--format=list", "--data=json",
c480ed
-                         "--no-headings", "--columns=statistics", "list",
c480ed
-                         "Interface", ifname, NULL);
c480ed
-    virCommandSetOutputBuffer(cmd, &output);
c480ed
+    stats->rx_bytes = stats->rx_packets = stats->rx_errs = stats->rx_drop = -1;
c480ed
+    stats->tx_bytes = stats->tx_packets = stats->tx_errs = stats->tx_drop = -1;
c480ed
 
c480ed
-    /* The above command returns either:
c480ed
-     * 1) empty string if @ifname doesn't exist, or
c480ed
-     * 2) a JSON array, for instance:
c480ed
-     *    ["map",[["collisions",0],["rx_bytes",0],["rx_crc_err",0],["rx_dropped",0],
c480ed
-     *    ["rx_errors",0],["rx_frame_err",0],["rx_over_err",0],["rx_packets",0],
c480ed
-     *    ["tx_bytes",12406],["tx_dropped",0],["tx_errors",0],["tx_packets",173]]]
c480ed
-     */
c480ed
-
c480ed
-    if (virCommandRun(cmd, NULL) < 0 ||
c480ed
-        STREQ_NULLABLE(output, "")) {
c480ed
-        /* no ovs-vsctl or interface 'ifname' doesn't exists in ovs */
c480ed
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
-                       _("Interface not found"));
c480ed
-        return -1;
c480ed
-    }
c480ed
-
c480ed
-    if (!(jsonStats = virJSONValueFromString(output)) ||
c480ed
+    if (!(jsonStats = virJSONValueFromString(json)) ||
c480ed
         !virJSONValueIsArray(jsonStats) ||
c480ed
         !(jsonMap = virJSONValueArrayGet(jsonStats, 1))) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
@@ -356,9 +337,6 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
         return -1;
c480ed
     }
c480ed
 
c480ed
-    stats->rx_bytes = stats->rx_packets = stats->rx_errs = stats->rx_drop = -1;
c480ed
-    stats->tx_bytes = stats->tx_packets = stats->tx_errs = stats->tx_drop = -1;
c480ed
-
c480ed
     for (i = 0; i < virJSONValueArraySize(jsonMap); i++) {
c480ed
         virJSONValuePtr item = virJSONValueArrayGet(jsonMap, i);
c480ed
         virJSONValuePtr jsonKey;
c480ed
@@ -399,6 +377,51 @@ virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
         }
c480ed
     }
c480ed
 
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+/**
c480ed
+ * virNetDevOpenvswitchInterfaceStats:
c480ed
+ * @ifname: the name of the interface
c480ed
+ * @stats: the retrieved domain interface stat
c480ed
+ *
c480ed
+ * Retrieves the OVS interfaces stats
c480ed
+ *
c480ed
+ * Returns 0 in case of success or -1 in case of failure
c480ed
+ */
c480ed
+int
c480ed
+virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
+                                   virDomainInterfaceStatsPtr stats)
c480ed
+{
c480ed
+    VIR_AUTOPTR(virCommand) cmd = NULL;
c480ed
+    VIR_AUTOFREE(char *) output = NULL;
c480ed
+
c480ed
+    cmd = virCommandNew(OVSVSCTL);
c480ed
+    virNetDevOpenvswitchAddTimeout(cmd);
c480ed
+    virCommandAddArgList(cmd, "--if-exists", "--format=list", "--data=json",
c480ed
+                         "--no-headings", "--columns=statistics", "list",
c480ed
+                         "Interface", ifname, NULL);
c480ed
+    virCommandSetOutputBuffer(cmd, &output);
c480ed
+
c480ed
+    /* The above command returns either:
c480ed
+     * 1) empty string if @ifname doesn't exist, or
c480ed
+     * 2) a JSON array, for instance:
c480ed
+     *    ["map",[["collisions",0],["rx_bytes",0],["rx_crc_err",0],["rx_dropped",0],
c480ed
+     *    ["rx_errors",0],["rx_frame_err",0],["rx_over_err",0],["rx_packets",0],
c480ed
+     *    ["tx_bytes",12406],["tx_dropped",0],["tx_errors",0],["tx_packets",173]]]
c480ed
+     */
c480ed
+
c480ed
+    if (virCommandRun(cmd, NULL) < 0 ||
c480ed
+        STREQ_NULLABLE(output, "")) {
c480ed
+        /* no ovs-vsctl or interface 'ifname' doesn't exists in ovs */
c480ed
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
c480ed
+                       _("Interface not found"));
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    if (virNetDevOpenvswitchInterfaceParseStats(output, stats) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
     if (stats->rx_bytes == -1 &&
c480ed
         stats->rx_packets == -1 &&
c480ed
         stats->rx_errs == -1 &&
c480ed
diff --git a/src/util/virnetdevopenvswitch.h b/src/util/virnetdevopenvswitch.h
c480ed
index 6f6e620c22..c1a211dec1 100644
c480ed
--- a/src/util/virnetdevopenvswitch.h
c480ed
+++ b/src/util/virnetdevopenvswitch.h
c480ed
@@ -53,6 +53,10 @@ int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
c480ed
 int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
c480ed
     ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
c480ed
 
c480ed
+int virNetDevOpenvswitchInterfaceParseStats(const char *json,
c480ed
+                                            virDomainInterfaceStatsPtr stats)
c480ed
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
c480ed
+
c480ed
 int virNetDevOpenvswitchInterfaceStats(const char *ifname,
c480ed
                                        virDomainInterfaceStatsPtr stats)
c480ed
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
c480ed
diff --git a/tests/Makefile.am b/tests/Makefile.am
c480ed
index c0337ea10c..201de1d37d 100644
c480ed
--- a/tests/Makefile.am
c480ed
+++ b/tests/Makefile.am
c480ed
@@ -157,6 +157,7 @@ EXTRA_DIST = \
c480ed
 	virmock.h \
c480ed
 	virnetdaemondata \
c480ed
 	virnetdevtestdata \
c480ed
+	virnetdevopenvswitchdata \
c480ed
 	virnwfilterbindingxml2xmldata \
c480ed
 	virpcitestdata \
c480ed
 	virscsidata \
c480ed
@@ -1230,9 +1231,17 @@ virmacmaptest_SOURCES = \
c480ed
 	virmacmaptest.c testutils.h testutils.c
c480ed
 virmacmaptest_LDADD = $(LDADDS)
c480ed
 
c480ed
-test_programs += virmacmaptest
c480ed
+virnetdevopenvswitchtest_SOURCES = \
c480ed
+	virnetdevopenvswitchtest.c testutils.h testutils.c
c480ed
+virnetdevopenvswitchtest_LDADD = $(LDADDS)
c480ed
+
c480ed
+test_programs += \
c480ed
+	virmacmaptest \
c480ed
+	virnetdevopenvswitchtest
c480ed
 else ! WITH_YAJL
c480ed
-EXTRA_DIST +=  virmacmaptest.c
c480ed
+EXTRA_DIST += \
c480ed
+	virmacmaptest.c \
c480ed
+	virnetdevopenvswitchtest.c
c480ed
 endif ! WITH_YAJL
c480ed
 
c480ed
 virnetdevtest_SOURCES = \
c480ed
diff --git a/tests/virnetdevopenvswitchdata/stats1.json b/tests/virnetdevopenvswitchdata/stats1.json
c480ed
new file mode 100644
c480ed
index 0000000000..1138c6271e
c480ed
--- /dev/null
c480ed
+++ b/tests/virnetdevopenvswitchdata/stats1.json
c480ed
@@ -0,0 +1 @@
c480ed
+["map",[["collisions",1],["rx_bytes",2],["rx_crc_err",3],["rx_dropped",4],["rx_errors",5],["rx_frame_err",6],["rx_over_err",7],["rx_packets",8],["tx_bytes",9],["tx_dropped",10],["tx_errors",11],["tx_packets",12]]]
c480ed
diff --git a/tests/virnetdevopenvswitchdata/stats2.json b/tests/virnetdevopenvswitchdata/stats2.json
c480ed
new file mode 100644
c480ed
index 0000000000..d84be7e011
c480ed
--- /dev/null
c480ed
+++ b/tests/virnetdevopenvswitchdata/stats2.json
c480ed
@@ -0,0 +1 @@
c480ed
+["map",[["collisions",0],["rx_bytes",0],["rx_crc_err",0],["rx_dropped",0],["rx_errors",0],["rx_frame_err",0],["rx_over_err",0],["rx_packets",0],["tx_bytes",12406],["tx_dropped",0],["tx_errors",0],["tx_packets",173]]]
c480ed
diff --git a/tests/virnetdevopenvswitchtest.c b/tests/virnetdevopenvswitchtest.c
c480ed
new file mode 100644
c480ed
index 0000000000..f01e77cbba
c480ed
--- /dev/null
c480ed
+++ b/tests/virnetdevopenvswitchtest.c
c480ed
@@ -0,0 +1,101 @@
c480ed
+/*
c480ed
+ * Copyright (C) 2019 Red Hat, Inc.
c480ed
+ *
c480ed
+ * This library is free software; you can redistribute it and/or
c480ed
+ * modify it under the terms of the GNU Lesser General Public
c480ed
+ * License as published by the Free Software Foundation; either
c480ed
+ * version 2.1 of the License, or (at your option) any later version.
c480ed
+ *
c480ed
+ * This library is distributed in the hope that it will be useful,
c480ed
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
c480ed
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c480ed
+ * Lesser General Public License for more details.
c480ed
+ *
c480ed
+ * You should have received a copy of the GNU Lesser General Public
c480ed
+ * License along with this library.  If not, see
c480ed
+ * <http://www.gnu.org/licenses/>.
c480ed
+ */
c480ed
+
c480ed
+#include <config.h>
c480ed
+
c480ed
+#include "testutils.h"
c480ed
+#include "virnetdevopenvswitch.h"
c480ed
+
c480ed
+#define VIR_FROM_THIS VIR_FROM_NONE
c480ed
+
c480ed
+typedef struct _InterfaceParseStatsData InterfaceParseStatsData;
c480ed
+struct _InterfaceParseStatsData {
c480ed
+    const char *filename;
c480ed
+    const virDomainInterfaceStatsStruct stats;
c480ed
+};
c480ed
+
c480ed
+
c480ed
+static int
c480ed
+testInterfaceParseStats(const void *opaque)
c480ed
+{
c480ed
+    const InterfaceParseStatsData *data = opaque;
c480ed
+    VIR_AUTOFREE(char *) filename = NULL;
c480ed
+    VIR_AUTOFREE(char *) buf = NULL;
c480ed
+    virDomainInterfaceStatsStruct actual;
c480ed
+
c480ed
+    if (virAsprintf(&filename, "%s/virnetdevopenvswitchdata/%s",
c480ed
+                    abs_srcdir, data->filename) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    if (virFileReadAll(filename, 1024, &buf) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    if (virNetDevOpenvswitchInterfaceParseStats(buf, &actual) < 0)
c480ed
+        return -1;
c480ed
+
c480ed
+    if (memcmp(&actual, &data->stats, sizeof(actual)) != 0) {
c480ed
+        fprintf(stderr,
c480ed
+                "Expected stats: %lld %lld %lld %lld %lld %lld %lld %lld\n"
c480ed
+                "Actual stats: %lld %lld %lld %lld %lld %lld %lld %lld",
c480ed
+                data->stats.rx_bytes,
c480ed
+                data->stats.rx_packets,
c480ed
+                data->stats.rx_errs,
c480ed
+                data->stats.rx_drop,
c480ed
+                data->stats.tx_bytes,
c480ed
+                data->stats.tx_packets,
c480ed
+                data->stats.tx_errs,
c480ed
+                data->stats.tx_drop,
c480ed
+                actual.rx_bytes,
c480ed
+                actual.rx_packets,
c480ed
+                actual.rx_errs,
c480ed
+                actual.rx_drop,
c480ed
+                actual.tx_bytes,
c480ed
+                actual.tx_packets,
c480ed
+                actual.tx_errs,
c480ed
+                actual.tx_drop);
c480ed
+
c480ed
+        return -1;
c480ed
+    }
c480ed
+
c480ed
+    return 0;
c480ed
+}
c480ed
+
c480ed
+
c480ed
+static int
c480ed
+mymain(void)
c480ed
+{
c480ed
+    int ret = 0;
c480ed
+
c480ed
+#define TEST_INTERFACE_STATS(file, \
c480ed
+                             rxBytes, rxPackets, rxErrs, rxDrop, \
c480ed
+                             txBytes, txPackets, txErrs, txDrop) \
c480ed
+    do { \
c480ed
+        const InterfaceParseStatsData data = {.filename = file, .stats = { \
c480ed
+                             rxBytes, rxPackets, rxErrs, rxDrop, \
c480ed
+                             txBytes, txPackets, txErrs, txDrop}}; \
c480ed
+        if (virTestRun("Interface stats " file, testInterfaceParseStats, &data) < 0) \
c480ed
+            ret = -1; \
c480ed
+    } while (0)
c480ed
+
c480ed
+    TEST_INTERFACE_STATS("stats1.json", 9, 12, 11, 10, 2, 8, 5, 4);
c480ed
+    TEST_INTERFACE_STATS("stats2.json", 12406, 173, 0, 0, 0, 0, 0, 0);
c480ed
+
c480ed
+    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
c480ed
+}
c480ed
+
c480ed
+VIR_TEST_MAIN(mymain);
c480ed
-- 
c480ed
2.22.0
c480ed