From 704da86e9bc24df6d292b4ae04454f813c6f263a Mon Sep 17 00:00:00 2001 From: Tim Zhang Date: Wed, 24 Apr 2024 15:53:33 +0800 Subject: [PATCH] CI: Add tests for stdio Add tests for stdio Signed-off-by: Tim Zhang --- .github/workflows/basic-ci-amd64.yaml | 31 ++++++ tests/integration/stdio/gha-run.sh | 63 +++++++++++ tests/integration/stdio/stdio-tests.sh | 145 +++++++++++++++++++++++++ 3 files changed, 239 insertions(+) create mode 100755 tests/integration/stdio/gha-run.sh create mode 100755 tests/integration/stdio/stdio-tests.sh diff --git a/.github/workflows/basic-ci-amd64.yaml b/.github/workflows/basic-ci-amd64.yaml index fdd0a09401..40f2ca8eb2 100644 --- a/.github/workflows/basic-ci-amd64.yaml +++ b/.github/workflows/basic-ci-amd64.yaml @@ -168,6 +168,37 @@ jobs: - name: Run runk tests timeout-minutes: 10 run: bash tests/integration/runk/gha-run.sh run + run-stdio: + runs-on: garm-ubuntu-2204-smaller + env: + CONTAINERD_VERSION: lts + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ inputs.commit-hash }} + fetch-depth: 0 + + - name: Rebase atop of the latest target branch + run: | + ./tests/git-helper.sh "rebase-atop-of-the-latest-target-branch" + env: + TARGET_BRANCH: ${{ inputs.target-branch }} + + - name: Install dependencies + run: bash tests/integration/stdio/gha-run.sh install-dependencies + + - name: get-kata-tarball + uses: actions/download-artifact@v4 + with: + name: kata-static-tarball-amd64${{ inputs.tarball-suffix }} + path: kata-artifacts + + - name: Install kata + run: bash tests/integration/stdio/gha-run.sh install-kata kata-artifacts + + - name: Run stdio tests + timeout-minutes: 10 + run: bash tests/integration/stdio/gha-run.sh run-tracing: strategy: diff --git a/tests/integration/stdio/gha-run.sh b/tests/integration/stdio/gha-run.sh new file mode 100755 index 0000000000..5db12bb24c --- /dev/null +++ b/tests/integration/stdio/gha-run.sh @@ -0,0 +1,63 @@ +#!/bin/bash +# +# Copyright (c) 2023 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# + +set -o errexit +set -o nounset +set -o pipefail + +kata_tarball_dir="${2:-kata-artifacts}" +runk_dir="$(dirname "$(readlink -f "$0")")" +source "${runk_dir}/../../common.bash" +source "${runk_dir}/../../gha-run-k8s-common.sh" + +function install_dependencies() { + info "Installing the dependencies needed for running the stdio tests" + + # Dependency list of projects that we can rely on the system packages + # - jq + declare -a system_deps=( + jq + ) + + sudo apt-get update + sudo apt-get -y install "${system_deps[@]}" + + ensure_yq + + # Dependency list of projects that we can install them + # directly from their releases on GitHub: + # - containerd + # - cri-container-cni release tarball already includes CNI plugins + declare -a github_deps + github_deps[0]="cri_containerd:$(get_from_kata_deps "externals.containerd.${CONTAINERD_VERSION}")" + + for github_dep in "${github_deps[@]}"; do + IFS=":" read -r -a dep <<< "${github_dep}" + install_${dep[0]} "${dep[1]}" + done + + # Requires bats to run the tests + install_bats +} + +function run() { + info "Running stdio tests" + + bats "${runk_dir}/stdio-tests.bats" +} + +function main() { + action="${1:-}" + case "${action}" in + install-dependencies) install_dependencies ;; + install-kata) install_kata ;; + run) run ;; + *) >&2 die "Invalid argument" ;; + esac +} + +main "$@" diff --git a/tests/integration/stdio/stdio-tests.sh b/tests/integration/stdio/stdio-tests.sh new file mode 100755 index 0000000000..5d5b7c6a12 --- /dev/null +++ b/tests/integration/stdio/stdio-tests.sh @@ -0,0 +1,145 @@ +#!/bin/bash +# +# Copyright (c) 2024 Kata Contributors +# +# SPDX-License-Identifier: Apache-2.0 +# +# This test will validate stdio with containerd + +source "../../common.bash" +source "../../metrics/lib/common.bash" + +export TEST_IMAGE="docker.io/library/busybox:latest" +export CONTAINER_ID="hello" +export LOG_FILE="/tmp/stdio-tests-log-file" +export TEST_RUNTIME="io.containerd.run.kata.v2" +export LARGE_FILE_SIZE=1000000000 + +echo "pull container image" +check_images ${TEST_IMAGE} + +teardown() { + echo "delete the container" + if sudo ctr t list -q | grep -q "${CONTAINER_ID}"; then + stop_container + fi + + if sudo ctr c list -q | grep -q "${CONTAINER_ID}"; then + sudo ctr c rm "${CONTAINER_ID}" + fi +} + +stop_container() { + local cmd + sudo ctr t kill --signal SIGKILL --all ${CONTAINER_ID} + # poll for a while until the task receives signal and exit + cmd='[ "STOPPED" == "$(sudo ctr t ls | grep ${CONTAINER_ID} | awk "{print \$3}")" ]' + waitForProcess 10 1 "${cmd}" + + echo "check the container is stopped" + # there is only title line of ps command + [ "1" == "$(sudo ctr t ps ${CONTAINER_ID} | wc -l)" ] +} + +assert_eq() { + local actual="$1" + local expected="$2" + + if [ "$expected" != "$actual" ]; then + echo "Assertion failed: Expected '$expected', but got '$actual'" + exit -1 + fi +} + +echo "1. Start a container (using terminal)" +unbuffer sudo ctr run --runtime $TEST_RUNTIME --rm -t ${TEST_IMAGE} ${CONTAINER_ID} whoami> $LOG_FILE +output=$(cat ${LOG_FILE}| tr -d '[:space:]') +assert_eq $output "root" + +/usr/bin/expect <<-EOF +set timeout 5 +spawn sudo ctr run --runtime $TEST_RUNTIME --rm -t ${TEST_IMAGE} ${CONTAINER_ID} sh + +expect "#" +send "id\r" + +expect { + "uid=0(root) gid=0(root) groups=0(root),10(wheel)" { send_user "Ok\n" } + timeout { send_user "Failed\n"; exit 1 } +} + +send "exit\r" +EOF +teardown + +echo "2. Start a container (not using terminal)" +output=$(sudo ctr run --runtime $TEST_RUNTIME --rm ${TEST_IMAGE} ${CONTAINER_ID} whoami) +assert_eq $output root + +/usr/bin/expect <<-EOF +set timeout 5 +spawn sudo ctr run --runtime $TEST_RUNTIME --rm ${TEST_IMAGE} ${CONTAINER_ID} sh + +send "whoami\r" + +expect { + "root" { send_user "Ok\n" } + timeout { send_user "Failed\n"; exit 1 } +} + +send "exit\r" + +EOF + +teardown + +echo "3. Start a detached container (using terminal)" +sudo ctr run --runtime $TEST_RUNTIME -d -t ${TEST_IMAGE} ${CONTAINER_ID} +read CID IMAGE RUNTIME <<< $(sudo ctr c ls | grep ${CONTAINER_ID}) + +assert_eq $CID $CONTAINER_ID +assert_eq $IMAGE $TEST_IMAGE +assert_eq $RUNTIME "io.containerd.run.kata.v2" + +teardown + +echo "4. Execute command (using terminal) in an existing container" +sudo ctr run --runtime $TEST_RUNTIME -d ${TEST_IMAGE} ${CONTAINER_ID} + +unbuffer sudo ctr t exec -t --exec-id foobar ${CONTAINER_ID} whoami>$LOG_FILE +output=$(cat ${LOG_FILE}|head -n 1|tr -d '[:space:]') +echo $output +assert_eq $output "root" + +teardown + +echo "5. Execute command (not using terminal) in an existing container" +sudo ctr run --runtime $TEST_RUNTIME -d ${TEST_IMAGE} ${CONTAINER_ID} +output=$(sudo ctr t exec --exec-id foobar ${CONTAINER_ID} whoami) +assert_eq $output "root" + +teardown + +echo "6. Execute command (not using terminal, pipe stdin) in an existing container" +sudo ctr run --runtime $TEST_RUNTIME -d ${TEST_IMAGE} ${CONTAINER_ID} +# Word count +read F1 F2 F3 <<< $(printf "aaa\nbbb\nccc\n" | sudo ctr t exec --exec-id foobar ${CONTAINER_ID} wc) +assert_eq $F1 3 +assert_eq $F2 3 +assert_eq $F3 12 + +# Large file count +head -c $LARGE_FILE_SIZE /dev/random > /tmp/input +output=$(cat /tmp/input | wc -c|tr -d '[:space:]') +assert_eq $output $LARGE_FILE_SIZE + +output=$(cat /tmp/input | sudo ctr t exec --exec-id foobar ${CONTAINER_ID} wc -c) +assert_eq $output $LARGE_FILE_SIZE + +output=$(cat /tmp/input | sudo ctr t exec --exec-id foobar ${CONTAINER_ID} cat | wc -c) +assert_eq $output $LARGE_FILE_SIZE +# Large file copy +cat /tmp/input | sudo ctr t exec --exec-id foobar ${CONTAINER_ID} cat > /tmp/output +diff -q /tmp/input /tmp/output + +teardown