From 712d82656ac9bafda7d29be70e7dbcd761a01f98 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Sun, 17 Dec 2023 05:58:56 +0000
Subject: [PATCH 61/65] !2320 improve CI test * improve CI test

---
 .../container_cases/restartpolicy.sh          | 35 ++++++++++++++-----
 CI/test_cases/image_cases/image_digest.sh     | 10 +++---
 CI/test_cases/image_cases/image_search.sh     | 26 ++------------
 3 files changed, 35 insertions(+), 36 deletions(-)

diff --git a/CI/test_cases/container_cases/restartpolicy.sh b/CI/test_cases/container_cases/restartpolicy.sh
index 11c3608f..0ab09636 100755
--- a/CI/test_cases/container_cases/restartpolicy.sh
+++ b/CI/test_cases/container_cases/restartpolicy.sh
@@ -23,18 +23,37 @@ curr_path=$(dirname $(readlink -f "$0"))
 data_path=$(realpath $curr_path/../data)
 source ../helpers.sh
 
+# $1 : retry limit
+# $2 : retry_interval
+# $3 : container name
+# $4 : expect restart count
+function do_retry()
+{
+    for i in $(seq 1 "$1"); do
+        count=`isula inspect --format='{{json .RestartCount}}' ${3}`
+        if [ $count -eq $4 ]; then
+            return 0
+        fi
+        sleep $2
+    done
+    echo "expect $4, get $count"
+    return 1
+}
+
 function do_test_on_failure()
 {
+    local retry_limit=15
+    local retry_interval=1
     containername=test_rp_on_failure
     isula run  --name $containername  -td --restart on-failure:3  busybox /bin/sh -c "exit 2"
     fn_check_eq "$?" "0" "run failed"
 
-    sleep 8
-    count=`isula inspect --format='{{json .RestartCount}}' $containername`
-    if [[ $count != "3"  ]];then
-        echo "expect 3 but get $count"
+    do_retry ${retry_limit} ${retry_interval} ${containername} 3
+    if [[ $? -ne 0 ]];then
         TC_RET_T=$(($TC_RET_T+1))
     fi
+
+    isula stop -t 0 $containername
     testcontainer $containername exited
 
     isula rm $containername
@@ -43,14 +62,14 @@ function do_test_on_failure()
 
 function do_test_unless_stopped()
 {
+    local retry_limit=15
+    local retry_interval=1
     containername=test_rp_unless_stopped
     isula run  --name $containername  -td --restart unless-stopped  busybox /bin/sh -c "exit 2"
     fn_check_eq "$?" "0" "run failed"
 
-    sleep 8
-    count=`isula inspect --format='{{json .RestartCount}}' $containername`
-    if [[ $count == "0"  ]];then
-        echo "expect not 0 but get $count"
+    do_retry ${retry_limit} ${retry_interval} ${containername} 0
+    if [[ $? -ne 0 ]];then
         TC_RET_T=$(($TC_RET_T+1))
     fi
 
diff --git a/CI/test_cases/image_cases/image_digest.sh b/CI/test_cases/image_cases/image_digest.sh
index e30f29f0..cc8b0e48 100755
--- a/CI/test_cases/image_cases/image_digest.sh
+++ b/CI/test_cases/image_cases/image_digest.sh
@@ -25,14 +25,14 @@ source ../helpers.sh
 function test_image_with_digest()
 {
   local ret=0
-  local image="busybox"
-  local image2="ubuntu"
-  local image_digest="busybox@sha256:5cd3db04b8be5773388576a83177aff4f40a03457a63855f4b9cbe30542b9a43"
+  local image="3laho3y3.mirror.aliyuncs.com/library/busybox"
+  local image2="3laho3y3.mirror.aliyuncs.com/library/ubuntu"
+  local image_digest="3laho3y3.mirror.aliyuncs.com/library/busybox@sha256:62ffc2ed7554e4c6d360bce40bbcf196573dd27c4ce080641a2c59867e732dee"
   local test="pull && inspect && tag image with digest test => (${FUNCNAME[@]})"
 
   msg_info "${test} starting..."
 
-  isula pull docker.io/library/${image_digest}
+  isula pull ${image_digest}
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${image}" && return ${FAILURE}
   
   isula tag ${image_digest} ${image}:digest_test
@@ -71,7 +71,7 @@ function test_image_with_digest()
   isula inspect -f '{{.image.repo_tags}}' ${image_digest} | grep "${image}:digest_test"
   [[ $? -eq 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - image digest delete error: ${image_digest}" && ((ret++))
 
-  isula pull docker.io/library/${image2}:latest
+  isula pull ${image2}:latest
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${image2}" && return ${FAILURE}
 
   digest=$(isula inspect "${image2}:latest" | grep "@sha256" | awk -F"\"" '{print $2}')
diff --git a/CI/test_cases/image_cases/image_search.sh b/CI/test_cases/image_cases/image_search.sh
index 1d281cb2..11af02f1 100755
--- a/CI/test_cases/image_cases/image_search.sh
+++ b/CI/test_cases/image_cases/image_search.sh
@@ -33,6 +33,7 @@ function test_image_search()
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - stop isulad failed" && return ${FAILURE}
 
   msg_info "${test} starting..."
+  rm -rf /etc/isulad/daemon.bak
   cp /etc/isulad/daemon.json /etc/isulad/daemon.bak
   sed -i "/registry-mirrors/a\        \"docker.io\"," /etc/isulad/daemon.json
 
@@ -49,39 +50,18 @@ function test_image_search()
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} -  search  ${invalid_image} should fail as it's search name is invalid" && return ${FAILURE}
 
   # test search options   
-  isula search --no-trunc ${image}
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with no-trunc: ${image}" && ((ret++))
-
-  isula search --limit 5 ${image}
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with limit: ${image}" && ((ret++))
+  isula search  --no-trunc --limit 5 --filter stars=3  --filter is-official=true  --filter is-automated=false  --format "table {{.Name}}\t{{.IsOfficial}}" ${image}
+  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with search options: ${image}" && ((ret++))
 
   isula search --limit -1 ${image} 2>&1 | grep "Invalid value"
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with limit: ${image} and and catch error msg" && ((ret++))
 
-  isula search --filter stars=3 ${image}
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with filter stars: ${image}" && ((ret++))
-
-  isula search --filter is-official=true ${image}
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with filter is-official: ${image}" && ((ret++))
-
-  isula search --filter is-automated=true ${image} 2>&1 | grep "AUTOMATED"
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with filter is-automated: ${image}" && ((ret++))
-
   isula search --filter aa=true ${image} 2>&1 | grep "Invalid filter"
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to set filter for search ${image} and catch error msg" && ((ret++))
 
-  isula search ${image} 2>&1 | grep "NAME"
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with default table format: ${image}" && ((ret++))
-
-  isula search --format "table {{.IsAutomated}}\t{{.IsOfficial}}" ${image} 2>&1 | grep "AUTOMATED"
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with table format: ${image}" && ((ret++))
-
   isula search --format "{{Name}}" ${image} 2>&1 | grep "invalid format field"
   [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to set format for search ${image} and catch error msg" && ((ret++))
 
-  isula search --format "{{.Name}}" ${image} 2>&1
-  [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to search images with none-table format: ${image}" && ((ret++))
-
   cp -f /etc/isulad/daemon.bak /etc/isulad/daemon.json
 
   check_valgrind_log
-- 
2.42.0

