render / rpms / libvirt

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