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 | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 21 | "fmt" |
| 22 | "time" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 23 | "errors" |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 24 | "context" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 25 | "encoding/json" |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 26 | //"golang.org/x/net/context" |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc/metadata" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 28 | "github.com/opencord/voltha-go/common/log" |
| 29 | {{range .Imports}} |
| 30 | _ "{{.}}" |
| 31 | {{end}} |
| 32 | {{range .ProtoImports}} |
| 33 | {{.Short}} "{{.Package}}" |
| 34 | {{end}} |
| 35 | ) |
| 36 | |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 37 | var glCtx context.Context |
| 38 | |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 39 | func resetChannels() { |
| 40 | // Drain all channels of data |
| 41 | for _,v := range servers { |
| 42 | done := false |
| 43 | for { |
| 44 | select { |
| 45 | case _ = <-v.incmg: |
| 46 | case _ = <-v.replyData: |
| 47 | default: |
| 48 | done = true |
| 49 | } |
| 50 | if done == true { |
| 51 | break |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 57 | func runTests() { |
| 58 | {{range $k,$v := .Tests}} |
| 59 | if err := test{{$k}}(); err == nil { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 60 | resFile.testLog("\tTest Successful\n") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 61 | } else { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 62 | resFile.testLog("\tTest Failed\n") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 63 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 64 | //resetChannels() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 65 | {{end}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | {{range $k,$v := .Tests}} |
| 69 | func test{{$k}}() error { |
| 70 | var rtrn error = nil |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 71 | var cancel context.CancelFunc |
| 72 | |
| 73 | glCtx, cancel = context.WithTimeout(context.Background(), 900*time.Millisecond) |
| 74 | defer cancel() |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 75 | // Announce the test being run |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 76 | resFile.testLog("******************** Running test case ({{$k}}): {{$v.Name}}\n") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 77 | // Acquire the client used to run the test |
| 78 | cl := clients["{{$v.Send.Client}}"] |
| 79 | // Create the server's reply data structure |
| 80 | repl := &reply{repl:&{{$v.Send.ExpectType}}{{$v.Send.Expect}}} |
| 81 | // Send the reply data structure to each of the servers |
| 82 | {{range $s := .Srvr}} |
| 83 | if servers["{{$s.Name}}"] == nil { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 84 | err := errors.New("Server '{{$s.Name}}' is nil") |
| 85 | log.Error(err) |
| 86 | return err |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 87 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 88 | // Start a go routine to send the the reply data to the |
| 89 | // server. The go routine blocks until the server picks |
| 90 | // up the data or the timeout is exceeded. |
| 91 | go func (ctx context.Context) { |
| 92 | select { |
| 93 | case servers["{{$s.Name}}"].replyData <- repl: |
| 94 | case <-ctx.Done(): |
| 95 | rtrn := errors.New("Could not provide server {{$s.Name}} with reply data") |
| 96 | log.Error(rtrn) |
| 97 | resFile.testLog("%s\n", rtrn.Error()) |
| 98 | } |
| 99 | }(glCtx) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 100 | {{end}} |
| 101 | |
| 102 | // Now call the RPC with the data provided |
| 103 | if expct,err := json.Marshal(repl.repl); err != nil { |
| 104 | log.Errorf("Marshaling the reply for test {{$v.Name}}: %v",err) |
| 105 | } else { |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 106 | // Create the context for the call |
| 107 | ctx := context.Background() |
| 108 | {{range $m := $v.Send.MetaData}} |
| 109 | ctx = metadata.AppendToOutgoingContext(ctx, "{{$m.Key}}", "{{$m.Val}}") |
| 110 | {{end}} |
| 111 | var md map[string]string = make(map[string]string) |
| 112 | {{range $m := $v.Send.ExpectMeta}} |
| 113 | md["{{$m.Key}}"] = "{{$m.Val}}" |
| 114 | {{end}} |
| 115 | expectMd := metadata.New(md) |
| 116 | if err := cl.send("{{$v.Send.Method}}", ctx, |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 117 | &{{$v.Send.ParamType}}{{$v.Send.Param}}, |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 118 | string(expct), expectMd); err != nil { |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 119 | log.Errorf("Test case {{$v.Name}} failed!: %v", err) |
| 120 | |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Now read the servers' information to validate it |
| 125 | var s *serverCtl |
| 126 | var payload string |
| 127 | var i *incoming |
| 128 | if pld, err := json.Marshal(&{{$v.Send.ParamType}}{{$v.Send.Param}}); err != nil { |
| 129 | log.Errorf("Marshaling paramter for test {{$v.Name}}: %v", err) |
| 130 | } else { |
| 131 | payload = string(pld) |
| 132 | } |
| 133 | {{range $s := .Srvr}} |
| 134 | s = servers["{{$s.Name}}"] |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 135 | // Oddly sometimes the data isn't in the channel yet when we come to read it. |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 136 | select { |
| 137 | case i = <-s.incmg: |
| 138 | if i.payload != payload { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 139 | rtrn = errors.New(fmt.Sprintf("Mismatched payload expected '%s', got '%s'", payload, i.payload)) |
| 140 | log.Error(rtrn.Error()) |
| 141 | resFile.testLog("%s\n", rtrn.Error()) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 142 | } |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 143 | {{range $m := $s.Meta}} |
| 144 | if mv,ok := i.meta["{{$m.Key}}"]; ok == true { |
| 145 | if "{{$m.Val}}" != mv[0] { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 146 | rtrn=errors.New(fmt.Sprintf("Mismatched metadata on server '%s' expected '%s', got '%s'", "{{$s.Name}}", "{{$m.Val}}", mv[0])) |
| 147 | log.Error(rtrn.Error()) |
| 148 | resFile.testLog("%s\n", rtrn.Error()) |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | {{end}} |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 152 | case <-glCtx.Done(): |
| 153 | rtrn = errors.New("Timeout: no response data available for server {{$s.Name}}") |
| 154 | resFile.testLog("%s\n", rtrn.Error()) |
| 155 | log.Error(rtrn) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 156 | } |
| 157 | {{end}} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 158 | |
| 159 | return rtrn |
| 160 | } |
| 161 | {{end}} |
| 162 | |
| 163 | |