From 513d78a2a90e0748e4a5394c8cbeac8cd67eb159 Mon Sep 17 00:00:00 2001 From: xingweizheng Date: Mon, 10 Jan 2022 19:36:49 +0800 Subject: [PATCH 04/20] Refactor: integration test basic framework, and adapt existing testcases to it --- tests/lib/common.sh | 329 ++++++++++-------- ...on_test_save_single_image_multiple_tags.sh | 86 ++--- tests/src/integration_test_set_new_root.sh | 31 +- tests/test.sh | 30 +- 4 files changed, 243 insertions(+), 233 deletions(-) diff --git a/tests/lib/common.sh b/tests/lib/common.sh index 4dd34aa..3c031f9 100755 --- a/tests/lib/common.sh +++ b/tests/lib/common.sh @@ -18,7 +18,7 @@ declare -x pidofbuilder # check if legacy builder exists function pre_check() { - if pgrep isula-builder > /dev/null 2>&1; then + if pgrep isula-builder >/dev/null 2>&1; then echo "isula-builder is already running, please stop it first" exit 1 fi @@ -26,13 +26,13 @@ function pre_check() { # start isula-builder function start_isula_builder() { - nohup isula-builder > /tmp/buildlog-daemon 2>&1 & + nohup isula-builder >/tmp/buildlog-daemon 2>&1 & pidofbuilder=$! # check if isula-builder is started builder_started=0 for _ in $(seq 1 30); do - if ! grep -i "is listening on" /tmp/buildlog-daemon > /dev/null 2>&1; then + if ! grep -i "is listening on" /tmp/buildlog-daemon >/dev/null 2>&1; then sleep 0.1 continue else @@ -47,198 +47,233 @@ function start_isula_builder() { } function cleanup() { - isula-build ctr-img rm -p > /dev/null 2>&1 - kill -15 "${pidofbuilder}" > /dev/null 2>&1 + isula-build ctr-img rm -p >/dev/null 2>&1 + kill -15 "${pidofbuilder}" >/dev/null 2>&1 rm -f /tmp/buildlog-* } # test build image without output with default docker format function test_build_without_output() { - if ! isula-build ctr-img build --format docker --tag "$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker format without output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + commands=( + "isula-build ctr-img build --format docker --tag $1:latest $2" + "isula-build ctr-img rm $1:latest" + ) - if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker format without output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + for command in "${commands[@]}"; do show_and_run_command "$command"; done } # test build image without output with oci format function test_build_without_output_with_oci_format() { - if ! isula-build ctr-img build --format oci --tag "$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with oci format without output)" - kill -15 "${pidofbuilder}" - exit 1 - fi - - if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with oci format without output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + declare -a commands=( + "isula-build ctr-img build --format oci --tag $1:latest $2" + "isula-build ctr-img rm $1:latest" + ) + for command in "${commands[@]}"; do show_and_run_command "$command"; done } # test build image with docker-archive output function test_build_with_docker_archive_output() { - if ! isula-build ctr-img build --output=docker-archive:/tmp/"$1".tar:"$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-archive output)" - kill -15 "${pidofbuilder}" - exit 1 - else - rm -f /tmp/"$1".tar - fi - - if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-archive output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + declare -a commands=( + "isula-build ctr-img build --output=docker-archive:/tmp/$1.tar:$1:latest $2" + "isula-build ctr-img rm $1:latest" + ) + for command in "${commands[@]}"; do show_and_run_command "$command"; done + rm -f /tmp/"$1".tar } # test build image with oci-archive output function test_build_with_oci_archive_output() { - if ! isula-build ctr-img build --output=oci-archive:/tmp/"$1".tar:"$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with oci-archive output)" - kill -15 "${pidofbuilder}" - exit 1 - else - rm -f /tmp/"$1".tar - fi - - if ! isula-build ctr-img rm "$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with oci-archive output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + declare -a commands=( + "isula-build ctr-img build --output=oci-archive:/tmp/$1.tar:$1:latest $2" + "isula-build ctr-img rm $1:latest" + ) + for command in "${commands[@]}"; do show_and_run_command "$command"; done + rm -f /tmp/"$1".tar } # test build image with docker-daemon output function test_build_with_docker_daemon_output() { - if ! systemctl status docker > /dev/null 2>&1; then + if ! systemctl status docker >/dev/null 2>&1; then return 0 fi - if ! isula-build ctr-img build --output=docker-daemon:isula/"$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-daemon output)" - kill -15 "${pidofbuilder}" - exit 1 - else - docker rmi isula/"$1" > /dev/null 2>&1 - fi - - if ! isula-build ctr-img rm isula/"$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with docker-daemon output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + declare -a commands=( + "isula-build ctr-img build --output=docker-daemon:isula/$1:latest $2" + "isula-build ctr-img rm isula/$1:latest" + ) + for command in "${commands[@]}"; do show_and_run_command "$command"; done + docker rmi isula/"$1" >/dev/null 2>&1 } # test build image with isulad output -function test_build_with_isulad_output() { - if ! systemctl status isulad > /dev/null 2>&1; then +function test_build_with_isulad_output() { + if ! systemctl status isulad >/dev/null 2>&1; then return 0 fi - if ! isula-build ctr-img build --output=isulad:isula/"$1":latest "$2" > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with isulad output)" - kill -15 "${pidofbuilder}" - exit 1 - else - isula rmi isula/"$1" > /dev/null 2>&1 - fi - - if ! isula-build ctr-img rm isula/"$1":latest > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon (build with isulad output)" - kill -15 "${pidofbuilder}" - exit 1 - fi + declare -a commands=( + "isula-build ctr-img build --output=isulad:isula/$1:latest $2" + "isula-build ctr-img rm isula/$1:latest" + ) + for command in "${commands[@]}"; do show_and_run_command "$command"; done + isula rmi isula/"$1" >/dev/null 2>&1 } # test isula build base command function test_isula_build_base_command() { - show_and_run_command "Build docker format image:" \ - " isula-build ctr-img build --tag $1-docker:latest --output=docker-archive:/tmp/$1-docker.tar:$1-docker:latest $2" + declare -A commands=( + ["Build docker format image"]="isula-build ctr-img build --tag $1-docker:latest --output=docker-archive:/tmp/$1-docker.tar:$1-docker:latest $2" + ["Build oci format image"]="isula-build ctr-img build --tag $1-oci:latest --output=oci-archive:/tmp/$1-oci.tar:$1-oci:latest $2" + ["List all images"]="isula-build ctr-img images" + ["List docker format image"]="isula-build ctr-img images $1-docker:latest" + ["List oci format image"]="isula-build ctr-img images $1-oci:latest" + ["Save image with docker format"]="isula-build ctr-img save -f docker $1-docker:latest -o /tmp/$1-save-docker.tar" + ["Save image with oci format"]="isula-build ctr-img save -f oci $1-oci:latest -o /tmp/$1-save-oci.tar" + ["Load docker format images"]="isula-build ctr-img load -i /tmp/$1-docker.tar" + ["Load oci format images"]="isula-build ctr-img load -i /tmp/$1-oci.tar" + ["Save multipile images with docker format"]="isula-build ctr-img save -f docker $1-docker:latest $1-oci:latest -o /tmp/$1-all.tar" + ["Remove images"]="isula-build ctr-img rm $1-docker:latest $1-oci:latest" + ) + declare -a orders + orders+=("Build docker format image") + orders+=("Build oci format image") + orders+=("List all images") + orders+=("List docker format image") + orders+=("List oci format image") + orders+=("Save image with docker format") + orders+=("Save image with oci format") + orders+=("Load docker format images") + orders+=("Load oci format images") + orders+=("Save multipile images with docker format") + orders+=("Remove images") + for i in "${!orders[@]}"; do + show_and_run_command "${orders[$i]}" "${commands[${orders[$i]}]}" + done - show_and_run_command "Build oci format image:" \ - "isula-build ctr-img build --tag $1-oci:latest --output=oci-archive:/tmp/$1-oci.tar:$1-oci:latest $2" + rm -f /tmp/*.tar +} - show_and_run_command "List all images:" \ - "isula-build ctr-img images" - - show_and_run_command "List docker format image:" \ - "isula-build ctr-img images $1-docker:latest" +# print callstack of shell function +function shell_print_callstack() { + echo "Shell Function Call Stack:" + for ((index = 1; index < ${#FUNCNAME[@]}; index++)); do + printf "\t%d - %s\n" "$((index - 1))" "${FUNCNAME[index]} (${BASH_SOURCE[index + 1]}:${BASH_LINENO[index]})" + done +} - show_and_run_command "List oci format image:" \ - "isula-build ctr-img images $1-oci:latest" +# show command brief and run +# $1 (command brief) +# $2 (concrete command) +function show_and_run_command() { + function run_command() { + if ! $command >/tmp/buildlog-client 2>&1; then + echo "FAIL" + echo "Failed when running command: $command" + echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon" + kill -15 "${pidofbuilder}" + shell_print_callstack + exit 1 + fi + } + if [ $# -eq 1 ]; then + local -r command="$1" + run_command + return 0 + fi + local -r brief="$1" + local -r command="$2" + printf "%-45s" "$brief"":" + run_command + echo "PASS" +} - rm -f /tmp/"$1"-docker.tar /tmp/"$1"-oci.tar +exit_flag=0 - show_and_run_command "Save image with docker format:" \ - "isula-build ctr-img save -f docker $1-docker:latest -o /tmp/$1-docker.tar" +# run command when isula-builder running in systemd mode +# $1 (concrete command) +function systemd_run_command() { + local -r command="$1" - show_and_run_command "Save image with oci format:" \ - "isula-build ctr-img save -f oci $1-oci:latest -o /tmp/$1-oci.tar" + start_time=$(date '+%Y-%m-%d %H:%M:%S') + if ! $command >/tmp/buildlog-client 2>&1; then + { + echo "Error from client:" + cat /tmp/buildlog-client + echo "Error from daemon:" + journalctl -u isula-build --since "$start_time" --no-pager + shell_print_callstack + } >>/tmp/buildlog-failed - show_and_run_command "Load docker format images:" \ - "isula-build ctr-img load -i /tmp/$1-docker.tar" + ((exit_flag++)) + fi +} - show_and_run_command "Load oci format images:" \ - "isula-build ctr-img load -i /tmp/$1-oci.tar" +# run command and check its result +# $1 (command) +# $2 (expected command return value) +function run_check_result() { + local -r command="$1" + local -r expected="$2" + + eval "$command" >/dev/null 2>&1 + result=$? + if [ "$result" != "$expected" ]; then + debug "expected $expected, get $result" + testcase_path="${BASH_SOURCE[1]}" + testcase="${testcase_path##/*/}" + echo "$testcase:${BASH_LINENO[0]}" "$command" >>/tmp/buildlog-failed + echo expected "$expected", get "$result" >>/tmp/buildlog-failed + ((exit_flag++)) + fi +} - show_and_run_command "Save multipile images with docker format:" \ - "isula-build ctr-img save -f docker $1-docker:latest $1-oci:latest -o /tmp/$1-all.tar" +# check actual result and expected value +# $1 (result) +# $2 (expected) +function check_value() { + local -r result="$1" + local -r expected="$2" + + if [ "$result" != "$expected" ]; then + debug "expected $expected, get $result" + testcase_path="${BASH_SOURCE[1]}" + testcase="${testcase_path##/*/}" + echo "TESTCASE: $testcase:${BASH_LINENO[0]}" "${FUNCNAME[0]}" >>/tmp/buildlog-failed + echo expected "$expected", get "$result" >>/tmp/buildlog-failed + ((exit_flag++)) + fi +} - rm -f /tmp/"$1"-docker.tar /tmp/"$1"-oci.tar /tmp/"$1"-all.tar +# print debug message +# $1 (debug message) +function debug() { + local -r message="$1" - show_and_run_command "Remove images:" \ - "isula-build ctr-img rm $1-docker:latest $1-oci:latest" + if [ "$TEST_DEBUG" == "true" ]; then + printf "(%s %s) " "DEBUG:" "$message" + fi } -function show_and_run_command() { - printf "%-45s" "$1" - if ! $2 > /tmp/buildlog-client 2>&1; then - echo "FAIL" - echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon, failed when running command: $2" - kill -15 "${pidofbuilder}" - exit 1 - fi - echo "PASS" +run_root="/var/run/integration-isula-build" +data_root="/var/lib/integration-isula-build" +config_file="/etc/isula-build/configuration.toml" + +function pre_integration() { + rm -rf /tmp/buildlog-failed + + cp $config_file "$config_file".integration + sed -i "/run_root/d;/data_root/d" $config_file + echo "run_root = \"${run_root}\"" >>$config_file + echo "data_root = \"${data_root}\"" >>$config_file + + systemctl restart isula-build } -function run_with_debug() { - function fail_and_exit(){ - echo "FAIL" - echo "Run \"journalctl -xefu isula-build\" to get the log." - systemctl stop isula-build - exit 1 - } +function after_integration() { + systemd_run_command "isula-build ctr-img rm -a" - if [ "${DEBUG:-0}" -eq 0 ]; then - if ! $1 > /dev/null 2>&1; then - fail_and_exit - fi - return - fi - echo "$1" - if ! $1; then - fail_and_exit - fi - echo "------------command-delimiter-----------" - echo " " -} \ No newline at end of file + rm -f $config_file + mv "$config_file".integration $config_file + systemctl stop isula-build + rm -rf $run_root $data_root +} diff --git a/tests/src/integration_test_save_single_image_multiple_tags.sh b/tests/src/integration_test_save_single_image_multiple_tags.sh index 1eaeb8d..46df444 100644 --- a/tests/src/integration_test_save_single_image_multiple_tags.sh +++ b/tests/src/integration_test_save_single_image_multiple_tags.sh @@ -12,69 +12,49 @@ # Author: Weizheng Xing # Create: 2021-08-24 # Description: check if saving single image with multiple tags has been corrected +# History: 2022-01-10 Weizheng Xing Refactor: use systemd_run_command common function top_dir=$(git rev-parse --show-toplevel) -# shellcheck disable=SC1091 source "$top_dir"/tests/lib/common.sh -image_name=add-chown-basic -context_dir="$top_dir"/tests/data/add-chown-basic +image_name=build-from-scratch +context_dir="$top_dir"/tests/data/build-from-scratch -function clean() -{ - isula-build ctr-img rm -p > /dev/null 2>&1 - systemctl stop isula-build - rm -rf "$temp_tar" -} - -function pre_test() -{ +function pre_test() { temp_tar=$(mktemp -u --suffix=.tar) - systemctl restart isula-build } -function do_test() -{ +function do_test() { # get image id - if ! image_id1=$(isula-build ctr-img build -t $image_name:latest "$context_dir"|grep "Build success with image id: "|cut -d ":" -f 2); then - echo "FAIL" - fi - if ! image_id2=$(isula-build ctr-img build -t $image_name:latest2 "$context_dir"|grep "Build success with image id: "|cut -d ":" -f 2); then - echo "FAIL" - fi - - ! run_with_debug "isula-build ctr-img tag $image_name:latest $image_name:latest-child" - - # save with id + name - ! run_with_debug "isula-build ctr-img save -f docker $image_id1 $image_name:latest-child -o $temp_tar" - rm -rf "$temp_tar" - - # save with name + id - ! run_with_debug "isula-build ctr-img save -f docker $image_name:latest-child $image_id1 -o $temp_tar" - rm -rf "$temp_tar" - - # save with name + name - ! run_with_debug "isula-build ctr-img save -f docker $image_name:latest $image_name:latest-child -o $temp_tar" - rm -rf "$temp_tar" - - # save with different images id1 + id2 - ! run_with_debug "isula-build ctr-img save -f docker $image_id1 $image_id2 -o $temp_tar" - rm -rf "$temp_tar" - - # save with different images "without latest tag" + id2 - ! run_with_debug "isula-build ctr-img save -f docker $image_name $image_id2 -o $temp_tar" - rm -rf "$temp_tar" - - # save with id1 + id2 + name - ! run_with_debug "isula-build ctr-img save -f docker $image_id1 $image_id2 $image_name:latest2 -o $temp_tar" - rm -rf "$temp_tar" - - ! run_with_debug "isula-build ctr-img rm $image_name:latest $image_name:latest-child" - ! run_with_debug "isula-build ctr-img rm $image_name:latest2" - - echo "PASS" + systemd_run_command "isula-build ctr-img build -t $image_name:latest $context_dir" + image_id1=$(grep use busyobx instead of openeuler base image to speed up test +# History: 2022-01-10 Weizheng Xing Refactor: use systemd_run_command common function top_dir=$(git rev-parse --show-toplevel) # shellcheck disable=SC1091 @@ -21,45 +22,37 @@ source "$top_dir"/tests/lib/common.sh run_root="/var/run/new-isula-build" data_root="/var/lib/new-isula-build" config_file="/etc/isula-build/configuration.toml" -image="hub.oepkgs.net/openeuler/busybox:latest" +image="hub.oepkgs.net/library/busybox:latest" -function clean() -{ +function clean() { rm -f $config_file mv "$config_file".bak $config_file - isula-build ctr-img rm -p > /dev/null 2>&1 - systemctl stop isula-build + systemctl restart isula-build rm -rf $run_root $data_root } # change to new data and run root -function pre_test() -{ +function pre_test() { cp $config_file "$config_file".bak sed -i "/run_root/d;/data_root/d" $config_file - echo "run_root = \"${run_root}\"" >> $config_file - echo "data_root = \"${data_root}\"" >> $config_file + echo "run_root = \"${run_root}\"" >>$config_file + echo "data_root = \"${data_root}\"" >>$config_file systemctl restart isula-build } # check if new resources are downloaded in new root -function do_test() -{ +function do_test() { tree_node_befor=$(tree -L 3 $data_root | wc -l) - run_with_debug "isula-build ctr-img pull $image" + systemd_run_command "isula-build ctr-img pull $image" tree_node_after=$(tree -L 3 $data_root | wc -l) - if [ $((tree_node_after - tree_node_befor)) -eq 8 ] && run_with_debug "isula-build ctr-img rm $image"; then - echo "PASS" - else - echo "Sets of run and data root are not effective" - clean - exit 1 - fi + check_value $((tree_node_after - tree_node_befor)) 8 + systemd_run_command "isula-build ctr-img rm $image" } pre_test do_test clean +exit "$exit_flag" diff --git a/tests/test.sh b/tests/test.sh index 01f0f31..df29c22 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -36,36 +36,38 @@ function fuzz() { # integration test function integration() { source "$top_dir"/tests/lib/common.sh - systemctl restart isula-build + pre_integration while IFS= read -r testfile; do printf "%-65s" "test $(basename "$testfile"): " if ! bash "$testfile"; then - exit 1 + echo "FAIL" + continue fi + echo "PASS" done < <(find "$top_dir"/tests/src -maxdepth 1 -name "integration_test*" -type f -print) + after_integration } # main function to chose which kind of test function main() { case "$1" in - fuzz) - fuzz "$2" - ;; - base) - base + fuzz) + fuzz "$2" ;; - integration) - integration + base) + base ;; - *) - echo "Unknow test type." - exit 1 + integration) + integration + ;; + *) + echo "Unknow test type." + exit 1 ;; esac } -export "ISULABUILD_CLI_EXPERIMENTAL"="enabled" -export DEBUG=0 +export ISULABUILD_CLI_EXPERIMENTAL="enabled" main "$@" -- 2.27.0