[CRIU] [PATCH 2/2] tests: add a test for --cgroup-root
Tycho Andersen
tycho.andersen at canonical.com
Fri Aug 15 15:02:22 PDT 2014
Signed-off-by: Tycho Andersen <tycho.andersen at canonical.com>
---
test/zdtm.sh | 2 +
test/zdtm/live/static/Makefile | 1 +
test/zdtm/live/static/cgroup02.c | 157 ++++++++++++++++++++++++++++++++++++
test/zdtm/live/static/cgroup02.hook | 27 +++++++
test/zdtm/live/static/cgroup02.opts | 1 +
5 files changed, 188 insertions(+)
create mode 100644 test/zdtm/live/static/cgroup02.c
create mode 100755 test/zdtm/live/static/cgroup02.hook
create mode 100644 test/zdtm/live/static/cgroup02.opts
diff --git a/test/zdtm.sh b/test/zdtm.sh
index da85c92..06152a6 100755
--- a/test/zdtm.sh
+++ b/test/zdtm.sh
@@ -173,6 +173,7 @@ static/netns-nf
static/netns
static/cgroup00
static/cgroup01
+static/cgroup02
ns/static/clean_mntns
"
@@ -218,6 +219,7 @@ mountpoints
inotify_irmap
cgroup00
cgroup01
+cgroup02
clean_mntns
deleted_dev
mntns_open
diff --git a/test/zdtm/live/static/Makefile b/test/zdtm/live/static/Makefile
index 07d1114..f2ed101 100644
--- a/test/zdtm/live/static/Makefile
+++ b/test/zdtm/live/static/Makefile
@@ -165,6 +165,7 @@ TST_DIR = \
cgroup00 \
rmdir_open \
cgroup01 \
+ cgroup02 \
mntns_open \
mntns_link_remap \
mntns_link_ghost \
diff --git a/test/zdtm/live/static/cgroup02.c b/test/zdtm/live/static/cgroup02.c
new file mode 100644
index 0000000..b6f2b51
--- /dev/null
+++ b/test/zdtm/live/static/cgroup02.c
@@ -0,0 +1,157 @@
+#include <unistd.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/mount.h>
+#include "zdtmtst.h"
+
+const char *test_doc = "Check that empty cgroups are preserved";
+const char *test_author = "Tycho Andersen <tycho.andersen at canonical.com>";
+
+char *dirname;
+TEST_OPTION(dirname, string, "cgroup directory name", 1);
+static const char *cgname = "zdtmtst";
+static const char *subname = "oldroot";
+static const char *cgname2 = "defaultroot";
+
+int mount_and_add(const char *controller, const char *path)
+{
+ char aux[1024], paux[1024], subdir[1024];
+ int cgfd, l;
+
+ if (mkdir(dirname, 0700) < 0 && errno != EEXIST) {
+ err("Can't make dir");
+ return -1;
+ }
+
+ sprintf(subdir, "%s/%s", dirname, controller);
+ if (mkdir(subdir, 0700) < 0) {
+ err("Can't make dir");
+ return -1;
+ }
+
+ sprintf(aux, "none,name=%s", controller);
+ if (mount("none", subdir, "cgroup", 0, aux)) {
+ err("Can't mount cgroups");
+ goto err_rd;
+ }
+
+ sprintf(paux, "%s/%s", subdir, path);
+ mkdir(paux, 0600);
+
+ l = sprintf(aux, "%d", getpid());
+ sprintf(paux, "%s/%s/tasks", subdir, path);
+
+ cgfd = open(paux, O_WRONLY);
+ if (cgfd < 0) {
+ err("Can't open tasks");
+ goto err_rs;
+ }
+
+ l = write(cgfd, aux, l);
+ close(cgfd);
+
+ if (l < 0) {
+ err("Can't move self to subcg");
+ goto err_rs;
+ }
+
+ return 0;
+err_rs:
+ umount(dirname);
+err_rd:
+ rmdir(dirname);
+ return -1;
+}
+
+bool test_exists(char *mountinfo_line, char *path)
+{
+ char aux[1024], paux[1024];
+ struct stat st;
+
+ sscanf(mountinfo_line, "%*d %*d %*d:%*d %*s %s", aux);
+ test_msg("found cgroup at %s\n", aux);
+
+ sprintf(paux, "%s/%s", aux, path);
+ if (stat(paux, &st)) {
+ return false;
+ }
+
+ if (!S_ISDIR(st.st_mode)) {
+ return false;
+ }
+
+ return true;
+}
+
+int main(int argc, char **argv)
+{
+ FILE *cgf;
+ bool found_zdtmtstroot = false, found_newroot = false;
+ char paux[1024];
+ int ret = -1;
+
+ test_init(argc, argv);
+
+ if (mount_and_add(cgname, subname))
+ goto out;
+ if (mount_and_add(cgname2, subname)) {
+ sprintf(paux, "%s/%s", dirname, cgname);
+ umount(paux);
+ rmdir(paux);
+ goto out;
+ }
+
+ test_daemon();
+ test_waitsig();
+
+ cgf = fopen("/proc/self/mountinfo", "r");
+ if (cgf == NULL) {
+ fail("No mountinfo file");
+ goto out_umount;
+ }
+
+ while (fgets(paux, sizeof(paux), cgf)) {
+ char *s;
+
+ s = strstr(paux, cgname);
+ if (s && test_exists(paux, "zdtmtstroot")) {
+ found_zdtmtstroot = true;
+ }
+
+ s = strstr(paux, cgname2);
+ if (s && test_exists(paux, "newroot")) {
+ found_newroot = true;
+ }
+ }
+
+ if (!found_zdtmtstroot) {
+ fail("oldroot not rewritten to zdtmtstroot!\n");
+ goto out_close;
+ }
+
+ if (!found_newroot) {
+ fail("oldroot not rewritten to newroot!\n");
+ goto out_close;
+ }
+
+ pass();
+ ret = 0;
+
+
+out_close:
+ fclose(cgf);
+out_umount:
+ sprintf(paux, "%s/%s", dirname, cgname);
+ umount(paux);
+ rmdir(paux);
+
+ sprintf(paux, "%s/%s", dirname, cgname2);
+ umount(paux);
+ rmdir(paux);
+out:
+ return ret;
+}
diff --git a/test/zdtm/live/static/cgroup02.hook b/test/zdtm/live/static/cgroup02.hook
new file mode 100755
index 0000000..c99f4ea
--- /dev/null
+++ b/test/zdtm/live/static/cgroup02.hook
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+set -e
+
+rmroots() {
+ echo "Cleaning $tname"
+
+ set +e
+ rmdir "$tname/oldroot"
+ rmdir "$tname/newroot"
+ rmdir "$tname/zdtmtstroot"
+ set -e
+
+ echo "Left there is:"
+ ls "$tname"
+ umount "$tname"
+ rmdir "$tname"
+}
+
+tname=$(mktemp -d cgclean.XXXXXX)
+mount -t cgroup none $tname -o "none,name=zdtmtst"
+rmroots
+
+tname=$(mktemp -d cgclean.XXXXXX)
+mount -t cgroup none $tname -o "none,name=defaultroot"
+rmroots
+
diff --git a/test/zdtm/live/static/cgroup02.opts b/test/zdtm/live/static/cgroup02.opts
new file mode 100644
index 0000000..159938e
--- /dev/null
+++ b/test/zdtm/live/static/cgroup02.opts
@@ -0,0 +1 @@
+--manage-cgroups --cgroup-root /newroot --cgroup-root name=zdtmtst:/zdtmtstroot
--
1.9.1
More information about the CRIU
mailing list