render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From d323cd99b256b368c37f0a36eef8b3b2d6213346 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <d323cd99b256b368c37f0a36eef8b3b2d6213346@dist-git>
c480ed
From: Pavel Hrdina <phrdina@redhat.com>
c480ed
Date: Mon, 1 Jul 2019 17:06:18 +0200
c480ed
Subject: [PATCH] vircgroupmock: rewrite cgroup fopen mocking
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Move all the cgroup data into separate files out of vircgroupmock.c
c480ed
and rework the fopen function to load data from files.  This will
c480ed
make it easier to add more test cases.
c480ed
c480ed
Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
(cherry picked from commit 5cf1b25708328ad4006572801d0829548418cfe7)
c480ed
c480ed
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1689297
c480ed
c480ed
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
c480ed
Message-Id: <efee26b41a026de90498f4abd724f1c7d85d6731.1561993099.git.phrdina@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 tests/vircgroupdata/all-in-one.cgroups     |   7 ++
c480ed
 tests/vircgroupdata/all-in-one.mounts      |   2 +-
c480ed
 tests/vircgroupdata/all-in-one.parsed      |  12 +-
c480ed
 tests/vircgroupdata/all-in-one.self.cgroup |   1 +
c480ed
 tests/vircgroupdata/logind.cgroups         |  10 ++
c480ed
 tests/vircgroupdata/logind.mounts          |   2 +
c480ed
 tests/vircgroupdata/logind.self.cgroup     |   1 +
c480ed
 tests/vircgroupdata/systemd.cgroups        |   8 ++
c480ed
 tests/vircgroupdata/systemd.mounts         |  11 ++
c480ed
 tests/vircgroupdata/systemd.self.cgroup    |   6 +
c480ed
 tests/vircgroupmock.c                      | 136 +++------------------
c480ed
 tests/vircgrouptest.c                      |  10 +-
c480ed
 12 files changed, 79 insertions(+), 127 deletions(-)
c480ed
 create mode 100644 tests/vircgroupdata/all-in-one.cgroups
c480ed
 create mode 100644 tests/vircgroupdata/all-in-one.self.cgroup
c480ed
 create mode 100644 tests/vircgroupdata/logind.cgroups
c480ed
 create mode 100644 tests/vircgroupdata/logind.mounts
c480ed
 create mode 100644 tests/vircgroupdata/logind.self.cgroup
c480ed
 create mode 100644 tests/vircgroupdata/systemd.cgroups
c480ed
 create mode 100644 tests/vircgroupdata/systemd.mounts
c480ed
 create mode 100644 tests/vircgroupdata/systemd.self.cgroup
c480ed
c480ed
diff --git a/tests/vircgroupdata/all-in-one.cgroups b/tests/vircgroupdata/all-in-one.cgroups
c480ed
new file mode 100644
c480ed
index 0000000000..7208e5a0b6
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/all-in-one.cgroups
c480ed
@@ -0,0 +1,7 @@
c480ed
+#subsys_name    hierarchy       num_cgroups     enabled
c480ed
+cpuset   6   1  1
c480ed
+cpu      6   1  1
c480ed
+cpuacct  6   1  1
c480ed
+memory   6   1  1
c480ed
+devices  6   1  1
c480ed
+blkio    6   1  1
c480ed
diff --git a/tests/vircgroupdata/all-in-one.mounts b/tests/vircgroupdata/all-in-one.mounts
c480ed
index 14093b961c..76c579ff69 100644
c480ed
--- a/tests/vircgroupdata/all-in-one.mounts
c480ed
+++ b/tests/vircgroupdata/all-in-one.mounts
c480ed
@@ -4,4 +4,4 @@ proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
c480ed
 udev /dev devtmpfs rw,relatime,size=16458560k,nr_inodes=4114640,mode=755 0 0
c480ed
 devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
c480ed
 nfsd /proc/fs/nfsd nfsd rw,relatime 0 0
