Skip to content

Commit 0b25104

Browse files
committed
test/others: add tests for check() with pycriu
Signed-off-by: Radostin Stoyanov <[email protected]>
1 parent 3d265d4 commit 0b25104

File tree

9 files changed

+215
-0
lines changed

9 files changed

+215
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,7 @@ ruff:
452452
test/inhfd/*.py \
453453
test/others/rpc/config_file.py \
454454
test/others/action-script/check_actions.py \
455+
test/others/pycriu/*.py \
455456
lib/pycriu/criu.py \
456457
lib/pycriu/__init__.py \
457458
lib/pycriu/images/pb2dict.py \

test/others/pycriu/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/

test/others/pycriu/Makefile

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.SHELLFLAGS := -eu -o pipefail -c
2+
.ONESHELL:
3+
4+
CRIU ?= ../../../criu/criu
5+
BUILD_DIR ?= build
6+
SOCKET_NAME ?= criu_service.socket
7+
PIDFILE_NAME ?= pidfile
8+
SERVICE_LOG ?= service.log
9+
PYTHON ?= python3
10+
11+
PIDFILE := $(BUILD_DIR)/$(PIDFILE_NAME)
12+
CRIU_SOCKET := $(BUILD_DIR)/$(SOCKET_NAME)
13+
STATUS_FIFO := $(BUILD_DIR)/startup.status
14+
STATUS_FD := 200
15+
16+
run: start
17+
cleanup() { $(MAKE) --no-print-directory stop || true; }
18+
trap cleanup EXIT INT TERM
19+
"$(PYTHON)" test_check.py
20+
"$(PYTHON)" test_check_fail.py
21+
"$(PYTHON)" test_check_images_dir.py
22+
"$(PYTHON)" test_check_work_dir_fd.py
23+
24+
start:
25+
mkdir -p "$(BUILD_DIR)"
26+
if [ -s "$(PIDFILE)" ] && kill -0 "$$(cat "$(PIDFILE)")" 2>/dev/null; then
27+
echo "Service running (PID $$(cat "$(PIDFILE)"))."
28+
exit 0
29+
fi
30+
if ! command -v "$(CRIU)" >/dev/null 2>&1; then
31+
echo "CRIU not found at $(CRIU)"
32+
exit 1
33+
fi
34+
mkfifo "$(STATUS_FIFO)"
35+
exec $(STATUS_FD)<>"$(STATUS_FIFO)"
36+
"$(CRIU)" service \
37+
-v4 \
38+
-W "$(BUILD_DIR)" \
39+
--address "$(SOCKET_NAME)" \
40+
-d \
41+
--pidfile "$(PIDFILE_NAME)" \
42+
-o "$(SERVICE_LOG)" \
43+
--status-fd "$(STATUS_FD)"
44+
"$(PYTHON)" read.py "$(STATUS_FIFO)"
45+
46+
stop:
47+
if [ ! -s "$(PIDFILE)" ]; then
48+
echo "pidfile missing or empty"
49+
exit 1
50+
fi
51+
pid=$$(cat "$(PIDFILE)")
52+
if kill -0 "$$pid" 2>/dev/null; then
53+
kill -9 "$$pid" || true
54+
fi
55+
rm -f "$(PIDFILE)" "$(CRIU_SOCKET)" "$(STATUS_FIFO)"
56+
57+
clean:
58+
if [ -s "$(PIDFILE)" ] && kill -0 "$$(cat "$(PIDFILE)")" 2>/dev/null; then
59+
kill -9 "$$(cat "$(PIDFILE)")" || true
60+
fi
61+
rm -rf "$(BUILD_DIR)"
62+
63+
.PHONY: start stop clean run

test/others/pycriu/read.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../rpc/read.py

test/others/pycriu/test_check.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
5+
# Add ../../../lib so we can import pycriu
6+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
7+
LIB_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "../../../lib"))
8+
if LIB_DIR not in sys.path:
9+
sys.path.insert(0, LIB_DIR)
10+
11+
import pycriu # noqa: E402
12+
13+
def main():
14+
socket_path = os.path.join(SCRIPT_DIR, "build", "criu_service.socket")
15+
16+
criu = pycriu.criu()
17+
criu.use_sk(socket_path)
18+
19+
try:
20+
criu.check()
21+
except Exception as e:
22+
print(f"FAIL: {e}")
23+
return 1
24+
25+
print("PASS")
26+
return 0
27+
28+
if __name__ == "__main__":
29+
sys.exit(main())
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
5+
# Add ../../../lib so we can import pycriu
6+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
7+
LIB_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "../../../lib"))
8+
if LIB_DIR not in sys.path:
9+
sys.path.insert(0, LIB_DIR)
10+
11+
import pycriu # noqa: E402
12+
13+
def main():
14+
socket_path = os.path.join(SCRIPT_DIR, "build", "criu_service.socket")
15+
16+
criu = pycriu.criu()
17+
criu.use_sk(socket_path)
18+
19+
# Intentionally set only log_file (no images/work dir) to ensure check() fails
20+
criu.opts.log_file = "check.log"
21+
22+
try:
23+
criu.check()
24+
except Exception:
25+
print("PASS")
26+
return 0
27+
28+
print("FAIL: check() did not fail when log_file is set without images/work dir")
29+
return 1
30+
31+
if __name__ == "__main__":
32+
sys.exit(main())
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
5+
# Add ../../../lib so we can import pycriu
6+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
7+
LIB_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "../../../lib"))
8+
if LIB_DIR not in sys.path:
9+
sys.path.insert(0, LIB_DIR)
10+
11+
import pycriu # noqa: E402
12+
13+
def _log_path(images_dir, log_file):
14+
return log_file if os.path.isabs(log_file) else os.path.join(images_dir, log_file)
15+
16+
def main():
17+
build_dir = os.path.join(SCRIPT_DIR, "build")
18+
socket_path = os.path.join(build_dir, "criu_service.socket")
19+
20+
criu = pycriu.criu()
21+
criu.use_sk(socket_path)
22+
23+
criu.opts.images_dir = build_dir
24+
criu.opts.log_file = "check.log"
25+
criu.opts.log_level = 4
26+
27+
try:
28+
criu.check()
29+
except Exception as e:
30+
lp = _log_path(build_dir, criu.opts.log_file)
31+
msg = f"FAIL: {e} ({'see log: ' + lp if os.path.exists(lp) else 'no log found'})"
32+
print(msg)
33+
return 1
34+
35+
lp = _log_path(build_dir, criu.opts.log_file)
36+
if not (os.path.isfile(lp) and os.path.getsize(lp) > 0):
37+
print(f"FAIL: log file missing or empty: {lp}")
38+
return 1
39+
40+
print("PASS")
41+
return 0
42+
43+
if __name__ == "__main__":
44+
sys.exit(main())
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import sys
4+
5+
# Add ../../../lib so we can import pycriu
6+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
7+
LIB_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "../../../lib"))
8+
if LIB_DIR not in sys.path:
9+
sys.path.insert(0, LIB_DIR)
10+
11+
import pycriu # noqa: E402
12+
13+
def main():
14+
build_dir = os.path.join(SCRIPT_DIR, "build")
15+
socket_path = os.path.join(build_dir, "criu_service.socket")
16+
os.makedirs(build_dir, exist_ok=True)
17+
18+
# Open a directory FD to use as work_dir_fd (prefer O_PATH if available)
19+
flags = getattr(os, "O_PATH", 0) or os.O_RDONLY
20+
fd = os.open(build_dir, flags)
21+
22+
criu = pycriu.criu()
23+
criu.use_sk(socket_path)
24+
25+
criu.opts.work_dir_fd = fd
26+
criu.opts.log_file = "check.log"
27+
criu.opts.log_level = 4
28+
29+
try:
30+
criu.check()
31+
except Exception as e:
32+
print(f"FAIL: {e}")
33+
return 1
34+
finally:
35+
try:
36+
os.close(fd)
37+
except Exception:
38+
pass
39+
40+
print("PASS")
41+
return 0
42+
43+
if __name__ == "__main__":
44+
sys.exit(main())

test/others/rpc/read.py

100644100755
File mode changed.

0 commit comments

Comments
 (0)