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