[CRIU] [PATCH 4/4] test: A simple test for criu_restore_sub call

Pavel Emelyanov xemul at parallels.com
Tue Jun 17 10:11:29 PDT 2014


Which is at the same time the demonstration of how to do the trick.

Signed-off-by: Pavel Emelyanov <xemul at parallels.com>
---
 test/libcriu/Makefile   | 10 +++++--
 test/libcriu/run_sub.sh | 32 ++++++++++++++++++++
 test/libcriu/test_sub.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 118 insertions(+), 2 deletions(-)
 create mode 100644 test/libcriu/run_sub.sh
 create mode 100644 test/libcriu/test_sub.c

diff --git a/test/libcriu/Makefile b/test/libcriu/Makefile
index a1af1f5..474f3d1 100644
--- a/test/libcriu/Makefile
+++ b/test/libcriu/Makefile
@@ -1,6 +1,12 @@
-all: build/test
+all: build/test test_sub
 .PHONY: all
 
+test_sub: test_sub.o
+	gcc $^ -L ../../lib -lcriu -o $@
+
+test_sub.o: test_sub.c
+	gcc -c $^ -I ../../lib -o $@
+
 build/test: build/test.o
 	gcc $^ -L ../../lib -lcriu -o $@
 
@@ -8,5 +14,5 @@ build/test.o: test.c
 	gcc -c $^ -I ../../lib -o $@
 
 clean:
-	rm -rf build
+	rm -rf build test_sub test_sub.o
 .PHONY: clean
diff --git a/test/libcriu/run_sub.sh b/test/libcriu/run_sub.sh
new file mode 100644
index 0000000..ca2f9e2
--- /dev/null
+++ b/test/libcriu/run_sub.sh
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+source ../env.sh || exit 1
+
+LOOP_PID=0
+
+echo "== Clean"
+make clean
+rm -rf wdir
+rm -f ./libcriu.so.1
+
+echo "== Prepare"
+make test_sub || { echo "FAIL"; exit 1; }
+
+mkdir -p wdir/s/
+mkdir -p wdir/i/
+echo "== Start service"
+${CRIU} service -v4 -o service.log --address cs.sk -d --pidfile pidfile -W wdir/s/ || { echo "FAIL"; exit 1; }
+ 
+echo "== Run loop.sh"
+setsid ./loop.sh < /dev/null &> loop.log &
+LOOP_PID=${!}
+echo "pid ${LOOP_PID}"
+
+echo "== Run test_sub"
+ln -s ../../lib/libcriu.so libcriu.so.1
+export LD_LIBRARY_PATH=.
+export PATH="`dirname ${BASH_SOURCE[0]}`/../../:$PATH"
+./test_sub ${LOOP_PID} wdir/s/cs.sk wdir/i/
+
+echo "== Stopping service"
+kill -TERM $(cat wdir/s/pidfile)
diff --git a/test/libcriu/test_sub.c b/test/libcriu/test_sub.c
new file mode 100644
index 0000000..7a1b3c9
--- /dev/null
+++ b/test/libcriu/test_sub.c
@@ -0,0 +1,78 @@
+#include "criu.h"
+#include <fcntl.h>
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+
+static void what_err_ret_mean(ret)
+{
+	/* NOTE: errno is set by libcriu */
+	switch (ret) {
+	case -EBADE:
+		perror("RPC has returned fail");
+		break;
+	case -ECONNREFUSED:
+		perror("Unable to connect to CRIU");
+		break;
+	case -ECOMM:
+		perror("Unable to send/recv msg to/from CRIU");
+		break;
+	case -EINVAL:
+		perror("CRIU doesn't support this type of request."
+		       "You should probably update CRIU");
+		break;
+	case -EBADMSG:
+		perror("Unexpected response from CRIU."
+		       "You should probably update CRIU");
+		break;
+	default:
+		perror("Unknown error type code."
+		       "You should probably update CRIU");
+	}
+}
+
+int main(int argc, char **argv)
+{
+	int ret, fd;
+
+	printf("--- Dump loop ---\n");
+	criu_init_opts();
+	criu_set_service_address(argv[2]);
+	criu_set_pid(atoi(argv[1]));
+	criu_set_log_file("dump.log");
+	criu_set_log_level(4);
+	fd = open(argv[3], O_DIRECTORY);
+	criu_set_images_dir_fd(fd);
+
+	ret = criu_dump();
+	if (ret < 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	} else if (ret == 0)
+		printf("   `- Success\n");
+
+	sleep(1); /* Ugly -- wait for pid from dump to free */
+
+	printf("--- Restore loop ---\n");
+	criu_init_opts();
+	criu_set_log_level(4);
+	criu_set_log_file("restore.log");
+	criu_set_images_dir_fd(fd);
+
+	printf("PATH=%s\n", getenv("PATH"));
+	ret = criu_restore_sub();
+	if (ret <= 0) {
+		what_err_ret_mean(ret);
+		return -1;
+	}
+
+	printf("   Restore returned pid %d\n", ret);
+	kill(ret, 9);
+	if (waitpid(ret, NULL, 0) < 0) {
+		perror("   Can't wait kid");
+		return -1;
+	}
+
+	printf("   `- Success\n");
+	return 0;
+}
-- 
1.8.4.2


More information about the CRIU mailing list