blob: c7c89f4d397db65f370f2643eec66d7611989414 [file] [log] [blame]
sslobodrd6e07e72019-01-31 16:07:20 -05001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16// gRPC affinity router with active/active backends
17
18package main
19
sslobodrd6e07e72019-01-31 16:07:20 -050020import (
sslobodrd6e07e72019-01-31 16:07:20 -050021 "encoding/json"
David Bainbridgeec4ff512019-04-19 18:59:40 +000022 "errors"
23 "flag"
24 "fmt"
sslobodrd6e07e72019-01-31 16:07:20 -050025 "github.com/golang/protobuf/proto"
sslobodrd6e07e72019-01-31 16:07:20 -050026 pb "github.com/golang/protobuf/protoc-gen-go/descriptor"
David Bainbridgeec4ff512019-04-19 18:59:40 +000027 "github.com/opencord/voltha-go/common/log"
28 "io/ioutil"
29 "math"
30 "os"
31 "os/exec"
32 "path"
33 "regexp"
34 "strconv"
35 "text/template"
sslobodrd6e07e72019-01-31 16:07:20 -050036)
37
sslobodr1d1e50b2019-03-14 09:17:40 -040038const MAX_CT = 500
39
sslobodrd6e07e72019-01-31 16:07:20 -050040type TestConfig struct {
41 configFile *string
David Bainbridgeec4ff512019-04-19 18:59:40 +000042 logLevel *int
43 grpcLog *bool
44 Suites []string `json:"suites"`
sslobodrd6e07e72019-01-31 16:07:20 -050045}
46
sslobodrd6e07e72019-01-31 16:07:20 -050047type Connection struct {
48 Name string `json:"name"`
49 Port string `json:"port"`
50}
51
sslobodr1d1e50b2019-03-14 09:17:40 -040052type EndpointList struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +000053 Imports []string `json:"imports"`
sslobodr1d1e50b2019-03-14 09:17:40 -040054 Endpoints []Connection `json:"endPoints"`
55}
56
sslobodrd6e07e72019-01-31 16:07:20 -050057type ProtoFile struct {
58 ImportPath string `json:"importPath"`
David Bainbridgeec4ff512019-04-19 18:59:40 +000059 Service string `json:"service"`
60 Package string `json:"package"`
sslobodrd6e07e72019-01-31 16:07:20 -050061}
62
63type ProtoSubst struct {
64 From string `json:"from"`
David Bainbridgeec4ff512019-04-19 18:59:40 +000065 To string `json:"to"`
sslobodrd6e07e72019-01-31 16:07:20 -050066}
67
68type Environment struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +000069 Command string `json:"cmdLine"`
70 ProtoFiles []ProtoFile `json:"protoFiles"`
71 ProtoDesc string `json:"protoDesc"`
72 Clients EndpointList `json:"clients"`
73 Servers EndpointList `json:"servers"`
74 Imports []string `json:"imports"`
sslobodrd6e07e72019-01-31 16:07:20 -050075 ProtoSubsts []ProtoSubst `json:"protoSubst"`
76}
77
78type Rpc struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +000079 Client string `json:"client"`
80 Method string `json:"method"`
81 Param string `json:"param"`
82 Expect string `json:"expect"`
83 MetaData []MetaD `json:"meta"`
sslobodrd9daabf2019-02-05 13:14:21 -050084 ExpectMeta []MetaD `json:"expectMeta"`
sslobodrd6e07e72019-01-31 16:07:20 -050085}
86
87type MetaD struct {
88 Key string `json:"key"`
89 Val string `json:"value"`
90}
91
92type Server struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +000093 Name string `json:"name"`
sslobodrd6e07e72019-01-31 16:07:20 -050094 Meta []MetaD `json:"meta"`
95}
96
97type Test struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +000098 Name string `json:"name"`
99 InfoOnly bool `json:"infoOnly"`
100 Send Rpc `json:"send"`
101 Servers []Server `json:"servers"`
sslobodrd6e07e72019-01-31 16:07:20 -0500102}
103
104type TestSuite struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000105 Env Environment `json:"environment"`
106 Tests []Test `json:"tests"`
sslobodrd6e07e72019-01-31 16:07:20 -0500107}
108
109type ProtoImport struct {
110 Service string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000111 Short string
sslobodrd6e07e72019-01-31 16:07:20 -0500112 Package string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000113 Used bool
sslobodrd6e07e72019-01-31 16:07:20 -0500114}
115
116type SendItem struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000117 Client string
118 Method string
119 Param string
120 ParamType string
121 Expect string
sslobodrd6e07e72019-01-31 16:07:20 -0500122 ExpectType string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000123 MetaData []MetaD
sslobodrd9daabf2019-02-05 13:14:21 -0500124 ExpectMeta []MetaD
sslobodrd6e07e72019-01-31 16:07:20 -0500125}
126
127type TestCase struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000128 Name string
sslobodr1d1e50b2019-03-14 09:17:40 -0400129 InfoOnly bool
David Bainbridgeec4ff512019-04-19 18:59:40 +0000130 Send SendItem
131 Srvr []Server
sslobodrd6e07e72019-01-31 16:07:20 -0500132}
133
sslobodr1d1e50b2019-03-14 09:17:40 -0400134type MethodTypes struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000135 ParamType string
136 ReturnType string
sslobodr1d1e50b2019-03-14 09:17:40 -0400137 CodeGenerated bool
138}
139
140type Import struct {
141 Package string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000142 Used bool
sslobodr1d1e50b2019-03-14 09:17:40 -0400143}
144
sslobodrd6e07e72019-01-31 16:07:20 -0500145type TestList struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000146 ProtoImports []ProtoImport
147 Imports []Import
148 Tests []TestCase
149 Funcs map[string]MethodTypes
sslobodr1d1e50b2019-03-14 09:17:40 -0400150 RunTestsCallList []string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000151 FileNum int
sslobodr1d1e50b2019-03-14 09:17:40 -0400152 //NextFile int
153 HasFuncs bool
David Bainbridgeec4ff512019-04-19 18:59:40 +0000154 Offset int
sslobodrd6e07e72019-01-31 16:07:20 -0500155}
156
157type ClientConfig struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000158 Ct int
159 Name string
160 Port string
161 Imports []string
162 Methods map[string]*mthd
sslobodrd6e07e72019-01-31 16:07:20 -0500163 ProtoImports []ProtoImport
164}
165
166type ServerConfig struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000167 Ct int
168 Name string
169 Port string
170 Imports []string
171 Methods map[string]*mthd
sslobodrd6e07e72019-01-31 16:07:20 -0500172 ProtoImports []ProtoImport
173}
174
175type mthd struct {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000176 Pkg string
177 Svc string
178 Name string
sslobodrd6e07e72019-01-31 16:07:20 -0500179 Param string
David Bainbridgeec4ff512019-04-19 18:59:40 +0000180 Rtrn string
181 Ss bool // Server streaming
182 Cs bool // Clieent streaming
sslobodrd6e07e72019-01-31 16:07:20 -0500183}
184
sslobodrd6e07e72019-01-31 16:07:20 -0500185func parseCmd() (*TestConfig, error) {
186 config := &TestConfig{}
David Bainbridgeec4ff512019-04-19 18:59:40 +0000187 cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError)
sslobodrd6e07e72019-01-31 16:07:20 -0500188 config.configFile = cmdParse.String("config", "suites.json", "The configuration file for the affinity router tester")
189 config.logLevel = cmdParse.Int("logLevel", 0, "The log level for the affinity router tester")
190 config.grpcLog = cmdParse.Bool("grpclog", false, "Enable GRPC logging")
191
David Bainbridgeec4ff512019-04-19 18:59:40 +0000192 err := cmdParse.Parse(os.Args[1:])
sslobodrd6e07e72019-01-31 16:07:20 -0500193 if err != nil {
194 //return err
David Bainbridgeec4ff512019-04-19 18:59:40 +0000195 return nil, errors.New("Error parsing the command line")
sslobodrd6e07e72019-01-31 16:07:20 -0500196 }
197 return config, nil
198}
199
David Bainbridgeec4ff512019-04-19 18:59:40 +0000200func (conf *TestConfig) loadConfig() error {
sslobodrd6e07e72019-01-31 16:07:20 -0500201
David Bainbridgeec4ff512019-04-19 18:59:40 +0000202 configF, err := os.Open(*conf.configFile)
sslobodrd6e07e72019-01-31 16:07:20 -0500203 log.Info("Loading configuration from: ", *conf.configFile)
204 if err != nil {
205 log.Error(err)
206 return err
207 }
208
209 defer configF.Close()
210
211 if configBytes, err := ioutil.ReadAll(configF); err != nil {
212 log.Error(err)
213 return err
214 } else if err := json.Unmarshal(configBytes, conf); err != nil {
215 log.Error(err)
216 return err
217 }
218
219 return nil
220}
221
David Bainbridgeec4ff512019-04-19 18:59:40 +0000222func (suite *TestSuite) loadSuite(suiteN string) error {
sslobodr13182842019-02-08 14:40:30 -0500223 // Check if there's a corresponding go file for the suite.
224 // If it is present then the json test suite file is a
225 // template. Compile the go file and run it to process
226 // the template.
David Bainbridgeec4ff512019-04-19 18:59:40 +0000227 if _, err := os.Stat(suiteN + ".go"); err == nil {
sslobodr13182842019-02-08 14:40:30 -0500228 // Compile and run the the go file
229 log.Infof("Suite '%s' is a template, compiling '%s'", suiteN, suiteN+".go")
230 cmd := exec.Command("go", "build", "-o", suiteN+".te", suiteN+".go")
231 cmd.Stdin = os.Stdin
232 cmd.Stdout = os.Stdout
233 cmd.Stderr = os.Stderr
234 if err := cmd.Run(); err != nil {
235 log.Errorf("Error running the compile command:%v", err)
236 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000237 cmd = exec.Command("./" + suiteN + ".te")
sslobodr13182842019-02-08 14:40:30 -0500238 cmd.Stdin = os.Stdin
239 cmd.Stdout = os.Stdout
240 cmd.Stderr = os.Stderr
241 if err := cmd.Run(); err != nil {
242 log.Errorf("Error running the %s command:%v", suiteN+".te", err)
243 }
244 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000245 suiteF, err := os.Open(suiteN + ".json")
sslobodr13182842019-02-08 14:40:30 -0500246 log.Infof("Loading test suite from: %s", suiteN+".json")
sslobodrd6e07e72019-01-31 16:07:20 -0500247 if err != nil {
248 log.Error(err)
249 return err
250 }
251
252 defer suiteF.Close()
253
254 if suiteBytes, err := ioutil.ReadAll(suiteF); err != nil {
255 log.Error(err)
256 return err
257 } else if err := json.Unmarshal(suiteBytes, suite); err != nil {
258 log.Error(err)
259 return err
260 }
261
262 return nil
263}
264
265func loadProtoMap(fileName string, pkg string, svc string, substs []ProtoSubst) (map[string]*mthd, error) {
266 var mthds map[string]*mthd = make(map[string]*mthd)
267 var rtrn_err bool
268
269 // Load the protobuf descriptor file
270 protoDescriptor := &pb.FileDescriptorSet{}
David Bainbridgeec4ff512019-04-19 18:59:40 +0000271 fb, err := ioutil.ReadFile(fileName)
sslobodrd6e07e72019-01-31 16:07:20 -0500272 if err != nil {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000273 log.Errorf("Could not open proto file '%s'", fileName)
sslobodrd6e07e72019-01-31 16:07:20 -0500274 rtrn_err = true
275 }
276 err = proto.Unmarshal(fb, protoDescriptor)
277 if err != nil {
278 log.Errorf("Could not unmarshal %s, %v", "proto.pb", err)
279 rtrn_err = true
280 }
281
David Bainbridgeec4ff512019-04-19 18:59:40 +0000282 var substM map[string]string = make(map[string]string)
sslobodrd6e07e72019-01-31 16:07:20 -0500283 // Create a substitution map
284 log.Debugf("Creating import map")
David Bainbridgeec4ff512019-04-19 18:59:40 +0000285 for _, v := range substs {
sslobodrd6e07e72019-01-31 16:07:20 -0500286 log.Debugf("Mapping from %s to %s", v.From, v.To)
287 substM[v.From] = v.To
288 }
289
sslobodrd6e07e72019-01-31 16:07:20 -0500290 // Build the a map containing the method as the key
291 // and the paramter and return types as the fields
David Bainbridgeec4ff512019-04-19 18:59:40 +0000292 for _, f := range protoDescriptor.File {
sslobodrd6e07e72019-01-31 16:07:20 -0500293 if *f.Package == pkg {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000294 for _, s := range f.Service {
sslobodrd6e07e72019-01-31 16:07:20 -0500295 if *s.Name == svc {
296 log.Debugf("Loading package data '%s' for service '%s'", *f.Package, *s.Name)
297 // Now create a map keyed by method name with the value being the
298 // field number of the route selector.
299 //var ok bool
David Bainbridgeec4ff512019-04-19 18:59:40 +0000300 for _, m := range s.Method {
sslobodrd6e07e72019-01-31 16:07:20 -0500301 // Find the input type in the messages and extract the
302 // field number and save it for future reference.
David Bainbridgeec4ff512019-04-19 18:59:40 +0000303 log.Debugf("Processing method (%s(%s) (%s){}", *m.Name, (*m.InputType)[1:], (*m.OutputType)[1:])
304 mthds[*m.Name] = &mthd{Pkg: pkg, Svc: svc, Name: *m.Name, Param: (*m.InputType)[1:],
305 Rtrn: (*m.OutputType)[1:]}
sslobodrd6e07e72019-01-31 16:07:20 -0500306 if m.ClientStreaming != nil && *m.ClientStreaming == true {
307 log.Debugf("Method %s is a client streaming method", *m.Name)
308 mthds[*m.Name].Cs = true
309 }
310 if m.ServerStreaming != nil && *m.ServerStreaming == true {
311 log.Debugf("Method %s is a server streaming method", *m.Name)
312 mthds[*m.Name].Ss = true
313 }
314 // Perform the required substitutions
David Bainbridgeec4ff512019-04-19 18:59:40 +0000315 if _, ok := substM[mthds[*m.Name].Param]; ok == true {
sslobodrd6e07e72019-01-31 16:07:20 -0500316 mthds[*m.Name].Param = substM[mthds[*m.Name].Param]
317 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000318 if _, ok := substM[mthds[*m.Name].Rtrn]; ok == true {
sslobodrd6e07e72019-01-31 16:07:20 -0500319 mthds[*m.Name].Rtrn = substM[mthds[*m.Name].Rtrn]
320 }
321 }
322 }
323 }
324 }
325 }
326 if rtrn_err {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000327 return nil, errors.New(fmt.Sprintf("Failed to load protobuf descriptor file '%s'", fileName))
sslobodrd6e07e72019-01-31 16:07:20 -0500328 }
329 return mthds, nil
330}
331
332// Server source code generation
David Bainbridgeec4ff512019-04-19 18:59:40 +0000333func generateServers(conf *TestConfig, suiteDir string, ts *TestSuite,
334 t *template.Template) error {
sslobodrd6e07e72019-01-31 16:07:20 -0500335 var servers []ServerConfig
336
David Bainbridgeec4ff512019-04-19 18:59:40 +0000337 for k, v := range ts.Env.Servers.Endpoints {
sslobodrd6e07e72019-01-31 16:07:20 -0500338 log.Infof("Generating the code for server[%d]: %s", k, v.Name)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000339 sc := &ServerConfig{Name: v.Name, Port: v.Port, Ct: k, Imports: ts.Env.Servers.Imports,
340 Methods: make(map[string]*mthd)}
341 for k1, v1 := range ts.Env.ProtoFiles {
342 imp := &ProtoImport{Short: "pb" + strconv.Itoa(k1),
343 Package: v1.ImportPath + v1.Package,
344 Service: v1.Service,
345 Used: true}
346 imp = &ProtoImport{Short: v1.Package,
347 Package: v1.ImportPath + v1.Package,
348 Service: v1.Service,
349 Used: true}
sslobodrd6e07e72019-01-31 16:07:20 -0500350 sc.ProtoImports = append(sc.ProtoImports, *imp)
351 // Compile the template from the file
352 log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts)
353 if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v1.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000354 v1.Service, ts.Env.ProtoSubsts); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500355 log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s",
David Bainbridgeec4ff512019-04-19 18:59:40 +0000356 ts.Env.ProtoDesc, v1.Package, v1.Service)
sslobodrd6e07e72019-01-31 16:07:20 -0500357 return err
358 } else {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000359 //Generate all the function calls required by the
360 for k, v := range mthds {
sslobodr1d1e50b2019-03-14 09:17:40 -0400361 sc.Methods[k] = v
362 }
363 //sc.Methods = mthds
sslobodrd6e07e72019-01-31 16:07:20 -0500364 }
365 }
366 log.Debugf("Server: %v", *sc)
367 // Save this server for the next steop
368 servers = append(servers, *sc)
369 // Open an output file to put the output in.
David Bainbridgeec4ff512019-04-19 18:59:40 +0000370 if f, err := os.Create(suiteDir + "/" + v.Name + ".go"); err == nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500371 defer f.Close()
David Bainbridgeec4ff512019-04-19 18:59:40 +0000372 //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {}
373 if err := t.ExecuteTemplate(f, "server.go.tmpl", *sc); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500374 log.Errorf("Unable to execute template for server[%d]: %s: %v", k, v.Name, err)
375 return err
376 }
377 }
378 }
379 // Generate the server initialization code
David Bainbridgeec4ff512019-04-19 18:59:40 +0000380 if f, err := os.Create(suiteDir + "/serverInit.go"); err == nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500381 defer f.Close()
David Bainbridgeec4ff512019-04-19 18:59:40 +0000382 //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {}
383 if err := t.ExecuteTemplate(f, "serverInit.go.tmpl", servers); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500384 log.Errorf("Unable to execute template for serverInit.go: %v", err)
385 return err
386 }
387 }
388
389 return nil
390}
391
David Bainbridgeec4ff512019-04-19 18:59:40 +0000392func generateClients(conf *TestConfig, suiteDir string, ts *TestSuite,
393 t *template.Template) error {
sslobodrd6e07e72019-01-31 16:07:20 -0500394 var clients []ClientConfig
David Bainbridgeec4ff512019-04-19 18:59:40 +0000395 for k, v := range ts.Env.Clients.Endpoints {
sslobodrd6e07e72019-01-31 16:07:20 -0500396 log.Infof("Generating the code for client[%d]: %s", k, v.Name)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000397 cc := &ClientConfig{Name: v.Name, Port: v.Port, Ct: k, Imports: ts.Env.Clients.Imports,
398 Methods: make(map[string]*mthd)}
399 for _, v1 := range ts.Env.ProtoFiles {
400 imp := &ProtoImport{Short: v1.Package,
401 Package: v1.ImportPath + v1.Package,
402 Service: v1.Service}
sslobodrd6e07e72019-01-31 16:07:20 -0500403 cc.ProtoImports = append(cc.ProtoImports, *imp)
404 // Compile the template from the file
405 log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts)
406 if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v1.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000407 v1.Service, ts.Env.ProtoSubsts); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500408 log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s",
David Bainbridgeec4ff512019-04-19 18:59:40 +0000409 ts.Env.ProtoDesc, v1.Package, v1.Service)
sslobodrd6e07e72019-01-31 16:07:20 -0500410 return err
411 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400412 // Add to the known methods
David Bainbridgeec4ff512019-04-19 18:59:40 +0000413 for k, v := range mthds {
sslobodr1d1e50b2019-03-14 09:17:40 -0400414 cc.Methods[k] = v
415 }
sslobodrd6e07e72019-01-31 16:07:20 -0500416 }
417 }
418 clients = append(clients, *cc)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000419 if f, err := os.Create(suiteDir + "/" + v.Name + "_client.go"); err == nil {
420 _ = f
sslobodrd6e07e72019-01-31 16:07:20 -0500421 defer f.Close()
David Bainbridgeec4ff512019-04-19 18:59:40 +0000422 if err := t.ExecuteTemplate(f, "client.go.tmpl", cc); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500423 log.Errorf("Unable to execute template for client.go: %v", err)
424 return err
425 }
426 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400427 log.Errorf("Couldn't create file %s : %v", suiteDir+"/"+v.Name+"_client.go", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500428 return err
429 }
430 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000431 if f, err := os.Create(suiteDir + "/clientInit.go"); err == nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500432 defer f.Close()
David Bainbridgeec4ff512019-04-19 18:59:40 +0000433 //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {}
434 if err := t.ExecuteTemplate(f, "clientInit.go.tmpl", clients); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500435 log.Errorf("Unable to execute template for clientInit.go: %v", err)
436 return err
437 }
438 }
439 return nil
440}
441
David Bainbridgeec4ff512019-04-19 18:59:40 +0000442func serverExists(srvr string, ts *TestSuite) bool {
443 for _, v := range ts.Env.Servers.Endpoints {
sslobodrd6e07e72019-01-31 16:07:20 -0500444 if v.Name == srvr {
445 return true
446 }
447 }
448 return false
449}
sslobodr1d1e50b2019-03-14 09:17:40 -0400450
451func getPackageList(tests []TestCase) map[string]struct{} {
452 var rtrn map[string]struct{} = make(map[string]struct{})
453 var p string
454 var e string
455 r := regexp.MustCompile(`^([^.]+)\..*$`)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000456 for _, v := range tests {
sslobodr1d1e50b2019-03-14 09:17:40 -0400457 pa := r.FindStringSubmatch(v.Send.ParamType)
458 if len(pa) == 2 {
459 p = pa[1]
460 } else {
461 log.Errorf("Internal error regexp returned %v", pa)
462 }
463 ea := r.FindStringSubmatch(v.Send.ExpectType)
464 if len(ea) == 2 {
465 e = ea[1]
466 } else {
467 log.Errorf("Internal error regexp returned %v", pa)
468 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000469 if _, ok := rtrn[p]; ok == false {
sslobodr1d1e50b2019-03-14 09:17:40 -0400470 rtrn[p] = struct{}{}
471 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000472 if _, ok := rtrn[e]; ok == false {
sslobodr1d1e50b2019-03-14 09:17:40 -0400473 rtrn[e] = struct{}{}
474 }
475 }
476 return rtrn
477}
478
479func fixupProtoImports(protoImports []ProtoImport, used map[string]struct{}) []ProtoImport {
480 var rtrn []ProtoImport
481 //log.Infof("Updating imports %v, using %v", protoImports, used)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000482 for _, v := range protoImports {
sslobodr1d1e50b2019-03-14 09:17:40 -0400483 //log.Infof("Looking for package %s", v.Package)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000484 if _, ok := used[v.Short]; ok == true {
485 rtrn = append(rtrn, ProtoImport{
sslobodr1d1e50b2019-03-14 09:17:40 -0400486 Service: v.Service,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000487 Short: v.Short,
sslobodr1d1e50b2019-03-14 09:17:40 -0400488 Package: v.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000489 Used: true,
sslobodr1d1e50b2019-03-14 09:17:40 -0400490 })
491 } else {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000492 rtrn = append(rtrn, ProtoImport{
sslobodr1d1e50b2019-03-14 09:17:40 -0400493 Service: v.Service,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000494 Short: v.Short,
sslobodr1d1e50b2019-03-14 09:17:40 -0400495 Package: v.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000496 Used: false,
sslobodr1d1e50b2019-03-14 09:17:40 -0400497 })
498 }
499 }
500 //log.Infof("After update %v", rtrn)
501 return rtrn
502}
503
504func fixupImports(imports []Import, used map[string]struct{}) []Import {
505 var rtrn []Import
506 var p string
507 re := regexp.MustCompile(`^.*/([^/]+)$`)
508 //log.Infof("Updating imports %v, using %v", protoImports, used)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000509 for _, v := range imports {
sslobodr1d1e50b2019-03-14 09:17:40 -0400510 //log.Infof("Looking for match in %s", v.Package)
511 pa := re.FindStringSubmatch(v.Package)
512 if len(pa) == 2 {
513 p = pa[1]
514 } else {
515 log.Errorf("Substring match failed, regexp returned %v", pa)
516 }
517 //log.Infof("Looking for package %s", v.Package)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000518 if _, ok := used[p]; ok == true {
519 rtrn = append(rtrn, Import{
sslobodr1d1e50b2019-03-14 09:17:40 -0400520 Package: v.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000521 Used: true,
sslobodr1d1e50b2019-03-14 09:17:40 -0400522 })
523 } else {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000524 rtrn = append(rtrn, Import{
sslobodr1d1e50b2019-03-14 09:17:40 -0400525 Package: v.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000526 Used: false,
sslobodr1d1e50b2019-03-14 09:17:40 -0400527 })
528 }
529 }
530 //log.Infof("After update %v", rtrn)
531 return rtrn
532}
533
David Bainbridgeec4ff512019-04-19 18:59:40 +0000534func generateTestCases(conf *TestConfig, suiteDir string, ts *TestSuite,
535 t *template.Template) error {
sslobodrd6e07e72019-01-31 16:07:20 -0500536 var mthdMap map[string]*mthd
sslobodr1d1e50b2019-03-14 09:17:40 -0400537 mthdMap = make(map[string]*mthd)
sslobodrd6e07e72019-01-31 16:07:20 -0500538 // Generate the test cases
539 log.Info("Generating the test cases: runTests.go")
David Bainbridgeec4ff512019-04-19 18:59:40 +0000540 tc := &TestList{Funcs: make(map[string]MethodTypes),
541 FileNum: 0, HasFuncs: false,
542 Offset: 0}
543 for _, v := range ts.Env.Imports {
544 tc.Imports = append(tc.Imports, Import{Package: v, Used: true})
sslobodr1d1e50b2019-03-14 09:17:40 -0400545 }
sslobodrd6e07e72019-01-31 16:07:20 -0500546
547 // Load the proto descriptor file
David Bainbridgeec4ff512019-04-19 18:59:40 +0000548 for _, v := range ts.Env.ProtoFiles {
549 imp := &ProtoImport{Short: v.Package,
550 Package: v.ImportPath + v.Package,
551 Service: v.Service,
552 Used: true}
sslobodrd6e07e72019-01-31 16:07:20 -0500553 tc.ProtoImports = append(tc.ProtoImports, *imp)
554 // Compile the template from the file
555 log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts)
556 if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v.Package,
David Bainbridgeec4ff512019-04-19 18:59:40 +0000557 v.Service, ts.Env.ProtoSubsts); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500558 log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s",
David Bainbridgeec4ff512019-04-19 18:59:40 +0000559 ts.Env.ProtoDesc, v.Package, v.Service)
sslobodrd6e07e72019-01-31 16:07:20 -0500560 return err
561 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400562 // Add to the known methods
David Bainbridgeec4ff512019-04-19 18:59:40 +0000563 for k, v := range mthds {
sslobodr1d1e50b2019-03-14 09:17:40 -0400564 mthdMap[k] = v
565 }
sslobodrd6e07e72019-01-31 16:07:20 -0500566 }
567 }
sslobodr1d1e50b2019-03-14 09:17:40 -0400568 // Since the input file can possibly be a template with loops that
569 // make multiple calls to exactly the same method with potentially
570 // different parameters it is best to try to optimize for that case.
571 // Creating a function for each method used will greatly reduce the
572 // code size and repetition. In the case where a method is used only
573 // once the resulting code will be bigger but this is an acceptable
574 // tradeoff to allow huge suites that call the same method repeatedly
575 // to test for leaks.
576
577 // The go compiler has trouble compiling files that are too large. In order
578 // to mitigate that, It's necessary to create more smaller files than the
579 // single one large one while making sure that the functions for the distinct
580 // methods are only defined once in one of the files.
581
582 // Yet another hiccup with the go compiler, it doesn't like deep function
583 // nesting meaning that each runTests function can't call the next without
584 // eventually blowing the stack check. In order to work around this, the
585 // first runTests function needs to call all the others in sequence to
586 // keep the stack depth constant.
587
588 // Counter limiting the number of tests to output to each file
589 maxCt := MAX_CT
590
591 // Get the list of distinct methods
592 //for _,v := range ts.Tests {
593 // if _,ok := tc.Funcs[v.Send.Method]; ok == false {
594 // tc.Funcs[v.Send.Method] = MethodTypes{ParamType:mthdMap[v.Send.Method].Param,
595 // ReturnType:mthdMap[v.Send.Method].Rtrn}
596 // }
597 //}
David Bainbridgeec4ff512019-04-19 18:59:40 +0000598 for i := 1; i < int(math.Ceil(float64(len(ts.Tests))/float64(MAX_CT))); i++ {
sslobodr1d1e50b2019-03-14 09:17:40 -0400599 tc.RunTestsCallList = append(tc.RunTestsCallList, "runTests"+strconv.Itoa(i))
600 }
601
sslobodrd6e07e72019-01-31 16:07:20 -0500602 // Create the test data structure for the template
David Bainbridgeec4ff512019-04-19 18:59:40 +0000603 for _, v := range ts.Tests {
sslobodrd6e07e72019-01-31 16:07:20 -0500604 var test TestCase
605
David Bainbridgeec4ff512019-04-19 18:59:40 +0000606 if _, ok := tc.Funcs[v.Send.Method]; ok == false {
607 tc.Funcs[v.Send.Method] = MethodTypes{ParamType: mthdMap[v.Send.Method].Param,
608 ReturnType: mthdMap[v.Send.Method].Rtrn,
609 CodeGenerated: false}
sslobodr1d1e50b2019-03-14 09:17:40 -0400610 tc.HasFuncs = true
611 }
612
sslobodrd6e07e72019-01-31 16:07:20 -0500613 test.Name = v.Name
sslobodr1d1e50b2019-03-14 09:17:40 -0400614 test.InfoOnly = v.InfoOnly
sslobodrd6e07e72019-01-31 16:07:20 -0500615 test.Send.Client = v.Send.Client
616 test.Send.Method = v.Send.Method
617 test.Send.Param = v.Send.Param
618 test.Send.ParamType = mthdMap[test.Send.Method].Param
619 test.Send.Expect = v.Send.Expect
620 test.Send.ExpectType = mthdMap[test.Send.Method].Rtrn
David Bainbridgeec4ff512019-04-19 18:59:40 +0000621 for _, v1 := range v.Send.MetaData {
622 test.Send.MetaData = append(test.Send.MetaData, v1)
sslobodrd9daabf2019-02-05 13:14:21 -0500623 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000624 for _, v1 := range v.Send.ExpectMeta {
625 test.Send.ExpectMeta = append(test.Send.ExpectMeta, v1)
sslobodrd9daabf2019-02-05 13:14:21 -0500626 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000627 for _, v1 := range v.Servers {
sslobodrd6e07e72019-01-31 16:07:20 -0500628 var srvr Server
629 if serverExists(v1.Name, ts) == false {
630 log.Errorf("Server '%s' is not defined!!", v1.Name)
631 return errors.New(fmt.Sprintf("Failed to build test case %s", v.Name))
632 }
633 srvr.Name = v1.Name
634 srvr.Meta = v1.Meta
635 test.Srvr = append(test.Srvr, srvr)
636 }
637 tc.Tests = append(tc.Tests, test)
sslobodr1d1e50b2019-03-14 09:17:40 -0400638 if maxCt--; maxCt == 0 {
639 // Get the list of proto pacakges required for this file
640 pkgs := getPackageList(tc.Tests)
641 // Adjust the proto import data accordingly
642 tc.ProtoImports = fixupProtoImports(tc.ProtoImports, pkgs)
643 tc.Imports = fixupImports(tc.Imports, pkgs)
644 //log.Infof("The packages needed are: %v", pkgs)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000645 if f, err := os.Create(suiteDir + "/runTests" + strconv.Itoa(tc.FileNum) + ".go"); err == nil {
646 if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil {
647 log.Errorf("Unable to execute template for runTests.go: %v", err)
sslobodr1d1e50b2019-03-14 09:17:40 -0400648 }
649 f.Close()
650 } else {
651 log.Errorf("Couldn't create file %s : %v",
David Bainbridgeec4ff512019-04-19 18:59:40 +0000652 suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err)
sslobodr1d1e50b2019-03-14 09:17:40 -0400653 }
654 tc.FileNum++
655 //tc.NextFile++
656 maxCt = MAX_CT
657 // Mark the functions as generated.
658 tc.Tests = []TestCase{}
David Bainbridgeec4ff512019-04-19 18:59:40 +0000659 for k, v := range tc.Funcs {
660 tc.Funcs[k] = MethodTypes{ParamType: v.ParamType,
661 ReturnType: v.ReturnType,
662 CodeGenerated: true}
sslobodr1d1e50b2019-03-14 09:17:40 -0400663 }
664 tc.HasFuncs = false
665 tc.Offset += MAX_CT
666 //tmp,_ := strconv.Atoi(tc.Offset)
667 //tc.Offset = strconv.Itoa(tmp+500)
668 }
sslobodrd6e07e72019-01-31 16:07:20 -0500669 }
sslobodr1d1e50b2019-03-14 09:17:40 -0400670 //tc.NextFile = 0
671 // Get the list of proto pacakges required for this file
672 pkgs := getPackageList(tc.Tests)
673 // Adjust the proto import data accordingly
674 tc.ProtoImports = fixupProtoImports(tc.ProtoImports, pkgs)
675 tc.Imports = fixupImports(tc.Imports, pkgs)
676 //log.Infof("The packages needed are: %v", pkgs)
David Bainbridgeec4ff512019-04-19 18:59:40 +0000677 if f, err := os.Create(suiteDir + "/runTests" + strconv.Itoa(tc.FileNum) + ".go"); err == nil {
678 if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil {
679 log.Errorf("Unable to execute template for runTests.go: %v", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500680 }
681 f.Close()
682 } else {
sslobodr1d1e50b2019-03-14 09:17:40 -0400683 log.Errorf("Couldn't create file %s : %v",
David Bainbridgeec4ff512019-04-19 18:59:40 +0000684 suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500685 }
sslobodr1d1e50b2019-03-14 09:17:40 -0400686
687 //if f,err := os.Create(suiteDir+"/runTests.go"); err == nil {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000688 // if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil {
sslobodr1d1e50b2019-03-14 09:17:40 -0400689 // log.Errorf("Unable to execute template for runTests.go: %v", err)
690 // }
691 // f.Close()
692 //} else {
693 // log.Errorf("Couldn't create file %s : %v", suiteDir+"/runTests.go", err)
694 //}
sslobodrd6e07e72019-01-31 16:07:20 -0500695 return nil
696}
697
698func generateTestSuites(conf *TestConfig, srcDir string, outDir string) error {
699
700 // Create a directory for the tests
701 if err := os.Mkdir(srcDir, 0777); err != nil {
702 log.Errorf("Unable to create directory 'tests':%v\n", err)
703 return err
704 }
705
David Bainbridgeec4ff512019-04-19 18:59:40 +0000706 for k, v := range conf.Suites {
707 var suiteDir string = srcDir + "/" + v
sslobodrd6e07e72019-01-31 16:07:20 -0500708 log.Debugf("Suite[%d] - %s", k, v)
709 ts := &TestSuite{}
710 ts.loadSuite(v)
711 log.Debugf("Suite %s: %v", v, ts)
sslobodr1d1e50b2019-03-14 09:17:40 -0400712 log.Infof("Processing test suite %s", v)
sslobodrd6e07e72019-01-31 16:07:20 -0500713
David Bainbridgeec4ff512019-04-19 18:59:40 +0000714 t := template.Must(template.New("").ParseFiles("../templates/server.go.tmpl",
715 "../templates/serverInit.go.tmpl",
716 "../templates/client.go.tmpl",
717 "../templates/clientInit.go.tmpl",
718 "../templates/runTests.go.tmpl",
719 "../templates/stats.go.tmpl",
720 "../templates/main.go.tmpl"))
sslobodrd6e07e72019-01-31 16:07:20 -0500721 // Create a directory for he source code for this test suite
722 if err := os.Mkdir(suiteDir, 0777); err != nil {
723 log.Errorf("Unable to create directory '%s':%v\n", v, err)
724 return err
725 }
726 // Generate the server source files
727 if err := generateServers(conf, suiteDir, ts, t); err != nil {
728 log.Errorf("Unable to generate server source files: %v", err)
729 return err
730 }
731 // Generate the client source files
732 if err := generateClients(conf, suiteDir, ts, t); err != nil {
733 log.Errorf("Unable to generate client source files: %v", err)
734 return err
735 }
736 // Generate the test case source file
737 if err := generateTestCases(conf, suiteDir, ts, t); err != nil {
738 log.Errorf("Unable to generate test case source file: %v", err)
739 return err
740 }
741
742 // Finally generate the main file
743 log.Info("Generating main.go")
David Bainbridgeec4ff512019-04-19 18:59:40 +0000744 if f, err := os.Create(suiteDir + "/main.go"); err == nil {
745 if err := t.ExecuteTemplate(f, "main.go.tmpl", ts.Env); err != nil {
746 log.Errorf("Unable to execute template for main.go: %v", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500747 }
748 f.Close()
749 } else {
750 log.Errorf("Couldn't create file %s : %v", suiteDir+"/main.go", err)
751 }
752
sslobodr1d1e50b2019-03-14 09:17:40 -0400753 log.Infof("Copying over common modules")
David Bainbridgeec4ff512019-04-19 18:59:40 +0000754 if f, err := os.Create(suiteDir + "/stats.go"); err == nil {
755 if err := t.ExecuteTemplate(f, "stats.go.tmpl", ts.Env); err != nil {
756 log.Errorf("Unable to execute template for stats.go: %v", err)
sslobodr1d1e50b2019-03-14 09:17:40 -0400757 }
758 f.Close()
759 } else {
760 log.Errorf("Couldn't create file %s : %v", suiteDir+"/stats.go", err)
761 }
762
sslobodrd6e07e72019-01-31 16:07:20 -0500763 log.Infof("Compiling test suite: %s in directory %s", v, suiteDir)
764 if err := os.Chdir(suiteDir); err != nil {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000765 log.Errorf("Could not change to directory '%s':%v", suiteDir, err)
sslobodrd6e07e72019-01-31 16:07:20 -0500766 }
sslobodr13182842019-02-08 14:40:30 -0500767 cmd := exec.Command("go", "build", "-o", outDir+"/"+v+".e")
sslobodrd6e07e72019-01-31 16:07:20 -0500768 cmd.Stdin = os.Stdin
769 cmd.Stdout = os.Stdout
770 cmd.Stderr = os.Stderr
771 if err := cmd.Run(); err != nil {
772 log.Errorf("Error running the compile command:%v", err)
773 }
774 if err := os.Chdir("../../suites"); err != nil {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000775 log.Errorf("Could not change to directory '%s':%v", "../../suites", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500776 }
sslobodrd6e07e72019-01-31 16:07:20 -0500777 }
778 return nil
779
780}
781
782func generateTestDriver(conf *TestConfig, srcDir string, outDir string) error {
783 // Generate the main test driver file
784 if err := os.Mkdir(srcDir, 0777); err != nil {
785 log.Errorf("Unable to create directory 'driver':%v\n", err)
786 return err
787 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000788 t := template.Must(template.New("").ParseFiles("../templates/runAll.go.tmpl",
789 "../templates/stats.go.tmpl"))
790 if f, err := os.Create(srcDir + "/runAll.go"); err == nil {
791 if err := t.ExecuteTemplate(f, "runAll.go.tmpl", conf.Suites); err != nil {
792 log.Errorf("Unable to execute template for runAll.go: %v", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500793 }
794 f.Close()
795 } else {
796 log.Errorf("Couldn't create file %s : %v", srcDir+"/runAll.go", err)
797 }
David Bainbridgeec4ff512019-04-19 18:59:40 +0000798 if f, err := os.Create(srcDir + "/stats.go"); err == nil {
799 if err := t.ExecuteTemplate(f, "stats.go.tmpl", conf.Suites); err != nil {
800 log.Errorf("Unable to execute template for stats.go: %v", err)
sslobodr1d1e50b2019-03-14 09:17:40 -0400801 }
802 f.Close()
803 } else {
804 log.Errorf("Couldn't create file %s : %v", srcDir+"/stats.go", err)
805 }
sslobodrd6e07e72019-01-31 16:07:20 -0500806
807 // Compile the test driver file
808 log.Info("Compiling the test driver")
809 if err := os.Chdir("../tests/driver"); err != nil {
810 log.Errorf("Could not change to directory 'driver':%v", err)
811 }
812 cmd := exec.Command("go", "build", "-o", outDir+"/runAll")
813 cmd.Stdin = os.Stdin
814 cmd.Stdout = os.Stdout
815 cmd.Stderr = os.Stderr
816 if err := cmd.Run(); err != nil {
817 log.Errorf("Error running the compile command:%v", err)
818 }
819 if err := os.Chdir("../../suites"); err != nil {
David Bainbridgeec4ff512019-04-19 18:59:40 +0000820 log.Errorf("Could not change to directory 'driver':%v", err)
sslobodrd6e07e72019-01-31 16:07:20 -0500821 }
822
823 return nil
824}
825
sslobodrd6e07e72019-01-31 16:07:20 -0500826func main() {
827
David Bainbridgeec4ff512019-04-19 18:59:40 +0000828 conf, err := parseCmd()
sslobodrd6e07e72019-01-31 16:07:20 -0500829 if err != nil {
830 fmt.Printf("Error: %v\n", err)
831 return
832 }
833
834 // Setup logging
835 if _, err := log.SetDefaultLogger(log.JSON, *conf.logLevel, nil); err != nil {
836 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
837 }
838
839 defer log.CleanUp()
840
841 // Parse the config file
842 if err := conf.loadConfig(); err != nil {
843 log.Error(err)
844 }
845
846 generateTestSuites(conf, "../tests", "/src/tests")
847 generateTestDriver(conf, "../tests/driver", "/src/tests")
848 return
849}