super-test 1.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGE.txt +9 -0
- data/README.txt +13 -0
- data/bin/idb +19 -0
- data/bin/ii +113 -0
- data/bin/istart_mock +36 -0
- data/bin/istop_mock +16 -0
- data/bin/kk +181 -0
- data/bin/rubyatp +42 -0
- data/bin/start_mock +36 -0
- data/bin/stop_mock +16 -0
- data/bin/xgo +45 -0
- data/conf/conf.rb +11 -0
- data/conf/db03.yml +31 -0
- data/conf/db13.yml +37 -0
- data/conf/db19.yml +33 -0
- data/conf/online.yml +32 -0
- data/conf/super_test.yml +40 -0
- data/conf/system.yml +26 -0
- data/conf/sz00.yml +31 -0
- data/conf/sz06.yml +34 -0
- data/conf/wise00.yml +11 -0
- data/data/case.house +0 -0
- data/data/char.house +0 -0
- data/data/images/Bluehills.jpg +0 -0
- data/data/images/Sunset.jpg +0 -0
- data/data/images/Waterlilies.jpg +0 -0
- data/data/images/Winter.jpg +0 -0
- data/data/query.house +18595 -0
- data/data/user.house +8 -0
- data/lib/action/module/check_action.rb +58 -0
- data/lib/action/module/cnnt_action.rb +161 -0
- data/lib/action/module/datacenter_action.rb +48 -0
- data/lib/action/module/http_pack_helper.rb +163 -0
- data/lib/action/module/httpserver_action.rb +297 -0
- data/lib/action/module/inc.rb +21 -0
- data/lib/action/module/mcpack_action.rb +57 -0
- data/lib/action/module/sql_action.rb +84 -0
- data/lib/action/module/ssh_action.rb +37 -0
- data/lib/action/system/user_action.rb +180 -0
- data/lib/common/appium_helper.rb +217 -0
- data/lib/common/atp_helper.rb +74 -0
- data/lib/common/atp_report.rb +344 -0
- data/lib/common/data_house.rb +405 -0
- data/lib/common/data_model.rb +87 -0
- data/lib/common/http/assert_helper.rb +343 -0
- data/lib/common/http/fix_hpricot.rb +118 -0
- data/lib/common/http/html_helper.rb +362 -0
- data/lib/common/http/http_helper.rb +469 -0
- data/lib/common/http/multipart.rb +81 -0
- data/lib/common/isandbox_helper.rb +195 -0
- data/lib/common/json_helper.rb +43 -0
- data/lib/common/mock_helper.rb +168 -0
- data/lib/common/mtop_helper.rb +141 -0
- data/lib/common/myconf_helper.rb +49 -0
- data/lib/common/mylog_helper.rb +77 -0
- data/lib/common/pairwise.rb +121 -0
- data/lib/common/query_house.rb +89 -0
- data/lib/common/report_helper.rb +97 -0
- data/lib/common/robot_helper.rb +85 -0
- data/lib/common/socket/check.rb +325 -0
- data/lib/common/socket/conf_modifier.rb +149 -0
- data/lib/common/socket/context.rb +55 -0
- data/lib/common/socket/data.rb +392 -0
- data/lib/common/socket/data_helper.rb +41 -0
- data/lib/common/socket/env.rb +317 -0
- data/lib/common/socket/inc.rb +24 -0
- data/lib/common/socket/log.rb +213 -0
- data/lib/common/socket/log4s.rb +58 -0
- data/lib/common/socket/log_helper.rb +332 -0
- data/lib/common/socket/my_sql.rb +77 -0
- data/lib/common/socket/net.rb +74 -0
- data/lib/common/socket/network.rb +115 -0
- data/lib/common/socket/rlib/conf_modifier.rb +130 -0
- data/lib/common/socket/rlib/data_helper.rb +33 -0
- data/lib/common/socket/rlib/log_helper.rb +303 -0
- data/lib/common/socket/rlib/process_helper.rb +91 -0
- data/lib/common/socket/stub.rb +276 -0
- data/lib/common/socket/util.rb +266 -0
- data/lib/patterns/inc.rb +10 -0
- data/lib/patterns/mc_base_pattern.rb +474 -0
- data/lib/patterns/some_pattern.rb +145 -0
- data/lib/super_test.rb +114 -0
- data/log/super_test.log +0 -0
- data/log/super_test.log.wf +0 -0
- data/test/cover_me.rb +6 -0
- data/tool/jenny +0 -0
- data/tool/mcsend/mcsend2 +0 -0
- data/tool/mcsend/mcsend2_shead +0 -0
- data/tool/mcserver/conf/mcserver.conf +77 -0
- data/tool/mcserver/conf/mcserverauthip +17 -0
- data/tool/mcserver/mcserver +0 -0
- data/tool/mcserver/mcserver_trans +0 -0
- data/tool/mysql +0 -0
- data/tool/net/mcsend/mcsend +0 -0
- data/tool/net/mcsend/mcsend_trans +0 -0
- data/tool/net/mcsend/nshead.data +7 -0
- data/tool/net/mcsend/res_nshead.data +7 -0
- data/tool/net/my_client/my_client +0 -0
- data/tool/net/tsclient/client.tcl +205 -0
- data/tool/others/kk +190 -0
- data/tool/others/rubyatp +42 -0
- data/tool/others/supertest_install.sh +34 -0
- data/tool/trip/idb +0 -0
- data/tool/ts-agent/nshead.bft +12 -0
- data/tool/ts-agent/server.conf +10 -0
- data/tool/ts-agent/server.tcl +376 -0
- metadata +275 -0
data/tool/others/kk
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# space自动化的综合性帮助工具
|
3
|
+
#
|
4
|
+
# Author::liqiang01
|
5
|
+
# Date::2009-8-12
|
6
|
+
|
7
|
+
|
8
|
+
gen_space_case(){
|
9
|
+
#casename=`ruby -e "
|
10
|
+
#str=\"${1%_*}\"
|
11
|
+
#str.gsub!(/_/,\" \")
|
12
|
+
#str.gsub!(/\b\w/){$&.upcase}
|
13
|
+
#str.gsub!(/\s/,\"\")
|
14
|
+
#print str
|
15
|
+
#"`
|
16
|
+
|
17
|
+
filename=${1%.rb}".rb"
|
18
|
+
casename=${1%_*}
|
19
|
+
casename=`ruby -e "'$casename'.split('_').each{|s| print s.capitalize}"`
|
20
|
+
casename=${casename%.rb}
|
21
|
+
|
22
|
+
echo "#!/usr/bin/env ruby
|
23
|
+
# coding: utf-8
|
24
|
+
#
|
25
|
+
# Author::
|
26
|
+
# Date:: `date`
|
27
|
+
|
28
|
+
\$CASE_ROOT = \"#{File.dirname(__FILE__)}\"
|
29
|
+
require \"${gname2}_test\"
|
30
|
+
|
31
|
+
|
32
|
+
# ===== 全局配置参数
|
33
|
+
#TODO
|
34
|
+
|
35
|
+
|
36
|
+
# ===== case描述
|
37
|
+
#TODO
|
38
|
+
class $casename < ${gname1}Test # :nodoc:
|
39
|
+
|
40
|
+
# 测试点
|
41
|
+
#TODO
|
42
|
+
def test_${1%_*}
|
43
|
+
#TODO
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
" > $filename
|
49
|
+
vim $filename
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
gen_space_keyword(){
|
54
|
+
echo "#!/usr/bin/env ruby
|
55
|
+
# coding: utf-8
|
56
|
+
#
|
57
|
+
# Author::
|
58
|
+
# Date:: `date`
|
59
|
+
|
60
|
+
|
61
|
+
module ...ActionHelper
|
62
|
+
|
63
|
+
# === 功能:
|
64
|
+
# .
|
65
|
+
#
|
66
|
+
# === 参数解释:
|
67
|
+
# - .
|
68
|
+
# - .
|
69
|
+
# - .
|
70
|
+
#
|
71
|
+
# === Example:
|
72
|
+
# ...
|
73
|
+
#
|
74
|
+
# === 备注:
|
75
|
+
#
|
76
|
+
def ...(...)
|
77
|
+
...
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
if __FILE__ == \$0
|
82
|
+
end
|
83
|
+
|
84
|
+
" > $1
|
85
|
+
vim $1
|
86
|
+
}
|
87
|
+
|
88
|
+
|
89
|
+
default(){
|
90
|
+
echo "KK
|
91
|
+
|
92
|
+
NAME
|
93
|
+
kk - 取名kk是因为输入起来很简单 : )
|
94
|
+
|
95
|
+
SYNOPSIS
|
96
|
+
kk [OPTION]... [FILE]...
|
97
|
+
|
98
|
+
DESCRIPTION
|
99
|
+
-c, --case
|
100
|
+
后面跟着文件名将生成自动化case模板
|
101
|
+
case名称规范: {case含义备注字段}_{文本case的atp上的id}.rb (如: my_demo_125698.rb)
|
102
|
+
-h, --help
|
103
|
+
帮助文档
|
104
|
+
-i, --interactive
|
105
|
+
进入交互模式,在交互模式中你可以直接使用super-test的所有关键字
|
106
|
+
-k, --keyword
|
107
|
+
后面跟着文件名将生成关键字定义模板
|
108
|
+
|
109
|
+
|
110
|
+
其他信息:
|
111
|
+
关键字获取网页:
|
112
|
+
http://db-testing-space007.db01.baidu.com:6666/super-test/rdoc/
|
113
|
+
|
114
|
+
|
115
|
+
case配置文件的读取:
|
116
|
+
增加执行case前去require 同级目录下的 init_dir.rb
|
117
|
+
对于case本身的配置建议放到case文件中
|
118
|
+
|
119
|
+
|
120
|
+
部署相关:
|
121
|
+
1. 为了定位ruby路径,需要:
|
122
|
+
RUBYLIB=/home/space/cvs/app-test/search/space/super-test/lib:/home/space/cvs/app-test/search/space/space-test/lib
|
123
|
+
export RUBYLIB
|
124
|
+
2. ruby发布到每个机器上
|
125
|
+
super_test和space_test发布到每个机器上
|
126
|
+
|
127
|
+
AUTHOR
|
128
|
+
kk
|
129
|
+
|
130
|
+
SEE ALSO
|
131
|
+
http://com.baidu.com/twiki/bin/view/Test/NSTest_space_super_test
|
132
|
+
|
133
|
+
kk (spaceutils) 1.0.1
|
134
|
+
|
135
|
+
|
136
|
+
" | less
|
137
|
+
}
|
138
|
+
|
139
|
+
|
140
|
+
if echo $HOME | grep -q -E "space|ufriend";then
|
141
|
+
gname1="Space"
|
142
|
+
gname2="space"
|
143
|
+
else
|
144
|
+
gname1="Wise"
|
145
|
+
gname2="wise"
|
146
|
+
fi
|
147
|
+
|
148
|
+
super_irb="
|
149
|
+
require \"irb\"
|
150
|
+
require \"${gname2}_test\"
|
151
|
+
|
152
|
+
if File.exist?(\"init_dir.rb\")
|
153
|
+
require \"init_dir.rb\"
|
154
|
+
end
|
155
|
+
if __FILE__ == \$0
|
156
|
+
IRB.start(__FILE__)
|
157
|
+
else
|
158
|
+
# check -e option
|
159
|
+
if /^-e$/ =~ \$0
|
160
|
+
IRB.start(__FILE__)
|
161
|
+
else
|
162
|
+
IRB.setup(__FILE__)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
"
|
166
|
+
|
167
|
+
if [ ${#@} -eq 0 ];then
|
168
|
+
default
|
169
|
+
fi
|
170
|
+
while getopts "chik" optname
|
171
|
+
do
|
172
|
+
case $optname in
|
173
|
+
"c")
|
174
|
+
eval _f='$'$OPTIND
|
175
|
+
gen_space_case $_f
|
176
|
+
exit 0;;
|
177
|
+
"h")
|
178
|
+
default
|
179
|
+
exit 0;;
|
180
|
+
"i")
|
181
|
+
ruby -e "$super_irb"
|
182
|
+
exit 0;;
|
183
|
+
"k")
|
184
|
+
eval _f='$'$OPTIND
|
185
|
+
gen_space_keyword $_f
|
186
|
+
exit 0;;
|
187
|
+
esac
|
188
|
+
done
|
189
|
+
|
190
|
+
|
data/tool/others/rubyatp
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
#
|
4
|
+
# Author:: liqiang01
|
5
|
+
# Date:: 2009-8-31 20:50:10
|
6
|
+
|
7
|
+
=begin
|
8
|
+
|
9
|
+
使用atp调用supertest case的adapter
|
10
|
+
atp发起执行字段为:
|
11
|
+
rubyatp #{@this.id}
|
12
|
+
|
13
|
+
=end
|
14
|
+
|
15
|
+
def run_case line
|
16
|
+
casenames = Dir.glob("#{line}/**/*_#{ARGV[0]}.rb")
|
17
|
+
puts "Got case"
|
18
|
+
if ( casenames.length == 0 )
|
19
|
+
puts "Case Not found"
|
20
|
+
else
|
21
|
+
puts casenames
|
22
|
+
puts "Run case"
|
23
|
+
casenames.each{|casei|
|
24
|
+
puts "Runing: #{casei}"
|
25
|
+
system( "source ~/.bash_profile; ruby #{casei}" )
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# get case path from env param RUBYLIB
|
31
|
+
dir = `source ~/.bash_profile; echo $RUBYLIB`.chop!
|
32
|
+
libs = dir.split(":")
|
33
|
+
libs.each{ |line|
|
34
|
+
if(! (line =~ /super-test/) )
|
35
|
+
line.gsub!(/lib/, "case")
|
36
|
+
run_case "#{line}/module"
|
37
|
+
run_case "#{line}/system"
|
38
|
+
end
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
current_dir=`pwd`
|
3
|
+
current_home=$HOME
|
4
|
+
echo "Now donwload to $current_dir/tmp"
|
5
|
+
wget ftp://db-testing-space03.db01.baidu.com:/tmp/super-test/ -nH -r >/dev/null 2>&1
|
6
|
+
echo "Now cp to $current_home/autotest"
|
7
|
+
if [ -d $current_home/autotest ];then
|
8
|
+
echo "$current_home/autotest already exist"
|
9
|
+
echo "Are U sure to continue?[Y/N]"
|
10
|
+
read ans
|
11
|
+
if [ "$ans" = "Y" ];then
|
12
|
+
if [ -d $current_home/autotest/super-test ];then
|
13
|
+
echo "$current_home/autotest/super-test already exist"
|
14
|
+
echo "Now we exit. Bye"
|
15
|
+
exit
|
16
|
+
fi
|
17
|
+
mv $current_dir/tmp/super-test $current_home/autotest/
|
18
|
+
fi
|
19
|
+
if [ "$ans" = "N" ];then
|
20
|
+
echo "Bye"
|
21
|
+
exit
|
22
|
+
fi
|
23
|
+
else
|
24
|
+
echo "Now mkdir $current_home/autotest"
|
25
|
+
mkdir $current_home/autotest
|
26
|
+
mv $current_dir/tmp/super-test $current_home/autotest/
|
27
|
+
fi
|
28
|
+
rm -r $current_dir/tmp
|
29
|
+
echo "Now changing your .bash_profile"
|
30
|
+
grep -v -P "RUBYLIB" ~/.bash_profile > ~/.bashtmp
|
31
|
+
cp ~/.bashtmp ~/.bash_profile; rm ~/.bashtmp;
|
32
|
+
echo 'export RUBYLIB="$HOME/autotest/super-test/lib"' >> ~/.bash_profile
|
33
|
+
source ~/.bash_profile
|
34
|
+
echo "Now finish, have fun:)"
|
data/tool/trip/idb
ADDED
Binary file
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#stub名称
|
2
|
+
stubs : s1, s2, s3
|
3
|
+
#stub端口
|
4
|
+
ports : 22222, 33333, 44444
|
5
|
+
#stub工作模式
|
6
|
+
actions : 0, 1, 2
|
7
|
+
#stub应该返回的nshead文件名
|
8
|
+
heads : /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s1.head, /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s2.head, /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s3.head
|
9
|
+
#stub应该返回的文件名
|
10
|
+
files : /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s1.stub, /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s2.stub, /home/space/tools/app-test/search/space/module-test/case/test/stub_test/stub/1_s3.stub
|
@@ -0,0 +1,376 @@
|
|
1
|
+
load bft.so
|
2
|
+
load cnnt.so
|
3
|
+
load mc_pack.so
|
4
|
+
|
5
|
+
#=============vars=============
|
6
|
+
set conf_path "server.conf"
|
7
|
+
set debug "1"
|
8
|
+
set wait_time 3000
|
9
|
+
|
10
|
+
#配置项的内存镜像数组
|
11
|
+
set stubs {}
|
12
|
+
set ports {}
|
13
|
+
set actions {}
|
14
|
+
set heads {}
|
15
|
+
set files {}
|
16
|
+
|
17
|
+
#当前处理的head hash
|
18
|
+
set head_arr(0) 0
|
19
|
+
|
20
|
+
|
21
|
+
#========helper methods========
|
22
|
+
|
23
|
+
proc log {info} {
|
24
|
+
global debug
|
25
|
+
if {$debug == 1} {
|
26
|
+
puts $info
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
# 判断传入参数是否是注视
|
31
|
+
proc is_cmt { line } {
|
32
|
+
if { [string compare [string index $line 0] "#"] == 0 } {
|
33
|
+
return 1
|
34
|
+
}
|
35
|
+
|
36
|
+
return 0
|
37
|
+
}
|
38
|
+
|
39
|
+
|
40
|
+
# 在如配置文件,初始化4个全局数组
|
41
|
+
proc load_conf {} {
|
42
|
+
global conf_path
|
43
|
+
global stubs
|
44
|
+
global ports
|
45
|
+
global actions
|
46
|
+
global heads
|
47
|
+
global files
|
48
|
+
|
49
|
+
log "\nloading conf..."
|
50
|
+
set conf [open $conf_path "r"]
|
51
|
+
while { [gets $conf line] != -1 } {
|
52
|
+
if { [is_cmt $line] == 0 } {
|
53
|
+
#log $line
|
54
|
+
|
55
|
+
set item [split $line :]
|
56
|
+
set key [string trim [lindex $item 0]]
|
57
|
+
set value [string trim [lindex $item 1]]
|
58
|
+
|
59
|
+
switch $key {
|
60
|
+
"stubs" { set stubs [split $value ","]
|
61
|
+
|
62
|
+
log "loading stubs..."
|
63
|
+
set i 0
|
64
|
+
foreach item $stubs {
|
65
|
+
lset stubs $i [string trim $item]
|
66
|
+
log [lindex $stubs $i]
|
67
|
+
incr i
|
68
|
+
}
|
69
|
+
}
|
70
|
+
"ports" { set ports [split $value ","]
|
71
|
+
|
72
|
+
log "loading ports..."
|
73
|
+
set i 0
|
74
|
+
foreach item $ports {
|
75
|
+
lset ports $i [string trim $item]
|
76
|
+
log [lindex $ports $i]
|
77
|
+
incr i
|
78
|
+
}
|
79
|
+
|
80
|
+
}
|
81
|
+
"actions" { set actions [split $value ","]
|
82
|
+
|
83
|
+
log "loading actions..."
|
84
|
+
set i 0
|
85
|
+
foreach item $actions {
|
86
|
+
lset actions $i [string trim $item]
|
87
|
+
log [lindex $actions $i]
|
88
|
+
incr i
|
89
|
+
}
|
90
|
+
|
91
|
+
}
|
92
|
+
"heads" { set heads [split $value ","]
|
93
|
+
|
94
|
+
log "loading heads..."
|
95
|
+
set i 0
|
96
|
+
foreach item $heads {
|
97
|
+
lset heads $i [string trim $item]
|
98
|
+
log [lindex $heads $i]
|
99
|
+
incr i
|
100
|
+
}
|
101
|
+
}
|
102
|
+
"files" { set files [split $value ","]
|
103
|
+
|
104
|
+
log "loading files..."
|
105
|
+
set i 0
|
106
|
+
foreach item $files {
|
107
|
+
lset files $i [string trim $item]
|
108
|
+
log [lindex $files $i]
|
109
|
+
incr i
|
110
|
+
}
|
111
|
+
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
# 加载当前nshead
|
119
|
+
# 成功加载:head变量被赋予head文件中的值; 加载失败: head变量的hash表被清空
|
120
|
+
proc load_head { head_path } {
|
121
|
+
global head_arr
|
122
|
+
|
123
|
+
#clear head array
|
124
|
+
array unset head_arr
|
125
|
+
set head_arr(0) 0
|
126
|
+
|
127
|
+
if { [file exists $head_path] == 0 } {
|
128
|
+
log "response mcpack file not exist and load head failed"
|
129
|
+
} else {
|
130
|
+
set hf [open $head_path "r"]
|
131
|
+
while { [gets $hf line] != -1 } {
|
132
|
+
if { [is_cmt $line] == 0 } {
|
133
|
+
set item [split $line :]
|
134
|
+
set key [string trim [lindex $item 0]]
|
135
|
+
set value [string trim [lindex $item 1]]
|
136
|
+
|
137
|
+
switch $key {
|
138
|
+
"id"
|
139
|
+
{
|
140
|
+
set head_arr("id") $value
|
141
|
+
log "loading id = $value"
|
142
|
+
log "loading id = $head_arr(\"id\")"
|
143
|
+
}
|
144
|
+
"version"
|
145
|
+
{
|
146
|
+
set head_arr("version") $value
|
147
|
+
log "loading version = $value"
|
148
|
+
log "loading version = $head_arr(\"version\")"
|
149
|
+
}
|
150
|
+
"log_id"
|
151
|
+
{
|
152
|
+
set head_arr("log_id") $value
|
153
|
+
log "loading log_id = $value"
|
154
|
+
log "loading log_id = $head_arr(\"log_id\")"
|
155
|
+
}
|
156
|
+
"provider"
|
157
|
+
{
|
158
|
+
set head_arr("provider") $value
|
159
|
+
log "loading provider = $value"
|
160
|
+
log "loading provider = $head_arr(\"provider\")"
|
161
|
+
}
|
162
|
+
"magic_num"
|
163
|
+
{
|
164
|
+
set head_arr("magic_num") $value
|
165
|
+
log "loading magic_num = $value"
|
166
|
+
log "loading magic_num = $head_arr(\"magic_num\")"
|
167
|
+
}
|
168
|
+
"reserved"
|
169
|
+
{
|
170
|
+
set head_arr("reserved") $value
|
171
|
+
log "loading reserved = $value"
|
172
|
+
log "loading reserved = $head_arr(\"reserved\")"
|
173
|
+
}
|
174
|
+
}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
}
|
180
|
+
|
181
|
+
#启动所有桩
|
182
|
+
proc start_srvs {} {
|
183
|
+
global stubs
|
184
|
+
|
185
|
+
set idx 0
|
186
|
+
foreach stub $stubs {
|
187
|
+
start_svr $idx
|
188
|
+
|
189
|
+
incr idx
|
190
|
+
}
|
191
|
+
}
|
192
|
+
|
193
|
+
#启动一个桩
|
194
|
+
proc start_svr {idx} {
|
195
|
+
global stubs
|
196
|
+
global ports
|
197
|
+
global actions
|
198
|
+
global heads
|
199
|
+
global files
|
200
|
+
|
201
|
+
set stub [lindex $stubs $idx]
|
202
|
+
set port [lindex $ports $idx]
|
203
|
+
set action [lindex $actions $idx]
|
204
|
+
set head [lindex $heads $idx]
|
205
|
+
set file [lindex $files $idx]
|
206
|
+
|
207
|
+
log "\nstarting stub..."
|
208
|
+
log "stub=$stub, port=$port, action=$action, file=$file"
|
209
|
+
|
210
|
+
cnnt new $stub
|
211
|
+
cnnt config $stub "server;ip-local:0.0.0.0;port-local:$port;type-data:raw;"
|
212
|
+
cnnt onrecv $stub svrOnRecv -id -name
|
213
|
+
cnnt start $stub
|
214
|
+
log [cnnt list]
|
215
|
+
}
|
216
|
+
|
217
|
+
# 根据桩的名称获取其索引
|
218
|
+
# return:存在,返回索引值>=0 ; 不存在,返回-1
|
219
|
+
proc get_idx_by_name { stub_name } {
|
220
|
+
global stubs
|
221
|
+
|
222
|
+
set idx 0
|
223
|
+
foreach stub $stubs {
|
224
|
+
if { [string compare $stub_name $stub] == 0 } {
|
225
|
+
return $idx
|
226
|
+
}
|
227
|
+
incr idx
|
228
|
+
}
|
229
|
+
|
230
|
+
return -1
|
231
|
+
}
|
232
|
+
|
233
|
+
# 桩回调函数
|
234
|
+
proc svrOnRecv { data id name } {
|
235
|
+
global stubs
|
236
|
+
global actions
|
237
|
+
|
238
|
+
#重新加载配置!
|
239
|
+
load_conf
|
240
|
+
|
241
|
+
log "serve for $name..."
|
242
|
+
set idx [get_idx_by_name $name]
|
243
|
+
if { $idx < 0 } {
|
244
|
+
log "get index by name: $name failed"
|
245
|
+
exit
|
246
|
+
}
|
247
|
+
|
248
|
+
set action [lindex $actions $idx]
|
249
|
+
|
250
|
+
switch $action {
|
251
|
+
"0" { action0 $data, $id, $name $idx}
|
252
|
+
"1" { action1 $id, $name }
|
253
|
+
"2" { action2 $id, $name }
|
254
|
+
}
|
255
|
+
|
256
|
+
}
|
257
|
+
|
258
|
+
# 正常回调
|
259
|
+
proc action0 { data id name idx} {
|
260
|
+
global stubs
|
261
|
+
global ports
|
262
|
+
global actions
|
263
|
+
global heads
|
264
|
+
global files
|
265
|
+
|
266
|
+
global head_arr
|
267
|
+
|
268
|
+
log "\n============action0(name=$name)==============\n"
|
269
|
+
|
270
|
+
set res_head 0
|
271
|
+
set res_body 0
|
272
|
+
set res_buf 0
|
273
|
+
|
274
|
+
#create res_body and get body_len
|
275
|
+
#if file not exist, just return nshead
|
276
|
+
set mcpack 0
|
277
|
+
set has_body 0
|
278
|
+
|
279
|
+
if { [file exists [lindex $files $idx]] == 0 } {
|
280
|
+
set has_body 0
|
281
|
+
log "response mcpack file not exist and just return nshead"
|
282
|
+
} else {
|
283
|
+
set has_body 1
|
284
|
+
}
|
285
|
+
|
286
|
+
if { $has_body == 1 } {
|
287
|
+
set f [open [lindex $files $idx] "r"]
|
288
|
+
gets $f line
|
289
|
+
mc_pack text2pack $mcpack $line
|
290
|
+
set body_len [mc_pack size $mcpack]
|
291
|
+
mc_pack close $mcpack
|
292
|
+
close $f
|
293
|
+
set res_body [mc_pack export $mcpack]
|
294
|
+
log "body_len = $body_len"
|
295
|
+
}
|
296
|
+
|
297
|
+
#create res_head
|
298
|
+
set head [bft gen "nshead.bft"]
|
299
|
+
if { [file exists [lindex $heads $idx]] == 0 } {
|
300
|
+
bft set $head nshead.magic_num 4218459028
|
301
|
+
log "response nshead file not exist and just return default nshead"
|
302
|
+
} else {
|
303
|
+
log "response nshead exist"
|
304
|
+
|
305
|
+
load_head [lindex $heads $idx]
|
306
|
+
bft set $head nshead.id $head_arr("id")
|
307
|
+
bft set $head nshead.version $head_arr("version")
|
308
|
+
bft set $head nshead.log_id $head_arr("log_id")
|
309
|
+
bft set $head nshead.provider $head_arr("provider")
|
310
|
+
bft set $head nshead.magic_num $head_arr("magic_num")
|
311
|
+
bft set $head nshead.reserved $head_arr("reserved")
|
312
|
+
}
|
313
|
+
|
314
|
+
if { $has_body == 1 } {
|
315
|
+
bft set $head nshead.body_len $body_len
|
316
|
+
}
|
317
|
+
set res_head [bft export $head]
|
318
|
+
|
319
|
+
#create res_buf
|
320
|
+
if { $has_body == 1 } {
|
321
|
+
set res_buf $res_head$res_body
|
322
|
+
} else {
|
323
|
+
set res_buf $res_head
|
324
|
+
}
|
325
|
+
|
326
|
+
#send
|
327
|
+
log " ===>> server sending..."
|
328
|
+
cnnt send $name $res_buf -id $id
|
329
|
+
|
330
|
+
}
|
331
|
+
|
332
|
+
# 1 -- close connection after recv data, not to send data to client.
|
333
|
+
proc action1 { id name } {
|
334
|
+
log "\n============action1(name=$name)==============\n"
|
335
|
+
cnnt delete $name -id $id
|
336
|
+
}
|
337
|
+
|
338
|
+
# 2 -- connection timeout. just to wait a long while(3s).
|
339
|
+
proc action2 { id name } {
|
340
|
+
global wait_time
|
341
|
+
|
342
|
+
log "\n============action2(name=$name, wait=$wait_time)==============\n"
|
343
|
+
asyn_wait $wait_time
|
344
|
+
cnnt delete $name -id $id
|
345
|
+
}
|
346
|
+
|
347
|
+
proc asyn_wait { time } {
|
348
|
+
after $time [list set done ""]
|
349
|
+
vwait done
|
350
|
+
}
|
351
|
+
|
352
|
+
# 初始化检查
|
353
|
+
proc init {} {
|
354
|
+
log "\niniting..."
|
355
|
+
global conf_path
|
356
|
+
|
357
|
+
#conf
|
358
|
+
if { [file exists $conf_path] == 0 } {
|
359
|
+
log "conf file $conf_path not exists!"
|
360
|
+
exit
|
361
|
+
}
|
362
|
+
|
363
|
+
#nshead.bft
|
364
|
+
if { [file exists "nshead.bft"] == 0 } {
|
365
|
+
log "nshead template nshead.bft not exists!"
|
366
|
+
exit
|
367
|
+
}
|
368
|
+
}
|
369
|
+
|
370
|
+
#==============main============
|
371
|
+
|
372
|
+
init
|
373
|
+
load_conf
|
374
|
+
start_srvs
|
375
|
+
|
376
|
+
vwait forever
|