forked from brahma-adshonor/gorr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.go
271 lines (224 loc) · 6.51 KB
/
recorder.go
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
package gorr
import (
"gorr/util"
"bytes"
"encoding/json"
//"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/golang/protobuf/proto"
)
var (
tsChan = make(chan string, 128)
s3CaseDir string
envFlagFile string
testCaseHandler string
/*
s3CaseDir = flag.String("case_store_dir", "", "s3 path to store test case")
envFlagFile = flag.String("env_flag_file", "", "flag file to server")
testCaseHandler = flag.String("test_case_handler_tool", "", "tool to handle test case")
*/
)
func init() {
envFlagFile = os.Getenv("ENV_FLAG_FILE")
s3CaseDir = os.Getenv("TEST_CASE_STORE_DIR")
testCaseHandler = os.Getenv("TEST_CASE_HANDLER")
}
type MoveData struct {
Src string `json:"src"`
Dst string `json:"dst"`
}
type TestCase struct {
Req string `json:"req"`
Rsp string `json:"rsp"`
ReqType int `json:"ReqType"`
RspType int `json:"RspType"`
Desc string `json:"Desc"`
URI string `json:"Uri"`
Runner string `json:"runner"`
}
type TestItem struct {
DB []string `json:"db"`
Flags []string `json:"flags,omitempty"`
Input []MoveData `json:"input,omitempty"`
TestCases []*TestCase `json:"cases"`
Version int `json:"version"`
EnvFlagFile string `json:"env_flag_files,omitempty"`
}
var (
gnum int
glock sync.Mutex
)
const (
RecorderDataTypeUnknown = 23
RecorderDataTypeJson = 24
RecorderDataTypePbText = 25
RecorderDataTypePbBinary = 26
RecorderDataTypeHttpJson = 27
)
type HttpSerializedData struct {
Body []byte `json:"body"`
BodyType int `json:"btype"`
Header map[string][]string `json:"header"`
}
func RecordHttpExt(uri, desc, name string, head http.Header, req, rsp []byte, t1, t2 int) (string, error) {
data := HttpSerializedData{
Body: req,
BodyType: t1,
Header: head,
}
req2, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return "", fmt.Errorf("serialized http data with header failed, err:%s", err)
}
return RecordData(uri, "", name, req2, RecorderDataTypeHttpJson, rsp, t2, desc, nil)
}
func RecordHttp(outDir, desc string, req *http.Request, rsp *http.Response, db []string) (string, error) {
if req.Body == nil || rsp.Body == nil {
return "", fmt.Errorf("invalid http data, req/rsp must contain body")
}
reqBody, err1 := ioutil.ReadAll(req.Body)
if err1 != nil {
return "", fmt.Errorf("read http req body failed, err:%s", err1.Error())
}
req.Body.Close()
req.Body = ioutil.NopCloser(bytes.NewBuffer(reqBody))
rspBody, err2 := ioutil.ReadAll(rsp.Body)
if err1 != nil {
return "", fmt.Errorf("read http rsp body failed, err:%s", err2.Error())
}
rsp.Body.Close()
rsp.Body = ioutil.NopCloser(bytes.NewBuffer(rspBody))
return RecordData("", outDir, "http", reqBody, RecorderDataTypeJson, rspBody, RecorderDataTypeJson, desc, db)
}
func RecordGrpc(outDir, desc string, req proto.Message, rsp proto.Message, db []string) (string, error) {
d1, err1 := json.MarshalIndent(req, "", "\t")
if err1 != nil {
return "", fmt.Errorf("marshal grpc request failed, err:%s", err1.Error())
}
d2, err2 := json.MarshalIndent(rsp, "", "\t")
if err2 != nil {
return "", fmt.Errorf("marshal grpc response failed, err:%s", err2.Error())
}
return RecordData("", outDir, "grpc", d1, RecorderDataTypePbBinary, []byte(d2), RecorderDataTypeJson, desc, db)
}
func genUniqueFileName(dir, prefix, suggest string) string {
name := fmt.Sprintf("%s_%s.dat", prefix, suggest)
path := dir + "/" + name
if _, err := os.Stat(path); os.IsNotExist(err) {
return name
}
for i := 0; i < 102400; i++ {
name = fmt.Sprintf("%s_%s_%d.dat", prefix, suggest, i)
path = dir + "/" + name
if _, err := os.Stat(path); os.IsNotExist(err) {
return name
}
}
panic("can not create gorr output dir")
}
func RecordData(uri, outDir, name string, req []byte, t1 int, rsp []byte, t2 int, desc string, db []string) (string, error) {
glock.Lock()
defer glock.Unlock()
gnum++
if gnum%100 == 0 {
GlobalMgr.ResetTestSuitDir()
}
if len(outDir) == 0 {
outDir = GlobalMgr.GetCurrentTestSuitDir()
}
files := GlobalMgr.GetDbFiles()
files = append(files, db...)
f1 := genUniqueFileName(outDir, "reg_req", name)
f2 := genUniqueFileName(outDir, "reg_rsp", name)
reqFile := outDir + "/" + f1
rspFile := outDir + "/" + f2
configFile := outDir + "/reg_config.json"
err := ioutil.WriteFile(reqFile, req, 0644)
if err != nil {
return "", err
}
err = ioutil.WriteFile(rspFile, rsp, 0644)
if err != nil {
return "", err
}
data := make([]string, 0, len(files))
for _, f := range files {
name := filepath.Base(f)
to := outDir + "/" + name
err = util.CopyFile(f, to)
if err != nil {
return "", fmt.Errorf("copy db file failed, from:%s, to:%s, err:%s", f, to, err.Error())
}
data = append(data, name)
}
ts := time.Now().Format(time.RFC3339)
td := TestItem{
DB: data,
Version: 2,
Flags: []string{"-gorr_run_type=2", fmt.Sprintf("-server_time=%s", ts)},
TestCases: []*TestCase{
&TestCase{
Req: f1,
Rsp: f2,
URI: uri,
ReqType: t1,
RspType: t2,
Desc: desc,
},
},
}
if len(data) > 0 {
mainDb := fmt.Sprintf("-gorr_db_file=%s", data[0])
td.Flags = append(td.Flags, mainDb)
}
if len(envFlagFile) > 0 {
name := filepath.Base(envFlagFile)
to := outDir + "/" + name
err = util.CopyFile(envFlagFile, to)
if err != nil {
return "", fmt.Errorf("copy env flag file failed, from:%s, to:%s, err:%s", envFlagFile, to, err)
}
td.EnvFlagFile = name
}
cd, err := ioutil.ReadFile(configFile)
if err == nil && len(cd) > 0 {
var cf TestItem
err = json.Unmarshal(cd, &cf)
if err != nil {
return "", fmt.Errorf("unmarshal existed config failed, err:%s", err)
}
td.TestCases = append(td.TestCases, cf.TestCases...)
}
conf, err2 := json.MarshalIndent(td, "", "\t")
if err2 != nil {
return "", fmt.Errorf("generate test case config failed, err:%s", err2.Error())
}
err = ioutil.WriteFile(configFile, conf, 0644)
if err != nil {
return "", fmt.Errorf("writing config file failed, err:%s", err.Error())
}
if len(testCaseHandler) > 0 && len(s3CaseDir) > 0 {
tsChan <- outDir
}
return outDir, nil
}
func RunTestCaseUploader() {
if len(testCaseHandler) > 0 && len(s3CaseDir) > 0 {
go doUpload()
}
}
func doUpload() {
for {
d := <-tsChan
cmd := fmt.Sprintf("S3_CASE_DIR=%s LOCAL_CASE_DIR=%s %s", s3CaseDir, d, testCaseHandler)
out, err := util.RunCmd(cmd)
m := fmt.Sprintf("output:%s, err:%s", out, err)
GlobalMgr.notifier("upload test case done", cmd, []byte(m))
}
}