c480ed
-cgroup /sys/fs/cgroup cgroup rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup cgroup rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset 0 0
c480ed
diff --git a/tests/vircgroupdata/all-in-one.parsed b/tests/vircgroupdata/all-in-one.parsed
c480ed
index 2701778fea..d703d08fb9 100644
c480ed
--- a/tests/vircgroupdata/all-in-one.parsed
c480ed
+++ b/tests/vircgroupdata/all-in-one.parsed
c480ed
@@ -1,10 +1,10 @@
c480ed
-cpu          /sys/fs/cgroup
c480ed
-cpuacct      /sys/fs/cgroup
c480ed
-cpuset       /sys/fs/cgroup
c480ed
-memory       /sys/fs/cgroup
c480ed
-devices      /sys/fs/cgroup
c480ed
+cpu          /not/really/sys/fs/cgroup
c480ed
+cpuacct      /not/really/sys/fs/cgroup
c480ed
+cpuset       /not/really/sys/fs/cgroup
c480ed
+memory       /not/really/sys/fs/cgroup
c480ed
+devices      /not/really/sys/fs/cgroup
c480ed
 freezer      <null>
c480ed
-blkio        /sys/fs/cgroup
c480ed
+blkio        /not/really/sys/fs/cgroup
c480ed
 net_cls      <null>
c480ed
 perf_event   <null>
c480ed
 name=systemd <null>
c480ed
diff --git a/tests/vircgroupdata/all-in-one.self.cgroup b/tests/vircgroupdata/all-in-one.self.cgroup
c480ed
new file mode 100644
c480ed
index 0000000000..cf237502e9
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/all-in-one.self.cgroup
c480ed
@@ -0,0 +1 @@
c480ed
+6:blkio,devices,memory,cpuacct,cpu,cpuset:/
c480ed
diff --git a/tests/vircgroupdata/logind.cgroups b/tests/vircgroupdata/logind.cgroups
c480ed
new file mode 100644
c480ed
index 0000000000..9d46f130e0
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/logind.cgroups
c480ed
@@ -0,0 +1,10 @@
c480ed
+#subsys_name    hierarchy       num_cgroups     enabled
c480ed
+cpuset    0  1  1
c480ed
+cpu       0  1  1
c480ed
+cpuacct   0  1  1
c480ed
+memory    0  1  0
c480ed
+devices   0  1  1
c480ed
+freezer   0  1  1
c480ed
+net_cls   0  1  1
c480ed
+blkio     0  1  1
c480ed
+perf_event  0  1  1
c480ed
diff --git a/tests/vircgroupdata/logind.mounts b/tests/vircgroupdata/logind.mounts
c480ed
new file mode 100644
c480ed
index 0000000000..3ab908aee9
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/logind.mounts
c480ed
@@ -0,0 +1,2 @@
c480ed
+none /not/really/sys/fs/cgroup tmpfs rw,rootcontext=system_u:object_r:sysfs_t:s0,seclabel,relatime,size=4k,mode=755 0 0
c480ed
+systemd /not/really/sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0
c480ed
diff --git a/tests/vircgroupdata/logind.self.cgroup b/tests/vircgroupdata/logind.self.cgroup
c480ed
new file mode 100644
c480ed
index 0000000000..31e0cfe8eb
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/logind.self.cgroup
c480ed
@@ -0,0 +1 @@
c480ed
+0:name=systemd:/
c480ed
diff --git a/tests/vircgroupdata/systemd.cgroups b/tests/vircgroupdata/systemd.cgroups
c480ed
new file mode 100644
c480ed
index 0000000000..d32dfab222
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/systemd.cgroups
c480ed
@@ -0,0 +1,8 @@
c480ed
+#subsys_name    hierarchy       num_cgroups     enabled
c480ed
+cpuset  2       4       1
c480ed
+cpu     3       48      1
c480ed
+cpuacct 3       48      1
c480ed
+memory  4       4       1
c480ed
+devices 5       4       1
c480ed
+freezer 6       4       1
c480ed
+blkio   8       4       1
c480ed
diff --git a/tests/vircgroupdata/systemd.mounts b/tests/vircgroupdata/systemd.mounts
c480ed
new file mode 100644
c480ed
index 0000000000..75572c86f7
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/systemd.mounts
c480ed
@@ -0,0 +1,11 @@
c480ed
+rootfs / rootfs rw 0 0
c480ed
+tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0
c480ed
+tmpfs /not/really/sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
c480ed
+cgroup /not/really/sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
c480ed
+/dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0
c480ed
+tmpfs /tmp tmpfs rw,seclabel,relatime,size=1024000k 0 0
c480ed
diff --git a/tests/vircgroupdata/systemd.self.cgroup b/tests/vircgroupdata/systemd.self.cgroup
c480ed
new file mode 100644
c480ed
index 0000000000..2b95af79d2
c480ed
--- /dev/null
c480ed
+++ b/tests/vircgroupdata/systemd.self.cgroup
c480ed
@@ -0,0 +1,6 @@
c480ed
+115:memory:/
c480ed
+8:blkio:/
c480ed
+6:freezer:/
c480ed
+3:cpuacct,cpu:/system
c480ed
+2:cpuset:/
c480ed
+1:name=systemd:/user/berrange/123
c480ed
diff --git a/tests/vircgroupmock.c b/tests/vircgroupmock.c
c480ed
index cd5422dcba..3afe2fe192 100644
c480ed
--- a/tests/vircgroupmock.c
c480ed
+++ b/tests/vircgroupmock.c
c480ed
@@ -81,85 +81,6 @@ const char *fakedevicedir1 = FAKEDEVDIR1;
c480ed
  * of files beneath it
