-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestRunner.go
382 lines (345 loc) · 12.7 KB
/
testRunner.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/fatih/color"
"trpc/functions"
"trpc/grpcrunner"
"trpc/trpc_marshal"
"github.com/alecthomas/participle/v2/lexer"
//"bytes"
//"github.com/fullstorydev/grpcurl"
//"github.com/golang/protobuf/jsonpb"
//"github.com/golang/protobuf/proto"
)
const acceptableVersion = "v0.0.1"
func TasteAndRun(trpc *Trpc) {
verbose := 0
_maxTime := 0.0
maxTime := &_maxTime
_timeOut := 0.0
connectionTimeout := &_timeOut
//keepalive := &_timeOut
protoImportPaths := make([]string, 0)
protoFiles := make([]string, 0)
protoSets := make([]string, 0)
namedEndpointes := make(map[string]Endpoint, 0)
invokeOrder := make([]string, 0)
namedInvokes := make(NamedInvokes, 0)
defaultFailBehave := "Panic"
//expects := make([]Expect, 0)
namedInvokeHandlers := make(map[string]grpcrunner.TRPCHandler, 0)
mainEntery := trpc.Entries[0]
if len(mainEntery.TestName) == 0 {
print("Be kind and name your test")
return
} else {
if mainEntery.MaxTime > 0 {
maxTime = &mainEntery.MaxTime
}
if mainEntery.Timeout > 0 {
connectionTimeout = &mainEntery.Timeout
}
verbose = mainEntery.VerboseLevel
}
for _, entery := range trpc.Entries[1:] {
if entery.Description != "" {
}
if len(entery.ImportPath) > 0 {
iPath, _ := strconv.Unquote(entery.ImportPath)
protoImportPaths = append(protoImportPaths, iPath)
}
if len(entery.ImportProto) > 0 {
pFile, _ := strconv.Unquote(entery.ImportProto)
protoFiles = append(protoFiles, pFile)
}
if len(entery.ImportProtoSet) > 0 {
pSet, _ := strconv.Unquote(entery.ImportProtoSet)
protoSets = append(protoSets, pSet)
}
if entery.Endpoint != nil {
unQuoted, _ := strconv.Unquote(entery.Endpoint.IPDomain)
entery.Endpoint.IPDomain = unQuoted
namedEndpointes[entery.Endpoint.Name] = *entery.Endpoint
}
if entery.Invoke != nil {
if existInvoke, exists := namedInvokes[entery.Invoke.Name]; exists {
fmt.Printf("Duplicate invoke name %s in %s:%d which defined at %s:%d\n", entery.Invoke.Name, entery.Pos.Filename, entery.Pos.Line, existInvoke.Pos.Filename, existInvoke.Pos.Line)
return
} else {
//jb, _ := json.MarshalIndent(entery.Invoke.ParsedData, "", " ")
//println(entery.Invoke.Name, " ", string(jb), entery.Invoke.ParsedData, entery.Invoke.Headers)
namedInvokes[entery.Invoke.Name] = entery.Invoke
entery.Invoke.Parse((*mainEntery).Lines, &namedInvokes, false, true)
entery.Invoke.Response = nil
entery.Invoke.Goal, _ = strconv.Unquote(entery.Invoke.Goal)
invokeOrder = append(invokeOrder, entery.Invoke.Name)
entery.Invoke.Entry = mainEntery
entery.Invoke.Conditions = make([]InvokeCondition, 0)
for _, expect := range entery.Invoke.SourceExpects {
//expect.Reference. = entery.Invoke.Name + "." + expect.Reference
//expect.Code = strings.Split(expect.Reference, ".")
expect.Invoke = entery.Invoke
if expect.OnFail == nil {
expect.OnFail = &defaultFailBehave
} else if *expect.OnFail != "Warn" && *expect.OnFail != "Ignore" {
syntaxError(mainEntery.Lines, expect.Pos, 0, "Invalid onFail param", *expect.OnFail)
}
entery.Invoke.Expects = append(entery.Invoke.Expects, *expect)
}
}
}
}
for _, invokeName := range invokeOrder {
invoke := namedInvokes[invokeName]
fmt.Println("===========================\ninvoke: ", invokeName)
if endPoint, ok := namedEndpointes[invoke.EndPoint]; ok {
//fmt.Printf("call %s:%d%s/%s/%s %v\n", endPoint.IPDomain, endPoint.Port, endPoint.PerfixPath, invoke.Service, invoke.RPC, invoke.ContaineReferences)
if invoke.ContaineReferences {
invoke.Parse((*mainEntery).Lines, &namedInvokes, true, false)
}
handler, _ := grpcrunner.Run(grpcrunner.RunParams{
ProtoFiles: protoFiles,
ImportPaths: protoImportPaths,
Protoset: protoSets,
Target: fmt.Sprintf("%s:%d", endPoint.IPDomain, endPoint.Port),
PrefixPath: endPoint.PerfixPath,
// ReflectionPrefixPath: endPoint.ReflectionPerfixPath,
Plaintext: !endPoint.Tls,
Data: invoke.RequestData,
RPCHeaders: invoke.RequestHeaders,
ServiceName: invoke.Service,
MethodName: invoke.RPC,
Verbose: verbose <= 2,
VeryVerbose: verbose == 2,
MaxTime: *maxTime,
KeepaliveTime: 0.0,
ConnectTimeout: *connectionTimeout,
})
//if err != nil {
// if false {
// fmt.Println(err)
// }
//}
namedInvokeHandlers[invokeName] = *handler
//var message protoiface.MessageV1
if len(handler.ResponseData) > 0 {
//message = handler.ResponseData[0]
//response := make(map[string]interface{})
//jsonB, jErr := marshal(message, handler.Descriptor)
//if jErr != nil {
// println("error on marshal message: ", jErr)
//} else {
// println("message json: ", string(jsonB))
//}
//jErr = json.Unmarshal(jsonB, &response)
//invoke.ResponseJson = string(jsonB)
//invoke.Response = &response
//fmt.Printf("MD: %v\n------------------------------------\n", handler.MethodDescriptor)
////mdReq := handler.MethodDescriptor.GetInputType()
//mdRes := handler.MethodDescriptor.GetOutputType()
////fmt.Printf("MdReq: %s: %v\n------------------------------------\n", mdReq.GetFullyQualifiedName(), mdReq)
//fmt.Printf("MdRes: %s: %v\n------------------------------------\n", mdRes.GetFullyQualifiedName(), mdRes)
////refl := proto.MessageReflect(message)
//refl2, _ := dynamic.AsDynamicMessage(message)
//for _, fld := range mdRes.GetFields() {
// if fld.IsRepeated() {
// repeated := refl2.GetField(fld).([]interface{})
// fmt.Printf("fld is repeated (%T) with len: %d ~= %d\n", refl2.GetField(fld), len(repeated), refl2.FieldLength(fld))
// for i, v := range repeated {
// fmt.Printf("fld(rp) %v[%d] =>(%T) %v\n", fld, i, v, v.(*dynamic.Message))
// }
// } else {
// fmt.Printf("fld => %v: %v\n", fld, refl2.GetField(fld))
// }
//}
m := trpc_marshal.RPCMessageToMap(*handler)
//fmt.Printf("----------------------------------===\nmarshaled : %v\n", m)
jb, _ := json.MarshalIndent(m, "", " ")
//fmt.Println("----------====------------\n" + string(jb))
invoke.Response = &m
invoke.ResponseJson = string(jb)
//md, err := desc.LoadMessageDescriptorForMessage(message)
//namedFields := make(map[string]*desc.FieldDescriptor)
//var fields []*desc.FieldDescriptor
//if err != nil {
// println("error: ", err)
//} else {
// //println("message desc: ", md.String())
// fields = md.GetFields()
// for _, field := range fields {
// //println("field: ", field.String())
// namedFields[field.GetName()] = field
// if _, exists := response[field.GetName()]; !exists {
// response[field.GetName()] = nil
// }
// }
//}
//invoke.NamedFields = namedFields
} else {
invoke.Response = &map[string]interface{}{}
invoke.ResponseJson = "{}"
}
for _, expect := range invoke.Expects {
code := expect.Path.Parts
if code[0].Obj == "code" {
//fmt.Printf("code !? : <<%v>>\n", handler.Status)
codeFn, err := functions.CodeFunction(expect.Function.Name)
if err != nil {
syntaxError((*mainEntery).Lines, invoke.Pos, 0, err.Error())
}
fnErr := codeFn(handler.Status.Code())
if fnErr != nil {
testFailed(mainEntery, invoke, &expect, 0, fnErr.Error())
}
} else if code[0].Obj == "message" {
fn, err := functions.MessageFunction(expect.Function.Name)
if err != nil {
syntaxError((*mainEntery).Lines, invoke.Pos, 0, err.Error())
}
value, _ := expect.Function.Arg.value((*mainEntery).Lines, &namedInvokes, true)
fnErr := fn(value.(string), handler.Status.Message())
if fnErr != nil {
testFailed(mainEntery, invoke, &expect, 0, fnErr.Error())
}
} else if code[0].Obj == "response" {
if invoke.Response == nil {
testFailed(mainEntery, invoke, &expect, 0, "Expect check value on invocation with error response")
}
if len(code) == 1 {
if expect.Function.Name == "isEmpty" {
if len(*invoke.Response) != 0 {
testFailed(mainEntery, invoke, &expect, 0, "Response expected to be empty but it is not")
}
} else {
syntaxError((*mainEntery).Lines, invoke.Pos, 0, "Unknown function %v", expect.Function.Name)
}
}
code := code[1:]
//fmt.Printf(" %s expect %v\n", invokeName, code)
offset := len("expect " + invokeName + "." + "response.")
if val, exists := (*invoke.Response)[code[0].Obj]; exists {
parts := []Part{
{
Obj: invokeName,
},
}
expect.Path.Parts = append(parts, expect.Path.Parts...)
val, _ = Value{Reference: expect.Path}.value((*mainEntery).Lines, &namedInvokes, true)
switch expect.Function.Name {
case "hasValue":
{
if val == nil {
println("Test failed: ", invokeName, " expected data is not null but got null")
}
}
case "isNull":
{
if val != nil {
testFailed(mainEntery, invoke, &expect, offset, "%s expected to be null but got (%v) %T\nActual response:\n%s", expect.Path.String(), val, val, invoke.ResponseJson)
}
}
case "isEqual":
{
expectValue, _ := expect.Function.Arg.value((*mainEntery).Lines, &namedInvokes, true)
if val != expectValue {
testFailed(mainEntery, invoke, &expect, offset, "%s expected to be \"%v\" but got \"%v\"\n\nActual response:\n%s", expect.Path.String(), expectValue, val, invoke.ResponseJson)
}
}
case "isNotEmpty":
{
if val == nil {
println("Test failed: ", invokeName, " expected data is not empty but got null")
} else if val == "" {
println("Test failed: ", invokeName, " expected data is not empty but got empty string")
}
}
default:
fmt.Printf("Test failed unknown expect operator %s at %s:%d\n", expect.Function.Name, expect.Pos.Filename, expect.Pos.Line)
}
} else {
testFailed(mainEntery, invoke, &expect, offset, "Field %s not found on %s", code[0], invoke.RPC)
}
} else {
syntaxError((*mainEntery).Lines, expect.Pos, 0, "Unknown expect code \"%v\"", code[0])
}
}
} else {
offset := len("invoke " + invokeName + " ")
knownEndPoints := make([]string, 0)
for k := range namedEndpointes {
knownEndPoints = append(knownEndPoints, k)
}
invalidParameter((*mainEntery).Lines, invoke.Pos, offset, "Endpoint \"%s\" not found for Invoke \"%s\"\nKnown endpoints:\n%s\n", invoke.EndPoint, invokeName, strings.Join(knownEndPoints, "\n"))
}
}
if mainEntery.Warnings+mainEntery.Warnings > 0 {
fmt.Printf("Test done with %d warning(s) and %d ignoration(s) \n", mainEntery.Warnings, mainEntery.Ignores)
}
println("✅ All tests passed as expected 😎")
}
func fatal(lines *[]string, pos lexer.Position, offset int, msg string, a ...interface{}) {
line := (*lines)[pos.Line-1]
newFormat := msg + "\nRelated line on file: %s:%d\n%s\n"
if offset > 0 {
newFormat += strings.Repeat(" ", offset) + "^\n"
}
a = append(a, pos.Filename, pos.Line, line)
//println("MSG IS :", newFormat)
//for _, v := range a {
// fmt.Printf("a: %v\n", v)
//}
fmt.Printf(newFormat, a...)
}
func syntaxError(lines *[]string, pos lexer.Position, offset int, msg string, a ...interface{}) {
fmt.Println("Something went wrong!")
fatal(lines, pos, offset, msg, a...)
os.Exit(2)
}
func testFailed(testEntery *Entry, invoke *Invoke, expect *Expect, offset int, msg string, a ...interface{}) string {
invokeCondition := NewInvokeCondition(*expect.OnFail, expect, fmt.Sprintf(msg, a...))
colorFn := color.Red
severityStr := "Panic"
failSign := "⛔"
switch invokeCondition.Condition {
case InvokeDoneWithIgnores:
{
colorFn = color.Yellow
failSign = "‼️"
testEntery.Warnings += 1
}
case InvokeDoneWithWarnings:
{
colorFn = color.HiYellow
failSign = "❕"
testEntery.Ignores += 1
}
//case InvokeFailed:
//Done already
}
colorFn("Test \"%s\" failed ! %s", testEntery.TestName, failSign)
colorFn("Description: %s", testEntery.Description)
if invoke.Goal != "" {
colorFn("Goal: %s", invoke.Goal)
}
fatal(testEntery.Lines, expect.Pos, offset, severityStr+": "+msg, a...)
expect.Invoke.Conditions = append(expect.Invoke.Conditions, invokeCondition)
//Warn and Ignore will not break the test follow
if severityStr == "Warn" || severityStr == "Ignore" {
return severityStr
}
os.Exit(3)
//We get out I knew, but needed to compile
return ""
}
func invalidParameter(lines *[]string, pos lexer.Position, offset int, msg string, a ...interface{}) {
fmt.Println("Invalid code error :")
fatal(lines, pos, offset, msg, a...)
os.Exit(2)
}
func typeOf(val interface{}) string {
return fmt.Sprintf("%T", val)
}