render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c873f9
From 4a8593827fa1440e741b685e9ba3f0c7e3b17e99 Mon Sep 17 00:00:00 2001
c873f9
Message-Id: <4a8593827fa1440e741b685e9ba3f0c7e3b17e99@dist-git>
c873f9
From: Michal Privoznik <mprivozn@redhat.com>
c873f9
Date: Wed, 10 Oct 2018 17:25:54 +0200
c873f9
Subject: [PATCH] virfiletst: Test virFileIsSharedFS
c873f9
c873f9
RHEL-7.7: https://bugzilla.redhat.com/show_bug.cgi?id=1632711
c873f9
RHEL-8.0: https://bugzilla.redhat.com/show_bug.cgi?id=1634782
c873f9
RHEL-7.6.z: https://bugzilla.redhat.com/show_bug.cgi?id=1635705
c873f9
c873f9
Introduce some basic test cases for virFileIsSharedFS(). More
c873f9
will be added later. In order to achieve desired result, mocks
c873f9
for setmntent() and statfs() need to be invented because the
c873f9
first thing that virFileIsSharedFS() does is calling the latter.
c873f9
If it finds a FUSE mount it'll call the former.
c873f9
c873f9
The mock might look a bit complicated, but in fact it's quite
c873f9
simple. The test sets LIBVIRT_MTAB env variable to hold the
c873f9
absolute path to a file containing mount table. Then, statfs()
c873f9
returns matching FS it finds, and setmntent() is there just to
c873f9
replace /proc/mounts with the file the test wants to load.
c873f9
c873f9
Adding this test also exposed a bug we have - because we assume
c873f9
the given path points to a file we cut off what we assume is a
c873f9
file name to obtain directory path and only then we call
c873f9
statfs(). This is buggy because the passed path could be already
c873f9
a mount point.
c873f9
c873f9
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c873f9
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
c873f9
(cherry picked from commit a7b4eb7d261255d70d90047ae34e8eea849053f2)
c873f9
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
c873f9
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
c873f9
---
c873f9
 tests/Makefile.am             |   7 +-
c873f9
 tests/virfiledata/mounts3.txt |  33 ++++++++
c873f9
 tests/virfilemock.c           | 154 ++++++++++++++++++++++++++++++++++
c873f9
 tests/virfiletest.c           |  62 +++++++++++++-
c873f9
 4 files changed, 254 insertions(+), 2 deletions(-)
c873f9
 create mode 100644 tests/virfiledata/mounts3.txt
c873f9
 create mode 100644 tests/virfilemock.c
c873f9
c873f9
diff --git a/tests/Makefile.am b/tests/Makefile.am
c873f9
index f5872fa42b..a4ebed489a 100644
c873f9
--- a/tests/Makefile.am
c873f9
+++ b/tests/Makefile.am
c873f9
@@ -242,6 +242,7 @@ test_libraries += virusbmock.la \
c873f9
 	virnetdevbandwidthmock.la \
c873f9
 	virnumamock.la \
c873f9
 	virtestmock.la \
c873f9
+	virfilemock.la \
c873f9
 	$(NULL)
c873f9
 endif WITH_LINUX
c873f9
 
c873f9
@@ -1163,9 +1164,13 @@ virresctrltest_SOURCES = \
c873f9
 	virresctrltest.c testutils.h testutils.c virfilewrapper.h virfilewrapper.c
c873f9
 virresctrltest_LDADD = $(LDADDS)
c873f9
 
c873f9
+virfilemock_la_SOURCES = \
c873f9
+	virfilemock.c
c873f9
+virfilemock_la_LDFLAGS = $(MOCKLIBS_LDFLAGS)
c873f9
+virfilemock_la_LIBADD = $(MOCKLIBS_LIBS)
c873f9
 else ! WITH_LINUX
c873f9
 EXTRA_DIST += vircaps2xmltest.c virnumamock.c virfilewrapper.c \
c873f9
-			  virfilewrapper.h virresctrltest.c
c873f9
+			  virfilewrapper.h virresctrltest.c virfilemock.c
c873f9
 endif ! WITH_LINUX
c873f9
 
c873f9
 if WITH_NSS
