render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
Blob Blame History Raw
From c94691d796682d951ffa8fb3a4fcb985aae17d9b Mon Sep 17 00:00:00 2001
Message-Id: <c94691d796682d951ffa8fb3a4fcb985aae17d9b@dist-git>
From: Pavel Hrdina <phrdina@redhat.com>
Date: Fri, 19 Feb 2021 13:34:00 +0100
Subject: [PATCH] tests: add cgroup nested tests
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 85099c339346e41f457234e8ad831841aef1d5e3)

Conflicts:
    tests/vircgrouptest.c
        - missing upstream g_autofree rewrite

Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
Message-Id: <abf8f7673bd59c6e3d9b596cf9a86029b1f1e9c1.1613737828.git.phrdina@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 tests/vircgroupdata/systemd-legacy.cgroups    | 12 +++
 tests/vircgroupdata/systemd-legacy.mounts     | 11 +++
 .../vircgroupdata/systemd-legacy.self.cgroup  | 11 +++
 tests/vircgroupdata/systemd-unified.cgroups   | 13 +++
 tests/vircgroupdata/systemd-unified.mounts    |  1 +
 .../vircgroupdata/systemd-unified.self.cgroup |  1 +
 tests/vircgrouptest.c                         | 82 +++++++++++++++++++
 7 files changed, 131 insertions(+)
 create mode 100644 tests/vircgroupdata/systemd-legacy.cgroups
 create mode 100644 tests/vircgroupdata/systemd-legacy.mounts
 create mode 100644 tests/vircgroupdata/systemd-legacy.self.cgroup
 create mode 100644 tests/vircgroupdata/systemd-unified.cgroups
 create mode 100644 tests/vircgroupdata/systemd-unified.mounts
 create mode 100644 tests/vircgroupdata/systemd-unified.self.cgroup

diff --git a/tests/vircgroupdata/systemd-legacy.cgroups b/tests/vircgroupdata/systemd-legacy.cgroups
new file mode 100644
index 0000000000..444354e3c8
--- /dev/null
+++ b/tests/vircgroupdata/systemd-legacy.cgroups
@@ -0,0 +1,12 @@
+#subsys_name	hierarchy	num_cgroups	enabled
+blkio       1       1       1
+cpu         2       1       1
+cpuacct     3       1       1
+cpuset      4       1       1
+devices     5       1       1
+freezer     6       1       1
+hugetlb     7       1       1
+memory      8       1       1
+net_cls     9       1       1
+perf_event  10      1       1
+pids        11      1       1
diff --git a/tests/vircgroupdata/systemd-legacy.mounts b/tests/vircgroupdata/systemd-legacy.mounts
new file mode 100644
index 0000000000..23462e9e68
--- /dev/null
+++ b/tests/vircgroupdata/systemd-legacy.mounts
@@ -0,0 +1,11 @@
+cgroup /not/really/sys/fs/cgroup/blkio cgroup rw,seclabel,nosuid,nodev,noexec,relatime,blkio 0 0
+cgroup /not/really/sys/fs/cgroup/cpu cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpu 0 0
+cgroup /not/really/sys/fs/cgroup/cpuacct cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuacct 0 0
+cgroup /not/really/sys/fs/cgroup/cpuset cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuset 0 0
+cgroup /not/really/sys/fs/cgroup/devices cgroup rw,seclabel,nosuid,nodev,noexec,relatime,devices 0 0
+cgroup /not/really/sys/fs/cgroup/freezer cgroup rw,seclabel,nosuid,nodev,noexec,relatime,freezer 0 0
+cgroup /not/really/sys/fs/cgroup/hugetlb cgroup rw,seclabel,nosuid,nodev,noexec,relatime,hugetlb 0 0
+cgroup /not/really/sys/fs/cgroup/memory cgroup rw,seclabel,nosuid,nodev,noexec,relatime,memory 0 0
+cgroup /not/really/sys/fs/cgroup/net_cls cgroup rw,seclabel,nosuid,nodev,noexec,relatime,net_cls 0 0
+cgroup /not/really/sys/fs/cgroup/perf_event cgroup rw,seclabel,nosuid,nodev,noexec,relatime,perf_event 0 0
+cgroup /not/really/sys/fs/cgroup/pids cgroup rw,seclabel,nosuid,nodev,noexec,relatime,pids 0 0
diff --git a/tests/vircgroupdata/systemd-legacy.self.cgroup b/tests/vircgroupdata/systemd-legacy.self.cgroup
new file mode 100644
index 0000000000..5c133a3c08
--- /dev/null
+++ b/tests/vircgroupdata/systemd-legacy.self.cgroup
@@ -0,0 +1,11 @@
+1:blkio:/libvirt
+2:cpu:/libvirt/emulator
+3:cpuacct:/libvirt/emulator
+4:cpuset:/libvirt/emulator
+5:devices:/libvirt
+6:freezer:/libvirt
+7:hugetlb:/
+8:memory:/libvirt
+9:net_cls:/libvirt
+10:perf_event:/libvirt
+11:pids:/
diff --git a/tests/vircgroupdata/systemd-unified.cgroups b/tests/vircgroupdata/systemd-unified.cgroups
new file mode 100644
index 0000000000..e0d8a3561c
--- /dev/null
+++ b/tests/vircgroupdata/systemd-unified.cgroups
@@ -0,0 +1,13 @@
+#subsys_name	hierarchy	num_cgroups	enabled
+cpuset      0       1       1
+cpu         0       1       1
+cpuacct     0       1       1
+blkio       0       1       1
+memory      0       1       1
+devices     0       1       1
+freezer     0       1       1
+net_cls     0       1       1
+perf_event  0       1       1
+net_prio    0       1       1
+hugetlb     0       1       1
+pids        0       1       1
diff --git a/tests/vircgroupdata/systemd-unified.mounts b/tests/vircgroupdata/systemd-unified.mounts
new file mode 100644
index 0000000000..8225f37f45
--- /dev/null
+++ b/tests/vircgroupdata/systemd-unified.mounts
@@ -0,0 +1 @@
+cgroup2 /not/really/sys/fs/cgroup cgroup2 rw,seclabel,nosuid,nodev,noexec,relatime,nsdelegate 0 0
diff --git a/tests/vircgroupdata/systemd-unified.self.cgroup b/tests/vircgroupdata/systemd-unified.self.cgroup
new file mode 100644
index 0000000000..6007ce7e18
--- /dev/null
+++ b/tests/vircgroupdata/systemd-unified.self.cgroup
@@ -0,0 +1 @@
+0::/libvirt/emulator
diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
index 2d6f52fb6e..aebb90c16c 100644
--- a/tests/vircgrouptest.c
+++ b/tests/vircgrouptest.c
@@ -636,6 +636,74 @@ static int testCgroupNewForSelfHybrid(const void *args G_GNUC_UNUSED)
 }
 
 
