summaryrefslogtreecommitdiff
path: root/0012-add-runc-attach-implement-unit-test-and-ci-test.patch
blob: 401d89b517aaa1c16b673412a81a905fe4938e06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
From d37c0c7ded0e107167a98dc1eda2000142d274f0 Mon Sep 17 00:00:00 2001
From: zhongtao <zhongtao17@huawei.com>
Date: Tue, 7 Nov 2023 16:39:50 +0800
Subject: [PATCH 12/14] add runc attach implement unit test and ci test

Signed-off-by: zhongtao <zhongtao17@huawei.com>
---
 CI/test_cases/container_cases/attach.sh     | 153 ++++++++++++++++++++
 CI/test_cases/container_cases/cri_stream.sh |   6 +-
 test/cmd/isulad-shim/common/common_ut.cc    |  42 ++++++
 3 files changed, 197 insertions(+), 4 deletions(-)
 create mode 100755 CI/test_cases/container_cases/attach.sh

diff --git a/CI/test_cases/container_cases/attach.sh b/CI/test_cases/container_cases/attach.sh
new file mode 100755
index 00000000..0d362757
--- /dev/null
+++ b/CI/test_cases/container_cases/attach.sh
@@ -0,0 +1,153 @@
+#!/bin/bash
+#
+# attributes: isula attach test
+# concurrent: NA
+# spend time: 5
+
+#######################################################################
+##- Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
+# - iSulad licensed under the Mulan PSL v2.
+# - You can use this software according to the terms and conditions of the Mulan PSL v2.
+# - You may obtain a copy of Mulan PSL v2 at:
+# -     http://license.coscl.org.cn/MulanPSL2
+# - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
+# - IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
+# - PURPOSE.
+# - See the Mulan PSL v2 for more details.
+##- @Description:CI
+##- @Author: zhongtao
+##- @Create: 2023-11-06
+#######################################################################
+
+declare -r curr_path=$(dirname $(readlink -f "$0"))
+source ../helpers.sh
+
+# $1 : retry limit
+# $2 : retry_interval
+# $3 : retry function
+function do_retry()
+{
+    for i in $(seq 1 "$1"); do
+        $3 $4 $5
+        if [ $? -ne 0 ]; then
+            return 0
+        fi
+        sleep $2
+    done
+    return 1
+}
+
+function get_ioCopy()
+{
+    ps -T -p $(cat /var/run/isulad.pid) | grep IoCopy
+    return $?
+}
+
+function inspect_container_status()
+{
+    [[ $(isula inspect -f '{{.State.Status}}' ${1}) != "${2}" ]]
+    return $?
+}
+
+function set_up()
+{
+    local ret=0
+    local runtime=$1
+
+    isula run -tid --name test --runtime $runtime busybox sh
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to run container with image: ${image}" && ((ret++))
+    
+    msg_info "${test} finished with return ${ret}..."
+    return ${ret}
+}
+
+function test_attach_fun()
+{
+    local ret=0
+    local retry_limit=20
+    local retry_interval=1
+    container_name="test"
+    local test="test_attach_fun => (${FUNCNAME[@]})"
+
+    msg_info "${test} starting..."
+
+    expect <<-END
+spawn isula attach test
+send \n
+expect "*"
+sleep 1
+send "ls \r"
+expect "*"
+send "exit \r"
+expect "*"
+sleep 2
+expect eof
+END
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to attach container test" && ((ret++))
+
+    count=$(isula logs test | grep ls | wc -l)
+    [[ $count -ne 1 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do attach" && ((ret++))
+
+    do_retry ${retry_limit} ${retry_interval} inspect_container_status  ${container_name} exited
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} -  incorrent container status: not Exited" && ((ret++))
+
+    (isula attach test > /tmp/test_attach1.log 2>&1) &
+    sleep 2
+    cat /tmp/test_attach1.log | grep "You cannot attach to a stopped container, start it first"
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to do attach, except fail" && ((ret++))
+
+    rm -rf /tmp/test_attach1.log
+
+    do_retry ${retry_limit} ${retry_interval} get_ioCopy
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - residual IO copy thread in CRI exec operation" && ((ret++))
+
+    msg_info "${test} finished with return ${ret}..."
+    return ${ret}
+}
+
+function tear_down()
+{
+    local ret=0
+
+    isula rm -f test
+    [[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to rm container: test" && ((ret++))
+
+    return ${ret}
+}
+
+function do_test_t()
+{
+    local ret=0
+    local runtime=$1
+    local test="basic attach test => (${runtime})"
+    msg_info "${test} starting..."
+
+    set_up $runtime || ((ret++))
+
+    test_attach_fun  || ((ret++))
+
+    tear_down || ((ret++))
+
+    msg_info "${test} finished with return ${ret}..."
+
+    return $ret
+}
+
+ret=0
+
+isula pull busybox
+[[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - failed to pull image: ${image}" && return ${FAILURE}
+
+isula images | grep busybox
+[[ $? -ne 0 ]] && msg_err "${FUNCNAME[0]}:${LINENO} - missing list image: ${image}" && ((ret++))
+
+for element in ${RUNTIME_LIST[@]};
+do
+    do_test_t $element
+    if [ $? -ne 0 ];then
+        let "ret=$ret + 1"
+    fi
+done
+
+show_result $ret "basic attach"
+
diff --git a/CI/test_cases/container_cases/cri_stream.sh b/CI/test_cases/container_cases/cri_stream.sh
index 2360e240..43ed3891 100755
--- a/CI/test_cases/container_cases/cri_stream.sh
+++ b/CI/test_cases/container_cases/cri_stream.sh
@@ -187,10 +187,8 @@ function do_test_t()
     test_cri_exec_fun  || ((ret++))
     test_cri_exec_abn || ((ret++))
 
-    # runc attach not support
-    if [ $runtime == "lcr" ]; then
-        test_cri_attach || ((ret++))
-    fi
+    test_cri_attach || ((ret++))
+
     tear_down || ((ret++))
 
     msg_info "${test} finished with return ${ret}..."
diff --git a/test/cmd/isulad-shim/common/common_ut.cc b/test/cmd/isulad-shim/common/common_ut.cc
index 63395232..fb60f628 100644
--- a/test/cmd/isulad-shim/common/common_ut.cc
+++ b/test/cmd/isulad-shim/common/common_ut.cc
@@ -87,3 +87,45 @@ TEST_F(CommonUnitTest, test_combined_output)
     params[0] = non_cmd.c_str();
     EXPECT_EQ(cmd_combined_output(non_cmd.c_str(), params, output, &output_len), -1);
 }
+
+TEST_F(CommonUnitTest, test_get_attach_fifo_item)
+{
+    struct isula_linked_list *attach_fifos = NULL;
+    attach_fifos = (struct isula_linked_list *)isula_common_calloc_s(sizeof(struct isula_linked_list));
+    ASSERT_TRUE(attach_fifos != nullptr);
+
+    isula_linked_list_init(attach_fifos);
+
+    EXPECT_EQ(get_attach_fifo_item(4, attach_fifos), nullptr);
+    EXPECT_EQ(get_attach_fifo_item(-1, attach_fifos), nullptr);
+    EXPECT_EQ(get_attach_fifo_item(4, NULL), nullptr);
+
+    struct shim_fifos_fd fifos1 = {
+        .in_fd = 1,
+        .out_fd = 2,
+        .err_fd = 3,
+    };
+    struct shim_fifos_fd fifos2 = {
+        .in_fd = 4,
+        .out_fd = 5,
+        .err_fd = 6,
+    };
+    struct isula_linked_list *node1 = NULL;
+    struct isula_linked_list *node2 = NULL;
+    node1 = (struct isula_linked_list *)isula_common_calloc_s(sizeof(struct isula_linked_list));
+    ASSERT_TRUE(node1 != nullptr);
+    node1->elem = &fifos1;
+    isula_linked_list_add(attach_fifos, node1);
+
+    node2 = (struct isula_linked_list *)isula_common_calloc_s(sizeof(struct isula_linked_list));
+    ASSERT_TRUE(node2 != nullptr);
+    node2->elem = &fifos2;
+    isula_linked_list_add(attach_fifos, node2);
+
+    EXPECT_EQ(get_attach_fifo_item(1, attach_fifos), node1);
+    EXPECT_EQ(get_attach_fifo_item(4, attach_fifos), node2);
+
+    free(node1);
+    free(node2);
+    free(attach_fifos);
+}
-- 
2.42.0