c873f9
diff --git a/tests/virfiledata/mounts3.txt b/tests/virfiledata/mounts3.txt
c873f9
new file mode 100644
c873f9
index 0000000000..226f67dc00
c873f9
--- /dev/null
c873f9
+++ b/tests/virfiledata/mounts3.txt
c873f9
@@ -0,0 +1,33 @@
c873f9
+/dev/root / xfs rw,noatime,attr2,inode64,noquota 0 0
c873f9
+proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+tmpfs /run tmpfs rw,nodev,relatime,size=3281436k,mode=755 0 0
c873f9
+sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+dev /dev devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0
c873f9
+securityfs /sys/kernel/security securityfs rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+debugfs /sys/kernel/debug debugfs rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+mqueue /dev/mqueue mqueue rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+configfs /sys/kernel/config configfs rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
c873f9
+fusectl /sys/fs/fuse/connections fusectl rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+shm /dev/shm tmpfs rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+cgroup_root /sys/fs/cgroup tmpfs rw,nosuid,nodev,noexec,relatime,size=10240k,mode=755 0 0
c873f9
+openrc /sys/fs/cgroup/openrc cgroup rw,nosuid,nodev,noexec,relatime,release_agent=/lib/rc/sh/cgroup-release-agent.sh,name=openrc 0 0
c873f9
+none /sys/fs/cgroup/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0
c873f9
+cpuset /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
c873f9
+cpu /sys/fs/cgroup/cpu cgroup rw,nosuid,nodev,noexec,relatime,cpu 0 0
c873f9
+cpuacct /sys/fs/cgroup/cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct 0 0
c873f9
+blkio /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
c873f9
+memory /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
c873f9
+devices /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
c873f9
+freezer /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
c873f9
+net_cls /sys/fs/cgroup/net_cls cgroup rw,nosuid,nodev,noexec,relatime,net_cls 0 0
c873f9
+perf_event /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
c873f9
+net_prio /sys/fs/cgroup/net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_prio 0 0
c873f9
+hugetlb /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0
c873f9
+pids /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
c873f9
+rdma /sys/fs/cgroup/rdma cgroup rw,nosuid,nodev,noexec,relatime,rdma 0 0
c873f9
+binfmt_misc /proc/sys/fs/binfmt_misc binfmt_misc rw,nosuid,nodev,noexec,relatime 0 0
c873f9
+hugetlbfs /hugepages2M hugetlbfs rw,relatime,mode=1777,pagesize=2M 0 0
c873f9
+none /run/user/1000 tmpfs rw,relatime,mode=700,uid=1000 0 0
c873f9
+host:/nfs /nfs nfs4 rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp6,timeo=600,retrans=2,sec=sys,clientaddr=::,local_lock=none,addr=:: 0 0
c873f9
+dev /nfs/blah devtmpfs rw,nosuid,relatime,size=10240k,nr_inodes=4093060,mode=755 0 0
c873f9
diff --git a/tests/virfilemock.c b/tests/virfilemock.c
c873f9
new file mode 100644
c873f9
index 0000000000..822c757380
c873f9
--- /dev/null
c873f9
+++ b/tests/virfilemock.c
c873f9
@@ -0,0 +1,154 @@
c873f9
+/*
c873f9
+ * Copyright (C) 2018 Red Hat, Inc.
c873f9
+ *
c873f9
+ * This library is free software; you can redistribute it and/or
c873f9
+ * modify it under the terms of the GNU Lesser General Public
c873f9
+ * License as published by the Free Software Foundation; either
c873f9
+ * version 2.1 of the License, or (at your option) any later version.
c873f9
+ *
c873f9
+ * This library is distributed in the hope that it will be useful,
c873f9
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
c873f9
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
c873f9
+ * Lesser General Public License for more details.
c873f9
+ *
c873f9
+ * You should have received a copy of the GNU Lesser General Public
c873f9
+ * License along with this library.  If not, see
c873f9
+ * <http://www.gnu.org/licenses/>.
c873f9
+ *
c873f9
+ * Author: Michal Privoznik <mprivozn@redhat.com>
c873f9
+ */
c873f9
+
c873f9
+#include <config.h>
c873f9
+
c873f9
+#include <stdio.h>
c873f9
+#include <mntent.h>
c873f9
+#include <sys/vfs.h>
c873f9
+#if HAVE_LINUX_MAGIC_H
c873f9
+# include <linux/magic.h>
c873f9
+#endif
c873f9
+
c873f9
+#include "virmock.h"
c873f9
+
c873f9
+#define VIR_FROM_THIS VIR_FROM_NONE
c873f9
+
c873f9
+static FILE *(*real_setmntent)(const char *filename, const char *type);
c873f9
+static int (*real_statfs)(const char *path, struct statfs *buf);
c873f9
+
c873f9
+
c873f9
+static void
c873f9
+init_syms(void)
c873f9
+{
c873f9
+    if (real_setmntent)
c873f9
+        return;
c873f9
+
c873f9
+    VIR_MOCK_REAL_INIT(setmntent);
c873f9
+    VIR_MOCK_REAL_INIT(statfs);
c873f9
+}
c873f9
+
c873f9
+
c873f9
+FILE *
c873f9
+setmntent(const char *filename, const char *type)
c873f9
+{
c873f9
+    const char *mtab;
c873f9
+
c873f9
+    init_syms();
c873f9
+
c873f9
+    if ((mtab = getenv("LIBVIRT_MTAB")))
c873f9
+        filename = mtab;
c873f9
+
c873f9
+    return real_setmntent(filename, type);
c873f9
+}
c873f9
+
c873f9
+
c873f9
+#ifndef NFS_SUPER_MAGIC
c873f9
+# define NFS_SUPER_MAGIC 0x6969
c873f9
+#endif
c873f9
+#ifndef OCFS2_SUPER_MAGIC
c873f9
+# define OCFS2_SUPER_MAGIC 0x7461636f
c873f9
+#endif
c873f9
+#ifndef GFS2_MAGIC
c873f9
+# define GFS2_MAGIC 0x01161970
c873f9
+#endif
c873f9
+#ifndef AFS_FS_MAGIC
c873f9
+# define AFS_FS_MAGIC 0x6B414653
c873f9
+#endif
c873f9
+#ifndef SMB_SUPER_MAGIC
c873f9
+# define SMB_SUPER_MAGIC 0x517B
c873f9
+#endif
c873f9
+#ifndef CIFS_SUPER_MAGIC
c873f9
+# define CIFS_SUPER_MAGIC 0xFF534D42
c873f9
+#endif
c873f9
+#ifndef HUGETLBFS_MAGIC
c873f9
+# define HUGETLBFS_MAGIC 0x958458f6
c873f9
+#endif
c873f9
+#ifndef FUSE_SUPER_MAGIC
c873f9
+# define FUSE_SUPER_MAGIC 0x65735546
c873f9
+#endif
c873f9
+
c873f9
+
c873f9
+static int
c873f9
+statfs_mock(const char *mtab,
c873f9
+            const char *path,
c873f9
+            struct statfs *buf)
c873f9
+{
c873f9
+    FILE *f;
c873f9
+    struct mntent mb;
c873f9
+    char mntbuf[1024];
c873f9
+    int ret = -1;
c873f9
+
c873f9
+    if (!(f = real_setmntent(mtab, "r"))) {
c873f9
+        fprintf(stderr, "Unable to open %s", mtab);
c873f9
+        return -1;
c873f9
+    }
c873f9
+
c873f9
+    while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
c873f9
+        int ftype;
c873f9
+
c873f9
+        if (STRNEQ(mb.mnt_dir, path))
c873f9
+            continue;
c873f9
+
c873f9
+        if (STREQ(mb.mnt_type, "nfs") ||
c873f9
+            STREQ(mb.mnt_type, "nfs4")) {
c873f9
+            ftype = NFS_SUPER_MAGIC;
c873f9
+        } else if (STREQ(mb.mnt_type, "gfs2")||
c873f9
+                   STREQ(mb.mnt_type, "gfs2meta")) {
c873f9
+            ftype = GFS2_MAGIC;
c873f9
+        } else if (STREQ(mb.mnt_type, "ocfs2")) {
c873f9
+            ftype = OCFS2_SUPER_MAGIC;
c873f9
+        } else if (STREQ(mb.mnt_type, "afs")) {
c873f9
+            ftype = AFS_FS_MAGIC;
c873f9
+        } else if (STREQ(mb.mnt_type, "smb3")) {
c873f9
+            ftype = SMB_SUPER_MAGIC;
c873f9
+        } else if (STREQ(mb.mnt_type, "cifs")) {
c873f9
+            ftype = CIFS_SUPER_MAGIC;
c873f9
+        } else if (STRPREFIX(mb.mnt_type, "fuse")) {
c873f9
+            ftype = FUSE_SUPER_MAGIC;
c873f9
+        } else {
c873f9
+            /* Everything else is EXT4. We don't care really for other paths. */
c873f9
+            ftype = EXT4_SUPER_MAGIC;
c873f9
+        }
c873f9
+
c873f9
+        memset(buf, 0, sizeof(*buf));
c873f9
+        /* We only care about f_type so far. */
c873f9
+        buf->f_type = ftype;
c873f9
+        ret = 0;
c873f9
+        break;
c873f9
+    }
c873f9
+
c873f9
+    endmntent(f);
c873f9
+    return ret;
c873f9
+}
c873f9
+
c873f9
+
c873f9
+int
c873f9
+statfs(const char *path, struct statfs *buf)
c873f9
+{
c873f9
+    const char *mtab;
c873f9
+
c873f9
+    init_syms();
c873f9
+
c873f9
+    if ((mtab = getenv("LIBVIRT_MTAB")))
c873f9
+        return statfs_mock(mtab, path, buf);
c873f9
+
c873f9
+    return real_statfs(path, buf);
c873f9
+}
c873f9
diff --git a/tests/virfiletest.c b/tests/virfiletest.c
c873f9
index 790911cacb..85f22063fe 100644
c873f9
--- a/tests/virfiletest.c
c873f9
+++ b/tests/virfiletest.c
c873f9
@@ -31,6 +31,7 @@
c873f9
 # include <linux/falloc.h>
