forked from apache/rocketmq-test-tool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.sh
executable file
·327 lines (277 loc) · 8.81 KB
/
entry.sh
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/bin/sh -l
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
ACTION=$1
VERSION=$2
ASK_CONFIG=$3
DOCKER_REPO_USERNAME=$4
DOCKER_REPO_PASSWORD=$5
CHART_GIT=$6
CHART_BRANCH=$7
CHART_PATH=$8
TEST_CODE_GIT=${9}
TEST_CODE_BRANCH=${10}
TEST_CODE_PATH=${11}
TEST_CMD_BASE=${12}
JOB_INDEX=${13}
HELM_VALUES=${14}
export VERSION
export CHART_GIT
export CHART_BRANCH
export CHART_PATH
export REPO_NAME=`echo ${GITHUB_REPOSITORY#*/} | sed -e "s/\//-/g" | cut -c1-36 | tr '[A-Z]' '[a-z]'`
export WORKFLOW_NAME=${GITHUB_WORKFLOW}
export RUN_ID=${GITHUB_RUN_ID}
export TEST_CODE_GIT
export TEST_CODE_BRANCH
export TEST_CODE_PATH
export YAML_VALUES=`echo "${HELM_VALUES}" | sed -s 's/^/ /g'`
echo "Start test version: ${GITHUB_REPOSITORY}@${VERSION}"
echo "************************************"
echo "* Set config... *"
echo "************************************"
mkdir -p ${HOME}/.kube
kube_config=$(echo "${ASK_CONFIG}" | base64 -d)
echo "${kube_config}" > ${HOME}/.kube/config
export KUBECONFIG="${HOME}/.kube/config"
VELA_APP_TEMPLATE='
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: ${VELA_APP_NAME}
description: ${REPO_NAME}-${WORKFLOW_NAME}-${RUN_ID}@${VERSION}
spec:
components:
- name: ${REPO_NAME}
type: helm
properties:
chart: ${CHART_PATH}
git:
branch: ${CHART_BRANCH}
repoType: git
retries: 3
secretRef: \047\047
url: ${CHART_GIT}
values:
${YAML_VALUES}'
echo -e "${VELA_APP_TEMPLATE}" > ./velaapp.yaml
sed -i '1d' ./velaapp.yaml
env_uuid=${REPO_NAME}-${GITHUB_RUN_ID}-${JOB_INDEX}
if [ ${ACTION} == "deploy" ]; then
echo "************************************"
echo "* Create env and deploy... *"
echo "************************************"
echo ${VERSION}: ${env_uuid} deploy start
vela env init ${env_uuid} --namespace ${env_uuid}
export VELA_APP_NAME=${env_uuid}
envsubst < ./velaapp.yaml > velaapp-${REPO_NAME}.yaml
cat velaapp-${REPO_NAME}.yaml
vela env set ${env_uuid}
vela up -f "velaapp-${REPO_NAME}.yaml"
app=${env_uuid}
status=`vela status ${app} -n ${app}`
echo $status
res=`echo $status | grep "Create helm release successfully"`
let count=0
while [ -z "$res" ]
do
if [ $count -gt 240 ]; then
echo "env ${app} deploy timeout..."
exit 1
fi
echo "waiting for env ${app} ready..."
sleep 5
status=`vela status ${app} -n ${app}`
stopped=`echo $status | grep "not found"`
if [ ! -z "$stopped" ]; then
echo "env ${app} deploy stopped..."
exit 1
fi
res=`echo $status | grep "Create helm release successfully"`
let count=${count}+1
done
fi
TEST_POD_TEMPLATE='
apiVersion: v1
kind: Pod
metadata:
name: ${test_pod_name}
namespace: ${ns}
spec:
restartPolicy: Never
containers:
- name: ${test_pod_name}
image: cloudnativeofalibabacloud/test-runner:v0.0.3
resources:
limits:
cpu: "8"
memory: "8Gi"
requests:
cpu: "8"
memory: "8Gi"
env:
- name: CODE
value: ${TEST_CODE_GIT}
- name: BRANCH
value: ${TEST_CODE_BRANCH}
- name: CODE_PATH
value: ${TEST_CODE_PATH}
- name: ALL_IP
value: ${ALL_IP}
- name: CMD
value: |
${TEST_CMD}
'
echo -e "${TEST_POD_TEMPLATE}" > ./testpod.yaml
sed -i '1d' ./testpod.yaml
if [ ${ACTION} == "test" ]; then
echo "************************************"
echo "* E2E Test... *"
echo "************************************"
ns=${env_uuid}
test_pod_name=test-${env_uuid}-${RANDOM}
export test_pod_name
echo namespace: $ns
all_pod_name=`kubectl get pods --no-headers -o custom-columns=":metadata.name" -n ${ns}`
ALL_IP=""
for pod in $all_pod_name;
do
if [ ! -z `echo "${pod}" | grep "test-${env_uuid}"` ]; then
continue
fi
POD_HOST=$(kubectl get pod ${pod} --template={{.status.podIP}} -n ${ns})
ALL_IP=${pod}:${POD_HOST},${ALL_IP}
done
echo $ALL_IP
echo $TEST_CODE_GIT
echo $TEST_CMD_BASE
export ALL_IP
export ns
TEST_CMD=`echo "${TEST_CMD_BASE}" | sed -s 's/^/ /g'`
echo $TEST_CMD
export TEST_CMD
envsubst < ./testpod.yaml > ./testpod-${ns}.yaml
cat ./testpod-${ns}.yaml
kubectl apply -f ./testpod-${ns}.yaml
sleep 5
pod_status=`kubectl get pod ${test_pod_name} --template={{.status.phase}} -n ${ns}`
if [ -z "$pod_status" ]; then
pod_status="Pending"
fi
while [ "${pod_status}" == "Pending" ] || [ "${pod_status}" == "Running" ]
do
echo waiting for ${test_pod_name} test done...
sleep 5
pod_status=`kubectl get pod ${test_pod_name} --template={{.status.phase}} -n ${ns}`
if [ -z "$pod_status" ]; then
pod_status="Pending"
fi
test_done=`kubectl exec -i ${test_pod_name} -n ${ns} -- ls /root | grep testdone`
if [ ! -z "$test_done" ]; then
echo "Test status: test done"
if [ ! -d "./test_report" ]; then
echo "Copy test reports"
kubectl cp --retries=10 ${test_pod_name}:/root/testlog.txt testlog.txt -n ${ns}
mkdir -p test_report
cd test_report
kubectl cp --retries=10 ${test_pod_name}:/root/code/${TEST_CODE_PATH}/target/surefire-reports/. . -n ${ns}
rm -rf *.txt
ls
cd -
fi
fi
done
exit_code=`kubectl get pod ${test_pod_name} --output="jsonpath={.status.containerStatuses[].state.terminated.exitCode}" -n ${ns}`
kubectl delete pod ${test_pod_name} -n ${ns}
echo E2E Test exit code: ${exit_code}
exit ${exit_code}
fi
if [ ${ACTION} == "test_local" ]; then
echo "************************************"
echo "* E2E Test local... *"
echo "************************************"
wget https://dlcdn.apache.org/maven/maven-3/3.8.7/binaries/apache-maven-3.8.7-bin.tar.gz
tar -zxvf apache-maven-3.8.7-bin.tar.gz -C /opt/
export PATH=$PATH:/opt/apache-maven-3.8.7/bin
ns=${env_uuid}
echo namespace: $ns
all_pod_name=`kubectl get pods --no-headers -o custom-columns=":metadata.name" -n ${ns}`
ALL_IP=""
for pod in $all_pod_name;
do
label=`kubectl get pod ${pod} --output="jsonpath={.metadata.labels.app\.kubernetes\.io/name}" -n ${ns}`
pod_port=`kubectl get -o json services --selector="app.kubernetes.io/name=${label}" -n ${ns} | jq -r '.items[].spec.ports[].port'`
echo "${pod}: ${pod_port}"
for port in ${pod_port};
do
kubectl port-forward ${pod} ${port}:${port} -n ${ns} &
res=$?
if [ ${res} -ne 0 ]; then
echo "kubectl port-forward error: ${pod} ${port}:${port}"
exit ${res}
fi
done
ALL_IP=${pod}:"127.0.0.1",${ALL_IP}
sleep 3
done
echo $ALL_IP
echo $TEST_CODE_GIT
echo $TEST_CMD_BASE
export ALL_IP
export ns
is_mvn_cmd=`echo $TEST_CMD_BASE | grep "mvn"`
if [ ! -z "$is_mvn_cmd" ]; then
TEST_CMD="$TEST_CMD_BASE -DALL_IP=${ALL_IP}"
else
TEST_CMD=$TEST_CMD_BASE
fi
echo $TEST_CMD
git clone $TEST_CODE_GIT -b $TEST_CODE_BRANCH code
cd code
cd $TEST_CODE_PATH
${TEST_CMD}
exit_code=$?
killall kubectl
exit ${exit_code}
fi
if [ ${ACTION} == "clean" ]; then
echo "************************************"
echo "* Delete app and env... *"
echo "************************************"
env=${env_uuid}
vela delete ${env} -n ${env} -y
all_pod_name=`kubectl get pods --no-headers -o custom-columns=":metadata.name" -n ${env}`
for pod in $all_pod_name;
do
kubectl delete pod ${pod} -n ${env}
done
sleep 30
kubectl proxy &
PID=$!
sleep 3
DELETE_ENV=${env}
vela env delete ${DELETE_ENV} -y
sleep 3
kubectl delete namespace ${DELETE_ENV} --wait=false
kubectl get ns ${DELETE_ENV} -o json | jq '.spec.finalizers=[]' > ns-without-finalizers.json
cat ns-without-finalizers.json
curl -X PUT http://localhost:8001/api/v1/namespaces/${DELETE_ENV}/finalize -H "Content-Type: application/json" --data-binary @ns-without-finalizers.json
kill $PID
fi
if [ ${ACTION} == "try" ]; then
kubectl get pods --all-namespaces
fi