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 | |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | import ( |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 21 | {{if .HasFuncs}} |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 22 | "fmt" |
| 23 | "time" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 24 | "errors" |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 25 | "context" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 26 | "encoding/json" |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 27 | //"golang.org/x/net/context" |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc/metadata" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 30 | {{end}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 31 | {{range .Imports}} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 32 | {{if .Used}} |
| 33 | "{{.Package}}" |
| 34 | {{end}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 35 | {{end}} |
| 36 | {{range .ProtoImports}} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 37 | {{if .Used}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 38 | {{.Short}} "{{.Package}}" |
| 39 | {{end}} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 40 | {{end}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 41 | ) |
| 42 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 43 | |
| 44 | {{if .FileNum}} |
| 45 | {{else}} |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 46 | var glCtx context.Context |
| 47 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 48 | func resetChannels() { |
| 49 | // Drain all channels of data |
| 50 | for _,v := range servers { |
| 51 | done := false |
| 52 | for { |
| 53 | select { |
| 54 | case _ = <-v.incmg: |
| 55 | case _ = <-v.replyData: |
| 56 | default: |
| 57 | done = true |
| 58 | } |
| 59 | if done == true { |
| 60 | break |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 66 | type testData struct { |
| 67 | function func(int, string, string, []string,[]string,[]string, |
| 68 | map[string][]string,interface{}, interface{}) error |
| 69 | testNum int |
| 70 | testName string |
| 71 | sendClient string |
| 72 | srvrs []string |
| 73 | sendMeta []string |
| 74 | expectMeta []string // Send Meta |
| 75 | srvMeta map[string][]string |
| 76 | parm interface{} |
| 77 | ret interface{} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 78 | } |
| 79 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 80 | func addTestSlot(stats * TestRun) { |
| 81 | tsIdx := len(stats.TestSuites) - 1 |
| 82 | stats.TestSuites[tsIdx].TestCases = |
| 83 | append(stats.TestSuites[tsIdx].TestCases, TestCase{Info:[]string{}}) |
| 84 | } |
| 85 | {{end}} |
| 86 | |
| 87 | {{if .FileNum}} |
| 88 | func runTests{{.FileNum}}() { |
| 89 | {{else}} |
| 90 | func runTests() { |
| 91 | {{end}} |
| 92 | tsIdx := len(stats.TestSuites) - 1 |
| 93 | tests := []testData { |
| 94 | {{$ofs := .Offset}} |
| 95 | {{range $k,$v := .Tests}} |
| 96 | testData { |
| 97 | {{$v.Send.Method}}_test, |
| 98 | {{$k}} + {{$ofs}}, |
| 99 | "{{$v.Name}}", |
| 100 | "{{$v.Send.Client}}", |
| 101 | []string{ {{range $sk,$sv := $v.Srvr}} "{{$sv.Name}}",{{end}} }, |
| 102 | []string{ {{range $mk,$mv := $v.Send.MetaData}}"{{$mv.Key}}","{{$mv.Val}}",{{end}} }, |
| 103 | []string{ {{range $mk,$mv := $v.Send.ExpectMeta}}"{{$mv.Key}}","{{$mv.Val}}",{{end}} }, |
| 104 | map[string][]string { |
| 105 | {{range $sk,$sv := $v.Srvr}} |
| 106 | "{{$sv.Name}}":[]string { |
| 107 | {{range $mk, $mv := $sv.Meta}} |
| 108 | "{{$mv.Key}}","{{$mv.Val}}", |
| 109 | {{end}} |
| 110 | }, |
| 111 | {{end}} |
| 112 | }, |
| 113 | &{{$v.Send.ParamType}}{{$v.Send.Param}}, |
| 114 | &{{$v.Send.ExpectType}}{{$v.Send.Expect}}, |
| 115 | }, |
| 116 | {{end}} |
| 117 | } |
| 118 | |
| 119 | for _,v := range tests { |
| 120 | addTestSlot(stats) |
| 121 | stats.TestSuites[tsIdx].TestCases[v.testNum].Title = v.testName |
| 122 | if err := v.function( |
| 123 | v.testNum, |
| 124 | v.testName, |
| 125 | v.sendClient, |
| 126 | v.srvrs, |
| 127 | v.sendMeta, |
| 128 | v.expectMeta, |
| 129 | v.srvMeta, |
| 130 | v.parm, |
| 131 | v.ret); err != nil { |
| 132 | stats.TestSuites[tsIdx].TestCases[v.testNum].Result = false |
| 133 | } else { |
| 134 | stats.TestSuites[tsIdx].TestCases[v.testNum].Result = true |
| 135 | } |
| 136 | } |
| 137 | {{if .FileNum}} |
| 138 | {{else}} |
| 139 | {{range $k,$v := .RunTestsCallList}} |
| 140 | {{$v}}() |
| 141 | {{end}} |
| 142 | {{end}} |
| 143 | return |
| 144 | //resetChannels() |
| 145 | } |
| 146 | |
| 147 | {{range $k,$v := .Funcs }} |
| 148 | {{if $v.CodeGenerated}} |
| 149 | {{else}} |
| 150 | func {{$k}}_test(testNum int, testName string, sendClient string, srvrs []string, |
| 151 | sendMeta []string, expectMeta []string, srvrMeta map[string][]string, |
| 152 | parm interface{}, ret interface{}) error { |
| 153 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 154 | var rtrn error = nil |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 155 | var cancel context.CancelFunc |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 156 | var repl *reply |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 157 | |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 158 | log.Debug("Running Test %d",testNum) |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 159 | glCtx, cancel = context.WithTimeout(context.Background(), 900*time.Millisecond) |
| 160 | defer cancel() |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 161 | |
| 162 | cl := clients[sendClient] |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 163 | // Create the server's reply data structure |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 164 | switch r := ret.(type) { |
| 165 | case *{{$v.ReturnType}}: |
| 166 | repl = &reply{repl:r} |
| 167 | default: |
| 168 | log.Errorf("Invalid type in call to {{$k}}_test expecting {{$v.ReturnType}} got %T", ret) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 169 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 170 | // Send the reply data structure to each of the servers |
| 171 | for _,v := range srvrs { |
| 172 | if servers[v] == nil { |
| 173 | err := errors.New(fmt.Sprintf("Server %s is nil", v)) |
| 174 | log.Error(err) |
| 175 | return err |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 176 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 177 | // Start a go routine to send the the reply data to the |
| 178 | // server. The go routine blocks until the server picks |
| 179 | // up the data or the timeout is exceeded. |
| 180 | go func (ctx context.Context, srv string) { |
| 181 | select { |
| 182 | case servers[srv].replyData <- repl: |
| 183 | case <-ctx.Done(): |
| 184 | rtrn := errors.New(fmt.Sprintf("Could not provide server %s with reply data",srv)) |
| 185 | log.Error(rtrn) |
| 186 | stats.testLog("%s\n", rtrn.Error()) |
| 187 | } |
| 188 | }(glCtx,v) |
| 189 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 190 | |
| 191 | // Now call the RPC with the data provided |
| 192 | if expct,err := json.Marshal(repl.repl); err != nil { |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 193 | log.Errorf("Marshaling the reply for test %s: %v",testName, err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 194 | } else { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 195 | // Create the context for the call |
| 196 | ctx := context.Background() |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 197 | for i:=0; i<len(sendMeta); i += 2 { |
| 198 | ctx = metadata.AppendToOutgoingContext(ctx, sendMeta[i], sendMeta[i+1]) |
| 199 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 200 | var md map[string]string = make(map[string]string) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 201 | for i:=0; i<len(expectMeta); i+=2 { |
| 202 | md[expectMeta[i]] = expectMeta[i+1] |
| 203 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 204 | expectMd := metadata.New(md) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 205 | if err := cl.send("{{$k}}", ctx, parm, string(expct), expectMd); err != nil { |
| 206 | log.Errorf("Test case %s failed!: %v", testName, err) |
| 207 | rtrn = err |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | // Now read the servers' information to validate it |
| 212 | var s *serverCtl |
| 213 | var payload string |
| 214 | var i *incoming |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 215 | if pld, err := json.Marshal(parm); err != nil { |
| 216 | log.Errorf("Marshaling paramter for test %s: %v", testName, err) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 217 | } else { |
| 218 | payload = string(pld) |
| 219 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 220 | for _,v := range srvrs { |
| 221 | s = servers[v] |
| 222 | // Oddly sometimes the data isn't in the channel yet when we come to read it. |
| 223 | select { |
| 224 | case i = <-s.incmg: |
| 225 | if i.payload != payload { |
| 226 | rtrn = errors.New(fmt.Sprintf("Mismatched payload expected '%s', got '%s'", payload, i.payload)) |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 227 | log.Error(rtrn.Error()) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 228 | stats.testLog("%s\n", rtrn.Error()) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 229 | } |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 230 | for j:=0; j<len(srvrMeta[v]); j+=2 { |
| 231 | if mv,ok := i.meta[srvrMeta[v][j]]; ok == true { |
| 232 | if srvrMeta[v][j+1] != mv[0] { |
| 233 | rtrn=errors.New(fmt.Sprintf("Mismatched metadata on server '%s' expected '%s', got '%s'", srvrMeta[v][j], srvrMeta[v][j+1], mv[0])) |
| 234 | log.Error(rtrn.Error()) |
| 235 | stats.testLog("%s\n", rtrn.Error()) |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | case <-glCtx.Done(): |
Kent Hagerman | 1b9c706 | 2019-05-07 16:46:01 -0400 | [diff] [blame] | 240 | rtrn = errors.New(fmt.Sprintf("Timeout: no response data available for server %s (%s)", v, testName)) |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 241 | stats.testLog("%s\n", rtrn.Error()) |
| 242 | log.Error(rtrn) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 243 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 244 | } |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 245 | |
| 246 | return rtrn |
| 247 | } |
| 248 | {{end}} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 249 | {{end}} |