-
Notifications
You must be signed in to change notification settings - Fork 66
/
processor-interface.go
353 lines (278 loc) · 12 KB
/
processor-interface.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
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
package agentExec
import (
"encoding/json"
"github.com/aaronchen2k/deeptest/internal/agent/exec/domain"
"github.com/aaronchen2k/deeptest/internal/agent/exec/utils/exec"
"github.com/aaronchen2k/deeptest/internal/pkg/consts"
"github.com/aaronchen2k/deeptest/internal/pkg/domain"
checkpointHelper "github.com/aaronchen2k/deeptest/internal/pkg/helper/checkpoint"
databaseOptHelpper "github.com/aaronchen2k/deeptest/internal/pkg/helper/database-opt"
extractorHelper "github.com/aaronchen2k/deeptest/internal/pkg/helper/extractor"
scriptHelper "github.com/aaronchen2k/deeptest/internal/pkg/helper/script"
commonUtils "github.com/aaronchen2k/deeptest/pkg/lib/comm"
logUtils "github.com/aaronchen2k/deeptest/pkg/lib/log"
"github.com/jinzhu/copier"
uuid "github.com/satori/go.uuid"
"time"
)
type ProcessorInterface struct {
ID uint `json:"id"`
ProcessorEntityBase
domain.BaseRequest
Response domain.DebugResponse `json:"response"`
BaseUrl string `json:"baseUrl"`
PreConditions []domain.InterfaceExecCondition `json:"preConditions"`
PostConditions []domain.InterfaceExecCondition `json:"postConditions"`
}
func (entity ProcessorInterface) Run(processor *Processor, session *Session) (err error) {
defer func() {
if errX := recover(); errX != nil {
processor.Error(session, errX)
}
}()
logUtils.Infof("interface entity")
SetCurrDebugInterfaceId(session.ExecUuid, processor.EntityId)
execStartTime := time.Now()
processor.Result = &agentDomain.ScenarioExecResult{
ID: int(entity.ProcessorID),
Name: processor.Name,
ProcessorCategory: entity.ProcessorCategory,
ProcessorType: entity.ProcessorType,
StartTime: &execStartTime,
ParentId: int(entity.ParentID),
EndpointInterfaceId: processor.EndpointInterfaceId,
DebugInterfaceId: processor.EntityId,
ProcessorId: processor.ID,
ScenarioId: processor.ScenarioId,
LogId: uuid.NewV4(),
ParentLogId: processor.Parent.Result.LogId,
Round: processor.Round,
ResultStatus: consts.Pass,
}
detail := map[string]interface{}{}
//在循环过程中,processor 被执行多次,变量替换会受到影响,第一次跌替换之后,就不能根据实际情况替换了
var baseRequest domain.BaseRequest
copier.CopyWithOption(&baseRequest, &entity.BaseRequest, copier.Option{IgnoreEmpty: true, DeepCopy: true})
// init context
//InitJsRuntime(processor.ProjectId, session.ExecUuid)
SetReqValueToGoja(&baseRequest)
// exec pre-condition
entity.ExecPreConditions(processor, session)
// dealwith variables
ReplaceVariables(&baseRequest, session.ExecUuid)
GetReqValueFromGoja(session.ExecUuid, session.TenantId, processor.ProjectId)
// add cookies
DealwithCookies(&baseRequest, entity.ProcessorID, session.ExecUuid)
// gen request url
GenRequestUrlWithBaseUrlAndPathParam(&baseRequest, processor.EntityId, entity.BaseUrl, session.ExecUuid)
// send request
requestStartTime := time.Now()
entity.Response, err = Invoke(&baseRequest)
requestEndTime := time.Now()
// exec post-condition
SetRespValueToGoja(&entity.Response)
processor.Result.ResultStatus, _ = entity.ExecPostConditions(processor, &detail, session)
GetRespValueFromGoja(session.ExecUuid, session.TenantId, processor.ProjectId)
processor.Result.Detail = commonUtils.JsonEncode(detail)
// get the response data updated by script post-condition
if GetCurrResponse(session.ExecUuid).Data != nil {
entity.Response = GetCurrResponse(session.ExecUuid)
}
// dealwith response
ok := entity.GenResultFromResponse(processor, baseRequest, requestEndTime, requestStartTime, &detail, session, err)
if !ok {
return
}
for _, c := range entity.Response.Cookies {
SetCookie(processor.ParentId, c.Name, c.Value, c.Domain, c.ExpireTime, session.ExecUuid)
}
execUtils.SendExecMsg(*processor.Result, consts.Processor, session.WsMsg)
endTime := time.Now()
processor.Result.EndTime = &endTime
stat := CountInterfaceStat(session.ExecUuid, processor.Result)
execUtils.SendStatMsg(stat, session.WsMsg)
processor.AddResultToParent()
return
}
func (entity *ProcessorInterface) ExecPreConditions(processor *Processor, session *Session) (err error) {
for _, condition := range entity.PreConditions {
if condition.Type == consts.ConditionTypeScript {
entity.DealwithScriptCondition(condition, nil, session.TenantId, processor.ProjectId, &processor.Result.PreConditions,
session.ExecUuid, false)
} else if condition.Type == consts.ConditionTypeDatabase {
entity.DealwithDatabaseOptCondition(condition, processor.ID, processor.ParentId, &processor.Result.PreConditions,
session.ExecUuid)
}
}
return
}
func (entity *ProcessorInterface) ExecPostConditions(processor *Processor, detail *map[string]interface{}, session *Session) (
interfaceStatus consts.ResultStatus, err error) {
interfaceStatus = processor.Result.ResultStatus
for _, condition := range entity.PostConditions {
if condition.Type == consts.ConditionTypeScript {
entity.DealwithScriptCondition(condition, &interfaceStatus, session.TenantId, processor.ProjectId, &processor.Result.PostConditions,
session.ExecUuid, true)
} else if condition.Type == consts.ConditionTypeDatabase {
entity.DealwithDatabaseOptCondition(condition, processor.ID, processor.ParentId, &processor.Result.PostConditions, session.ExecUuid)
} else if condition.Type == consts.ConditionTypeExtractor {
entity.DealwithExtractorCondition(condition,
processor.ID, processor.ParentId, &processor.Result.PostConditions, session.ExecUuid)
} else if condition.Type == consts.ConditionTypeResponseDefine {
//entity.DealwithResponseDefineCondition(condition, &interfaceStatus, &processor.Result.PostConditions, detail, session.ExecUuid)
}
}
for _, condition := range entity.PostConditions {
if condition.Type == consts.ConditionTypeCheckpoint {
entity.DealwithCheckpointCondition(condition, &interfaceStatus, &processor.Result.PostConditions,
detail, session.ExecUuid)
}
}
return
}
func (entity *ProcessorInterface) DealwithScriptCondition(condition domain.InterfaceExecCondition,
interfaceStatus *consts.ResultStatus, tenantId consts.TenantId, projectId uint, conditions *[]domain.InterfaceExecCondition,
execUuid string, isPostCondition bool) {
var scriptBase domain.ScriptBase
json.Unmarshal(condition.Raw, &scriptBase)
if scriptBase.Disabled {
return
}
err := ExecScript(&scriptBase, tenantId, projectId, execUuid) // will set vari
if err != nil {
}
scriptHelper.GenResultMsg(&scriptBase)
scriptBase.VariableSettings = *GetGojaVariables(execUuid)
interfaceExecCondition := domain.InterfaceExecCondition{
Type: condition.Type,
}
interfaceExecCondition.Raw, _ = json.Marshal(scriptBase)
*conditions = append(*conditions, interfaceExecCondition)
if isPostCondition {
for _, item := range *GetGojaLogs(execUuid) {
createAssertFromScriptResult(item, conditions, interfaceStatus,
scriptBase.ConditionId, scriptBase.ConditionEntityId)
}
}
}
func (entity *ProcessorInterface) DealwithDatabaseOptCondition(condition domain.InterfaceExecCondition,
processorId, parentId uint, conditions *[]domain.InterfaceExecCondition, execUuid string) {
var databaseOptBase domain.DatabaseOptBase
json.Unmarshal(condition.Raw, &databaseOptBase)
if databaseOptBase.Disabled {
return
}
databaseOptBase.Sql = ReplaceVariableValue(databaseOptBase.Sql, execUuid)
conditionStatus := true
err := ExecDbOpt(&databaseOptBase)
if err != nil || databaseOptBase.ResultStatus == consts.Fail {
conditionStatus = false
}
databaseOptHelpper.GenResultMsg(&databaseOptBase)
if databaseOptBase.JsonPath != "" && databaseOptBase.Variable != "" && conditionStatus { // will set vari
scopeId := parentId
if databaseOptBase.Scope == consts.Private { // put vari in its own scope if Private
scopeId = processorId
}
SetVariable(scopeId, databaseOptBase.Variable, databaseOptBase.Result, databaseOptBase.ResultType,
consts.Public, execUuid)
}
condition.Raw, _ = json.Marshal(databaseOptBase)
*conditions = append(*conditions, condition)
}
func (entity *ProcessorInterface) DealwithExtractorCondition(condition domain.InterfaceExecCondition,
processorId, parentId uint, conditions *[]domain.InterfaceExecCondition, execUuid string) {
var extractorBase domain.ExtractorBase
json.Unmarshal(condition.Raw, &extractorBase)
if extractorBase.Disabled || extractorBase.Variable == "" {
return
}
resp := entity.Response
err := ExecExtract(&extractorBase, resp)
if err != nil || extractorBase.ResultStatus == consts.Fail {
logUtils.Infof("extract failed")
}
extractorHelper.GenResultMsg(&extractorBase)
if extractorBase.ResultStatus == consts.Pass {
scopeId := parentId
if extractorBase.Scope == consts.Private { // put vari in its own scope if Private
scopeId = processorId
}
SetVariable(scopeId, extractorBase.Variable, extractorBase.Result,
extractorBase.ResultType, extractorBase.Scope, execUuid)
}
interfaceExecCondition := domain.InterfaceExecCondition{
Type: condition.Type,
}
interfaceExecCondition.Raw, _ = json.Marshal(extractorBase)
*conditions = append(*conditions, interfaceExecCondition)
}
func (entity *ProcessorInterface) DealwithCheckpointCondition(condition domain.InterfaceExecCondition,
interfaceStatus *consts.ResultStatus, conditions *[]domain.InterfaceExecCondition,
detail *map[string]interface{}, execUuid string) {
var checkpointBase domain.CheckpointBase
json.Unmarshal(condition.Raw, &checkpointBase)
if checkpointBase.Disabled {
return
}
resp := entity.Response
err := ExecCheckPoint(&checkpointBase, resp, 0, execUuid)
if err != nil || checkpointBase.ResultStatus == consts.Fail {
*interfaceStatus = consts.Fail
}
checkpointHelper.GenResultMsg(&checkpointBase)
if checkpointBase.ResultStatus == consts.Fail {
*interfaceStatus = consts.Fail
}
interfaceExecCondition := domain.InterfaceExecCondition{
Type: condition.Type,
}
interfaceExecCondition.Raw, _ = json.Marshal(checkpointBase)
*conditions = append(*conditions, interfaceExecCondition)
if _, ok := (*detail)["checkpoint"]; !ok {
(*detail)["checkpoint"] = []map[string]interface{}{}
}
(*detail)["checkpoint"] = append((*detail)["checkpoint"].([]map[string]interface{}), map[string]interface{}{
"resultStatus": checkpointBase.ResultStatus, "resultMsg": checkpointBase.ResultMsg,
})
}
func (entity *ProcessorInterface) DealwithResponseDefineCondition(condition domain.InterfaceExecCondition,
interfaceStatus *consts.ResultStatus, conditions *[]domain.InterfaceExecCondition,
detail *map[string]interface{}, execUuid string) {
var responseDefineBase domain.ResponseDefineBase
json.Unmarshal(condition.Raw, &responseDefineBase)
if responseDefineBase.Disabled {
return
}
resp := entity.Response
err := ExecResponseDefine(&responseDefineBase, resp)
if err != nil || responseDefineBase.ResultStatus == consts.Fail {
*interfaceStatus = consts.Fail
}
interfaceExecCondition := domain.InterfaceExecCondition{
Type: condition.Type,
}
interfaceExecCondition.Raw, _ = json.Marshal(responseDefineBase)
*conditions = append(*conditions, interfaceExecCondition)
(*detail)["responseDefine"] = map[string]interface{}{"resultStatus": responseDefineBase.ResultStatus, "resultMsg": responseDefineBase.ResultMsg}
}
func (entity *ProcessorInterface) GenResultFromResponse(
processor *Processor, baseRequest domain.BaseRequest, requestEndTime, requestStartTime time.Time,
detail *map[string]interface{}, session *Session, err error) (ok bool) {
processor.Result.Cost = requestEndTime.UnixMilli() - requestStartTime.UnixMilli()
reqContent, _ := json.Marshal(baseRequest)
processor.Result.ReqContent = string(reqContent)
respContent, _ := json.Marshal(entity.Response)
processor.Result.RespContent = string(respContent)
if err != nil {
processor.Result.ResultStatus = consts.Fail
processor.Result.Summary = err.Error()
(*detail)["result"] = entity.Response.Content
processor.Result.Detail = commonUtils.JsonEncode(*detail)
execUtils.SendErrorMsg(*processor.Result, consts.Processor, session.WsMsg)
processor.AddResultToParent()
return
}
ok = true
return
}