+static int testCgroupNewForSelfSystemdLegacy(const void *args G_GNUC_UNUSED)
+{
+    virCgroupPtr cgroup = NULL;
+    int ret = -1;
+    const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 };
+    const char *mounts[VIR_CGROUP_CONTROLLER_LAST] = {
+        [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup/blkio",
+        [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu",
+        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpuacct",
+        [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup/cpuset",
+        [VIR_CGROUP_CONTROLLER_DEVICES] = "/not/really/sys/fs/cgroup/devices",
+        [VIR_CGROUP_CONTROLLER_FREEZER] = "/not/really/sys/fs/cgroup/freezer",
+        [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
+        [VIR_CGROUP_CONTROLLER_NET_CLS] = "/not/really/sys/fs/cgroup/net_cls",
+        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "/not/really/sys/fs/cgroup/perf_event",
+    };
+    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
+        [VIR_CGROUP_CONTROLLER_BLKIO] = "",
+        [VIR_CGROUP_CONTROLLER_CPU] = "",
+        [VIR_CGROUP_CONTROLLER_CPUACCT] = "",
+        [VIR_CGROUP_CONTROLLER_CPUSET] = "",
+        [VIR_CGROUP_CONTROLLER_DEVICES] = "",
+        [VIR_CGROUP_CONTROLLER_FREEZER] = "",
+        [VIR_CGROUP_CONTROLLER_MEMORY] = "",
+        [VIR_CGROUP_CONTROLLER_NET_CLS] = "",
+        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "",
+    };
+
+    if (virCgroupNewSelf(&cgroup) < 0) {
+        fprintf(stderr, "Cannot create cgroup for self\n");
+        goto cleanup;
+    }
+
+    ret = validateCgroup(cgroup, "", mounts, empty, placement, NULL, NULL, 0);
+
+ cleanup:
+    virCgroupFree(&cgroup);
+    return ret;
+}
+
+
+static int testCgroupNewForSelfSystemdUnified(const void *args G_GNUC_UNUSED)
+{
+    virCgroupPtr cgroup = NULL;
+    int ret = -1;
+    const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 };
+    unsigned int controllers =
+        (1 << VIR_CGROUP_CONTROLLER_CPU) |
+        (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
+        (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
+        (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
+        (1 << VIR_CGROUP_CONTROLLER_BLKIO);
+
+    if (virCgroupNewSelf(&cgroup) < 0) {
+        fprintf(stderr, "Cannot create cgroup for self\n");
+        goto cleanup;
+    }
+
+    ret = validateCgroup(cgroup, "", empty, empty, empty,
+                         "/not/really/sys/fs/cgroup", "",
+                         controllers);
+
+ cleanup:
+    virCgroupFree(&cgroup);
+    return ret;
+}
+
+
 static int testCgroupAvailable(const void *args)
 {
     bool got = virCgroupAvailable();
@@ -1125,6 +1193,20 @@ mymain(void)
         ret = -1;
     cleanupFakeFS(fakerootdir);
 
+    fakerootdir = initFakeFS("legacy", "systemd-legacy");
+    if (virTestRun("New cgroup for self (systemd-legacy)",
+                   testCgroupNewForSelfSystemdLegacy, NULL) < 0) {
+        ret = -1;
+    }
+    cleanupFakeFS(fakerootdir);
+
+    fakerootdir = initFakeFS("unified", "systemd-unified");
+    if (virTestRun("New cgroup for self (systemd-unified)",
+                   testCgroupNewForSelfSystemdUnified, NULL) < 0) {
+        ret = -1;
+    }
+    cleanupFakeFS(fakerootdir);
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 
-- 
2.30.0