[CRIU] [PATCH 2/2] test: Add test for notifications via libcriu

Pavel Emelyanov xemul at parallels.com
Wed Jun 25 08:36:29 PDT 2014


Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 test/libcriu/Makefile      | 10 ++++-
 test/libcriu/run.sh        |  9 +++++
 test/libcriu/test_notify.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+), 2 deletions(-)
 create mode 100644 test/libcriu/test_notify.c

diff --git a/test/libcriu/Makefile b/test/libcriu/Makefile
index c6a8697..a69b0bf 100644
--- a/test/libcriu/Makefile
+++ b/test/libcriu/Makefile
@@ -1,4 +1,4 @@
-all: test_sub test_self
+all: test_sub test_self test_notify
 .PHONY: all
 
 run: all
@@ -16,9 +16,15 @@ test_self: test_self.o lib.o
 test_self.o: test_self.c
 	gcc -c $^ -I ../../lib -o $@
 
+test_notify: test_notify.o lib.o
+	gcc $^ -L ../../lib -lcriu -o $@
+
+test_notify.o: test_notify.c
+	gcc -c $^ -I ../../lib -o $@
+
 lib.o: lib.c
 	gcc -c $^ -I ../../lib -o $@
 
 clean:
-	rm -rf test_sub test_sub.o test_self test_sub.o
+	rm -rf test_sub test_sub.o test_self test_sub.o test_notify test_notify.o
 .PHONY: clean
diff --git a/test/libcriu/run.sh b/test/libcriu/run.sh
index d96a2c6..cc24066 100755
--- a/test/libcriu/run.sh
+++ b/test/libcriu/run.sh
@@ -10,6 +10,7 @@ rm -f ./libcriu.so.1
 echo "== Prepare"
 make test_sub || { echo "FAIL build sub"; exit 1; }
 make test_self || { echo "FAIL build self"; exit 1; }
+make test_notify || { echo "FAIL build notify"; exit 1; }
 
 mkdir -p wdir/s/
 echo "== Start service"
@@ -38,6 +39,14 @@ if ! ./test_self wdir/s/cs.sk wdir/i/self/; then
 	RESULT=1
 fi
 
+# C/R with notifications
+echo "== Test NOTIFY"
+mkdir -p wdir/i/not/
+if ! ./test_notify wdir/s/cs.sk wdir/i/not/; then
+	echo "test_notify: FAIL"
+	RESULT=1
+fi
+
 echo "== Stopping service"
 kill -TERM $(cat wdir/s/pidfile)
 [ $RESULT ] && echo "Success" || echo "FAIL"
diff --git a/test/libcriu/test_notify.c b/test/libcriu/test_notify.c
new file mode 100644
index 0000000..ca9bbc7
--- /dev/null
+++ b/test/libcriu/test_notify.c
@@ -0,0 +1,93 @@
+#include "criu.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <signal.h>
+#include "lib.h"
+
+#define SUCC_ECODE	42
+
+static int actions_called = 0;
+static int notify(char *action)
+{
+	printf("ACTION: %s\n", action);
+	actions_called++;
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int pid, ret, fd, p[2];
+
+	printf("--- Start loop ---\n");
+	pipe(p);
+	pid = fork();
+	if (pid < 0) {
+		perror("Can't");
+		return -1;
+	}
+
+	if (!pid) {
+		printf("   `- loop: initializing\n");
+		if (setsid() < 0)
+			exit(1);
+
+		close(0);
+		close(1);
+		close(2);
+		close(p[0]);
+
+		ret = SUCC_ECODE;
+		write(p[1], &ret, sizeof(ret));
+		close(p[1]);
+
+		while (1)
+			sleep(1);
+
+		exit(SUCC_ECODE);
+	}
+
+	close(p[1]);
+
+	/* Wait for kid to start */
+	ret = -1;
+	read(p[0], &ret, sizeof(ret));
+	if (ret != SUCC_ECODE) {
+		printf("Error starting loop\n");
+		goto err;
+	}
+
+	/* Wait for pipe to get closed, then dump */
+	read(p[0], &ret, 1);
+	close(p[0]);
+
+	printf("--- Dump loop ---\n");
+	criu_init_opts();
+	criu_set_service_address(argv[1]);
+	criu_set_pid(pid);
+	criu_set_log_file("dump.log");
+	criu_set_log_level(4);
+	criu_set_notify(notify);
+	fd = open(argv[2], O_DIRECTORY);
+	criu_set_images_dir_fd(fd);
+
+	ret = criu_dump();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		kill(pid, SIGKILL);
+		goto err;
+	}
+
+	printf("   `- Dump succeeded\n");
+	ret = 0;
+err:
+	waitpid(pid, NULL, 0);
+	if (ret || !actions_called) {
+		printf("FAIL (%d/%d)\n", ret, actions_called);
+		return 1;
+	}
+
+	printf("   `- Success (%d actions)\n", actions_called);
+	return 0;
+}
-- 
1.8.4.2




More information about the CRIU mailing list