From 94122c5752936b4f5db14521cdd0f39a3dec6851 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Thu, 7 Dec 2023 03:32:15 +0000
Subject: [PATCH 49/65] !2298 bugfix for hook_ignore_poststart_error run in oci
 runtime * bugfix for hook_ignore_poststart_error run in oci runtime

---
 .../hook_ignore_poststart_error.sh            | 70 +++++++++++++------
 1 file changed, 50 insertions(+), 20 deletions(-)

diff --git a/CI/test_cases/container_cases/hook_ignore_poststart_error.sh b/CI/test_cases/container_cases/hook_ignore_poststart_error.sh
index 8c636f7e..38b6f021 100755
--- a/CI/test_cases/container_cases/hook_ignore_poststart_error.sh
+++ b/CI/test_cases/container_cases/hook_ignore_poststart_error.sh
@@ -24,6 +24,48 @@ source ../helpers.sh
 
 test_data_path=$(realpath $curr_path/test_data)
 
+# $1 hook process 
+# $2 container id
+# $3 expect container status
+# $4 process statement
+function test_kill_hook()
+{
+    for a in `seq 20`
+    do
+        bpid=`ps aux | grep "$1" | grep -v grep | awk '{print $2}'`
+        if [ "x" != "x$bpid" ];then
+            kill -9 $bpid
+            break
+        else
+            sleep .5
+            continue
+        fi
+    done
+
+    if [ "x" != "x$4" ];then
+        for a in `seq 20`
+        do
+            bpid=`ps aux | grep "$4" | grep -v grep | awk '{print $2}'`
+            if [ "x" != "x$bpid" ];then
+                kill -9 $bpid
+                break
+            else
+                sleep .5
+                continue
+            fi
+        done
+    fi
+
+    status=`isula inspect -f '{{json .State.Status}}' $2`
+    if [ "$status" == "$3" ];then
+        echo "get right status"
+        return 0
+    else
+        echo "expect $2 $3, but get $status"
+        return 1
+    fi
+}
+
 function test_hook_ignore_poststart_error_spec()
 {
     local ret=0
@@ -42,27 +84,15 @@ function test_hook_ignore_poststart_error_spec()
     [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${image}" && ((ret++))
 
     isula run -n $CONT -itd --runtime $runtime --hook-spec ${test_data_path}/oci_hook_poststart_check.json ${image} &
-
-    for a in `seq 20`
-    do
-        bpid=`ps aux | grep "poststart.sh" | grep -v grep | awk '{print $2}'`
-        if [ "x" != "x$bpid" ];then
-            kill -9 $bpid
-            break
-        else
-            sleep .5
-            continue
-        fi
-    done
-
-    status=`isula inspect -f '{{json .State.Status}}' $CONT`
-    if [ "$status" == "\"running\"" ];then
-        echo "get right status"
+    
+    # when runc container run poststart hook, the process structure is different from lxc
+    if [ $runtime == "lcr" ]; then
+        test_kill_hook "poststart.sh" $CONT \"running\"
     else
-        echo "expect $CONT running, but get $status"
-        ret++
+        test_kill_hook "poststart.sh" $CONT \"exited\" "sleep 300"
     fi
-
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to test kill hook: ${image}" && ((ret++))
+    
     isula stop -t 0 ${CONT}
     [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to stop ${CONT}" && ((ret++))
 
@@ -77,7 +107,7 @@ declare -i ans=0
 
 for element in ${RUNTIME_LIST[@]};
 do
-    test_hook_ignore_poststart_error_spec $1 || ((ans++))
+    test_hook_ignore_poststart_error_spec $element || ((ans++))
 done
 
 show_result ${ans} "${curr_path}/${0}"
-- 
2.42.0

