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