Skip to content

Commit

Permalink
test(crit): add E2E test for crit CLI
Browse files Browse the repository at this point in the history
The end-to-end test uses a simple C program to generate image files
and test all commands provided by the crit CLI.

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
  • Loading branch information
snprajwal committed Jul 31, 2022
1 parent 4099193 commit 6b808b6
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ test/test.coverage
test/piggie/piggie
test/phaul/phaul
test/phaul/phaul.coverage
test/crit/*.(img|log|json)
test/crit/stats-*
image
scripts/*.h
scripts/expected.go
Expand Down
7 changes: 5 additions & 2 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ TEST_PAYLOAD := piggie/piggie
TEST_BINARIES := test $(TEST_PAYLOAD) phaul/phaul
COVERAGE_BINARIES := test.coverage phaul/phaul.coverage

all: $(TEST_BINARIES) phaul-test
all: $(TEST_BINARIES) phaul-test crit-test
mkdir -p image
PID=$$(piggie/piggie) && { \
test dump $$PID image && \
Expand All @@ -37,6 +37,9 @@ phaul-test: $(TEST_BINARIES)
pkill -9 piggie; \
}

crit-test:
$(MAKE) -C crit

test.coverage: *.go
$(GO) test \
-covermode=count \
Expand Down Expand Up @@ -78,4 +81,4 @@ clean:
@rm -f $(TEST_BINARIES) $(COVERAGE_BINARIES) codecov
@rm -rf image $(COVERAGE_PATH)

.PHONY: all clean coverage codecov phaul-test
.PHONY: all clean coverage codecov crit-test phaul-test
14 changes: 14 additions & 0 deletions test/crit/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CC ?= gcc

all: test clean

test: loop
@./crit-test.sh

loop: loop.c
$(CC) $^ -o $@

clean:
@rm -f *.img *.log *.json stats-*

.PHONY: all test clean
104 changes: 104 additions & 0 deletions test/crit/crit-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/bin/bash

set -x

CRIT=../../crit/bin/crit

function gen_imgs {
PID=$(./loop)
if ! criu dump -v4 -o dump.log -D ./ -t "$PID"; then
echo "Failed to checkpoint process $PID"
cat dump.log
kill -9 "$PID"
exit 1
fi

images_list=$(ls -1 ./*.img)
if [ -z "$images_list" ]; then
echo "Failed to generate images"
exit 1
fi
}

function run_test1 {
for x in $images_list
do
echo "=== $x"
if [[ $x == *pages* ]]; then
echo "skip"
continue
fi

echo " -- to json"
$CRIT decode -i "$x" -o "$x"".json" --pretty || exit $?
echo " -- to img"
$CRIT encode -i "$x"".json" -o "$x"".json.img" || exit $?
echo " -- cmp"
cmp "$x" "$x"".json.img" || exit $?

echo "=== done"
done
}


function run_test2 {
mapfile -t array <<< "$images_list"

PROTO_IN=${array[0]}
JSON_IN=$(mktemp -p ./ tmp.XXXXXXXXXX.json)
OUT=$(mktemp -p ./ tmp.XXXXXXXXXX.log)

# prepare
${CRIT} decode -i "${PROTO_IN}" -o "${JSON_IN}"

# proto in - json out decode
cat "${PROTO_IN}" | ${CRIT} decode || exit 1
cat "${PROTO_IN}" | ${CRIT} decode -o "${OUT}" || exit 1
cat "${PROTO_IN}" | ${CRIT} decode > "${OUT}" || exit 1
${CRIT} decode -i "${PROTO_IN}" || exit 1
${CRIT} decode -i "${PROTO_IN}" -o "${OUT}" || exit 1
${CRIT} decode -i "${PROTO_IN}" > "${OUT}" || exit 1
${CRIT} decode < "${PROTO_IN}" || exit 1
${CRIT} decode -o "${OUT}" < "${PROTO_IN}" || exit 1
${CRIT} decode < "${PROTO_IN}" > "${OUT}" || exit 1

# proto in - json out encode -> should fail
cat "${PROTO_IN}" | ${CRIT} encode || true
cat "${PROTO_IN}" | ${CRIT} encode -o "${OUT}" || true
cat "${PROTO_IN}" | ${CRIT} encode > "${OUT}" || true
${CRIT} encode -i "${PROTO_IN}" || true
${CRIT} encode -i "${PROTO_IN}" -o "${OUT}" || true
${CRIT} encode -i "${PROTO_IN}" > "${OUT}" || true

# json in - proto out encode
cat "${JSON_IN}" | ${CRIT} encode || exit 1
cat "${JSON_IN}" | ${CRIT} encode -o "${OUT}" || exit 1
cat "${JSON_IN}" | ${CRIT} encode > "${OUT}" || exit 1
${CRIT} encode -i "${JSON_IN}" || exit 1
${CRIT} encode -i "${JSON_IN}" -o "${OUT}" || exit 1
${CRIT} encode -i "${JSON_IN}" > "${OUT}" || exit 1
${CRIT} encode < "${JSON_IN}" || exit 1
${CRIT} encode -o "${OUT}" < "${JSON_IN}" || exit 1
${CRIT} encode < "${JSON_IN}" > "${OUT}" || exit 1

# json in - proto out decode -> should fail
cat "${JSON_IN}" | ${CRIT} decode || true
cat "${JSON_IN}" | ${CRIT} decode -o "${OUT}" || true
cat "${JSON_IN}" | ${CRIT} decode > "${OUT}" || true
${CRIT} decode -i "${JSON_IN}" || true
${CRIT} decode -i "${JSON_IN}" -o "${OUT}" || true
${CRIT} decode -i "${JSON_IN}" > "${OUT}" || true

# explore image directory
${CRIT} x ./ ps || exit 1
${CRIT} x ./ fds || exit 1
${CRIT} x ./ mems || exit 1
${CRIT} x ./ rss || exit 1
}

echo "Generating images..."
gen_imgs
echo "Testing recode..."
run_test1
echo "Testing commands..."
run_test2
Binary file added test/crit/loop
Binary file not shown.
63 changes: 63 additions & 0 deletions test/crit/loop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main(void)
{
pid_t pid;
pid_t sid;
int res = EXIT_FAILURE;
int start_pipe[2];

if (pipe(start_pipe)) {
perror("pipe failed!");
goto out;
}

pid = fork();
if (pid < 0) {
perror("fork failed!");
goto out;
}

if (pid == 0) {
close(start_pipe[0]);

sid = setsid();
if (sid < 0) {
perror("setsid failed!");
res = EXIT_FAILURE;
write(start_pipe[1], &res, sizeof(res));
close(start_pipe[1]);
exit(1);
}

// Create a file descriptor for "crit x ./ fds" test
open("/dev/null", O_RDONLY);

chdir("/");
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

res = EXIT_SUCCESS;
write(start_pipe[1], &res, sizeof(res));
close(start_pipe[1]);

while (1) {
sleep(1);
}
}

close(start_pipe[1]);
read(start_pipe[0], &res, sizeof(res));
close(start_pipe[0]);

out:
if (res == EXIT_SUCCESS)
printf("%d\n", pid);
return res;
}

0 comments on commit 6b808b6

Please sign in to comment.