c873f9
 #endif
c873f9
 
c873f9
+#define VIR_FROM_THIS VIR_FROM_NONE
c873f9
 
c873f9
 #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R
c873f9
 static int testFileCheckMounts(const char *prefix,
c873f9
@@ -310,6 +311,48 @@ testFileInData(const void *opaque)
c873f9
 }
c873f9
 
c873f9
 
c873f9
+struct testFileIsSharedFSType {
c873f9
+    const char *mtabFile;
c873f9
+    const char *filename;
c873f9
+    const bool expected;
c873f9
+};
c873f9
+
c873f9
+static int
c873f9
+testFileIsSharedFSType(const void *opaque ATTRIBUTE_UNUSED)
c873f9
+{
c873f9
+#ifndef __linux__
c873f9
+    return EXIT_AM_SKIP;
c873f9
+#else
c873f9
+    const struct testFileIsSharedFSType *data = opaque;
c873f9
+    char *mtabFile = NULL;
c873f9
+    bool actual;
c873f9
+    int ret = -1;
c873f9
+
c873f9
+    if (virAsprintf(&mtabFile, abs_srcdir "/virfiledata/%s", data->mtabFile) < 0)
c873f9
+        return -1;
c873f9
+
c873f9
+    if (setenv("LIBVIRT_MTAB", mtabFile, 1) < 0) {
c873f9
+        fprintf(stderr, "Unable to set env variable\n");
c873f9
+        goto cleanup;
c873f9
+    }
c873f9
+
c873f9
+    actual = virFileIsSharedFS(data->filename);
c873f9
+
c873f9
+    if (actual != data->expected) {
c873f9
+        fprintf(stderr, "Unexpected FS type. Expected %d got %d\n",
c873f9
+                data->expected, actual);
c873f9
+        goto cleanup;
c873f9
+    }
c873f9
+
c873f9
+    ret = 0;
c873f9
+ cleanup:
c873f9
+    VIR_FREE(mtabFile);
c873f9
+    unsetenv("LIBVIRT_MTAB");
c873f9
+    return ret;
c873f9
+#endif
c873f9
+}
c873f9
+
c873f9
+
c873f9
 static int
