-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate-test-client.go
256 lines (220 loc) · 5.98 KB
/
generate-test-client.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
package main
//creates a client and uses that to call a protobuf.
import (
"bufio"
"bytes"
"flag"
"fmt"
"github.com/hailo-platform/H2O/hshell/send"
"io/ioutil"
"log"
"os"
"strings"
)
const (
GitProtoHome = "github.com/hailo-platform/H2O/protobufs/services"
IMPORT = "%IMPORT%"
REQUEST = "%REQUEST%"
RESPONSE = "%RESPONSE%"
ENDPOINT = "%ENDPOINT%"
OUTPUT = "%OUTPUT%"
REQUESTUPDATE = "%REQUESTUPDATE%"
SERVICE = "%SERVICE%"
CLIENTTEMPLATE = `
package main
import (
"github.com/hailo-platform/H2O/protobuf/proto"
"github.com/hailo-platform/H2O/platform/client"
"time"
"os"
"strings"
log "github.com/cihub/seelog"
"flag"
"fmt"
%IMPORT%
)
var _ = proto.String("NOOP")
func init() {
logger, err := log.LoggerFromConfigAsFile("seelog.xml")
if err != nil {
log.Debugf("Failed to load logger config from seelog.xml %v", err)
} else {
log.ReplaceLogger(logger)
log.Info("Custom logging enabled")
}
}
func main() {
defer log.Flush()
reqNumber := flag.Int("number", 1, "Number of iterations")
outFile := flag.String("output", "", "Location of the output log")
flag.Parse()
req := %REQUEST%
endpoint := "%ENDPOINT%"
service := "%SERVICE%"
fo, err := os.Create(*outFile)
if err != nil {
log.Error("output file creation failed:", err)
return
}
// close fo on exit and check for its returned error
defer func() {
if err := fo.Close(); err != nil {
log.Error("Cannot close output file:", err)
return
}
}()
writeChan := make(chan MessageData)
done := make(chan int)
go WriteOutput(fo, writeChan, done)
for i :=0; i < *reqNumber; i++ {
%REQUESTUPDATE%
request, err := client.NewRequest(
service,
endpoint,
req,
)
if err != nil {
log.Error(err)
return
}
rsp := &%RESPONSE%
ts := time.Now()
errReq := client.Req(request, rsp)
duration := time.Since(ts)
if errReq != nil {
writeChan <- MessageData{
ep: service +"."+endpoint,
rt: duration,
status: "false",
rs: 0,
err: errReq,
h: "",
r: "",
}
continue
}
%OUTPUT%
writeChan <- MessageData{
ep: service +"."+endpoint,
rt: duration,
status: "true",
rs: 0,
err: err,
r: rsp,
}
}
close(writeChan)
<-done
}
type MessageData struct {
ep string
rt time.Duration
status string
rs int
err ErrorResponse
h Header
r Response
}
type Header interface{}
type Response interface{}
type ErrorResponse interface{}
func WriteOutput(fo *os.File, writeChan chan MessageData, done chan int) {
for md := range writeChan {
toWrite := ""
results := fmt.Sprintf("%+v", md.r)
header := fmt.Sprintf("%+v", md.h)
results = fmt.Sprintf("\"%s\"", strings.Replace(results, "\"", "\"\"", -1))
header = fmt.Sprintf("\"%s\"", strings.Replace(header, "\"", "\"\"", -1))
ts := time.Now()
toWrite += fmt.Sprintf("%v,", ts.UnixNano())
toWrite += md.ep + ","
toWrite += fmt.Sprintf("%v,", md.rt.Nanoseconds())
toWrite += md.status + ","
toWrite += string(md.rs) + ","
if md.err != nil {
toWrite += fmt.Sprintf("%+v,", md.err)
} else {
toWrite += ","
}
toWrite += fmt.Sprintf("%+v,", results)
toWrite += fmt.Sprintf("%+v", header)
fo.WriteString(toWrite + "\n")
}
done <- 1
}
`
)
func main() {
outputFile := flag.String("output", "test-client.go", "Output filename")
importStr := flag.String("protobuf", "", "The import protobuf path, ie. go-banning-service/audit")
requestStr := flag.String("go", "", `The request object in native Go ie. "Name: proto.String(\"Moddie\"),"`)
endpointStr := flag.String("endpoint", "", "The endpoint to hit ie. com.hailocab.banning.retrieve")
jsonStr := flag.String("json", "", "the request object as json")
hint := flag.Bool("hint", false, "Give you a peek at the protobuf. Won't actually run the request")
defaultReq := flag.Bool("default", false, "if true, send the default request")
update := flag.Bool("update", false, "update Go files. That is the protobuf, the protobuf library and the client library")
flag.Parse()
tmpDir, err := send.CreateTempDir()
if err != nil {
log.Println("error creating tempdir", err)
}
origGoPath := os.Getenv("GOPATH")
os.Setenv("GOPATH", tmpDir)
defer os.Setenv("GOPATH", origGoPath)
protoArr := strings.Split(*importStr, "/")
err = send.GoGet(*importStr, *update)
if err != nil {
log.Println("problem getting code")
return
}
pbfile := fmt.Sprintf("%s/src/%s/%s.proto", tmpDir, *importStr, protoArr[len(protoArr)-1])
data, err := ioutil.ReadFile(pbfile)
if err != nil {
log.Println("Could not read protos:", pbfile)
}
pbreader := bytes.NewReader(data)
pbioreader := bufio.NewReader(pbreader)
pb := send.ParseProtobufRaw(pbioreader, protoArr[len(protoArr)-1], true, tmpDir)
if *hint {
send.PrintHint(pb)
send.PrintJsonExample(pb)
return
}
if *defaultReq {
*jsonStr, _ = send.PrintJsonExample(pb)
}
var req string
if *jsonStr != "" {
req = send.ConstructJsonRequest(pb, *jsonStr)
for _, pbVal := range pb {
if pbVal.Root {
*importStr = pbVal.Name + " \"" + *importStr + "\"\n\"encoding/json\""
}
}
} else {
req = send.ConstructRequest(pb, *requestStr)
for _, pbVal := range pb {
if pbVal.Root {
*importStr = pbVal.Name + " \"" + *importStr + "\""
}
}
}
rsp := send.ConstructResponse(pb)
outputStr := ""
service, endpoint := send.SeparateService(*endpointStr)
crArr := []send.ClientReplace{
send.ClientReplace{Find: IMPORT, Replace: *importStr},
send.ClientReplace{Find: REQUEST, Replace: req},
send.ClientReplace{Find: RESPONSE, Replace: rsp},
send.ClientReplace{Find: ENDPOINT, Replace: endpoint},
send.ClientReplace{Find: OUTPUT, Replace: outputStr},
send.ClientReplace{Find: SERVICE, Replace: service},
send.ClientReplace{Find: REQUESTUPDATE, Replace: ""},
}
rdrstr := strings.NewReader(CLIENTTEMPLATE)
rdr := bufio.NewReader(rdrstr)
_, err = send.WriteClient(rdr, crArr, *outputFile)
if err != nil {
log.Println("Error writing client:", err)
}
}