[CRIU] [PATCH 1/2] test: crit: enhance test process

Ruslan Kuprieiev kupruser at gmail.com
Thu Jan 22 07:59:14 PST 2015


Use mature technics to organize test process.

Signed-off-by: Ruslan Kuprieiev <kupruser at gmail.com>
---
 test/rpc/Makefile        |  1 +
 test/rpc/errno.py        | 16 ++++----
 test/rpc/ps_test.py      | 20 +++++-----
 test/rpc/restore-loop.py | 15 +++++---
 test/rpc/run.sh          | 97 ++++++++++++++++++++++++++++--------------------
 test/rpc/test.c          | 22 +++++------
 test/rpc/test.py         | 18 ++++-----
 7 files changed, 104 insertions(+), 85 deletions(-)

diff --git a/test/rpc/Makefile b/test/rpc/Makefile
index 2287b30..2cc13fd 100644
--- a/test/rpc/Makefile
+++ b/test/rpc/Makefile
@@ -2,6 +2,7 @@ all: test-c rpc_pb2.py
 .PHONY: all
 
 run: all
+	mkdir -p build
 	./run.sh
 
 test-c: rpc.pb-c.o test.o
diff --git a/test/rpc/errno.py b/test/rpc/errno.py
index 8c8dd96..4560b47 100755
--- a/test/rpc/errno.py
+++ b/test/rpc/errno.py
@@ -2,25 +2,25 @@
 # Test criu errno
 
 import socket, os, imp, sys, errno
-
-p = os.getcwd()
-sys.path.append(p)
 import rpc_pb2 as rpc
+import argparse
+
+parser = argparse.ArgumentParser(description="Test errno reported by CRIU RPC")
+parser.add_argument('socket', type = str, help = "CRIU service socket")
+parser.add_argument('dir', type = str, help = "Directory where CRIU images should be placed")
 
+args = vars(parser.parse_args())
 
 # Prepare dir for images
 class test:
 	def __init__(self):
-		imgs_path = "imgs_errno"
-		if not os.path.exists(imgs_path):
-			os.makedirs(imgs_path)
-		self.imgs_fd = os.open(imgs_path, os.O_DIRECTORY)
+		self.imgs_fd = os.open(args['dir'], os.O_DIRECTORY)
 		self.s = -1
 		self._MAX_MSG_SIZE = 1024
 
 	def connect(self):
 		self.s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
-		self.s.connect('criu_service.socket')
+		self.s.connect(args['socket'])
 
 	def get_base_req(self):
 		req			= rpc.criu_req()
diff --git a/test/rpc/ps_test.py b/test/rpc/ps_test.py
index c0a2e93..b8e57cf 100755
--- a/test/rpc/ps_test.py
+++ b/test/rpc/ps_test.py
@@ -1,14 +1,18 @@
 #!/usr/bin/python
 
 import socket, os, imp, sys
-
-p = os.getcwd()
-sys.path.append(p)
 import rpc_pb2 as rpc
+import argparse
+
+parser = argparse.ArgumentParser(description="Test page-server using CRIU RPC")
+parser.add_argument('socket', type = str, help = "CRIU service socket")
+parser.add_argument('dir', type = str, help = "Directory where CRIU images should be placed")
+
+args = vars(parser.parse_args())
 
 # Connect to service socket
 s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
-s.connect('criu_service.socket')
+s.connect(args['socket'])
 
 # Start page-server
 print 'Starting page-server'
@@ -16,11 +20,7 @@ req			= rpc.criu_req()
 req.type		= rpc.PAGE_SERVER
 req.opts.log_file	= 'page-server.log'
 req.opts.log_level	= 4
-
-if not os.path.exists('ps_test'):
-	os.makedirs('ps_test')
-
-req.opts.images_dir_fd	= os.open('ps_test', os.O_DIRECTORY)
+req.opts.images_dir_fd	= os.open(args['dir'], os.O_DIRECTORY)
 
 s.send(req.SerializeToString())
 
@@ -58,7 +58,7 @@ req.opts.leave_running	= True
 
 s.close()
 s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
-s.connect('criu_service.socket')
+s.connect(args['socket'])
 s.send(req.SerializeToString())
 
 resp.ParseFromString(s.recv(MAX_MSG_SIZE))
