forked from didi/sharingan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsut_replayer.sh
executable file
·366 lines (324 loc) · 12.9 KB
/
sut_replayer.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
workspace=$(cd $(dirname $0) && pwd -P)
cd ${workspace}
# TODO: Attention please!!! Modify-1! Your project's binary name. Is better that $app not contain '-'。
app=example
appcov="$app.test"
# TODO: Attenton please!!! Modify-2! Your project's root path.
root=$(dirname $(dirname $workspace))
# TODO: Attenton please!!! Modify-3! The path where you git clone sharingan. It's used to copy main_test.go & gocov tool when failed by wgetting from github.
replayer_agent_root=$root/replayer-agent
# set file
sut_file=$workspace/$app
sut_file_cov=$workspace/$appcov
sut_pid_file=$workspace/sut.pid
sut_log_file=$workspace/sut.log
# set msg
info_msg="\033[;32m[INFO]\033[0m\t"
warn_msg="\033[;33m[WARN]\033[0m\t"
error_msg="\033[;31m[ERROR]\033[0m\t"
#cov
cov_cmd=cov
cov_file_path='/tmp/ShaRinGan/'
cov_file_prefix='coverage.'
cov_file_suffix='.cov'
# set web server
local_ip="127.0.0.1"
local_port="8998"
# set github about go
git_replayer_agent='https://github.com/didi/sharingan/raw/master/replayer-agent'
git_go='https://raw.githubusercontent.com/didi/sharingan-go/recorder'
install_go_md='https://github.com/didi/sharingan-go/tree/recorder'
VERSION="go1.13"
function install_go() {
curl "$git_go/install/$VERSION" >> /dev/null
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at installing sharingan-go $VERSION : 'curl $git_go/install/$VERSION' \n"
printf "${error_msg}Please install sharingan-go $VERSION manually (refer to $install_go_md) and try again!!! \n"
printf "${warn_msg}Please execute 'export GOROOT=/tmp/recorder-$VERSION && export PATH=\$GOROOT/bin:\$PATH' after installing $VERSION!!!\n"
exit 1
fi
curl "$git_go/install/$VERSION" | sh
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at installing sharingan-go $VERSION : 'curl $git_go/install/$VERSION | sh' \n"
printf "${error_msg}Please install sharingan-go $VERSION manually (refer to $install_go_md) and try again!!! \n"
printf "${warn_msg}Please execute 'export GOROOT=/tmp/recorder-$VERSION && export PATH=\$GOROOT/bin:\$PATH' after installing $VERSION!!!\n"
exit 1
fi
export GOROOT="/tmp/recorder-$VERSION"
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at setting GOROOT, please check!!!\n"
exit 1
fi
export PATH=$GOROOT/bin:$PATH
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at updating PATH, please check!!!\n"
exit 1
fi
printf "${info_msg}GOROOT=$GOROOT \n"
}
# install go
function install_go_tar() {
# get go url
releases_go='https://github.com/didi/sharingan-go/releases/download/'
if [[ "$OSTYPE" =~ ^darwin.* ]]; then
goSuffix="$VERSION.recorder/$VERSION.darwin-amd64.tar.gz"
elif [[ "$OSTYPE" =~ ^linux.* ]]; then
goSuffix="$VERSION.recorder/$VERSION.linux-amd64.tar.gz"
else
printf "${error_msg} Unknown system type! build failed at installing go! \n"
exit 1
fi
# install golang
recorder_dir=/tmp/recorder-${VERSION}
wget "$releases_go/$goSuffix" -O "$recorder_dir.tar.gz"
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at downloading sharingan-go $VERSION : wget $releases_go/$goSuffix -O $recorder_dir.tar.gz !!!\n"
printf "${error_msg}Please install sharingan-go $VERSION manually (refer to $install_go_md) and try again!!! \n"
printf "${warn_msg}Please execute 'export GOROOT=/tmp/recorder-$VERSION && export PATH=\$GOROOT/bin:\$PATH' after installing $VERSION!!!\n"
exit 1
fi
rm -rf $recorder_dir && mkdir -p $recorder_dir
tar -xzf "$recorder_dir.tar.gz" -C ${recorder_dir} --strip-components=1
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at installing $VERSION, please check!!!\n"
exit 1
fi
rm -rf "$recorder_dir.tar.gz"
# export
export GOROOT="/tmp/recorder-$VERSION"
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at setting GOROOT, please check!!!\n"
exit 1
fi
export PATH=$GOROOT/bin:$PATH
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at updating PATH, please check!!!\n"
exit 1
fi
printf "${info_msg}GOROOT=$GOROOT \n"
}
# build
function build() {
preGoroot="/tmp/recorder-go"
if [[ -z $GOROOT || $GOROOT != $preGoroot* ]];then
printf "${info_msg}Installing sharingan-go for you ~~~ \n"
install_go
fi
which go &> /dev/null
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at executing 'which go', please execute 'export PATH=\$GOROOT/bin:\$PATH' !!!\n"
exit 1
fi
goVer=`go version`
if [[ $goVer < 'go version go1.11' ]];then
printf "${warn_msg}current is $goVer, go mod requires at least go1.11!!!\n"
install_go
fi
if [ -z $GOPATH ];then
mkdir -p /tmp/replayer
export GOPATH=/tmp/replayer
printf "${warn_msg}No GOPATH, so setting GOPATH=/tmp/replayer \n"
fi
cd $root
go get 'github.com/didi/sharingan'
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at executing go get github.com/didi/sharingan, please check!!!\n"
exit 1
fi
go clean -modcache
printf "${info_msg}go mod download, please wait~ \n"
go mod download
cd $workspace
binName=$app
if [[ $sub_cmd == $cov_cmd ]];then
# wget main_test.go
wget -O main_test.go "$git_replayer_agent/install/codeCov/main_test.go"
if [ $? -ne 0 ]; then
printf "${warn_msg}Failed to wget main_test.go. Now trying another way, copying from sharingan/replayer-agent!!!\n"
cp -rf $replayer_agent_root/install/codeCov/main_test.go .
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at copying main_test.go from $replayer_agent_root/install/codeCov/, please update var \$replayer_agent_root!!!\n"
exit 1
fi
printf "${info_msg}Successed to copy main_test.go. \n"
fi
# wget gocov* tool
if [[ "$OSTYPE" =~ ^darwin.* ]]; then
sys_dir='darwin'
wget -O gocov "$git_replayer_agent/install/codeCov/darwin/gocov"
wget -O gocov-html "$git_replayer_agent/install/codeCov/darwin/gocov-html"
elif [[ "$OSTYPE" =~ ^linux.* ]]; then
sys_dir='linux'
wget -O gocov "$git_replayer_agent/install/codeCov/linux/gocov"
wget -O gocov-html "$git_replayer_agent/install/codeCov/linux/gocov-html"
else
printf "${error_msg} Unknown system type! build failed at wgetting gocov* tool! \n"
exit 1
fi
if [ $? -ne 0 ]; then
printf "${warn_msg}Failed to wget gocov* tool, Now trying another way, copying from sharingan/replayer-agent!!!\n"
cp -rf $replayer_agent_root/install/codeCov/$sys_dir/gocov* .
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at copying gocov* tool from $replayer_agent_root/install/codeCov/$sys_dir/, please update var \$replayer_agent_root!!!\n"
exit 1
fi
printf "${info_msg}Successed to copy gocov* tool. \n"
fi
chmod 755 $workspace/gocov*
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at chmod $workspace/gocov* tool, please check!!!\n"
exit 1
fi
binName=$appcov
fi
pkgReplay='_ "github.com/didi/sharingan"'
find ./ -name "*\.go" -maxdepth 1 | xargs -n 1 grep $pkgReplay &> /dev/null
if [ $? -eq 0 ];then
printf "${error_msg} build failed for not importing package sharingan/replayer, please import it!!!\n"
exit 1
fi
if [[ $sub_cmd == $cov_cmd ]];then
go test -o $appcov -gcflags="all=-N -l" -tags="replayer" -v -c -covermode=count -coverpkg ./...
else
go build -o $app -tags="replayer" -gcflags="all=-N -l"
fi
if [ $? -ne 0 ]; then
printf "${error_msg}build failed at building sut, please check!!!\n"
exit 1
fi
printf "${info_msg}SUT $binName builds success!!!\n"
}
# start
function start() {
binName=$app
binFile=$sut_file
cmdStart="$sut_file >> $sut_log_file"
cmdBuild="sh sut_replayer.sh build"
if [[ $sub_cmd == $cov_cmd ]];then
binName=$appcov
binFile=$sut_file_cov
curTime=`date +%s`
cmdStart="$sut_file_cov -test.coverprofile=$cov_file_path$cov_file_prefix$app.$curTime$cov_file_suffix >> $sut_log_file"
cmdBuild="sh sut_replayer.sh build $cov_cmd"
fi
if [ ! -f "$binFile" ]; then
printf "${error_msg}please exesute $cmdBuild first!!!\n"
exit 1
fi
if [[ "$OSTYPE" =~ ^linux.* ]]; then
local_ip=`ip addr | grep "inet " | grep -v 127 | awk '{print $2}' | awk -F '/' '{print $1}'`
fi
#ps -ef | grep $binName | grep -v grep $grepV &> /dev/null
if [[ $sub_cmd == $cov_cmd ]];then
ps -ef | grep $binName | grep -v grep &> /dev/null
else
ps -ef | grep $binName | grep -v grep | grep -v $appcov &> /dev/null
fi
if [ $? -eq 0 ];then
printf "${warn_msg}SUT $binName has already started!!! Have fun with http://${local_ip}:${local_port} !\n"
exit 0
fi
# 默认值,如有需要可以修改
export REPLAYER_MOCK_IP="127.0.0.1"
export REPLAYER_MOCK_PORT="3515"
nohup $cmdStart >> $sut_log_file 2>&1 &
sleep 2
pid=$!
if [[ $sub_cmd == $cov_cmd ]];then
ps -ef | grep $binName | grep -v grep &> /dev/null
else
ps -ef | grep $binName | grep -v grep | grep -v $appcov &> /dev/null
fi
if [ $? -ne 0 ];then
printf "${error_msg} Failed to start sut $binName, please check $sut_log_file and try again!!!\n"
exit 1
fi
echo $pid > $sut_pid_file
printf "${info_msg}SUT $binName starts success!!! [pid=${pid}] Have fun with http://${local_ip}:${local_port} !\n"
}
# stop
function stop() {
binName=$app
if [[ $sub_cmd == $cov_cmd ]];then
binName=$appcov
fi
if [[ $sub_cmd == $cov_cmd ]];then
ps -ef | grep $binName | grep -v grep &> /dev/null
else
ps -ef | grep $binName | grep -v grep | grep -v $appcov &> /dev/null
fi
if [ $? -ne 0 ];then
printf "${warn_msg}SUT $binName is not running!!!\n"
exit 0
fi
pkill $binName
sleep 2
if [[ $sub_cmd == $cov_cmd ]];then
ps -ef | grep $binName | grep -v grep &> /dev/null
else
ps -ef | grep $binName | grep -v grep | grep -v $appcov &> /dev/null
fi
if [ $? -eq 0 ];then
printf "${error_msg}Failed to stop sut $binName, please check and try again!!!\n"
exit 1
fi
printf "${info_msg}SUT $binName stops success!!!\n"
# codeCov report
if [[ $sub_cmd == $cov_cmd ]];then
f="$cov_file_path$cov_file_prefix"'*'"$app"'*'"$cov_file_suffix"
covFile=`ls -trls $f | tail -n 1`
#echo $covFile
if [[ $covFile != *$cov_file_suffix ]];then
printf "${warn_msg}Failed to get *.cov file, please check $cov_file_path!!!\n"
exit 0
fi
# get the full path of covfile created by stop cmd
covFile=/`echo ${covFile#*/}`
curTime=`date +%s`
covFile_renamed=`echo ${covFile%cov*}`$curTime
mv $covFile $covFile_renamed
if [ $? -ne 0 ];then
printf "${warn_msg}Failed to rename $covFile to $covFile_renamed, please check !!!\n"
exit 0
fi
# convert cov file to html
covNameOnly_html=`echo ${covFile_renamed##*/}`
$workspace/gocov convert $covFile_renamed | $workspace/gocov-html > $workspace/$covNameOnly_html'.html'
mv $workspace/$covNameOnly_html'.html' $cov_file_path/$covNameOnly_html'.html'
if [ $? -ne 0 ];then
printf "${warn_msg}Failed to gocov convert $covFile_renamed to $covFile_renamed'.html', please check !!!\n"
exit 0
fi
if [[ "$OSTYPE" =~ ^linux.* ]]; then
local_ip=`ip addr | grep "inet " | grep -v 127 | awk '{print $2}' | awk -F '/' '{print $1}'`
fi
printf "${info_msg}Have fun with http://${local_ip}:${local_port}/coverage/report/$covNameOnly_html.html !!!\n"
fi
}
# cov or null
sub_cmd=$2
# check sub cmd
if [[ ! -z $sub_cmd && $sub_cmd != $cov_cmd ]];then
printf "${warn_msg} cmd lists:\n1. build [$cov_cmd]\n2. start [$cov_cmd]\n3. stop [$cov_cmd]\n4. reload [$cov_cmd]\n[$cov_cmd] is optional for codeCov\n"
exit 1
fi
case $1 in
"build" )
build
;;
"start" )
start
;;
"stop" )
stop
;;
"reload" )
stop
start
;;
* )
printf "${warn_msg} cmd lists:\n1. build [$cov_cmd]\n2. start [$cov_cmd]\n3. stop [$cov_cmd]\n4. reload [$cov_cmd]\n[$cov_cmd] is optional for codeCov\n"
exit 1
;;
esac