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