-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.go
117 lines (102 loc) · 2.22 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
package tester
import (
"encoding/json"
"fmt"
"io"
"github.com/shivuslr41/grpc-tester/exec"
"golang.org/x/sync/errgroup"
)
// Run execute grpcurl(calls grpc API) with the given configurations
func (r *Runner) Run(reader func(io.ReadCloser) error) error {
// if global flag -G is used, ignores test file configs
if GConf.Use {
r.replaceGconf()
}
// prepare grpcurl cmd to call grpc server
cmd := exec.NewCMD(
fmt.Sprintf(
"grpcurl %s %s %s -d @ %s %s",
r.GrpcurlFlags,
r.tlsFlag(),
r.protoFlag(),
r.Server,
r.Endpoint,
),
)
// create writer for passing payloads/requests
writer, err := cmd.StdinPipe()
if err != nil {
return err
}
// create errgroup
g := new(errgroup.Group)
// send requests
g.Go(func() error {
return r.write(writer)
})
// create reader to catch any server errors
stderrReader, err := cmd.StderrPipe()
if err != nil {
return err
}
// listen to errors in background
g.Go(func() error {
return readStdErr(stderrReader)
})
// create reader to collect server responses
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
// listen for results in the background
g.Go(func() error {
return reader(stdoutReader)
})
// execute the grpcurl cmd
if err := cmd.Start(); err != nil {
return err
}
// wait for any error from error group
if err = g.Wait(); err != nil {
return err
}
return nil
}
// write sends/streams grpc requests to stdin for grpcurl
func (r *Runner) write(writer io.WriteCloser) error {
defer writer.Close()
// write to stdin based type of payload
// if streamPayload then divide the requests and write into stdin,
// else write without dividing them
if r.StreamPayload {
data := r.Data
if !r.testerCall {
err := json.Unmarshal([]byte(r.Data[0].(string)), &data)
if err != nil {
return err
}
}
for i := range data {
b, err := json.Marshal(data[i])
if err != nil {
return err
}
io.WriteString(writer, string(b))
}
} else {
data := fmt.Sprint(r.Data[0])
if r.testerCall {
b, err := json.Marshal(r.Data[0])
if err != nil {
return err
}
data = string(b)
}
io.WriteString(writer, data)
}
return nil
}
// Execute starts run command
func (r *Runner) Execute() {
r.print()
}