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