render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c401cc
From 94bf0b8fd2a5fbda430476b8cea2246df0fbc3ed Mon Sep 17 00:00:00 2001
c401cc
Message-Id: <94bf0b8fd2a5fbda430476b8cea2246df0fbc3ed.1391615407.git.jdenemar@redhat.com>
c401cc
From: John Ferlan <jferlan@redhat.com>
c401cc
Date: Tue, 4 Feb 2014 13:34:54 -0500
c401cc
Subject: [PATCH] tests: Add test for new virkmod functions
c401cc
c401cc
https://bugzilla.redhat.com/show_bug.cgi?id=1045124
c401cc
c401cc
Adding tests for new virKMod{Config|Load|Unload}() API's.
c401cc
c401cc
A test for virKModIsBlacklisted() would require some setup which cannot
c401cc
be assumed.
c401cc
c401cc
(cherry picked from commit 02bf6568f4aaa595336f70d697b65e7c4f1c37a7)
c401cc
c401cc
Conflicts:
c401cc
	tests/Makefile.am
c401cc
c401cc
 * Resolved conflict by removing the virnetdevbandwidthtest references
c401cc
 * Also needed to add "1" as second parameter to virtTestRun() in order
c401cc
   to get things to complile/check
c401cc
c401cc
Signed-off-by: John Ferlan <jferlan@redhat.com>
c401cc
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c401cc
---
c401cc
 .gitignore          |   1 +
c401cc
 tests/Makefile.am   |   5 ++
c401cc
 tests/virkmodtest.c | 183 ++++++++++++++++++++++++++++++++++++++++++++++++++++
c401cc
 3 files changed, 189 insertions(+)
c401cc
 create mode 100644 tests/virkmodtest.c
c401cc
c401cc
diff --git a/tests/Makefile.am b/tests/Makefile.am
c401cc
index 4141c47..2ec8ffb 100644
c401cc
--- a/tests/Makefile.am
c401cc
+++ b/tests/Makefile.am
c401cc
@@ -133,6 +133,7 @@ test_programs = virshtest sockettest \
c401cc
 	virstoragetest \
c401cc
         fdstreamtest \
c401cc
         fchosttest \
c401cc
+	virkmodtest \
c401cc
 	$(NULL)
c401cc
 
c401cc
 if WITH_DBUS
c401cc
@@ -580,6 +581,10 @@ commandhelper_SOURCES = \
c401cc
 commandhelper_LDADD = $(LDADDS)
c401cc
 commandhelper_LDFLAGS = -static
c401cc
 
c401cc
+virkmodtest_SOURCES = \
c401cc
+	virkmodtest.c testutils.h testutils.c
c401cc
+virkmodtest_LDADD = $(LDADDS)
c401cc
+
c401cc
 if WITH_LIBVIRTD
c401cc
 libvirtdconftest_SOURCES = \
c401cc
 	libvirtdconftest.c testutils.h testutils.c \
