From 851d68654e8025bbb1fe2380eddb24457addb4e5 Mon Sep 17 00:00:00 2001
From: zhangxiaoyu <zhangxiaoyu58@huawei.com>
Date: Tue, 21 Nov 2023 03:49:56 +0000
Subject: [PATCH 159/181] !2262 bugfix of update restart policy for auto remove
 container * add update restart policy test * bugfix of update restart policy
 for auto remove container

---
 CI/test_cases/container_cases/update.sh       | 26 +++++++++++++++++++
 .../executor/container_cb/execution_extend.c  | 18 ++++++++-----
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/CI/test_cases/container_cases/update.sh b/CI/test_cases/container_cases/update.sh
index 9147b9e0..d180c195 100755
--- a/CI/test_cases/container_cases/update.sh
+++ b/CI/test_cases/container_cases/update.sh
@@ -138,6 +138,27 @@ function do_test_t()
     return $TC_RET_T
 }
 
+function test_autoremove_restartpolicy()
+{
+    containername=test_update2
+    containerid=`isula run -itd --runtime $1 --rm --name $containername busybox`
+    fn_check_eq "$?" "0" "run failed"
+
+    isula update --restart always $containerid
+    fn_check_ne "$?" "0" "update should fail"
+
+    isula update --restart nooooooooooo $containerid
+    fn_check_ne "$?" "0" "update should fail"
+
+    isula update --restart no $containerid
+    fn_check_eq "$?" "0" "update restart policy no failed"
+
+    isula rm -f $containername
+    fn_check_eq "$?" "0" "rm failed"
+
+    return $TC_RET_T
+}
+
 function do_test_t1()
 {
     containername=test_update1
@@ -173,6 +194,11 @@ do
         let "ret=$ret + 1"
     fi
 
+    test_autoremove_restartpolicy $element
+    if [ $? -ne 0 ];then
+        let "ret=$ret + 1"
+    fi
+
     if [ -f "/sys/fs/cgroup/memory/memory.memsw.usage_in_bytes" ];then
         do_test_t1 $element
         if [ $? -ne 0 ];then
diff --git a/src/daemon/executor/container_cb/execution_extend.c b/src/daemon/executor/container_cb/execution_extend.c
index 00d130ac..88569a4e 100644
--- a/src/daemon/executor/container_cb/execution_extend.c
+++ b/src/daemon/executor/container_cb/execution_extend.c
@@ -1023,15 +1023,14 @@ static int update_host_config_check(container_t *cont, host_config *hostconfig)
 
     ret = verify_host_config_settings(hostconfig, true);
     if (ret != 0) {
-        goto out;
+        return -1;
     }
 
     if (container_is_removal_in_progress(cont->state) || container_is_dead(cont->state)) {
         ERROR("Container is marked for removal and cannot be \"update\".");
         isulad_set_error_message(
             "Cannot update container %s: Container is marked for removal and cannot be \"update\".", id);
-        ret = -1;
-        goto out;
+        return -1;
     }
 
     if (container_is_running(cont->state) && hostconfig->kernel_memory) {
@@ -1039,12 +1038,17 @@ static int update_host_config_check(container_t *cont, host_config *hostconfig)
         isulad_set_error_message("Cannot update container %s: Can not update kernel memory to a running container,"
                                  " please stop it first.",
                                  id);
-        ret = -1;
-        goto out;
+        return -1;
     }
 
-out:
-    return ret;
+    if (cont->hostconfig->auto_remove && hostconfig->restart_policy != NULL &&
+        hostconfig->restart_policy->name != NULL && strcmp("no", hostconfig->restart_policy->name) != 0) {
+        ERROR("Cannot update restart policy for the auto remove container %s", id);
+        isulad_set_error_message("Cannot update restart policy for the auto remove container %s", id);
+        return -1;
+    }
+
+    return 0;
 }
 
 static int do_update_resources(const container_update_request *request, container_t *cont)
-- 
2.42.0

