render / rpms / libvirt

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