c401cc
diff --git a/tests/virkmodtest.c b/tests/virkmodtest.c
c401cc
new file mode 100644
c401cc
index 0000000..6569468
c401cc
--- /dev/null
c401cc
+++ b/tests/virkmodtest.c
c401cc
@@ -0,0 +1,183 @@
c401cc
+/*
c401cc
+ * Copyright (C) 2014 Red Hat, Inc.
c401cc
+ *
c401cc
+ * This library is free software; you can redistribute it and/or
c401cc
+ * modify it under the terms of the GNU Lesser General Public
c401cc
+ * License as published by the Free Software Foundation; either
c401cc
+ * version 2.1 of the License, or (at your option) any later version.
c401cc
+ *
c401cc
+ * This library is distributed in the hope that it will be useful,
c401cc
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
c401cc
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c401cc
+ * Lesser General Public License for more details.
c401cc
+ *
c401cc
+ * You should have received a copy of the GNU Lesser General Public
c401cc
+ * License along with this library.  If not, see
c401cc
+ * <http://www.gnu.org/licenses/>.
c401cc
+ */
c401cc
+
c401cc
+#include <config.h>
c401cc
+
c401cc
+#include "testutils.h"
c401cc
+
c401cc
+#ifdef __linux__
c401cc
+
c401cc
+# include <stdlib.h>
c401cc
+# include "vircommand.h"
c401cc
+# include "virkmod.h"
c401cc
+# include "virstring.h"
c401cc
+
c401cc
+struct testInfo {
c401cc
+    const char *module;
c401cc
+    const char *exp_cmd;
c401cc
+    bool useBlacklist;
c401cc
+};
c401cc
+
c401cc
+# define VIR_FROM_THIS VIR_FROM_NONE
c401cc
+
c401cc
+static int
c401cc
+testKModConfig(const void *args ATTRIBUTE_UNUSED)
c401cc
+{
c401cc
+    int ret = -1;
c401cc
+    char *outbuf = NULL;
c401cc
+
c401cc
+    /* This will return the contents of a 'modprobe -c' which can differ
c401cc
+     * from machine to machine - be happy that we get something.
c401cc
+     */
c401cc
+    outbuf = virKModConfig();
c401cc
+    if (!outbuf) {
c401cc
+        fprintf(stderr, "Failed to get config\n");
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+    ret = 0;
c401cc
+
c401cc
+cleanup:
c401cc
+    VIR_FREE(outbuf);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
+checkOutput(virBufferPtr buf, const char *exp_cmd)
c401cc
+{
c401cc
+    int ret = -1;
c401cc
+    char *actual_cmd = NULL;
c401cc
+
c401cc
+    if (!(actual_cmd = virBufferContentAndReset(buf))) {
c401cc
+        int err = virBufferError(buf);
c401cc
+        if (err)
c401cc
+            fprintf(stderr, "buffer's in error state: %d", err);
c401cc
+        else
c401cc
+            fprintf(stderr, "cannot compare buffer to exp: %s", exp_cmd);
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
+    if (STRNEQ(exp_cmd, actual_cmd)) {
c401cc
+        virtTestDifference(stderr, exp_cmd, actual_cmd);
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
+    ret = 0;
c401cc
+
c401cc
+cleanup:
c401cc
+    VIR_FREE(actual_cmd);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
+testKModLoad(const void *args)
c401cc
+{
c401cc
+    int ret = -1;
c401cc
+    char *errbuf = NULL;
c401cc
+    const struct testInfo *info = args;
c401cc
+    const char *module = info->module;
c401cc
+    bool useBlacklist = info->useBlacklist;
c401cc
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
c401cc
+
c401cc
+    virCommandSetDryRun(&buf;;
c401cc
+
c401cc
+    errbuf = virKModLoad(module, useBlacklist);
c401cc
+    if (errbuf) {
c401cc
+        fprintf(stderr, "Failed to load, error: %s\n", errbuf);
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
+    if (checkOutput(&buf, info->exp_cmd) < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    ret = 0;
c401cc
+
c401cc
+cleanup:
c401cc
+    virCommandSetDryRun(NULL);
c401cc
+    VIR_FREE(errbuf);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
+testKModUnload(const void *args)
c401cc
+{
c401cc
+    int ret = -1;
c401cc
+    char *errbuf = NULL;
c401cc
+    const struct testInfo *info = args;
c401cc
+    const char *module = info->module;
c401cc
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
c401cc
+
c401cc
+    virCommandSetDryRun(&buf;;
c401cc
+
c401cc
+    errbuf = virKModUnload(module);
c401cc
+    if (errbuf) {
c401cc
+        fprintf(stderr, "Failed to unload, error: %s\n", errbuf);
c401cc
+        goto cleanup;
c401cc
+    }
c401cc
+
c401cc
+    if (checkOutput(&buf, info->exp_cmd) < 0)
c401cc
+        goto cleanup;
c401cc
+
c401cc
+    ret = 0;
c401cc
+
c401cc
+cleanup:
c401cc
+    virCommandSetDryRun(NULL);
c401cc
+    VIR_FREE(errbuf);
c401cc
+    return ret;
c401cc
+}
c401cc
+
c401cc
+
c401cc
+static int
c401cc
+mymain(void)
c401cc
+{
c401cc
+    int ret = 0;
c401cc
+
c401cc
+    if (virtTestRun("config", 1, testKModConfig, NULL) < 0)
c401cc
+        ret = -1;
c401cc
+
c401cc
+    /* Although we cannot run the command on the host, we can compare
c401cc
+     * the output of the created command against what we'd expect to be
c401cc
+     * created. So let's at least do that.
c401cc
+     */
c401cc
+# define DO_TEST(_name, _cb, _blkflag, _exp_cmd)              \
c401cc
+    do {                                                      \
c401cc
+        struct testInfo data = {.module = "vfio-pci",         \
c401cc
+                                .exp_cmd = _exp_cmd,          \
c401cc
+                                .useBlacklist = _blkflag};    \
c401cc
+        if (virtTestRun(_name, 1, _cb,  &data) < 0)              \
c401cc
+            ret = -1;                                         \
c401cc
+    } while (0)
c401cc
+
c401cc
+    DO_TEST("load", testKModLoad, false, MODPROBE " vfio-pci\n");
c401cc
+    DO_TEST("unload", testKModUnload, false, RMMOD " vfio-pci\n");
c401cc
+    DO_TEST("blklist", testKModLoad, true, MODPROBE " -b vfio-pci\n");
c401cc
+
c401cc
+    return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
c401cc
+
c401cc
+}
c401cc
+
c401cc
+VIRT_TEST_MAIN(mymain);
c401cc
+#else
c401cc
+int
c401cc
+main(void)
c401cc
+{
c401cc
+    return EXIT_AM_SKIP;
c401cc
+}
c401cc
+#endif
c401cc
-- 
c401cc
1.8.5.3
c401cc