render / rpms / libvirt

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