From a543249b67efef7f82e21029f452047cd37f0a66 Mon Sep 17 00:00:00 2001 From: Radostin Stoyanov Date: Wed, 10 Jan 2024 20:19:50 +0000 Subject: [PATCH] test: add e2e test for json format Add e2e tests to verify the JSON output of `checkpointctl inspect`. Signed-off-by: Radostin Stoyanov --- .github/workflows/coverage.yml | 2 +- .github/workflows/tests.yml | 2 +- test/Makefile | 2 +- test/checkpointctl.bats | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 8ece99cf..b6285b27 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index edab0ba9..575ffe5e 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/test/Makefile b/test/Makefile index 81b4f6d7..9943e680 100644 --- a/test/Makefile +++ b/test/Makefile @@ -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) diff --git a/test/checkpointctl.bats b/test/checkpointctl.bats index 1fbd8a4e..dc4f2a69 100644 --- a/test/checkpointctl.bats +++ b/test/checkpointctl.bats @@ -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) } @@ -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 ] +}