c480ed
  */
c480ed
 
c480ed
-/*
c480ed
- * Intentionally missing the 'devices' mount.
c480ed
- * Co-mounting cpu & cpuacct controllers
c480ed
- * An anonymous controller for systemd
c480ed
- */
c480ed
-const char *procmounts =
c480ed
-    "rootfs / rootfs rw 0 0\n"
c480ed
-    "tmpfs /run tmpfs rw,seclabel,nosuid,nodev,mode=755 0 0\n"
c480ed
-    "tmpfs /not/really/sys/fs/cgroup tmpfs rw,seclabel,nosuid,nodev,noexec,mode=755 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpuacct,cpu 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0\n"
c480ed
-    "/dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0\n"
c480ed
-    "tmpfs /tmp tmpfs rw,seclabel,relatime,size=1024000k 0 0\n";
c480ed
-
c480ed
-const char *procselfcgroups =
c480ed
-    "115:memory:/\n"
c480ed
-    "8:blkio:/\n"
c480ed
-    "6:freezer:/\n"
c480ed
-    "3:cpuacct,cpu:/system\n"
c480ed
-    "2:cpuset:/\n"
c480ed
-    "1:name=systemd:/user/berrange/123\n";
c480ed
-
c480ed
-const char *proccgroups =
c480ed
-    "#subsys_name    hierarchy       num_cgroups     enabled\n"
c480ed
-    "cpuset  2       4       1\n"
c480ed
-    "cpu     3       48      1\n"
c480ed
-    "cpuacct 3       48      1\n"
c480ed
-    "memory  4       4       1\n"
c480ed
-    "devices 5       4       1\n"
c480ed
-    "freezer 6       4       1\n"
c480ed
-    "blkio   8       4       1\n";
c480ed
-
c480ed
-
c480ed
-const char *procmountsallinone =
c480ed
-    "rootfs / rootfs rw 0 0\n"
c480ed
-    "sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
c480ed
-    "proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0\n"
c480ed
-    "udev /dev devtmpfs rw,relatime,size=16458560k,nr_inodes=4114640,mode=755 0 0\n"
c480ed
-    "devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0\n"
c480ed
-    "nfsd /proc/fs/nfsd nfsd rw,relatime 0 0\n"
c480ed
-    "cgroup /not/really/sys/fs/cgroup cgroup rw,relatime,blkio,devices,memory,cpuacct,cpu,cpuset 0 0\n";
c480ed
-
c480ed
-const char *procselfcgroupsallinone =
c480ed
-    "6:blkio,devices,memory,cpuacct,cpu,cpuset:/";
c480ed
-
c480ed
-const char *proccgroupsallinone =
c480ed
-    "#subsys_name    hierarchy       num_cgroups     enabled\n"
c480ed
-    "cpuset   6   1  1\n"
c480ed
-    "cpu      6   1  1\n"
c480ed
-    "cpuacct  6   1  1\n"
c480ed
-    "memory   6   1  1\n"
c480ed
-    "devices  6   1  1\n"
c480ed
-    "blkio    6   1  1\n";
c480ed
-
c480ed
-const char *procmountslogind =
c480ed
-    "none /not/really/sys/fs/cgroup tmpfs rw,rootcontext=system_u:object_r:sysfs_t:s0,seclabel,relatime,size=4k,mode=755 0 0\n"
c480ed
-    "systemd /not/really/sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,name=systemd 0 0\n";
c480ed
-
c480ed
-const char *procselfcgroupslogind =
c480ed
-    "1:name=systemd:/\n";
c480ed
-
c480ed
-const char *proccgroupslogind =
c480ed
-    "#subsys_name    hierarchy       num_cgroups     enabled\n"
c480ed
-    "cpuset    0  1  1\n"
c480ed
-    "cpu       0  1  1\n"
c480ed
-    "cpuacct   0  1  1\n"
c480ed
-    "memory    0  1  0\n"
c480ed
-    "devices   0  1  1\n"
c480ed
-    "freezer   0  1  1\n"
c480ed
-    "net_cls   0  1  1\n"
c480ed
-    "blkio     0  1  1\n"
c480ed
-    "perf_event  0  1  1\n";
c480ed
-
c480ed
-
c480ed
-
c480ed
 static int make_file(const char *path,
c480ed
                      const char *name,
c480ed
                      const char *value)
