sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package main |
| 19 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 20 | import ( |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 21 | "encoding/json" |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 22 | "errors" |
| 23 | "flag" |
| 24 | "fmt" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 25 | "github.com/golang/protobuf/proto" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 26 | pb "github.com/golang/protobuf/protoc-gen-go/descriptor" |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 27 | "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" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 36 | ) |
| 37 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 38 | const MAX_CT = 500 |
| 39 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 40 | type TestConfig struct { |
| 41 | configFile *string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 42 | logLevel *int |
| 43 | grpcLog *bool |
| 44 | Suites []string `json:"suites"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 45 | } |
| 46 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 47 | type Connection struct { |
| 48 | Name string `json:"name"` |
| 49 | Port string `json:"port"` |
| 50 | } |
| 51 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 52 | type EndpointList struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 53 | Imports []string `json:"imports"` |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 54 | Endpoints []Connection `json:"endPoints"` |
| 55 | } |
| 56 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 57 | type ProtoFile struct { |
| 58 | ImportPath string `json:"importPath"` |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 59 | Service string `json:"service"` |
| 60 | Package string `json:"package"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | type ProtoSubst struct { |
| 64 | From string `json:"from"` |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 65 | To string `json:"to"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | type Environment struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 69 | 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"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 75 | ProtoSubsts []ProtoSubst `json:"protoSubst"` |
| 76 | } |
| 77 | |
| 78 | type Rpc struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 79 | Client string `json:"client"` |
| 80 | Method string `json:"method"` |
| 81 | Param string `json:"param"` |
| 82 | Expect string `json:"expect"` |
| 83 | MetaData []MetaD `json:"meta"` |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 84 | ExpectMeta []MetaD `json:"expectMeta"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | type MetaD struct { |
| 88 | Key string `json:"key"` |
| 89 | Val string `json:"value"` |
| 90 | } |
| 91 | |
| 92 | type Server struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 93 | Name string `json:"name"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 94 | Meta []MetaD `json:"meta"` |
| 95 | } |
| 96 | |
| 97 | type Test struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 98 | Name string `json:"name"` |
| 99 | InfoOnly bool `json:"infoOnly"` |
| 100 | Send Rpc `json:"send"` |
| 101 | Servers []Server `json:"servers"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | type TestSuite struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 105 | Env Environment `json:"environment"` |
| 106 | Tests []Test `json:"tests"` |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | type ProtoImport struct { |
| 110 | Service string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 111 | Short string |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 112 | Package string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 113 | Used bool |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | type SendItem struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 117 | Client string |
| 118 | Method string |
| 119 | Param string |
| 120 | ParamType string |
| 121 | Expect string |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 122 | ExpectType string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 123 | MetaData []MetaD |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 124 | ExpectMeta []MetaD |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | type TestCase struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 128 | Name string |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 129 | InfoOnly bool |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 130 | Send SendItem |
| 131 | Srvr []Server |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 132 | } |
| 133 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 134 | type MethodTypes struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 135 | ParamType string |
| 136 | ReturnType string |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 137 | CodeGenerated bool |
| 138 | } |
| 139 | |
| 140 | type Import struct { |
| 141 | Package string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 142 | Used bool |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 143 | } |
| 144 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 145 | type TestList struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 146 | ProtoImports []ProtoImport |
| 147 | Imports []Import |
| 148 | Tests []TestCase |
| 149 | Funcs map[string]MethodTypes |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 150 | RunTestsCallList []string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 151 | FileNum int |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 152 | //NextFile int |
| 153 | HasFuncs bool |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 154 | Offset int |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | type ClientConfig struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 158 | Ct int |
| 159 | Name string |
| 160 | Port string |
| 161 | Imports []string |
| 162 | Methods map[string]*mthd |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 163 | ProtoImports []ProtoImport |
| 164 | } |
| 165 | |
| 166 | type ServerConfig struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 167 | Ct int |
| 168 | Name string |
| 169 | Port string |
| 170 | Imports []string |
| 171 | Methods map[string]*mthd |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 172 | ProtoImports []ProtoImport |
| 173 | } |
| 174 | |
| 175 | type mthd struct { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 176 | Pkg string |
| 177 | Svc string |
| 178 | Name string |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 179 | Param string |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 180 | Rtrn string |
| 181 | Ss bool // Server streaming |
| 182 | Cs bool // Clieent streaming |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 183 | } |
| 184 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 185 | func parseCmd() (*TestConfig, error) { |
| 186 | config := &TestConfig{} |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 187 | cmdParse := flag.NewFlagSet(path.Base(os.Args[0]), flag.ContinueOnError) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 188 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 192 | err := cmdParse.Parse(os.Args[1:]) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 193 | if err != nil { |
| 194 | //return err |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 195 | return nil, errors.New("Error parsing the command line") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 196 | } |
| 197 | return config, nil |
| 198 | } |
| 199 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 200 | func (conf *TestConfig) loadConfig() error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 201 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 202 | configF, err := os.Open(*conf.configFile) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 203 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 222 | func (suite *TestSuite) loadSuite(suiteN string) error { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 223 | // 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 Bainbridge | 3122174 | 2019-04-19 19:49:51 +0000 | [diff] [blame] | 227 | if _, err := os.Stat(suiteN + "/" + suiteN + ".go"); err == nil { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 228 | // 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 |
David Bainbridge | 3122174 | 2019-04-19 19:49:51 +0000 | [diff] [blame] | 234 | cmd.Dir = suiteN |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 235 | if err := cmd.Run(); err != nil { |
| 236 | log.Errorf("Error running the compile command:%v", err) |
| 237 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 238 | cmd = exec.Command("./" + suiteN + ".te") |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 239 | cmd.Stdin = os.Stdin |
| 240 | cmd.Stdout = os.Stdout |
| 241 | cmd.Stderr = os.Stderr |
David Bainbridge | 3122174 | 2019-04-19 19:49:51 +0000 | [diff] [blame] | 242 | cmd.Dir = suiteN |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 243 | if err := cmd.Run(); err != nil { |
| 244 | log.Errorf("Error running the %s command:%v", suiteN+".te", err) |
| 245 | } |
| 246 | } |
David Bainbridge | 3122174 | 2019-04-19 19:49:51 +0000 | [diff] [blame] | 247 | suiteF, err := os.Open(suiteN + "/" + suiteN + ".json") |
| 248 | log.Infof("Loading test suite from: %s", suiteN+"/"+suiteN+".json") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 249 | if err != nil { |
| 250 | log.Error(err) |
| 251 | return err |
| 252 | } |
| 253 | |
| 254 | defer suiteF.Close() |
| 255 | |
| 256 | if suiteBytes, err := ioutil.ReadAll(suiteF); err != nil { |
| 257 | log.Error(err) |
| 258 | return err |
| 259 | } else if err := json.Unmarshal(suiteBytes, suite); err != nil { |
| 260 | log.Error(err) |
| 261 | return err |
| 262 | } |
| 263 | |
| 264 | return nil |
| 265 | } |
| 266 | |
| 267 | func loadProtoMap(fileName string, pkg string, svc string, substs []ProtoSubst) (map[string]*mthd, error) { |
| 268 | var mthds map[string]*mthd = make(map[string]*mthd) |
| 269 | var rtrn_err bool |
| 270 | |
| 271 | // Load the protobuf descriptor file |
| 272 | protoDescriptor := &pb.FileDescriptorSet{} |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 273 | fb, err := ioutil.ReadFile(fileName) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 274 | if err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 275 | log.Errorf("Could not open proto file '%s'", fileName) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 276 | rtrn_err = true |
| 277 | } |
| 278 | err = proto.Unmarshal(fb, protoDescriptor) |
| 279 | if err != nil { |
| 280 | log.Errorf("Could not unmarshal %s, %v", "proto.pb", err) |
| 281 | rtrn_err = true |
| 282 | } |
| 283 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 284 | var substM map[string]string = make(map[string]string) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 285 | // Create a substitution map |
| 286 | log.Debugf("Creating import map") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 287 | for _, v := range substs { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 288 | log.Debugf("Mapping from %s to %s", v.From, v.To) |
| 289 | substM[v.From] = v.To |
| 290 | } |
| 291 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 292 | // Build the a map containing the method as the key |
| 293 | // and the paramter and return types as the fields |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 294 | for _, f := range protoDescriptor.File { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 295 | if *f.Package == pkg { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 296 | for _, s := range f.Service { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 297 | if *s.Name == svc { |
| 298 | log.Debugf("Loading package data '%s' for service '%s'", *f.Package, *s.Name) |
| 299 | // Now create a map keyed by method name with the value being the |
| 300 | // field number of the route selector. |
| 301 | //var ok bool |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 302 | for _, m := range s.Method { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 303 | // Find the input type in the messages and extract the |
| 304 | // field number and save it for future reference. |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 305 | log.Debugf("Processing method (%s(%s) (%s){}", *m.Name, (*m.InputType)[1:], (*m.OutputType)[1:]) |
| 306 | mthds[*m.Name] = &mthd{Pkg: pkg, Svc: svc, Name: *m.Name, Param: (*m.InputType)[1:], |
| 307 | Rtrn: (*m.OutputType)[1:]} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 308 | if m.ClientStreaming != nil && *m.ClientStreaming == true { |
| 309 | log.Debugf("Method %s is a client streaming method", *m.Name) |
| 310 | mthds[*m.Name].Cs = true |
| 311 | } |
| 312 | if m.ServerStreaming != nil && *m.ServerStreaming == true { |
| 313 | log.Debugf("Method %s is a server streaming method", *m.Name) |
| 314 | mthds[*m.Name].Ss = true |
| 315 | } |
| 316 | // Perform the required substitutions |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 317 | if _, ok := substM[mthds[*m.Name].Param]; ok == true { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 318 | mthds[*m.Name].Param = substM[mthds[*m.Name].Param] |
| 319 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 320 | if _, ok := substM[mthds[*m.Name].Rtrn]; ok == true { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 321 | mthds[*m.Name].Rtrn = substM[mthds[*m.Name].Rtrn] |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | if rtrn_err { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 329 | return nil, errors.New(fmt.Sprintf("Failed to load protobuf descriptor file '%s'", fileName)) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 330 | } |
| 331 | return mthds, nil |
| 332 | } |
| 333 | |
| 334 | // Server source code generation |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 335 | func generateServers(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 336 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 337 | var servers []ServerConfig |
| 338 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 339 | for k, v := range ts.Env.Servers.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 340 | log.Infof("Generating the code for server[%d]: %s", k, v.Name) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 341 | sc := &ServerConfig{Name: v.Name, Port: v.Port, Ct: k, Imports: ts.Env.Servers.Imports, |
| 342 | Methods: make(map[string]*mthd)} |
| 343 | for k1, v1 := range ts.Env.ProtoFiles { |
| 344 | imp := &ProtoImport{Short: "pb" + strconv.Itoa(k1), |
| 345 | Package: v1.ImportPath + v1.Package, |
| 346 | Service: v1.Service, |
| 347 | Used: true} |
| 348 | imp = &ProtoImport{Short: v1.Package, |
| 349 | Package: v1.ImportPath + v1.Package, |
| 350 | Service: v1.Service, |
| 351 | Used: true} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 352 | sc.ProtoImports = append(sc.ProtoImports, *imp) |
| 353 | // Compile the template from the file |
| 354 | log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts) |
| 355 | if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v1.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 356 | v1.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 357 | log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 358 | ts.Env.ProtoDesc, v1.Package, v1.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 359 | return err |
| 360 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 361 | //Generate all the function calls required by the |
| 362 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 363 | sc.Methods[k] = v |
| 364 | } |
| 365 | //sc.Methods = mthds |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | log.Debugf("Server: %v", *sc) |
| 369 | // Save this server for the next steop |
| 370 | servers = append(servers, *sc) |
| 371 | // Open an output file to put the output in. |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 372 | if f, err := os.Create(suiteDir + "/" + v.Name + ".go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 373 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 374 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 375 | if err := t.ExecuteTemplate(f, "server.go.tmpl", *sc); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 376 | log.Errorf("Unable to execute template for server[%d]: %s: %v", k, v.Name, err) |
| 377 | return err |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | // Generate the server initialization code |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 382 | if f, err := os.Create(suiteDir + "/serverInit.go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 383 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 384 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 385 | if err := t.ExecuteTemplate(f, "serverInit.go.tmpl", servers); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 386 | log.Errorf("Unable to execute template for serverInit.go: %v", err) |
| 387 | return err |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | return nil |
| 392 | } |
| 393 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 394 | func generateClients(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 395 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 396 | var clients []ClientConfig |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 397 | for k, v := range ts.Env.Clients.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 398 | log.Infof("Generating the code for client[%d]: %s", k, v.Name) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 399 | cc := &ClientConfig{Name: v.Name, Port: v.Port, Ct: k, Imports: ts.Env.Clients.Imports, |
| 400 | Methods: make(map[string]*mthd)} |
| 401 | for _, v1 := range ts.Env.ProtoFiles { |
| 402 | imp := &ProtoImport{Short: v1.Package, |
| 403 | Package: v1.ImportPath + v1.Package, |
| 404 | Service: v1.Service} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 405 | cc.ProtoImports = append(cc.ProtoImports, *imp) |
| 406 | // Compile the template from the file |
| 407 | log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts) |
| 408 | if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v1.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 409 | v1.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 410 | log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 411 | ts.Env.ProtoDesc, v1.Package, v1.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 412 | return err |
| 413 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 414 | // Add to the known methods |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 415 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 416 | cc.Methods[k] = v |
| 417 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | clients = append(clients, *cc) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 421 | if f, err := os.Create(suiteDir + "/" + v.Name + "_client.go"); err == nil { |
| 422 | _ = f |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 423 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 424 | if err := t.ExecuteTemplate(f, "client.go.tmpl", cc); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 425 | log.Errorf("Unable to execute template for client.go: %v", err) |
| 426 | return err |
| 427 | } |
| 428 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 429 | log.Errorf("Couldn't create file %s : %v", suiteDir+"/"+v.Name+"_client.go", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 430 | return err |
| 431 | } |
| 432 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 433 | if f, err := os.Create(suiteDir + "/clientInit.go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 434 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 435 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 436 | if err := t.ExecuteTemplate(f, "clientInit.go.tmpl", clients); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 437 | log.Errorf("Unable to execute template for clientInit.go: %v", err) |
| 438 | return err |
| 439 | } |
| 440 | } |
| 441 | return nil |
| 442 | } |
| 443 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 444 | func serverExists(srvr string, ts *TestSuite) bool { |
| 445 | for _, v := range ts.Env.Servers.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 446 | if v.Name == srvr { |
| 447 | return true |
| 448 | } |
| 449 | } |
| 450 | return false |
| 451 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 452 | |
| 453 | func getPackageList(tests []TestCase) map[string]struct{} { |
| 454 | var rtrn map[string]struct{} = make(map[string]struct{}) |
| 455 | var p string |
| 456 | var e string |
| 457 | r := regexp.MustCompile(`^([^.]+)\..*$`) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 458 | for _, v := range tests { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 459 | pa := r.FindStringSubmatch(v.Send.ParamType) |
| 460 | if len(pa) == 2 { |
| 461 | p = pa[1] |
| 462 | } else { |
| 463 | log.Errorf("Internal error regexp returned %v", pa) |
| 464 | } |
| 465 | ea := r.FindStringSubmatch(v.Send.ExpectType) |
| 466 | if len(ea) == 2 { |
| 467 | e = ea[1] |
| 468 | } else { |
| 469 | log.Errorf("Internal error regexp returned %v", pa) |
| 470 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 471 | if _, ok := rtrn[p]; ok == false { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 472 | rtrn[p] = struct{}{} |
| 473 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 474 | if _, ok := rtrn[e]; ok == false { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 475 | rtrn[e] = struct{}{} |
| 476 | } |
| 477 | } |
| 478 | return rtrn |
| 479 | } |
| 480 | |
| 481 | func fixupProtoImports(protoImports []ProtoImport, used map[string]struct{}) []ProtoImport { |
| 482 | var rtrn []ProtoImport |
| 483 | //log.Infof("Updating imports %v, using %v", protoImports, used) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 484 | for _, v := range protoImports { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 485 | //log.Infof("Looking for package %s", v.Package) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 486 | if _, ok := used[v.Short]; ok == true { |
| 487 | rtrn = append(rtrn, ProtoImport{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 488 | Service: v.Service, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 489 | Short: v.Short, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 490 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 491 | Used: true, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 492 | }) |
| 493 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 494 | rtrn = append(rtrn, ProtoImport{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 495 | Service: v.Service, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 496 | Short: v.Short, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 497 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 498 | Used: false, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 499 | }) |
| 500 | } |
| 501 | } |
| 502 | //log.Infof("After update %v", rtrn) |
| 503 | return rtrn |
| 504 | } |
| 505 | |
| 506 | func fixupImports(imports []Import, used map[string]struct{}) []Import { |
| 507 | var rtrn []Import |
| 508 | var p string |
| 509 | re := regexp.MustCompile(`^.*/([^/]+)$`) |
| 510 | //log.Infof("Updating imports %v, using %v", protoImports, used) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 511 | for _, v := range imports { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 512 | //log.Infof("Looking for match in %s", v.Package) |
| 513 | pa := re.FindStringSubmatch(v.Package) |
| 514 | if len(pa) == 2 { |
| 515 | p = pa[1] |
| 516 | } else { |
| 517 | log.Errorf("Substring match failed, regexp returned %v", pa) |
| 518 | } |
| 519 | //log.Infof("Looking for package %s", v.Package) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 520 | if _, ok := used[p]; ok == true { |
| 521 | rtrn = append(rtrn, Import{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 522 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 523 | Used: true, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 524 | }) |
| 525 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 526 | rtrn = append(rtrn, Import{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 527 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 528 | Used: false, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 529 | }) |
| 530 | } |
| 531 | } |
| 532 | //log.Infof("After update %v", rtrn) |
| 533 | return rtrn |
| 534 | } |
| 535 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 536 | func generateTestCases(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 537 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 538 | var mthdMap map[string]*mthd |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 539 | mthdMap = make(map[string]*mthd) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 540 | // Generate the test cases |
| 541 | log.Info("Generating the test cases: runTests.go") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 542 | tc := &TestList{Funcs: make(map[string]MethodTypes), |
| 543 | FileNum: 0, HasFuncs: false, |
| 544 | Offset: 0} |
| 545 | for _, v := range ts.Env.Imports { |
| 546 | tc.Imports = append(tc.Imports, Import{Package: v, Used: true}) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 547 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 548 | |
| 549 | // Load the proto descriptor file |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 550 | for _, v := range ts.Env.ProtoFiles { |
| 551 | imp := &ProtoImport{Short: v.Package, |
| 552 | Package: v.ImportPath + v.Package, |
| 553 | Service: v.Service, |
| 554 | Used: true} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 555 | tc.ProtoImports = append(tc.ProtoImports, *imp) |
| 556 | // Compile the template from the file |
| 557 | log.Debugf("Proto substs: %v", ts.Env.ProtoSubsts) |
| 558 | if mthds, err := loadProtoMap(ts.Env.ProtoDesc, v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 559 | v.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 560 | log.Errorf("Unable to process proto descriptor file %s for package: %s, service: %s", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 561 | ts.Env.ProtoDesc, v.Package, v.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 562 | return err |
| 563 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 564 | // Add to the known methods |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 565 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 566 | mthdMap[k] = v |
| 567 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 568 | } |
| 569 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 570 | // Since the input file can possibly be a template with loops that |
| 571 | // make multiple calls to exactly the same method with potentially |
| 572 | // different parameters it is best to try to optimize for that case. |
| 573 | // Creating a function for each method used will greatly reduce the |
| 574 | // code size and repetition. In the case where a method is used only |
| 575 | // once the resulting code will be bigger but this is an acceptable |
| 576 | // tradeoff to allow huge suites that call the same method repeatedly |
| 577 | // to test for leaks. |
| 578 | |
| 579 | // The go compiler has trouble compiling files that are too large. In order |
| 580 | // to mitigate that, It's necessary to create more smaller files than the |
| 581 | // single one large one while making sure that the functions for the distinct |
| 582 | // methods are only defined once in one of the files. |
| 583 | |
| 584 | // Yet another hiccup with the go compiler, it doesn't like deep function |
| 585 | // nesting meaning that each runTests function can't call the next without |
| 586 | // eventually blowing the stack check. In order to work around this, the |
| 587 | // first runTests function needs to call all the others in sequence to |
| 588 | // keep the stack depth constant. |
| 589 | |
| 590 | // Counter limiting the number of tests to output to each file |
| 591 | maxCt := MAX_CT |
| 592 | |
| 593 | // Get the list of distinct methods |
| 594 | //for _,v := range ts.Tests { |
| 595 | // if _,ok := tc.Funcs[v.Send.Method]; ok == false { |
| 596 | // tc.Funcs[v.Send.Method] = MethodTypes{ParamType:mthdMap[v.Send.Method].Param, |
| 597 | // ReturnType:mthdMap[v.Send.Method].Rtrn} |
| 598 | // } |
| 599 | //} |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 600 | for i := 1; i < int(math.Ceil(float64(len(ts.Tests))/float64(MAX_CT))); i++ { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 601 | tc.RunTestsCallList = append(tc.RunTestsCallList, "runTests"+strconv.Itoa(i)) |
| 602 | } |
| 603 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 604 | // Create the test data structure for the template |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 605 | for _, v := range ts.Tests { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 606 | var test TestCase |
| 607 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 608 | if _, ok := tc.Funcs[v.Send.Method]; ok == false { |
| 609 | tc.Funcs[v.Send.Method] = MethodTypes{ParamType: mthdMap[v.Send.Method].Param, |
| 610 | ReturnType: mthdMap[v.Send.Method].Rtrn, |
| 611 | CodeGenerated: false} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 612 | tc.HasFuncs = true |
| 613 | } |
| 614 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 615 | test.Name = v.Name |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 616 | test.InfoOnly = v.InfoOnly |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 617 | test.Send.Client = v.Send.Client |
| 618 | test.Send.Method = v.Send.Method |
| 619 | test.Send.Param = v.Send.Param |
| 620 | test.Send.ParamType = mthdMap[test.Send.Method].Param |
| 621 | test.Send.Expect = v.Send.Expect |
| 622 | test.Send.ExpectType = mthdMap[test.Send.Method].Rtrn |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 623 | for _, v1 := range v.Send.MetaData { |
| 624 | test.Send.MetaData = append(test.Send.MetaData, v1) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 625 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 626 | for _, v1 := range v.Send.ExpectMeta { |
| 627 | test.Send.ExpectMeta = append(test.Send.ExpectMeta, v1) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 628 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 629 | for _, v1 := range v.Servers { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 630 | var srvr Server |
| 631 | if serverExists(v1.Name, ts) == false { |
| 632 | log.Errorf("Server '%s' is not defined!!", v1.Name) |
| 633 | return errors.New(fmt.Sprintf("Failed to build test case %s", v.Name)) |
| 634 | } |
| 635 | srvr.Name = v1.Name |
| 636 | srvr.Meta = v1.Meta |
| 637 | test.Srvr = append(test.Srvr, srvr) |
| 638 | } |
| 639 | tc.Tests = append(tc.Tests, test) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 640 | if maxCt--; maxCt == 0 { |
| 641 | // Get the list of proto pacakges required for this file |
| 642 | pkgs := getPackageList(tc.Tests) |
| 643 | // Adjust the proto import data accordingly |
| 644 | tc.ProtoImports = fixupProtoImports(tc.ProtoImports, pkgs) |
| 645 | tc.Imports = fixupImports(tc.Imports, pkgs) |
| 646 | //log.Infof("The packages needed are: %v", pkgs) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 647 | if f, err := os.Create(suiteDir + "/runTests" + strconv.Itoa(tc.FileNum) + ".go"); err == nil { |
| 648 | if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil { |
| 649 | log.Errorf("Unable to execute template for runTests.go: %v", err) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 650 | } |
| 651 | f.Close() |
| 652 | } else { |
| 653 | log.Errorf("Couldn't create file %s : %v", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 654 | suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 655 | } |
| 656 | tc.FileNum++ |
| 657 | //tc.NextFile++ |
| 658 | maxCt = MAX_CT |
| 659 | // Mark the functions as generated. |
| 660 | tc.Tests = []TestCase{} |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 661 | for k, v := range tc.Funcs { |
| 662 | tc.Funcs[k] = MethodTypes{ParamType: v.ParamType, |
| 663 | ReturnType: v.ReturnType, |
| 664 | CodeGenerated: true} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 665 | } |
| 666 | tc.HasFuncs = false |
| 667 | tc.Offset += MAX_CT |
| 668 | //tmp,_ := strconv.Atoi(tc.Offset) |
| 669 | //tc.Offset = strconv.Itoa(tmp+500) |
| 670 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 671 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 672 | //tc.NextFile = 0 |
| 673 | // Get the list of proto pacakges required for this file |
| 674 | pkgs := getPackageList(tc.Tests) |
| 675 | // Adjust the proto import data accordingly |
| 676 | tc.ProtoImports = fixupProtoImports(tc.ProtoImports, pkgs) |
| 677 | tc.Imports = fixupImports(tc.Imports, pkgs) |
| 678 | //log.Infof("The packages needed are: %v", pkgs) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 679 | if f, err := os.Create(suiteDir + "/runTests" + strconv.Itoa(tc.FileNum) + ".go"); err == nil { |
| 680 | if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil { |
| 681 | log.Errorf("Unable to execute template for runTests.go: %v", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 682 | } |
| 683 | f.Close() |
| 684 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 685 | log.Errorf("Couldn't create file %s : %v", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 686 | suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 687 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 688 | |
| 689 | //if f,err := os.Create(suiteDir+"/runTests.go"); err == nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 690 | // if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 691 | // log.Errorf("Unable to execute template for runTests.go: %v", err) |
| 692 | // } |
| 693 | // f.Close() |
| 694 | //} else { |
| 695 | // log.Errorf("Couldn't create file %s : %v", suiteDir+"/runTests.go", err) |
| 696 | //} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 697 | return nil |
| 698 | } |
| 699 | |
| 700 | func generateTestSuites(conf *TestConfig, srcDir string, outDir string) error { |
| 701 | |
| 702 | // Create a directory for the tests |
| 703 | if err := os.Mkdir(srcDir, 0777); err != nil { |
| 704 | log.Errorf("Unable to create directory 'tests':%v\n", err) |
| 705 | return err |
| 706 | } |
| 707 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 708 | for k, v := range conf.Suites { |
| 709 | var suiteDir string = srcDir + "/" + v |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 710 | log.Debugf("Suite[%d] - %s", k, v) |
| 711 | ts := &TestSuite{} |
| 712 | ts.loadSuite(v) |
| 713 | log.Debugf("Suite %s: %v", v, ts) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 714 | log.Infof("Processing test suite %s", v) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 715 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 716 | t := template.Must(template.New("").ParseFiles("../templates/server.go.tmpl", |
| 717 | "../templates/serverInit.go.tmpl", |
| 718 | "../templates/client.go.tmpl", |
| 719 | "../templates/clientInit.go.tmpl", |
| 720 | "../templates/runTests.go.tmpl", |
| 721 | "../templates/stats.go.tmpl", |
| 722 | "../templates/main.go.tmpl")) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 723 | // Create a directory for he source code for this test suite |
| 724 | if err := os.Mkdir(suiteDir, 0777); err != nil { |
| 725 | log.Errorf("Unable to create directory '%s':%v\n", v, err) |
| 726 | return err |
| 727 | } |
| 728 | // Generate the server source files |
| 729 | if err := generateServers(conf, suiteDir, ts, t); err != nil { |
| 730 | log.Errorf("Unable to generate server source files: %v", err) |
| 731 | return err |
| 732 | } |
| 733 | // Generate the client source files |
| 734 | if err := generateClients(conf, suiteDir, ts, t); err != nil { |
| 735 | log.Errorf("Unable to generate client source files: %v", err) |
| 736 | return err |
| 737 | } |
| 738 | // Generate the test case source file |
| 739 | if err := generateTestCases(conf, suiteDir, ts, t); err != nil { |
| 740 | log.Errorf("Unable to generate test case source file: %v", err) |
| 741 | return err |
| 742 | } |
| 743 | |
| 744 | // Finally generate the main file |
| 745 | log.Info("Generating main.go") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 746 | if f, err := os.Create(suiteDir + "/main.go"); err == nil { |
| 747 | if err := t.ExecuteTemplate(f, "main.go.tmpl", ts.Env); err != nil { |
| 748 | log.Errorf("Unable to execute template for main.go: %v", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 749 | } |
| 750 | f.Close() |
| 751 | } else { |
| 752 | log.Errorf("Couldn't create file %s : %v", suiteDir+"/main.go", err) |
| 753 | } |
| 754 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 755 | log.Infof("Copying over common modules") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 756 | if f, err := os.Create(suiteDir + "/stats.go"); err == nil { |
| 757 | if err := t.ExecuteTemplate(f, "stats.go.tmpl", ts.Env); err != nil { |
| 758 | log.Errorf("Unable to execute template for stats.go: %v", err) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 759 | } |
| 760 | f.Close() |
| 761 | } else { |
| 762 | log.Errorf("Couldn't create file %s : %v", suiteDir+"/stats.go", err) |
| 763 | } |
| 764 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 765 | log.Infof("Compiling test suite: %s in directory %s", v, suiteDir) |
| 766 | if err := os.Chdir(suiteDir); err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 767 | log.Errorf("Could not change to directory '%s':%v", suiteDir, err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 768 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 769 | cmd := exec.Command("go", "build", "-o", outDir+"/"+v+".e") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 770 | cmd.Stdin = os.Stdin |
| 771 | cmd.Stdout = os.Stdout |
| 772 | cmd.Stderr = os.Stderr |
| 773 | if err := cmd.Run(); err != nil { |
| 774 | log.Errorf("Error running the compile command:%v", err) |
| 775 | } |
| 776 | if err := os.Chdir("../../suites"); err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 777 | log.Errorf("Could not change to directory '%s':%v", "../../suites", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 778 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 779 | } |
| 780 | return nil |
| 781 | |
| 782 | } |
| 783 | |
| 784 | func generateTestDriver(conf *TestConfig, srcDir string, outDir string) error { |
| 785 | // Generate the main test driver file |
| 786 | if err := os.Mkdir(srcDir, 0777); err != nil { |
| 787 | log.Errorf("Unable to create directory 'driver':%v\n", err) |
| 788 | return err |
| 789 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 790 | t := template.Must(template.New("").ParseFiles("../templates/runAll.go.tmpl", |
| 791 | "../templates/stats.go.tmpl")) |
| 792 | if f, err := os.Create(srcDir + "/runAll.go"); err == nil { |
| 793 | if err := t.ExecuteTemplate(f, "runAll.go.tmpl", conf.Suites); err != nil { |
| 794 | log.Errorf("Unable to execute template for runAll.go: %v", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 795 | } |
| 796 | f.Close() |
| 797 | } else { |
| 798 | log.Errorf("Couldn't create file %s : %v", srcDir+"/runAll.go", err) |
| 799 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 800 | if f, err := os.Create(srcDir + "/stats.go"); err == nil { |
| 801 | if err := t.ExecuteTemplate(f, "stats.go.tmpl", conf.Suites); err != nil { |
| 802 | log.Errorf("Unable to execute template for stats.go: %v", err) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 803 | } |
| 804 | f.Close() |
| 805 | } else { |
| 806 | log.Errorf("Couldn't create file %s : %v", srcDir+"/stats.go", err) |
| 807 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 808 | |
| 809 | // Compile the test driver file |
| 810 | log.Info("Compiling the test driver") |
| 811 | if err := os.Chdir("../tests/driver"); err != nil { |
| 812 | log.Errorf("Could not change to directory 'driver':%v", err) |
| 813 | } |
| 814 | cmd := exec.Command("go", "build", "-o", outDir+"/runAll") |
| 815 | cmd.Stdin = os.Stdin |
| 816 | cmd.Stdout = os.Stdout |
| 817 | cmd.Stderr = os.Stderr |
| 818 | if err := cmd.Run(); err != nil { |
| 819 | log.Errorf("Error running the compile command:%v", err) |
| 820 | } |
| 821 | if err := os.Chdir("../../suites"); err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 822 | log.Errorf("Could not change to directory 'driver':%v", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | return nil |
| 826 | } |
| 827 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 828 | func main() { |
| 829 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 830 | conf, err := parseCmd() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 831 | if err != nil { |
| 832 | fmt.Printf("Error: %v\n", err) |
| 833 | return |
| 834 | } |
| 835 | |
| 836 | // Setup logging |
| 837 | if _, err := log.SetDefaultLogger(log.JSON, *conf.logLevel, nil); err != nil { |
| 838 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 839 | } |
| 840 | |
| 841 | defer log.CleanUp() |
| 842 | |
| 843 | // Parse the config file |
| 844 | if err := conf.loadConfig(); err != nil { |
| 845 | log.Error(err) |
| 846 | } |
| 847 | |
| 848 | generateTestSuites(conf, "../tests", "/src/tests") |
| 849 | generateTestDriver(conf, "../tests/driver", "/src/tests") |
| 850 | return |
| 851 | } |