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