From fbe5b10f83fa060ca2c2e2c3a6dc943dc9a878a2 Mon Sep 17 00:00:00 2001
From: xingweizheng <xingweizheng@huawei.com>
Date: Tue, 11 Jan 2022 15:59:18 +0800
Subject: [PATCH 07/20] integration test use TMPDIR

---
 tests/lib/base_commonlib.sh                   | 33 ++++++++++---------
 tests/lib/common.sh                           | 21 ++++++++----
 tests/lib/integration_commonlib.sh            |  2 +-
 tests/src/isula_build_base_command.sh         |  0
 .../test_image_name_conflict_with_image_id.sh | 12 +++----
 .../test_save_single_image_multiple_tags.sh   |  5 +--
 tests/src/test_set_new_root.sh                |  0
 tests/test.sh                                 |  8 +++--
 8 files changed, 46 insertions(+), 35 deletions(-)
 mode change 100644 => 100755 tests/src/isula_build_base_command.sh
 mode change 100644 => 100755 tests/src/test_image_name_conflict_with_image_id.sh
 mode change 100644 => 100755 tests/src/test_save_single_image_multiple_tags.sh
 mode change 100644 => 100755 tests/src/test_set_new_root.sh

diff --git a/tests/lib/base_commonlib.sh b/tests/lib/base_commonlib.sh
index 996fcdd..8893894 100644
--- a/tests/lib/base_commonlib.sh
+++ b/tests/lib/base_commonlib.sh
@@ -30,13 +30,13 @@ function pre_check() {
 
 # start isula-builder
 function start_isula_builder() {
-    nohup isula-builder >/tmp/buildlog-daemon 2>&1 &
+    nohup isula-builder >"$TMPDIR"/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" "$TMPDIR"/buildlog-daemon >/dev/null 2>&1; then
             sleep 0.1
             continue
         else
@@ -45,7 +45,8 @@ function start_isula_builder() {
         fi
     done
     if [ "${builder_started}" -eq 0 ]; then
-        echo "isula-builder start failed, log dir /tmp/buildlog-daemon"
+        echo "isula-builder start failed, log dir $TMPDIR/buildlog-daemon"
+        cat "$TMPDIR"/buildlog-daemon
         exit 1
     fi
 }
@@ -53,7 +54,7 @@ function start_isula_builder() {
 function cleanup() {
     isula-build ctr-img rm -p >/dev/null 2>&1
     kill -15 "${pidofbuilder}" >/dev/null 2>&1
-    rm -f /tmp/buildlog-*
+    rm -rf "$TMPDIR"
 }
 
 # isula-build builds with different output
@@ -97,21 +98,21 @@ function test_build_without_output_with_oci_format() {
 # test build image with docker-archive output
 function test_build_with_docker_archive_output() {
     declare -a commands=(
-        "isula-build ctr-img build --output=docker-archive:/tmp/$1.tar:$1:latest $2"
+        "isula-build ctr-img build --output=docker-archive:$TMPDIR/$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
+    rm -f "$TMPDIR"/"$1".tar
 }
 
 # test build image with oci-archive output
 function test_build_with_oci_archive_output() {
     declare -a commands=(
-        "isula-build ctr-img build --output=oci-archive:/tmp/$1.tar:$1:latest $2"
+        "isula-build ctr-img build --output=oci-archive:$TMPDIR/$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
+    rm -f "$TMPDIR"/"$1".tar
 }
 
 # test build image with docker-daemon output
@@ -145,16 +146,16 @@ function test_build_with_isulad_output() {
 # test isula build base command
 function test_isula_build_base_command() {
     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"
+        ["Build docker format image"]="isula-build ctr-img build --tag $1-docker:latest --output=docker-archive:$TMPDIR/$1-docker.tar:$1-docker:latest $2"
+        ["Build oci format image"]="isula-build ctr-img build --tag $1-oci:latest --output=oci-archive:$TMPDIR/$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"
+        ["Save image with docker format"]="isula-build ctr-img save -f docker $1-docker:latest -o $TMPDIR/$1-save-docker.tar"
+        ["Save image with oci format"]="isula-build ctr-img save -f oci $1-oci:latest -o $TMPDIR/$1-save-oci.tar"
+        ["Load docker format images"]="isula-build ctr-img load -i $TMPDIR/$1-docker.tar"
+        ["Load oci format images"]="isula-build ctr-img load -i $TMPDIR/$1-oci.tar"
+        ["Save multipile images with docker format"]="isula-build ctr-img save -f docker $1-docker:latest $1-oci:latest -o $TMPDIR/$1-all.tar"
         ["Remove images"]="isula-build ctr-img rm $1-docker:latest $1-oci:latest"
     )
     declare -a orders
@@ -173,5 +174,5 @@ function test_isula_build_base_command() {
         show_and_run_command "${orders[$i]}" "${commands[${orders[$i]}]}"
     done
 
-    rm -f /tmp/*.tar
+    rm -f "$TMPDIR"/*.tar
 }
diff --git a/tests/lib/common.sh b/tests/lib/common.sh
index 3710f62..c600616 100755
--- a/tests/lib/common.sh
+++ b/tests/lib/common.sh
@@ -16,16 +16,23 @@
 
 # exit_flag for a testcase, which will be added one when check or command goes something wrong
 exit_flag=0
+# TMPDIR use in all base or integration test process
+declare -x TMPDIR
+
+# only run once for create a temp dir
+function create_tmp_dir() {
+    TMPDIR=$(mktemp -d)
+}
 
 # 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
+        if ! $command >"$TMPDIR"/buildlog-client 2>&1; then
             echo "FAIL"
             echo "Failed when running command: $command"
-            echo "LOG DIR:/tmp/buildlog-client and /tmp/buildlog-daemon"
+            echo "LOG DIR:$TMPDIR/buildlog-client and $TMPDIR/buildlog-daemon"
             kill -15 "${pidofbuilder}"
             shell_print_callstack
             exit 1
@@ -49,14 +56,14 @@ function systemd_run_command() {
     local -r command="$1"
 
     start_time=$(date '+%Y-%m-%d %H:%M:%S')
-    if ! $command >/tmp/buildlog-client 2>&1; then
+    if ! $command >"$TMPDIR"/buildlog-client 2>&1; then
         {
             echo "Error from client:"
-            cat /tmp/buildlog-client
+            cat "$TMPDIR"/buildlog-client
             echo "Error from daemon:"
             journalctl -u isula-build --since "$start_time" --no-pager
             shell_print_callstack
-        } >>/tmp/buildlog-failed
+        } >>"$TMPDIR"/buildlog-failed
 
         ((exit_flag++))
     fi
@@ -78,7 +85,7 @@ function run_check_result() {
         {
             echo "$testcase:${BASH_LINENO[0]}" "$command"
             echo expected "$expected", get "$result"
-        } >>/tmp/buildlog-failed
+        } >>"$TMPDIR"/buildlog-failed
         ((exit_flag++))
     fi
 }
@@ -97,7 +104,7 @@ function check_value() {
         {
             echo "TESTCASE: $testcase:${BASH_LINENO[0]}" "${FUNCNAME[0]}"
             echo expected "$expected", get "$result"
-        } >>/tmp/buildlog-failed
+        } >>"$TMPDIR"/buildlog-failed
         ((exit_flag++))
     fi
 }
diff --git a/tests/lib/integration_commonlib.sh b/tests/lib/integration_commonlib.sh
index 9dcc415..44f186f 100644
--- a/tests/lib/integration_commonlib.sh
+++ b/tests/lib/integration_commonlib.sh
@@ -22,7 +22,7 @@ data_root="/var/lib/integration-isula-build"
 config_file="/etc/isula-build/configuration.toml"
 
 function pre_integration() {
-    rm -f /tmp/buildlog-failed
+    rm -f "$TMPDIR"/buildlog-failed
 
     cp $config_file "$config_file".integration
     sed -i "/run_root/d;/data_root/d" $config_file
diff --git a/tests/src/isula_build_base_command.sh b/tests/src/isula_build_base_command.sh
old mode 100644
new mode 100755
diff --git a/tests/src/test_image_name_conflict_with_image_id.sh b/tests/src/test_image_name_conflict_with_image_id.sh
old mode 100644
new mode 100755
index 2c436a0..cbcc353
--- a/tests/src/test_image_name_conflict_with_image_id.sh
+++ b/tests/src/test_image_name_conflict_with_image_id.sh
@@ -35,7 +35,7 @@ function clean() {
 function do_test() {
     systemd_run_command "isula-build ctr-img build -t $image_name:latest1 $context_dir"
     systemd_run_command "isula-build ctr-img build -t $image_name:latest2 $context_dir"
-    image_id2=$(grep </tmp/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
+    image_id2=$(grep <"$TMPDIR"/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
     short_id2=${image_id2:0:12}
     double_short_id2=${short_id2:0:6}
 
@@ -51,11 +51,11 @@ function do_test() {
 
     # analyse it
     declare -a commands=(
-        "cat /tmp/buildlog-client |grep $short_id2"
-        "tar -xvf $temp_tar_short -C /tmp manifest.json"
-        "cat /tmp/manifest.json | grep $short_id2:latest"
-        "tar -xvf $temp_tar_double_short -C /tmp manifest.json"
-        "cat /tmp/manifest.json | grep $image_id2"
+        "cat $TMPDIR/buildlog-client |grep $short_id2"
+        "tar -xvf $temp_tar_short -C $TMPDIR manifest.json"
+        "cat $TMPDIR/manifest.json | grep $short_id2:latest"
+        "tar -xvf $temp_tar_double_short -C $TMPDIR manifest.json"
+        "cat $TMPDIR/manifest.json | grep $image_id2"
     )
     for command in "${commands[@]}"; do run_check_result "$command" 0; done
 }
diff --git a/tests/src/test_save_single_image_multiple_tags.sh b/tests/src/test_save_single_image_multiple_tags.sh
old mode 100644
new mode 100755
index 46df444..7e8491e
--- a/tests/src/test_save_single_image_multiple_tags.sh
+++ b/tests/src/test_save_single_image_multiple_tags.sh
@@ -15,6 +15,7 @@
 # History: 2022-01-10 Weizheng Xing <xingweizheng@huawei.com> 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=build-from-scratch
@@ -27,10 +28,10 @@ function pre_test() {
 function do_test() {
     # get image id
     systemd_run_command "isula-build ctr-img build -t $image_name:latest $context_dir"
-    image_id1=$(grep </tmp/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
+    image_id1=$(grep <"$TMPDIR"/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
 
     systemd_run_command "isula-build ctr-img build -t $image_name:latest2 $context_dir"
-    image_id2=$(grep </tmp/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
+    image_id2=$(grep <"$TMPDIR"/buildlog-client "Build success with image id: " | cut -d ":" -f 2)
 
     declare -a commands=(
         "isula-build ctr-img tag $image_name:latest $image_name:latest-child"
diff --git a/tests/src/test_set_new_root.sh b/tests/src/test_set_new_root.sh
old mode 100644
new mode 100755
diff --git a/tests/test.sh b/tests/test.sh
index 6cf78c9..3f2785b 100755
--- a/tests/test.sh
+++ b/tests/test.sh
@@ -1,11 +1,12 @@
 #!/bin/bash
-
+# shellcheck disable=SC1091
 top_dir=$(git rev-parse --show-toplevel)
 
 # base test
 function base() {
     source "$top_dir"/tests/lib/base_commonlib.sh
     pre_check
+    create_tmp_dir
     start_isula_builder
 
     while IFS= read -r testfile; do
@@ -22,8 +23,8 @@ function base() {
 function fuzz() {
     failed=0
     while IFS= read -r testfile; do
-        printf "%-45s" "test $(basename "$testfile"): " | tee -a ${top_dir}/tests/fuzz.log
-        bash "$testfile" "$1" | tee -a ${top_dir}/tests/fuzz.log
+        printf "%-45s" "test $(basename "$testfile"): " | tee -a "$top_dir"/tests/fuzz.log
+        bash "$testfile" "$1" | tee -a "$top_dir"/tests/fuzz.log
         if [ $PIPESTATUS -ne 0 ]; then
             failed=1
         fi
@@ -36,6 +37,7 @@ function fuzz() {
 # integration test
 function integration() {
     source "$top_dir"/tests/lib/integration_commonlib.sh
+    create_tmp_dir
     pre_integration
 
     while IFS= read -r testfile; do
-- 
2.27.0

