render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
8c03ec
From c94691d796682d951ffa8fb3a4fcb985aae17d9b Mon Sep 17 00:00:00 2001
8c03ec
Message-Id: <c94691d796682d951ffa8fb3a4fcb985aae17d9b@dist-git>
8c03ec
From: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Date: Fri, 19 Feb 2021 13:34:00 +0100
8c03ec
Subject: [PATCH] tests: add cgroup nested tests
8c03ec
MIME-Version: 1.0
8c03ec
Content-Type: text/plain; charset=UTF-8
8c03ec
Content-Transfer-Encoding: 8bit
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
8c03ec
(cherry picked from commit 85099c339346e41f457234e8ad831841aef1d5e3)
8c03ec
8c03ec
Conflicts:
8c03ec
    tests/vircgrouptest.c
8c03ec
        - missing upstream g_autofree rewrite
8c03ec
8c03ec
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1798463
8c03ec
8c03ec
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
8c03ec
Message-Id: <abf8f7673bd59c6e3d9b596cf9a86029b1f1e9c1.1613737828.git.phrdina@redhat.com>
8c03ec
Reviewed-by: Ján Tomko <jtomko@redhat.com>
8c03ec
---
8c03ec
 tests/vircgroupdata/systemd-legacy.cgroups    | 12 +++
8c03ec
 tests/vircgroupdata/systemd-legacy.mounts     | 11 +++
8c03ec
 .../vircgroupdata/systemd-legacy.self.cgroup  | 11 +++
8c03ec
 tests/vircgroupdata/systemd-unified.cgroups   | 13 +++
8c03ec
 tests/vircgroupdata/systemd-unified.mounts    |  1 +
8c03ec
 .../vircgroupdata/systemd-unified.self.cgroup |  1 +
8c03ec
 tests/vircgrouptest.c                         | 82 +++++++++++++++++++
8c03ec
 7 files changed, 131 insertions(+)
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-legacy.cgroups
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-legacy.mounts
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-legacy.self.cgroup
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-unified.cgroups
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-unified.mounts
8c03ec
 create mode 100644 tests/vircgroupdata/systemd-unified.self.cgroup
