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