[CRIU] [PATCH 8/8] test: libcriu
Ruslan Kuprieiev
kupruser at gmail.com
Wed Dec 4 02:57:31 PST 2013
Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
Makefile | 1 +
test/libcriu/Makefile | 10 +++++++
test/libcriu/loop.sh | 4 +++
test/libcriu/run.sh | 63 +++++++++++++++++++++++++++++++++++++++
test/libcriu/test.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 160 insertions(+)
create mode 100644 test/libcriu/Makefile
create mode 100755 test/libcriu/loop.sh
create mode 100755 test/libcriu/run.sh
create mode 100644 test/libcriu/test.c
diff --git a/Makefile b/Makefile
index cd312e7..347c4f0 100644
--- a/Makefile
+++ b/Makefile
@@ -202,6 +202,7 @@ clean: clean-built
$(Q) $(RM) -r ./test/lib64/
$(Q) $(RM) protobuf-desc-gen.h
$(Q) $(MAKE) -C test/zdtm cleandep clean cleanout
+ $(Q) $(MAKE) -C test/libcriu clean
distclean: clean
$(E) " DISTCLEAN"
diff --git a/test/libcriu/Makefile b/test/libcriu/Makefile
new file mode 100644
index 0000000..830eec1
--- /dev/null
+++ b/test/libcriu/Makefile
@@ -0,0 +1,10 @@
+all: build/test
+
+build/test: build/test.o
+ gcc $^ -lprotobuf -L ../../lib -lcriu -o $@
+
+build/test.o: test.c
+ gcc -c $^ -I ../../lib -I ../../protobuf -o $@
+
+clean:
+ rm -rf build
diff --git a/test/libcriu/loop.sh b/test/libcriu/loop.sh
new file mode 100755
index 0000000..0ab34ce
--- /dev/null
+++ b/test/libcriu/loop.sh
@@ -0,0 +1,4 @@
+#!/bin/bash
+while :; do
+ sleep 1
+done
diff --git a/test/libcriu/run.sh b/test/libcriu/run.sh
new file mode 100755
index 0000000..04a8c10
--- /dev/null
+++ b/test/libcriu/run.sh
@@ -0,0 +1,63 @@
+#!/bin/bash
+
+source ../env.sh || exit 1
+
+export PROTODIR=`readlink -f "${PWD}/../../protobuf"`
+
+echo $PROTODIR
+
+LOOP_PID=0
+
+function title_print {
+ echo -e "\n**************************************************"
+ echo -e "\t\t"$1
+ echo -e "**************************************************\n"
+
+}
+
+function _exit {
+ if [ $1 -ne 0 ]; then
+ echo "FAIL"
+ fi
+
+ if [ $LOOP_PID -ne 0 ]; then
+ kill -SIGTERM $LOOP_PID
+ fi
+
+ title_print "Shutdown service server"
+ kill -SIGTERM `cat pidfile`
+
+ exit $1
+}
+
+function check_and_term {
+ title_print "Check and term $1"
+ ps -C $1
+ pkill $1
+}
+
+title_print "Build programs"
+make clean
+mkdir build
+cd build
+mkdir imgs_loop
+mkdir imgs_test
+make -C ../ || { echo "FAIL"; exit 1; }
+
+title_print "Start service server"
+${CRIU} service -v4 -o service.log --address criu_service.socket -d --pidfile `pwd`/pidfile || { echo "FAIL"; exit 1; }
+
+title_print "Run loop.sh"
+setsid ../loop.sh < /dev/null &> loop.log &
+LOOP_PID=${!}
+echo "pid ${LOOP_PID}"
+
+title_print "Run test.c"
+LD_LIBRARY_PATH=../../../lib
+export LD_LIBRARY_PATH
+./test ${LOOP_PID} || _exit $?
+
+title_print "Restore test.c"
+${CRIU} restore -v4 -o restore-test.log -D imgs_test --shell-job || _exit $?
+
+_exit 0
diff --git a/test/libcriu/test.c b/test/libcriu/test.c
new file mode 100644
index 0000000..e45abbc
--- /dev/null
+++ b/test/libcriu/test.c
@@ -0,0 +1,82 @@
+#include "criu.h"
+#include <fcntl.h>
+
+static void criu_error(int ret)
+{
+ switch (ret) {
+ case CRIU_ECONNECT:
+ puts("CRIU_ECONNECT");
+ break;
+ case CRIU_EREQ:
+ puts("CRIU_EREQ");
+ break;
+ case CRIU_ERESP:
+ puts("CRIU_ERESP");
+ break;
+ case CRIU_ECHECK:
+ puts("CRIU_ECONNECT");
+ break;
+
+ default:
+ puts("Unknown error");
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ int ret;
+ CriuOpts opts = CRIU_OPTS__INIT;
+ CriuResp *resp = NULL;
+
+ criu_set_service_address("criu_service.socket");
+
+ puts("--- Check ---");
+ ret = criu_check();
+ if (ret < 0) {
+ criu_error(ret);
+ return -1;
+ } else
+ puts("Success");
+
+ puts("--- Dump loop ---");
+ opts.has_pid = true;
+ opts.pid = atoi(argv[1]);
+ opts.has_log_level = true;
+ opts.log_level = 4;
+ opts.images_dir_fd = open("imgs_loop", O_DIRECTORY);
+
+ ret = criu_dump(&opts, &resp);
+ if (ret < 0) {
+ criu_error(ret);
+ return -1;
+ } else if (resp->success)
+ puts("Success");
+
+ puts("--- Restore loop ---");
+ ret = criu_restore(&opts, &resp);
+ if (ret < 0) {
+ criu_error(ret);
+ return -1;
+ } else if (resp->success)
+ puts("Success");
+
+ puts("--- Dump myself ---");
+ opts.has_pid = false; /* unset pid, so criu will dump requesting process */
+ opts.has_leave_running = true;
+ opts.leave_running = true;
+ opts.has_shell_job = true;
+ opts.shell_job = true;
+ opts.images_dir_fd = open("imgs_test", O_DIRECTORY);
+
+ ret = criu_dump(&opts, &resp);
+ if (ret < 0) {
+ criu_error(ret);
+ return -1;
+ } else if (resp->success) {
+ puts("Success");
+ if (resp->dump->has_restored && resp->dump->restored)
+ puts("Restored");
+ }
+
+ return 0;
+}
--
1.8.1.2
More information about the CRIU
mailing list