[PATCH v2 5/7] sysinfotest: Move from custom dmidecode scripts to virCommandSetDryRun()

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

 



Problem with custom dmidecode scripts is that they are hard to
modify, especially if we will want them to act differently based
on passed arguments. So far, we have two scripts which do no more
than 'cat $sysinfo' where $sysinfo is saved dmidecode output.

The virCommandSetDryRun() can be used to trick
virSysinfoReadDMI() thinking it executed real dmidecode.

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 src/util/virsysinfo.c                         |  3 +-
 .../sysinfodata/aarch64-gigabytedmidecode.sh  |  3 --
 tests/sysinfodata/x86dmidecode.sh             |  3 --
 tests/sysinfotest.c                           | 51 +++++++++++++------
 4 files changed, 38 insertions(+), 22 deletions(-)
 delete mode 100755 tests/sysinfodata/aarch64-gigabytedmidecode.sh
 delete mode 100755 tests/sysinfodata/x86dmidecode.sh

diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
index 0f1210ab37..0bf80c339b 100644
--- a/src/util/virsysinfo.c
+++ b/src/util/virsysinfo.c
@@ -60,7 +60,8 @@ virSysinfoSetup(const char *dmidecode,
                 const char *sysinfo,
                 const char *cpuinfo)
 {
-    sysinfoDmidecode = dmidecode;
+    if (dmidecode)
+        sysinfoDmidecode = dmidecode;
     sysinfoSysinfo = sysinfo;
     sysinfoCpuinfo = cpuinfo;
 }
diff --git a/tests/sysinfodata/aarch64-gigabytedmidecode.sh b/tests/sysinfodata/aarch64-gigabytedmidecode.sh
deleted file mode 100755
index 202918103c..0000000000
--- a/tests/sysinfodata/aarch64-gigabytedmidecode.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-DATAFILE=`dirname $0`/aarch64-gigabytesysinfo.data
-cat $DATAFILE
diff --git a/tests/sysinfodata/x86dmidecode.sh b/tests/sysinfodata/x86dmidecode.sh
deleted file mode 100755
index 28aed61459..0000000000
--- a/tests/sysinfodata/x86dmidecode.sh
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/sh
-DATAFILE=`dirname $0`/x86sysinfo.data
-cat $DATAFILE
diff --git a/tests/sysinfotest.c b/tests/sysinfotest.c
index 558dd60294..f080569730 100644
--- a/tests/sysinfotest.c
+++ b/tests/sysinfotest.c
@@ -34,14 +34,39 @@
 #define LIBVIRT_VIRSYSINFOPRIV_H_ALLOW
 #include "virsysinfopriv.h"
 
+#define LIBVIRT_VIRCOMMANDPRIV_H_ALLOW
+#include "vircommandpriv.h"
+
 #define VIR_FROM_THIS VIR_FROM_NONE
 
 struct testSysinfoData {
     const char *name; /* test name, also base name for result files */
     virSysinfoDefPtr (*func)(void); /* sysinfo gathering function */
-    const char *decoder; /* name of dmi decoder binary/script */
 };
 
+
+static void
+testDMIDecodeDryRun(const char *const*args G_GNUC_UNUSED,
+                    const char *const*env G_GNUC_UNUSED,
+                    const char *input G_GNUC_UNUSED,
+                    char **output,
+                    char **error,
+                    int *status,
+                    void *opaque)
+{
+    const char *sysinfo = opaque;
+
+    if (virFileReadAll(sysinfo, 10 * 1024 * 1024, output) < 0) {
+        *error = g_strdup(virGetLastErrorMessage());
+        *status = EXIT_FAILURE;
+        return;
+    }
+
+    *error = g_strdup("");
+    *status = 0;
+}
+
+
 static int
 testSysinfo(const void *data)
 {
@@ -52,18 +77,19 @@ testSysinfo(const void *data)
     g_autofree char *sysinfo = NULL;
     g_autofree char *cpuinfo = NULL;
     g_autofree char *expected = NULL;
-    g_autofree char *decoder = NULL;
 
     sysinfo = g_strdup_printf("%s/sysinfodata/%ssysinfo.data", abs_srcdir, testdata->name);
     cpuinfo = g_strdup_printf("%s/sysinfodata/%scpuinfo.data", abs_srcdir, testdata->name);
     expected = g_strdup_printf("%s/sysinfodata/%ssysinfo.expect", abs_srcdir, testdata->name);
 
-    if (testdata->decoder)
-        decoder = g_strdup_printf("%s/%s", abs_srcdir, testdata->decoder);
+    virCommandSetDryRun(NULL, testDMIDecodeDryRun, sysinfo);
 
-    virSysinfoSetup(decoder, sysinfo, cpuinfo);
+    virSysinfoSetup(NULL, sysinfo, cpuinfo);
 
-    if (!(ret = testdata->func()))
+    ret = testdata->func();
+    virCommandSetDryRun(NULL, NULL, NULL);
+
+    if (!ret)
         return -1;
 
     if (virSysinfoFormat(&buf, ret) < 0)
@@ -76,17 +102,13 @@ testSysinfo(const void *data)
 }
 
 
-#define TEST_FULL(name, func, decoder) \
+#define TEST(name, func) \
     do { \
-        struct testSysinfoData data = { name, func, decoder }; \
+        struct testSysinfoData data = { name, func }; \
         if (virTestRun(name " sysinfo", testSysinfo, &data) < 0) \
             ret = EXIT_FAILURE; \
     } while (0)
 
-
-#define TEST(name, func) \
-        TEST_FULL(name, func, NULL)
-
 static int
 mymain(void)
 {
@@ -95,13 +117,12 @@ mymain(void)
     TEST("s390", virSysinfoReadS390);
     TEST("s390-freq", virSysinfoReadS390);
     TEST("ppc", virSysinfoReadPPC);
-    TEST_FULL("x86", virSysinfoReadDMI, "/sysinfodata/x86dmidecode.sh");
+    TEST("x86", virSysinfoReadDMI);
     TEST("arm", virSysinfoReadARM);
     TEST("arm-rpi2", virSysinfoReadARM);
     TEST("aarch64", virSysinfoReadARM);
     TEST("aarch64-moonshot", virSysinfoReadARM);
-    TEST_FULL("aarch64-gigabyte", virSysinfoReadARM,
-              "/sysinfodata/aarch64-gigabytedmidecode.sh");
+    TEST("aarch64-gigabyte", virSysinfoReadARM);
 
     return ret;
 }
-- 
2.26.2




[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]

  Powered by Linux