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 | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 227 | if _, err := os.Stat(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 |
| 234 | if err := cmd.Run(); err != nil { |
| 235 | log.Errorf("Error running the compile command:%v", err) |
| 236 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 237 | cmd = exec.Command("./" + suiteN + ".te") |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 238 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 245 | suiteF, err := os.Open(suiteN + ".json") |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 246 | log.Infof("Loading test suite from: %s", suiteN+".json") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 247 | 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 | |
| 265 | func 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 271 | fb, err := ioutil.ReadFile(fileName) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 272 | if err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 273 | log.Errorf("Could not open proto file '%s'", fileName) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 274 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 282 | var substM map[string]string = make(map[string]string) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 283 | // Create a substitution map |
| 284 | log.Debugf("Creating import map") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 285 | for _, v := range substs { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 286 | log.Debugf("Mapping from %s to %s", v.From, v.To) |
| 287 | substM[v.From] = v.To |
| 288 | } |
| 289 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 290 | // Build the a map containing the method as the key |
| 291 | // and the paramter and return types as the fields |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 292 | for _, f := range protoDescriptor.File { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 293 | if *f.Package == pkg { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 294 | for _, s := range f.Service { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 295 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 300 | for _, m := range s.Method { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 301 | // Find the input type in the messages and extract the |
| 302 | // field number and save it for future reference. |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 303 | 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:]} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 306 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 315 | if _, ok := substM[mthds[*m.Name].Param]; ok == true { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 316 | mthds[*m.Name].Param = substM[mthds[*m.Name].Param] |
| 317 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 318 | if _, ok := substM[mthds[*m.Name].Rtrn]; ok == true { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 319 | mthds[*m.Name].Rtrn = substM[mthds[*m.Name].Rtrn] |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | if rtrn_err { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 327 | 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] | 328 | } |
| 329 | return mthds, nil |
| 330 | } |
| 331 | |
| 332 | // Server source code generation |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 333 | func generateServers(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 334 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 335 | var servers []ServerConfig |
| 336 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 337 | for k, v := range ts.Env.Servers.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 338 | log.Infof("Generating the code for server[%d]: %s", k, v.Name) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 339 | 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} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 350 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 354 | v1.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 355 | 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] | 356 | ts.Env.ProtoDesc, v1.Package, v1.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 357 | return err |
| 358 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 359 | //Generate all the function calls required by the |
| 360 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 361 | sc.Methods[k] = v |
| 362 | } |
| 363 | //sc.Methods = mthds |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 364 | } |
| 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 370 | if f, err := os.Create(suiteDir + "/" + v.Name + ".go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 371 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 372 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 373 | if err := t.ExecuteTemplate(f, "server.go.tmpl", *sc); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 374 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 380 | if f, err := os.Create(suiteDir + "/serverInit.go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 381 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 382 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 383 | if err := t.ExecuteTemplate(f, "serverInit.go.tmpl", servers); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 384 | log.Errorf("Unable to execute template for serverInit.go: %v", err) |
| 385 | return err |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return nil |
| 390 | } |
| 391 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 392 | func generateClients(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 393 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 394 | var clients []ClientConfig |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 395 | for k, v := range ts.Env.Clients.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 396 | log.Infof("Generating the code for client[%d]: %s", k, v.Name) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 397 | 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} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 403 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 407 | v1.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 408 | 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] | 409 | ts.Env.ProtoDesc, v1.Package, v1.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 410 | return err |
| 411 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 412 | // Add to the known methods |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 413 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 414 | cc.Methods[k] = v |
| 415 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | clients = append(clients, *cc) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 419 | if f, err := os.Create(suiteDir + "/" + v.Name + "_client.go"); err == nil { |
| 420 | _ = f |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 421 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 422 | if err := t.ExecuteTemplate(f, "client.go.tmpl", cc); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 423 | log.Errorf("Unable to execute template for client.go: %v", err) |
| 424 | return err |
| 425 | } |
| 426 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 427 | 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] | 428 | return err |
| 429 | } |
| 430 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 431 | if f, err := os.Create(suiteDir + "/clientInit.go"); err == nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 432 | defer f.Close() |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 433 | //if err := t.ExecuteTemplate(os.Stdout, "server.go.tmpl", *sc); err != nil {} |
| 434 | if err := t.ExecuteTemplate(f, "clientInit.go.tmpl", clients); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 435 | log.Errorf("Unable to execute template for clientInit.go: %v", err) |
| 436 | return err |
| 437 | } |
| 438 | } |
| 439 | return nil |
| 440 | } |
| 441 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 442 | func serverExists(srvr string, ts *TestSuite) bool { |
| 443 | for _, v := range ts.Env.Servers.Endpoints { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 444 | if v.Name == srvr { |
| 445 | return true |
| 446 | } |
| 447 | } |
| 448 | return false |
| 449 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 450 | |
| 451 | func 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 456 | for _, v := range tests { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 457 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 469 | if _, ok := rtrn[p]; ok == false { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 470 | rtrn[p] = struct{}{} |
| 471 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 472 | if _, ok := rtrn[e]; ok == false { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 473 | rtrn[e] = struct{}{} |
| 474 | } |
| 475 | } |
| 476 | return rtrn |
| 477 | } |
| 478 | |
| 479 | func fixupProtoImports(protoImports []ProtoImport, used map[string]struct{}) []ProtoImport { |
| 480 | var rtrn []ProtoImport |
| 481 | //log.Infof("Updating imports %v, using %v", protoImports, used) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 482 | for _, v := range protoImports { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 483 | //log.Infof("Looking for package %s", v.Package) |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 484 | if _, ok := used[v.Short]; ok == true { |
| 485 | rtrn = append(rtrn, ProtoImport{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 486 | Service: v.Service, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 487 | Short: v.Short, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 488 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 489 | Used: true, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 490 | }) |
| 491 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 492 | rtrn = append(rtrn, ProtoImport{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 493 | Service: v.Service, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 494 | Short: v.Short, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 495 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 496 | Used: false, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 497 | }) |
| 498 | } |
| 499 | } |
| 500 | //log.Infof("After update %v", rtrn) |
| 501 | return rtrn |
| 502 | } |
| 503 | |
| 504 | func 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 509 | for _, v := range imports { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 510 | //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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 518 | if _, ok := used[p]; ok == true { |
| 519 | rtrn = append(rtrn, Import{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 520 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 521 | Used: true, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 522 | }) |
| 523 | } else { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 524 | rtrn = append(rtrn, Import{ |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 525 | Package: v.Package, |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 526 | Used: false, |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 527 | }) |
| 528 | } |
| 529 | } |
| 530 | //log.Infof("After update %v", rtrn) |
| 531 | return rtrn |
| 532 | } |
| 533 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 534 | func generateTestCases(conf *TestConfig, suiteDir string, ts *TestSuite, |
| 535 | t *template.Template) error { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 536 | var mthdMap map[string]*mthd |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 537 | mthdMap = make(map[string]*mthd) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 538 | // Generate the test cases |
| 539 | log.Info("Generating the test cases: runTests.go") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 540 | 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}) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 545 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 546 | |
| 547 | // Load the proto descriptor file |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 548 | 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} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 553 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 557 | v.Service, ts.Env.ProtoSubsts); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 558 | 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] | 559 | ts.Env.ProtoDesc, v.Package, v.Service) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 560 | return err |
| 561 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 562 | // Add to the known methods |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 563 | for k, v := range mthds { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 564 | mthdMap[k] = v |
| 565 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 566 | } |
| 567 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 568 | // 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 598 | 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] | 599 | tc.RunTestsCallList = append(tc.RunTestsCallList, "runTests"+strconv.Itoa(i)) |
| 600 | } |
| 601 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 602 | // Create the test data structure for the template |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 603 | for _, v := range ts.Tests { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 604 | var test TestCase |
| 605 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 606 | 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} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 610 | tc.HasFuncs = true |
| 611 | } |
| 612 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 613 | test.Name = v.Name |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 614 | test.InfoOnly = v.InfoOnly |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 615 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 621 | for _, v1 := range v.Send.MetaData { |
| 622 | test.Send.MetaData = append(test.Send.MetaData, v1) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 623 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 624 | for _, v1 := range v.Send.ExpectMeta { |
| 625 | test.Send.ExpectMeta = append(test.Send.ExpectMeta, v1) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 626 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 627 | for _, v1 := range v.Servers { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 628 | 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) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 638 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 645 | 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) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 648 | } |
| 649 | f.Close() |
| 650 | } else { |
| 651 | log.Errorf("Couldn't create file %s : %v", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 652 | suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 653 | } |
| 654 | tc.FileNum++ |
| 655 | //tc.NextFile++ |
| 656 | maxCt = MAX_CT |
| 657 | // Mark the functions as generated. |
| 658 | tc.Tests = []TestCase{} |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 659 | for k, v := range tc.Funcs { |
| 660 | tc.Funcs[k] = MethodTypes{ParamType: v.ParamType, |
| 661 | ReturnType: v.ReturnType, |
| 662 | CodeGenerated: true} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 663 | } |
| 664 | tc.HasFuncs = false |
| 665 | tc.Offset += MAX_CT |
| 666 | //tmp,_ := strconv.Atoi(tc.Offset) |
| 667 | //tc.Offset = strconv.Itoa(tmp+500) |
| 668 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 669 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 670 | //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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 677 | 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) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 680 | } |
| 681 | f.Close() |
| 682 | } else { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 683 | log.Errorf("Couldn't create file %s : %v", |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 684 | suiteDir+"/runTests"+strconv.Itoa(tc.FileNum)+".go", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 685 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 686 | |
| 687 | //if f,err := os.Create(suiteDir+"/runTests.go"); err == nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 688 | // if err := t.ExecuteTemplate(f, "runTests.go.tmpl", tc); err != nil { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 689 | // 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 | //} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 695 | return nil |
| 696 | } |
| 697 | |
| 698 | func 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 706 | for k, v := range conf.Suites { |
| 707 | var suiteDir string = srcDir + "/" + v |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 708 | log.Debugf("Suite[%d] - %s", k, v) |
| 709 | ts := &TestSuite{} |
| 710 | ts.loadSuite(v) |
| 711 | log.Debugf("Suite %s: %v", v, ts) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 712 | log.Infof("Processing test suite %s", v) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 713 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 714 | 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")) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 721 | // 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 744 | 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) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 747 | } |
| 748 | f.Close() |
| 749 | } else { |
| 750 | log.Errorf("Couldn't create file %s : %v", suiteDir+"/main.go", err) |
| 751 | } |
| 752 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 753 | log.Infof("Copying over common modules") |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 754 | 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) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 757 | } |
| 758 | f.Close() |
| 759 | } else { |
| 760 | log.Errorf("Couldn't create file %s : %v", suiteDir+"/stats.go", err) |
| 761 | } |
| 762 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 763 | log.Infof("Compiling test suite: %s in directory %s", v, suiteDir) |
| 764 | if err := os.Chdir(suiteDir); err != nil { |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 765 | log.Errorf("Could not change to directory '%s':%v", suiteDir, err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 766 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 767 | cmd := exec.Command("go", "build", "-o", outDir+"/"+v+".e") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 768 | 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 775 | log.Errorf("Could not change to directory '%s':%v", "../../suites", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 776 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 777 | } |
| 778 | return nil |
| 779 | |
| 780 | } |
| 781 | |
| 782 | func 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 788 | 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) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 793 | } |
| 794 | f.Close() |
| 795 | } else { |
| 796 | log.Errorf("Couldn't create file %s : %v", srcDir+"/runAll.go", err) |
| 797 | } |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 798 | 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) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 801 | } |
| 802 | f.Close() |
| 803 | } else { |
| 804 | log.Errorf("Couldn't create file %s : %v", srcDir+"/stats.go", err) |
| 805 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 806 | |
| 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 Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 820 | log.Errorf("Could not change to directory 'driver':%v", err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | return nil |
| 824 | } |
| 825 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 826 | func main() { |
| 827 | |
David Bainbridge | ec4ff51 | 2019-04-19 18:59:40 +0000 | [diff] [blame] | 828 | conf, err := parseCmd() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 829 | 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 | } |