test/other: add a test to check the --shell-job option

Submitted by Andrey Vagin on March 29, 2018, 7:02 a.m.

Details

Message ID 20180329070229.959-1-avagin@virtuozzo.com
State Accepted
Series "test/other: add a test to check the --shell-job option"
Commit 1d5438310b7c458c75b7389bf352a62361cf3d39
Headers show

Commit Message

Andrey Vagin March 29, 2018, 7:02 a.m.
This test creates a pty pair, creates a test process and sets a slave
pty as control terminal for it.

Signed-off-by: Andrei Vagin <avagin@virtuozzo.com>
---
 scripts/travis/travis-tests    |  2 ++
 test/Makefile                  |  2 +-
 test/others/shell-job/Makefile |  2 ++
 test/others/shell-job/run.py   | 65 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 test/others/shell-job/Makefile
 create mode 100755 test/others/shell-job/run.py

Patch hide | download patch | download mbox

diff --git a/scripts/travis/travis-tests b/scripts/travis/travis-tests
index 0a0db0b3e..f7c3a1058 100755
--- a/scripts/travis/travis-tests
+++ b/scripts/travis/travis-tests
@@ -127,6 +127,8 @@  ip net add test
 
 ./test/zdtm.py run --empty-ns -T zdtm/static/socket-tcp*-local --iter 2
 
+make -C test/others/shell-job
+
 pip install flake8
 make lint
 
diff --git a/test/Makefile b/test/Makefile
index 0778d3a46..cf7dacac4 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -11,7 +11,7 @@  all:
 	$(MAKE) zdtm-freezer
 .PHONY: all
 
-TESTS = unix-callback mem-snap rpc libcriu mounts/ext security pipes crit socketpairs overlayfs mnt-ext-dev
+TESTS = unix-callback mem-snap rpc libcriu mounts/ext security pipes crit socketpairs overlayfs mnt-ext-dev shell-job
 
 other:
 	for t in $(TESTS); do				\
diff --git a/test/others/shell-job/Makefile b/test/others/shell-job/Makefile
new file mode 100644
index 000000000..d81733ef4
--- /dev/null
+++ b/test/others/shell-job/Makefile
@@ -0,0 +1,2 @@ 
+run:
+	../../zdtm_ct run.py
diff --git a/test/others/shell-job/run.py b/test/others/shell-job/run.py
new file mode 100755
index 000000000..9e87d12e4
--- /dev/null
+++ b/test/others/shell-job/run.py
@@ -0,0 +1,65 @@ 
+#!/usr/bin/env python2
+import os, pty, sys, subprocess
+import termios, fcntl, time, signal
+
+cr_bin = "../../../criu/criu"
+
+os.chdir(os.getcwd())
+
+def create_pty():
+        (fd1, fd2) = pty.openpty()
+        return (os.fdopen(fd1, "w+"), os.fdopen(fd2, "w+"))
+
+if not os.access("work", os.X_OK):
+    os.mkdir("work", 0755)
+
+open("running", "w").close()
+m,s = create_pty()
+p = os.pipe()
+pr = os.fdopen(p[0], "r")
+pw = os.fdopen(p[1], "w")
+
+pid = os.fork()
+if pid == 0:
+    m.close()
+    os.setsid()
+    os.dup2(s.fileno(), 0)
+    os.dup2(s.fileno(), 1)
+    os.dup2(s.fileno(), 2)
+    fcntl.ioctl(s.fileno(), termios.TIOCSCTTY, 1)
+    pr.close()
+    pw.close()
+    while True:
+        if not os.access("running", os.F_OK):
+            sys.exit(0)
+        time.sleep(1)
+    sys.exit(1)
+
+pw.close()
+pr.read(1)
+
+cmd = [cr_bin, "dump", "-j", "-t", str(pid), "-D", "work", "-v"]
+print("Run: %s" % " ".join(cmd))
+ret = subprocess.Popen(cmd).wait()
+if ret != 0:
+    sys.exit(1)
+os.wait()
+
+os.unlink("running")
+m,s = create_pty()
+cpid = os.fork()
+if cpid == 0:
+    os.setsid()
+    fcntl.ioctl(m.fileno(), termios.TIOCSCTTY, 1)
+    cmd = [cr_bin, "restore", "-j", "-D", "work", "-v"]
+    print("Run: %s" % " ".join(cmd))
+    ret = subprocess.Popen([cr_bin, "restore", "-j", "-D", "work", "-v"]).wait()
+    if ret != 0:
+        sys.exit(1)
+    sys.exit(0)
+
+pid, status = os.wait()
+if status != 0:
+    print("A child process exited with %d" % status)
+    sys.exit(1)
+