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