c480ed
@@ -379,29 +300,16 @@ static void init_sysfs(void)
c480ed
 
c480ed
 FILE *fopen(const char *path, const char *mode)
c480ed
 {
c480ed
-    const char *mock;
c480ed
-    bool allinone = false, logind = false;
c480ed
-    init_syms();
c480ed
+    char *filepath = NULL;
c480ed
+    const char *type = NULL;
c480ed
+    FILE *rc = NULL;
c480ed
+    const char *filename = getenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
 
c480ed
-    mock = getenv("VIR_CGROUP_MOCK_MODE");
c480ed
-    if (mock) {
c480ed
-        if (STREQ(mock, "allinone"))
c480ed
-            allinone = true;
c480ed
-        else if (STREQ(mock, "logind"))
c480ed
-            logind = true;
c480ed
-    }
c480ed
+    init_syms();
c480ed
 
c480ed
     if (STREQ(path, "/proc/mounts")) {
c480ed
         if (STREQ(mode, "r")) {
c480ed
-            if (allinone)
c480ed
-                return fmemopen((void *)procmountsallinone,
c480ed
-                                strlen(procmountsallinone) + 1, mode);
c480ed
-            else if (logind)
c480ed
-                return fmemopen((void *)procmountslogind,
c480ed
-                                strlen(procmountslogind) + 1, mode);
c480ed
-            else
c480ed
-                return fmemopen((void *)procmounts,
c480ed
-                                strlen(procmounts) + 1, mode);
c480ed
+            type = "mounts";
c480ed
         } else {
c480ed
             errno = EACCES;
c480ed
             return NULL;
c480ed
@@ -409,15 +317,7 @@ FILE *fopen(const char *path, const char *mode)
c480ed
     }
c480ed
     if (STREQ(path, "/proc/cgroups")) {
c480ed
         if (STREQ(mode, "r")) {
c480ed
-            if (allinone)
c480ed
-                return fmemopen((void *)proccgroupsallinone,
c480ed
-                                strlen(proccgroupsallinone) + 1, mode);
c480ed
-            else if (logind)
c480ed
-                return fmemopen((void *)proccgroupslogind,
c480ed
-                                strlen(proccgroupslogind) + 1, mode);
c480ed
-            else
c480ed
-                return fmemopen((void *)proccgroups,
c480ed
-                                strlen(proccgroups) + 1, mode);
c480ed
+            type = "cgroups";
c480ed
         } else {
c480ed
             errno = EACCES;
c480ed
             return NULL;
c480ed
@@ -425,21 +325,25 @@ FILE *fopen(const char *path, const char *mode)
c480ed
     }
c480ed
     if (STREQ(path, "/proc/self/cgroup")) {
c480ed
         if (STREQ(mode, "r")) {
c480ed
-            if (allinone)
c480ed
-                return fmemopen((void *)procselfcgroupsallinone,
c480ed
-                                strlen(procselfcgroupsallinone) + 1, mode);
c480ed
-            else if (logind)
c480ed
-                return fmemopen((void *)procselfcgroupslogind,
c480ed
-                                strlen(procselfcgroupslogind) + 1, mode);
c480ed
-            else
c480ed
-                return fmemopen((void *)procselfcgroups,
c480ed
-                                strlen(procselfcgroups) + 1, mode);
c480ed
+            type = "self.cgroup";
c480ed
         } else {
c480ed
             errno = EACCES;
c480ed
             return NULL;
c480ed
         }
c480ed
     }
c480ed
 
c480ed
+    if (type) {
c480ed
+        if (!filename)
c480ed
+            abort();
c480ed
+        if (virAsprintfQuiet(&filepath, "%s/vircgroupdata/%s.%s",
c480ed
+                             abs_srcdir, filename, type) < 0) {
c480ed
+            abort();
c480ed
+        }
c480ed
+        rc = real_fopen(filepath, mode);
c480ed
+        free(filepath);
c480ed
+        return rc;
c480ed
+    }
c480ed
+
c480ed
     return real_fopen(path, mode);
c480ed
 }
c480ed
 
c480ed
diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
c480ed
index be50f3e73c..7968903cad 100644
c480ed
--- a/tests/vircgrouptest.c
c480ed
+++ b/tests/vircgrouptest.c
c480ed
@@ -890,6 +890,7 @@ mymain(void)
c480ed
     DETECT_MOUNTS("no-cgroups");
c480ed
     DETECT_MOUNTS("kubevirt");
c480ed
 
c480ed
+    setenv("VIR_CGROUP_MOCK_FILENAME", "systemd", 1);
c480ed
     if (virTestRun("New cgroup for self", testCgroupNewForSelf, NULL) < 0)
c480ed
         ret = -1;
c480ed
 
c480ed
@@ -925,20 +926,21 @@ mymain(void)
c480ed
 
c480ed
     if (virTestRun("virCgroupGetPercpuStats works", testCgroupGetPercpuStats, NULL) < 0)
c480ed
         ret = -1;
c480ed
+    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
 
c480ed
-    setenv("VIR_CGROUP_MOCK_MODE", "allinone", 1);
c480ed
+    setenv("VIR_CGROUP_MOCK_FILENAME", "all-in-one", 1);
c480ed
     if (virTestRun("New cgroup for self (allinone)", testCgroupNewForSelfAllInOne, NULL) < 0)
c480ed
         ret = -1;
c480ed
     if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x1) < 0)
c480ed
         ret = -1;
c480ed
-    unsetenv("VIR_CGROUP_MOCK_MODE");
c480ed
+    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
 
c480ed
-    setenv("VIR_CGROUP_MOCK_MODE", "logind", 1);
c480ed
+    setenv("VIR_CGROUP_MOCK_FILENAME", "logind", 1);
c480ed
     if (virTestRun("New cgroup for self (logind)", testCgroupNewForSelfLogind, NULL) < 0)
c480ed
         ret = -1;
c480ed
     if (virTestRun("Cgroup available", testCgroupAvailable, (void*)0x0) < 0)
c480ed
         ret = -1;
c480ed
-    unsetenv("VIR_CGROUP_MOCK_MODE");
c480ed
+    unsetenv("VIR_CGROUP_MOCK_FILENAME");
c480ed
 
c480ed
     if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
c480ed
         virFileDeleteTree(fakerootdir);
c480ed
-- 
c480ed
2.22.0
c480ed