[PATCH v2 08/19] tests: Introduce virudevtest

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

 



Now that we have the virudev module somewhat working, lets
introduce some testing of it.

We also need mock for virRandomBits function. Without it, the
order in which entries occur in the hash table would be random
and thus test would fail some times (as we expect certain
ordering).

Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx>
---
 tests/Makefile.am                         |  12 +++
 tests/virudevmock.c                       |  29 +++++++
 tests/virudevtest.c                       | 124 ++++++++++++++++++++++++++++++
 tests/virudevtestdata/complex.json        |  30 ++++++++
 tests/virudevtestdata/empty.json          |   5 ++
 tests/virudevtestdata/simple-dac.json     |  13 ++++
 tests/virudevtestdata/simple-selinux.json |  13 ++++
 7 files changed, 226 insertions(+)
 create mode 100644 tests/virudevmock.c
 create mode 100644 tests/virudevtest.c
 create mode 100644 tests/virudevtestdata/complex.json
 create mode 100644 tests/virudevtestdata/empty.json
 create mode 100644 tests/virudevtestdata/simple-dac.json
 create mode 100644 tests/virudevtestdata/simple-selinux.json

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 924029a..824796b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -150,6 +150,7 @@ EXTRA_DIST =		\
 	virpcitestdata \
 	virscsidata \
 	virsh-uriprecedence \
+	virudevtestdata \
 	virusbtestdata \
 	vmwareverdata \
 	vmx2xmldata \
@@ -193,6 +194,7 @@ test_programs = virshtest sockettest \
 	vircaps2xmltest \
 	virnetdevtest \
 	virtypedparamtest \
+	virudevtest \
 	$(NULL)
 
 if WITH_REMOTE
@@ -406,6 +408,7 @@ test_libraries = libshunload.la \
 		virhostcpumock.la \
 		nssmock.la \
 		domaincapsmock.la \
+		virudevmock.la \
 		$(NULL)
 if WITH_QEMU
 test_libraries += libqemumonitortestutils.la \
@@ -1343,6 +1346,15 @@ virtypedparamtest_SOURCES = \
 	virtypedparamtest.c testutils.h testutils.c
 virtypedparamtest_LDADD = $(LDADDS)
 
+virudevtest_SOURCES = \
+	virudevtest.c testutils.h testutils.c
+virudevtest_LDADD = $(LDADDS)
+
+virudevmock_la_SOURCES = virudevmock.c
+virudevmock_la_CFLAGS = $(AM_CFLAGS)
+virudevmock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
+virudevmock_la_LIBADD = $(MOCKLIBS_LIBS)
+
 
 if WITH_LINUX
 fchosttest_SOURCES = \