8c03ec
8c03ec
diff --git a/tests/vircgroupdata/systemd-legacy.cgroups b/tests/vircgroupdata/systemd-legacy.cgroups
8c03ec
new file mode 100644
8c03ec
index 0000000000..444354e3c8
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-legacy.cgroups
8c03ec
@@ -0,0 +1,12 @@
8c03ec
+#subsys_name	hierarchy	num_cgroups	enabled
8c03ec
+blkio       1       1       1
8c03ec
+cpu         2       1       1
8c03ec
+cpuacct     3       1       1
8c03ec
+cpuset      4       1       1
8c03ec
+devices     5       1       1
8c03ec
+freezer     6       1       1
8c03ec
+hugetlb     7       1       1
8c03ec
+memory      8       1       1
8c03ec
+net_cls     9       1       1
8c03ec
+perf_event  10      1       1
8c03ec
+pids        11      1       1
8c03ec
diff --git a/tests/vircgroupdata/systemd-legacy.mounts b/tests/vircgroupdata/systemd-legacy.mounts
8c03ec
new file mode 100644
8c03ec
index 0000000000..23462e9e68
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-legacy.mounts
8c03ec
@@ -0,0 +1,11 @@
8c03ec
+cgroup /not/really/sys/fs/cgroup/blkio cgroup rw,seclabel,nosuid,nodev,noexec,relatime,blkio 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/cpu cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpu 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/cpuacct cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuacct 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/cpuset cgroup rw,seclabel,nosuid,nodev,noexec,relatime,cpuset 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/devices cgroup rw,seclabel,nosuid,nodev,noexec,relatime,devices 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/freezer cgroup rw,seclabel,nosuid,nodev,noexec,relatime,freezer 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/hugetlb cgroup rw,seclabel,nosuid,nodev,noexec,relatime,hugetlb 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/memory cgroup rw,seclabel,nosuid,nodev,noexec,relatime,memory 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/net_cls cgroup rw,seclabel,nosuid,nodev,noexec,relatime,net_cls 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/perf_event cgroup rw,seclabel,nosuid,nodev,noexec,relatime,perf_event 0 0
8c03ec
+cgroup /not/really/sys/fs/cgroup/pids cgroup rw,seclabel,nosuid,nodev,noexec,relatime,pids 0 0
8c03ec
diff --git a/tests/vircgroupdata/systemd-legacy.self.cgroup b/tests/vircgroupdata/systemd-legacy.self.cgroup
8c03ec
new file mode 100644
8c03ec
index 0000000000..5c133a3c08
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-legacy.self.cgroup
8c03ec
@@ -0,0 +1,11 @@
8c03ec
+1:blkio:/libvirt
8c03ec
+2:cpu:/libvirt/emulator
8c03ec
+3:cpuacct:/libvirt/emulator
8c03ec
+4:cpuset:/libvirt/emulator
8c03ec
+5:devices:/libvirt
8c03ec
+6:freezer:/libvirt
8c03ec
+7:hugetlb:/
8c03ec
+8:memory:/libvirt
8c03ec
+9:net_cls:/libvirt
8c03ec
+10:perf_event:/libvirt
8c03ec
+11:pids:/
8c03ec
diff --git a/tests/vircgroupdata/systemd-unified.cgroups b/tests/vircgroupdata/systemd-unified.cgroups
8c03ec
new file mode 100644
8c03ec
index 0000000000..e0d8a3561c
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-unified.cgroups
8c03ec
@@ -0,0 +1,13 @@
8c03ec
+#subsys_name	hierarchy	num_cgroups	enabled
8c03ec
+cpuset      0       1       1
8c03ec
+cpu         0       1       1
8c03ec
+cpuacct     0       1       1
8c03ec
+blkio       0       1       1
8c03ec
+memory      0       1       1
8c03ec
+devices     0       1       1
8c03ec
+freezer     0       1       1
8c03ec
+net_cls     0       1       1
8c03ec
+perf_event  0       1       1
8c03ec
+net_prio    0       1       1
8c03ec
+hugetlb     0       1       1
8c03ec
+pids        0       1       1
8c03ec
diff --git a/tests/vircgroupdata/systemd-unified.mounts b/tests/vircgroupdata/systemd-unified.mounts
8c03ec
new file mode 100644
8c03ec
index 0000000000..8225f37f45
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-unified.mounts
8c03ec
@@ -0,0 +1 @@
8c03ec
+cgroup2 /not/really/sys/fs/cgroup cgroup2 rw,seclabel,nosuid,nodev,noexec,relatime,nsdelegate 0 0
8c03ec
diff --git a/tests/vircgroupdata/systemd-unified.self.cgroup b/tests/vircgroupdata/systemd-unified.self.cgroup
8c03ec
new file mode 100644
8c03ec
index 0000000000..6007ce7e18
8c03ec
--- /dev/null
8c03ec
+++ b/tests/vircgroupdata/systemd-unified.self.cgroup
8c03ec
@@ -0,0 +1 @@
8c03ec
+0::/libvirt/emulator
8c03ec
diff --git a/tests/vircgrouptest.c b/tests/vircgrouptest.c
8c03ec
index 2d6f52fb6e..aebb90c16c 100644
8c03ec
--- a/tests/vircgrouptest.c
8c03ec
+++ b/tests/vircgrouptest.c
8c03ec
@@ -636,6 +636,74 @@ static int testCgroupNewForSelfHybrid(const void *args G_GNUC_UNUSED)
8c03ec
 }
8c03ec
 
8c03ec
 