diff --git a/test/rpc/restore-loop.py b/test/rpc/restore-loop.py
index bf91cb5..70cb64e 100755
--- a/test/rpc/restore-loop.py
+++ b/test/rpc/restore-loop.py
@@ -1,21 +1,24 @@
 #!/usr/bin/python
 
 import socket, os, imp, sys
-
-p = os.getcwd()
-sys.path.append(p)
 import rpc_pb2 as rpc
+import argparse
+
+parser = argparse.ArgumentParser(description="Test ability to restore a process from images using CRIU RPC")
+parser.add_argument('socket', type = str, help = "CRIU service socket")
+parser.add_argument('dir', type = str, help = "Directory where CRIU images could be found")
+
+args = vars(parser.parse_args())
 
 # Connect to service socket
 s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
-s.connect('criu_service.socket')
+s.connect(args['socket'])
 
 # Create criu msg, set it's type to dump request
 # and set dump options. Checkout more options in protobuf/rpc.proto
 req			= rpc.criu_req()
 req.type		= rpc.RESTORE
-
-req.opts.images_dir_fd	= os.open('imgs_loop', os.O_DIRECTORY)
+req.opts.images_dir_fd	= os.open(args['dir'], os.O_DIRECTORY)
 
 # Send request
 s.send(req.SerializeToString())
diff --git a/test/rpc/run.sh b/test/rpc/run.sh
index b9455f6..f14752d 100755
--- a/test/rpc/run.sh
+++ b/test/rpc/run.sh
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -e
+
 source ../env.sh || exit 1
 
 export PROTODIR=`readlink -f "${PWD}/../../protobuf"`
@@ -13,61 +15,76 @@ function title_print {
 
 }
 