c873f9
 mymain(void)
c873f9
 {
c873f9
@@ -397,7 +440,24 @@ mymain(void)
c873f9
         DO_TEST_IN_DATA(true, 8, 16, 32, 64, 128, 256, 512);
c873f9
         DO_TEST_IN_DATA(false, 8, 16, 32, 64, 128, 256, 512);
c873f9
     }
c873f9
+
c873f9
+#define DO_TEST_FILE_IS_SHARED_FS_TYPE(mtab, file, exp) \
c873f9
+    do { \
c873f9
+        struct testFileIsSharedFSType data = { \
c873f9
+            .mtabFile = mtab, .filename = file, .expected = exp \
c873f9
+        }; \
c873f9
+        if (virTestRun(virTestCounterNext(), testFileIsSharedFSType, &data) < 0) \
c873f9
+            ret = -1; \
c873f9
+    } while (0)
c873f9
+
c873f9
+    virTestCounterReset("testFileIsSharedFSType ");
c873f9
+    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts1.txt", "/boot/vmlinuz", false);
c873f9
+    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts2.txt", "/run/user/501/gvfs/some/file", false);
c873f9
+    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/file", true);
c873f9
+    /* TODO Detect bind mounts */
c873f9
+    DO_TEST_FILE_IS_SHARED_FS_TYPE("mounts3.txt", "/nfs/blah", true);
c873f9
+
c873f9
     return ret != 0 ? EXIT_FAILURE : EXIT_SUCCESS;
c873f9
 }
c873f9
 
c873f9
-VIR_TEST_MAIN(mymain)
c873f9
+VIR_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/virfilemock.so")
c873f9
-- 
c873f9
2.19.1
c873f9