Skip to content

Commit

Permalink
test: add e2e test for json format
Browse files Browse the repository at this point in the history
Add e2e tests to verify the JSON output of `checkpointctl inspect`.

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git committed Jan 11, 2024
1 parent 386c0bb commit a543249
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
fetch-depth: 0
- name: Install tools
run: |
sudo apt-get install -qqy bats iptables
sudo apt-get install -qqy bats iptables jq
# Add PPA for CRIU
sudo add-apt-repository ppa:criu/ppa
sudo apt-get update
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install tools
run: sudo dnf -y install ShellCheck bats golang criu asciidoctor iptables
run: sudo dnf -y install ShellCheck bats golang criu asciidoctor iptables jq
- name: Run make shellcheck
run: make shellcheck
- name: Run make all
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test-junit: test-imgs
bats -F junit checkpointctl.bats > junit.xml

test-imgs: piggie/piggie
$(eval PID := $(shell piggie/piggie))
$(eval PID := $(shell export TEST_ENV=BAR TEST_ENV_EMPTY=; piggie/piggie))
mkdir -p $@
$(CRIU) dump --tcp-established -v4 -o dump.log -D $@ -t $(PID)

Expand Down
59 changes: 59 additions & 0 deletions test/checkpointctl.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ function checkpointctl() {
echo "$output"
}

run_only_test() {
if [ "$BATS_TEST_NUMBER" -ne "$1" ]; then
skip
fi
}

function setup() {
# run_only_test 49

TEST_TMP_DIR1=$(mktemp -d)
TEST_TMP_DIR2=$(mktemp -d)
}
Expand Down Expand Up @@ -589,3 +597,54 @@ function teardown() {
[ "$status" -eq 1 ]
[[ ${lines[0]} == *"no process with PID 9999"* ]]
}

@test "Run checkpointctl inspect with json format" {
cp data/config.dump data/spec.dump test-imgs/stats-dump "$TEST_TMP_DIR1"
mkdir "$TEST_TMP_DIR1"/checkpoint
cp test-imgs/*.img "$TEST_TMP_DIR1"/checkpoint
( cd "$TEST_TMP_DIR1" && tar cf "$TEST_TMP_DIR2"/test.tar . )

# test function definitions for JSON output using jq
test_engine() { jq -e '.[0].engine == "Podman"'; }
export -f test_engine

test_pstree_cmd() { jq -e '.[0].process_tree.command == "piggie"'; }
export -f test_pstree_cmd

test_pstree_child1() { jq -e '.[0].process_tree.children[0].command == "tcp-server"'; }
export -f test_pstree_child1

test_pstree_child2() { jq -e '.[0].process_tree.children[1].command == "tcp-client"'; }
export -f test_pstree_child2

test_pstree_env() { jq -e '.[0].process_tree.environment_variables.TEST_ENV == "BAR"'; }
export -f test_pstree_env

test_pstree_env_empty() { jq -e '.[0].process_tree.environment_variables.TEST_ENV_EMPTY == ""'; }
export -f test_pstree_env_empty

test_socket_protocol() { jq -e '.[0].sockets[0].open_sockets[0].protocol == "TCP"'; }
export -f test_socket_protocol

test_socket_src_port() { jq -e '.[0].sockets[0].open_sockets[0].data.src_port == 5000'; }
export -f test_socket_src_port

# Run tests
run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json | test_engine"
[ "$status" -eq 0 ]

run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json --ps-tree | test_pstree_cmd"
[ "$status" -eq 0 ]

run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json --all | test_pstree_env"
[ "$status" -eq 0 ]

run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json --all | test_pstree_env_empty"
[ "$status" -eq 0 ]

run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json --sockets | test_socket_protocol"
[ "$status" -eq 0 ]

run bash -c "$CHECKPOINTCTL inspect $TEST_TMP_DIR2/test.tar --format=json --sockets | test_socket_src_port"
[ "$status" -eq 0 ]
}

0 comments on commit a543249

Please sign in to comment.