-function _exit {
-	if [ $1 -ne 0 ]; then
-		echo "FAIL"
-	fi
+function start_server {
+	title_print "Start service server"
+	${CRIU} service -v4 -W build -o service.log --address criu_service.socket -d --pidfile pidfile
+}
 
+function stop_server {
 	title_print "Shutdown service server"
-	kill -SIGTERM `cat pidfile`
+	kill -SIGTERM $(cat build/pidfile)
+}
 
-	exit $1
+function test_c {
+	mkdir -p build/imgs_c
+
+	title_print "Run test-c"
+	./test-c build/criu_service.socket build/imgs_c
+
+	title_print "Restore test-c"
+	${CRIU} restore -v4 -o restore-c.log -D build/imgs_c --shell-job
 }
 
-function check_and_term {
-	title_print "Check and term $1"
-	ps -C $1
-	pkill $1
+function test_py {
+	mkdir -p build/imgs_py
+
+	title_print "Run test-py"
+	./test.py build/criu_service.socket build/imgs_py
+
+	title_print "Restore test-py"
+	${CRIU} restore -v4 -o restore-py.log -D build/imgs_py --shell-job
 }
 
-title_print "Build programs"
-make clean
-mkdir build
-cd build
-make -C ../ || { echo "FAIL"; exit 1; }
+function test_restore_loop {
+	mkdir -p build/imgs_loop
 
-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 &> build/loop.log &
+	P=${!}
+	echo "pid ${P}"
 
-title_print "Run test-c"
-../test-c || _exit $?
+	title_print "Dump loop.sh"
+	${CRIU} dump -v4 -o dump-loop.log -D build/imgs_loop -t ${P}
 
-title_print "Run test-py"
-../test.py || _exit $?
+	title_print "Run restore-loop"
+	./restore-loop.py build/criu_service.socket build/imgs_loop
+	kill -SIGTERM ${P}
+}
 
-title_print "Restore test-c"
-${CRIU} restore -v4 -o restore-c.log -D imgs_c --shell-job || _exit $?
+function test_ps {
+	mkdir -p build/imgs_ps
 
-title_print "Restore test-py"
-${CRIU} restore -v4 -o restore-py.log -D imgs_py --shell-job || _exit $?
+	title_print "Run ps_test"
+	./ps_test.py build/criu_service.socket build/imgs_ps
+}
 
-title_print "Run loop.sh"
-setsid ../loop.sh < /dev/null &> loop.log &
-P=${!}
-echo "pid ${P}"
+function test_errno {
+	mkdir -p build/imgs_errno
+
+	title_print "Run cr_errno test"
+	./errno.py build/criu_service.socket build/imgs_errno
+}
 
-title_print "Dump loop.sh"
-mkdir imgs_loop
-${CRIU} dump -v4 -o dump-loop.log -D imgs_loop -t ${P} || _exit $?
+trap 'echo "FAIL"; stop_server' EXIT
 
-title_print "Run restore-loop"
-../restore-loop.py || _exit $?
-kill -SIGTERM ${P}
+start_server
 
-title_print "Run ps_test"
-../ps_test.py || _exit $?
+test_c
+test_py
+test_restore_loop
+test_ps
+test_errno
 
-title_print "Run cr_errno test"
-../errno.py || _exit $?
+stop_server
 
-_exit 0
+trap 'echo "Success"' EXIT
diff --git a/test/rpc/test.c b/test/rpc/test.c
index 3f356fa..9297675 100644
--- a/test/rpc/test.c
+++ b/test/rpc/test.c
@@ -50,7 +50,7 @@ static int send_req(int socket_fd, CriuReq *req)
 	return 0;
 }
 
-int main()
+int main(int argc, char *argv[])
 {
 	CriuReq req		= CRIU_REQ__INIT;
 	CriuResp *resp		= NULL;
@@ -60,22 +60,20 @@ int main()
 	socklen_t addr_len;
 	struct stat st = {0};
 
+	if (argc != 3) {
+		fprintf(stderr, "Usage: test-c criu-service.socket imgs_dir");
+		return -1;
+	}
+
 	/*
 	 * Open a directory, in which criu will
 	 * put images
 	 */
-	umask(0);
-
-	if (stat("imgs_c", &st)) {
-		if (mkdir("imgs_c", 0666)) {
-			perror("Can't create dir");
-			return -1;
-		}
-	}
 
-	dir_fd = open("imgs_c", O_DIRECTORY);
+	puts(argv[2]);
+	dir_fd = open(argv[2], O_DIRECTORY);
 	if (dir_fd == -1) {
-		perror("Can't open dir");
+		perror("Can't open imgs dir");
 		return -1;
 	}
 
@@ -116,7 +114,7 @@ int main()
 	memset(&addr, 0, sizeof(addr));
 	addr.sun_family = AF_LOCAL;
 
-	strcpy(addr.sun_path, "criu_service.socket");
+	strcpy(addr.sun_path, argv[1]);
 
 	addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family);
 
diff --git a/test/rpc/test.py b/test/rpc/test.py
index 5c30ef2..01daf25 100755
--- a/test/rpc/test.py
+++ b/test/rpc/test.py
@@ -1,14 +1,18 @@
 #!/usr/bin/python
 
 import socket, os, imp, sys
-
-p = os.getcwd()
-sys.path.append(p)
 import rpc_pb2 as rpc
+import argparse
+
+parser = argparse.ArgumentParser(description="Test dump/restore using CRIU RPC")
+parser.add_argument('socket', type = str, help = "CRIU service socket")
+parser.add_argument('dir', type = str, help = "Directory where CRIU images should be placed")
+
+args = vars(parser.parse_args())
 
 # Connect to service socket
 s = socket.socket(socket.AF_UNIX, socket.SOCK_SEQPACKET)
-s.connect('criu_service.socket')
+s.connect(args['socket'])
 
 # Create criu msg, set it's type to dump request
 # and set dump options. Checkout more options in protobuf/rpc.proto
@@ -17,11 +21,7 @@ req.type		= rpc.DUMP
 req.opts.leave_running	= True
 req.opts.shell_job	= True
 req.opts.log_level	= 4
-
-if not os.path.exists('imgs_py'):
-	os.makedirs('imgs_py')
-
-req.opts.images_dir_fd	= os.open('imgs_py', os.O_DIRECTORY)
+req.opts.images_dir_fd	= os.open(args['dir'], os.O_DIRECTORY)
 
 # Send request
 s.send(req.SerializeToString())
-- 
2.1.0



More information about the CRIU mailing list