8c03ec
+static int testCgroupNewForSelfSystemdLegacy(const void *args G_GNUC_UNUSED)
8c03ec
+{
8c03ec
+    virCgroupPtr cgroup = NULL;
8c03ec
+    int ret = -1;
8c03ec
+    const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 };
8c03ec
+    const char *mounts[VIR_CGROUP_CONTROLLER_LAST] = {
8c03ec
+        [VIR_CGROUP_CONTROLLER_BLKIO] = "/not/really/sys/fs/cgroup/blkio",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPU] = "/not/really/sys/fs/cgroup/cpu",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPUACCT] = "/not/really/sys/fs/cgroup/cpuacct",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPUSET] = "/not/really/sys/fs/cgroup/cpuset",
8c03ec
+        [VIR_CGROUP_CONTROLLER_DEVICES] = "/not/really/sys/fs/cgroup/devices",
8c03ec
+        [VIR_CGROUP_CONTROLLER_FREEZER] = "/not/really/sys/fs/cgroup/freezer",
8c03ec
+        [VIR_CGROUP_CONTROLLER_MEMORY] = "/not/really/sys/fs/cgroup/memory",
8c03ec
+        [VIR_CGROUP_CONTROLLER_NET_CLS] = "/not/really/sys/fs/cgroup/net_cls",
8c03ec
+        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "/not/really/sys/fs/cgroup/perf_event",
8c03ec
+    };
8c03ec
+    const char *placement[VIR_CGROUP_CONTROLLER_LAST] = {
8c03ec
+        [VIR_CGROUP_CONTROLLER_BLKIO] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPU] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPUACCT] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_CPUSET] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_DEVICES] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_FREEZER] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_MEMORY] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_NET_CLS] = "",
8c03ec
+        [VIR_CGROUP_CONTROLLER_PERF_EVENT] = "",
8c03ec
+    };
8c03ec
+
8c03ec
+    if (virCgroupNewSelf(&cgroup) < 0) {
8c03ec
+        fprintf(stderr, "Cannot create cgroup for self\n");
8c03ec
+        goto cleanup;
8c03ec
+    }
8c03ec
+
8c03ec
+    ret = validateCgroup(cgroup, "", mounts, empty, placement, NULL, NULL, 0);
8c03ec
+
8c03ec
+ cleanup:
8c03ec
+    virCgroupFree(&cgroup);
8c03ec
+    return ret;
8c03ec
+}
8c03ec
+
8c03ec
+
8c03ec
+static int testCgroupNewForSelfSystemdUnified(const void *args G_GNUC_UNUSED)
8c03ec
+{
8c03ec
+    virCgroupPtr cgroup = NULL;
8c03ec
+    int ret = -1;
8c03ec
+    const char *empty[VIR_CGROUP_CONTROLLER_LAST] = { 0 };
8c03ec
+    unsigned int controllers =
8c03ec
+        (1 << VIR_CGROUP_CONTROLLER_CPU) |
8c03ec
+        (1 << VIR_CGROUP_CONTROLLER_CPUACCT) |
8c03ec
+        (1 << VIR_CGROUP_CONTROLLER_MEMORY) |
8c03ec
+        (1 << VIR_CGROUP_CONTROLLER_DEVICES) |
8c03ec
+        (1 << VIR_CGROUP_CONTROLLER_BLKIO);
8c03ec
+
8c03ec
+    if (virCgroupNewSelf(&cgroup) < 0) {
8c03ec
+        fprintf(stderr, "Cannot create cgroup for self\n");
8c03ec
+        goto cleanup;
8c03ec
+    }
8c03ec
+
8c03ec
+    ret = validateCgroup(cgroup, "", empty, empty, empty,
8c03ec
+                         "/not/really/sys/fs/cgroup", "",
8c03ec
+                         controllers);
8c03ec
+
8c03ec
+ cleanup:
8c03ec
+    virCgroupFree(&cgroup);
8c03ec
+    return ret;
8c03ec
+}
8c03ec
+
8c03ec
+
8c03ec
 static int testCgroupAvailable(const void *args)
8c03ec
 {
8c03ec
     bool got = virCgroupAvailable();
8c03ec
@@ -1125,6 +1193,20 @@ mymain(void)
8c03ec
         ret = -1;
8c03ec
     cleanupFakeFS(fakerootdir);
8c03ec
 
8c03ec
+    fakerootdir = initFakeFS("legacy", "systemd-legacy");
8c03ec
+    if (virTestRun("New cgroup for self (systemd-legacy)",
8c03ec
+                   testCgroupNewForSelfSystemdLegacy, NULL) < 0) {
8c03ec
+        ret = -1;
8c03ec
+    }
8c03ec
+    cleanupFakeFS(fakerootdir);
8c03ec
+
8c03ec
+    fakerootdir = initFakeFS("unified", "systemd-unified");
8c03ec
+    if (virTestRun("New cgroup for self (systemd-unified)",
8c03ec
+                   testCgroupNewForSelfSystemdUnified, NULL) < 0) {
8c03ec
+        ret = -1;
8c03ec
+    }
8c03ec
+    cleanupFakeFS(fakerootdir);
8c03ec
+
8c03ec
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
8c03ec
 }
8c03ec
 
8c03ec
-- 
8c03ec
2.30.0
8c03ec