-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.go
132 lines (110 loc) · 2.46 KB
/
run.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
package cmd
import (
"bytes"
"context"
"fmt"
"github.com/mattn/go-zglob"
"cuelang.org/go/encoding/gocode/gocodec"
"github.com/urfave/cli/v2"
)
// Run requests grpc to server using input in test case file.
func Run(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Println("Please specify server name")
cli.ShowCommandHelpAndExit(c, "run", 1)
return nil
}
serverName := c.Args().Get(0)
if c.NArg() == 1 {
fmt.Println("Please specify test name")
cli.ShowCommandHelpAndExit(c, "run", 1)
return nil
}
headers := extractHeaders(c.StringSlice("header"))
testFiles, err := zglob.Glob(c.Args().Get(1))
if err != nil {
return err
}
targetTestName := ""
if c.NArg() > 2 {
targetTestName = c.Args().Get(2)
}
for _, testFile := range testFiles {
if err := run(serverName, testFile, targetTestName, headers); err != nil {
return err
}
}
return nil
}
func run(serverHost, testFile, testName string, headers map[string]string) error {
ins, err := readCueInstance(testFile)
if err != nil {
return err
}
insVal, _ := ins.Value().Struct()
cases, _ := insVal.FieldByName("cases", false)
codec := gocodec.New(&r, &gocodec.Config{})
testCases := []testCase{}
err = codec.Encode(cases.Value, &testCases)
if err != nil {
return err
}
fmt.Println(testFile)
for _, c := range testCases {
if testName != "" && testName != c.Name {
continue
}
h := mergeMap(headers, c.Headers)
testList := []testData{}
for _, td := range c.Tests {
testList = append(testList, testData{
name: c.Name,
method: c.Method,
headers: h,
input: td.Input,
output: td.Output,
})
}
if len(testList) == 0 {
testList = append(testList, testData{
name: c.Name,
method: c.Method,
headers: h,
input: c.Input,
output: c.Output,
})
}
err := runList(
context.Background(),
serverHost,
c.Proto,
c.ImportPath,
testList,
)
if err != nil {
return err
}
}
return nil
}
func runList(ctx context.Context, serverHost string, proto, importPath []string, tdList []testData) error {
for _, td := range tdList {
fmt.Printf("\ttest name: %s\n", td.name)
fmt.Printf("\tmethod: %s\n", td.method)
res := &bytes.Buffer{}
err := invokeRPC(
context.Background(),
serverHost,
td.method,
td.headers,
proto,
importPath,
td.input,
res)
if err != nil {
return fmt.Errorf("invoke grpc: %w", err)
}
fmt.Println("\toutput:", addTabToNewline(res.String(), 2))
}
return nil
}