diff --git a/tests/virudevmock.c b/tests/virudevmock.c
new file mode 100644
index 0000000..d01f1c9
--- /dev/null
+++ b/tests/virudevmock.c
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Michal Privoznik <mprivozn@xxxxxxxxxx>
+ */
+
+#include <config.h>
+
+#include "virrandom.h"
+
+uint64_t virRandomBits(int nbits ATTRIBUTE_UNUSED)
+{
+    return 4; /* chosen by fair dice roll.
+                 guaranteed to be random. */
+}
diff --git a/tests/virudevtest.c b/tests/virudevtest.c
new file mode 100644
index 0000000..883d751
--- /dev/null
+++ b/tests/virudevtest.c
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Author: Michal Privoznik <mprivozn@xxxxxxxxxx>
+ */
+
+#include <config.h>
+
+#include "testutils.h"
+#include "virudev.h"
+#include "virjson.h"
+
+#define VIR_FROM_THIS VIR_FROM_NONE
+
+struct testUdevData {
+    const char *file;
+    const char *const *labels;
+};
+
+
+static int
+testDump(const void *opaque)
+{
+    const struct testUdevData *data = opaque;
+    virUdevMgrPtr mgr = NULL;
+    int ret = -1;
+    const char * const *tmp;
+    char *state = NULL;
+    char *filename = NULL;
+
+    if (virAsprintf(&filename, "%s/virudevtestdata/%s.json",
+                    abs_srcdir, data->file) < 0)
+        goto cleanup;
+
+    if (!(mgr = virUdevMgrNew()))
+        goto cleanup;
+
+    tmp = data->labels;
+    while (*tmp) {
+        const char *device;
+        const char *model;
+        const char *label;
+        virSecurityDeviceLabelDefPtr seclabel;
+
+        device = *tmp;
+        if (!++tmp) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "Invalid seclabels list");
+            goto cleanup;
+        }
+        model = *tmp;
+        if (!++tmp) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s", "Invalid seclabels list");
+            goto cleanup;
+        }
+        label = *tmp;
+        tmp++;
+
+        if (!(seclabel = virSecurityDeviceLabelDefNewLabel(model, label)))
+            goto cleanup;
+
+        if (virUdevMgrAddLabel(mgr, device, seclabel) < 0)
+            goto cleanup;
+        virSecurityDeviceLabelDefFree(seclabel);
+    }
+
+    if (!(state = virUdevMgrDumpStr(mgr)))
+        goto cleanup;
+
+    if (virTestCompareToFile(state, filename) < 0)
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    VIR_FREE(filename);
+    VIR_FREE(state);
+    virObjectUnref(mgr);
+    return ret;
+}
+
+
+static int
+mymain(void)
+{
+    int ret = 0;
+
+#define DO_TEST_DUMP(filename, ...)                                 \
+    do {                                                            \
+        const char *labels[] = {__VA_ARGS__, NULL};                 \
+        struct testUdevData data = {                                \
+            .file = filename, .labels = labels,                     \
+        };                                                          \
+        if (virTestRun("Dump " filename, testDump, &data) < 0)      \
+            ret = -1;                                               \
+    } while (0)
+
+    DO_TEST_DUMP("empty", NULL);
+    DO_TEST_DUMP("simple-selinux",
+                 "/dev/sda", "selinux", "someSELinuxLabel");
+    DO_TEST_DUMP("simple-dac",
+                 "/dev/sda", "dac", "someDACLabel");
+    DO_TEST_DUMP("complex",
+                 "/dev/sda", "dac",     "someDACLabel",
+                 "/dev/sda", "selinux", "someSELinuxLabel",
+                 "/dev/sdb", "dac",     "otherDACLabel",
+                 "/dev/sdb", "selinux", "otherSELinuxLabel");
+
+    return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virudevmock.so")
diff --git a/tests/virudevtestdata/complex.json b/tests/virudevtestdata/complex.json
new file mode 100644
index 0000000..e1c8e69
--- /dev/null
+++ b/tests/virudevtestdata/complex.json
@@ -0,0 +1,30 @@
+{
+  "labels": [
+    {
+      "device": "/dev/sda",
+      "labels": [
+        {
+          "model": "dac",
+          "label": "someDACLabel"
+        },
+        {
+          "model": "selinux",
+          "label": "someSELinuxLabel"
+        }
+      ]
+    },
+    {
+      "device": "/dev/sdb",
+      "labels": [
+        {
+          "model": "dac",
+          "label": "otherDACLabel"
+        },
+        {
+          "model": "selinux",
+          "label": "otherSELinuxLabel"
+        }
+      ]
+    }
+  ]
+}
diff --git a/tests/virudevtestdata/empty.json b/tests/virudevtestdata/empty.json
new file mode 100644
index 0000000..763c0f2
--- /dev/null
+++ b/tests/virudevtestdata/empty.json
@@ -0,0 +1,5 @@
+{
+  "labels": [
+
+  ]
+}
diff --git a/tests/virudevtestdata/simple-dac.json b/tests/virudevtestdata/simple-dac.json
new file mode 100644
index 0000000..bbda6bb
--- /dev/null
+++ b/tests/virudevtestdata/simple-dac.json
@@ -0,0 +1,13 @@
+{
+  "labels": [
+    {
+      "device": "/dev/sda",
+      "labels": [
+        {
+          "model": "dac",
+          "label": "someDACLabel"
+        }
+      ]
+    }
+  ]
+}
diff --git a/tests/virudevtestdata/simple-selinux.json b/tests/virudevtestdata/simple-selinux.json
new file mode 100644
index 0000000..7398d7f
--- /dev/null
+++ b/tests/virudevtestdata/simple-selinux.json
@@ -0,0 +1,13 @@
+{
+  "labels": [
+    {
+      "device": "/dev/sda",
+      "labels": [
+        {
+          "model": "selinux",
+          "label": "someSELinuxLabel"
+        }
+      ]
+    }
+  ]
+}
-- 
2.8.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



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