forked from WanLinghao/coredump-detector
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-test.sh
executable file
·174 lines (149 loc) · 5.59 KB
/
run-test.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
#!/bin/bash
source "./kube.sh"
source "./util.sh"
IMAGE="wlhtorresowen\/fujitsu-coredump-integration-testing:v0.1"
DEPLOYMENT_TEMPLATE="deployment_template.yaml"
COREDUMP_TEMPLATE="coredumpendpoint_template.yaml"
LABEL="status=coredump-test"
test_authority_all(){
test_authority create deployment
test_authority create namespace
test_authority create coredumpendpoints
test_authority get coredumpendpoints
test_authority get coredumpendpoints dump
test_authority get pods log
}
# 'create_deployment default 10'
# means creating a deployment with replicaset 10 in namespace default
create_deployment(){
namespace=$1
pods_num=$2
deployment_name=$(generate_random_str_with_random_length 5 10)
yaml_file=$(echo "/tmp/dep_"$deployment_name".yaml")
cp $DEPLOYMENT_TEMPLATE $yaml_file
# replace namespace
sed -i "s/__NAMESPACE__/${namespace}/g" $yaml_file
# replace deployment name
sed -i "s/__DEPLOYMENT_NAME__/${deployment_name}/g" $yaml_file
# replace image name
sed -i "s/__CRASH_TRIGGER_CONTAINER__/${IMAGE}/g" $yaml_file
# replace container name
container_name_1=$(generate_random_str_with_random_length 5 10)
container_name_2=$(generate_random_str_with_random_length 5 10)
sed -i "s/__CONTAINER_NAME_1__/${container_name_1}/g" $yaml_file
sed -i "s/__CONTAINER_NAME_2__/${container_name_2}/g" $yaml_file
# replace job period duration for container
period_duration_1=$(generate_random_num 1 20)
period_duration_2=$(generate_random_num 1 20)
sed -i "s/__PERIOD_1__/${period_duration_1}/g" $yaml_file
sed -i "s/__PERIOD_2__/${period_duration_2}/g" $yaml_file
# replace pods num
pods_num=$(generate_random_num 1 10)
sed -i "s/__PODS_NUM__/${pods_num}/g" $yaml_file
create_via_file $yaml_file
}
# build_coredump $pod_max
build_coredump(){
current_pod_sum=0
pod_max=$1
while [ $current_pod_sum -lt $pod_max ]
do
namespace=$(generate_random_str_with_random_length 5 10)
kubectl create ns $namespace
# failure reason varied, we just ingore it and move to next round
if [ $? -ne 0 ];then
echo_red "Warning: faile to create namespace $namespace"
continue
fi
# pre condition: the namespace must be exist
kube::test::wait_object_assert "namespaces/${namespace}" '{{.metadata.name}}' "${namespace}"
kubectl label ns $namespace $LABEL
if [ $? -ne 0 ];then
echo_red "Warning: faile to label namespace $namespace, delete it"
kubectl delete ns $namespace
continue
fi
for i in $(seq 1 3)
do
pods_num=$(generate_random_num 1 4)
create_deployment $namespace $pods_num
if [ $? -eq 0 ];then
let current_pod_sum+=$pods_num
fi
done
done
}
test_create_coredumpendpoint(){
namespace=$1
pod=$2
yaml_file=$(echo "/tmp/cde_"$pod".yaml")
cp $COREDUMP_TEMPLATE $yaml_file
sed -i "s/__NAMESPACE__/${namespace}/g" $yaml_file
sed -i "s/__NAME__/${pod}/g" $yaml_file
sed -i "s/__UID__//g" $yaml_file
create_via_file $yaml_file
}
# test_download_core_files namespace pod container
test_download_core_files(){
namespace=$1
pod=$2
container=$3
# test if we have core files generated in that container
output_message=$(kubectl logs $pod -n $namespace $container)
kube::test::if_has_string "${output_message}" "core dumped" > /dev/null 2>&1
if [ $? -ne 0 ];then
return 0
fi
raw_link=$(echo "/apis/coredump.fujitsu.com/v1alpha1/namespaces/${namespace}/coredumpendpoints/${pod}/dump?${container}")
kubectl get --raw=$raw_link > /dev/null 2>&1
}
test_basic(){
for namespace in $(kubectl get ns -o custom-columns=:.metadata.name --no-headers -l $LABEL);do
echo "handle $namespace"
for pod in $(kubectl get pod -n $namespace -o custom-columns=:.metadata.name --no-headers);do
status=$(kubectl get pod $pod -n $namespace -o jsonpath="{.status.phase}")
if [ $status != "Running" ];then
continue
fi
test_create_coredumpendpoint $namespace $pod
if [ $? -ne 0 ];then
echo_red "Warning:create coredumpendpoint failed for $namespace/$pod"
continue
fi
for container in $(kubectl get pod $pod -n $namespace -o jsonpath={.spec.containers[*].name});do
test_download_core_files $namespace $pod $container
if [ $? -ne 0 ];then
echo_red "Error: unable to download core files for container $container in $namespace/$pod"
else
echo_green "Download core files for container $container in $namespace/$pod successed"
fi
done
done
done
}
clean_namespace(){
kubectl delete ns -l $LABEL
}
echo "Use default settings as kube config file to access k8s cluster"
test_connect
test_authority_all
if [ $# -gt 0 ];then
echo "Set test pod sum to $2"
pod_max=$2
else
pod_max=20
echo "Set test pod sum to default value: $pod_max"
fi
build_coredump $pod_max
echo "*****************NOW THE TEST DEPLOYMENTS HAS BEEN LAUNCHED************"
echo "Please enter 'y' when most pods are ready, enter other to exit the test and do clean job"
read answer
if [ $answer == "y" ];then
echo "*****************BASIC TEST BEGIN************"
test_basic
echo "*****************BASIC TEST DONE, NOW CLEAN************"
clean_namespace
else
echo "*****************SKIP TEST AND DO CLEAN JOBS************"
clean_namespace
fi