blob: 61ba5eb79e263c80075f84b36d9270de0cd2e863 [file] [log] [blame]
sslobodrd6e07e72019-01-31 16:07:20 -05001/*
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
18package main
19
20import (
sslobodr13182842019-02-08 14:40:30 -050021 "fmt"
22 "time"
sslobodrd6e07e72019-01-31 16:07:20 -050023 "errors"
sslobodr13182842019-02-08 14:40:30 -050024 "context"
sslobodrd6e07e72019-01-31 16:07:20 -050025 "encoding/json"
sslobodr13182842019-02-08 14:40:30 -050026 //"golang.org/x/net/context"
sslobodrd9daabf2019-02-05 13:14:21 -050027 "google.golang.org/grpc/metadata"
sslobodrd6e07e72019-01-31 16:07:20 -050028 "github.com/opencord/voltha-go/common/log"
29 {{range .Imports}}
30 _ "{{.}}"
31 {{end}}
32 {{range .ProtoImports}}
33 {{.Short}} "{{.Package}}"
34 {{end}}
35)
36
sslobodr13182842019-02-08 14:40:30 -050037var glCtx context.Context
38
sslobodrd9daabf2019-02-05 13:14:21 -050039func 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
sslobodrd6e07e72019-01-31 16:07:20 -050057func runTests() {
58 {{range $k,$v := .Tests}}
59 if err := test{{$k}}(); err == nil {
sslobodrd9daabf2019-02-05 13:14:21 -050060 resFile.testLog("\tTest Successful\n")
sslobodrd6e07e72019-01-31 16:07:20 -050061 } else {
sslobodrd9daabf2019-02-05 13:14:21 -050062 resFile.testLog("\tTest Failed\n")
sslobodrd6e07e72019-01-31 16:07:20 -050063 }
sslobodr13182842019-02-08 14:40:30 -050064 //resetChannels()
sslobodrd6e07e72019-01-31 16:07:20 -050065 {{end}}
sslobodrd6e07e72019-01-31 16:07:20 -050066}
67
68{{range $k,$v := .Tests}}
69func test{{$k}}() error {
70 var rtrn error = nil
sslobodr13182842019-02-08 14:40:30 -050071 var cancel context.CancelFunc
72
73 glCtx, cancel = context.WithTimeout(context.Background(), 900*time.Millisecond)
74 defer cancel()
sslobodrd6e07e72019-01-31 16:07:20 -050075 // Announce the test being run
sslobodr13182842019-02-08 14:40:30 -050076 resFile.testLog("******************** Running test case ({{$k}}): {{$v.Name}}\n")
sslobodrd6e07e72019-01-31 16:07:20 -050077 // 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 {
sslobodrd9daabf2019-02-05 13:14:21 -050084 err := errors.New("Server '{{$s.Name}}' is nil")
85 log.Error(err)
86 return err
sslobodrd6e07e72019-01-31 16:07:20 -050087 }
sslobodr13182842019-02-08 14:40:30 -050088 // 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)
sslobodrd6e07e72019-01-31 16:07:20 -0500100 {{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 {
sslobodrd9daabf2019-02-05 13:14:21 -0500106 // 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,
sslobodrd6e07e72019-01-31 16:07:20 -0500117 &{{$v.Send.ParamType}}{{$v.Send.Param}},
sslobodrd9daabf2019-02-05 13:14:21 -0500118 string(expct), expectMd); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500119 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}}"]
sslobodr13182842019-02-08 14:40:30 -0500135 // Oddly sometimes the data isn't in the channel yet when we come to read it.
sslobodrd9daabf2019-02-05 13:14:21 -0500136 select {
137 case i = <-s.incmg:
138 if i.payload != payload {
sslobodr13182842019-02-08 14:40:30 -0500139 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())
sslobodrd6e07e72019-01-31 16:07:20 -0500142 }
sslobodrd9daabf2019-02-05 13:14:21 -0500143 {{range $m := $s.Meta}}
144 if mv,ok := i.meta["{{$m.Key}}"]; ok == true {
145 if "{{$m.Val}}" != mv[0] {
sslobodr13182842019-02-08 14:40:30 -0500146 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())
sslobodrd9daabf2019-02-05 13:14:21 -0500149 }
150 }
151 {{end}}
sslobodr13182842019-02-08 14:40:30 -0500152 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)
sslobodrd6e07e72019-01-31 16:07:20 -0500156 }
157 {{end}}
sslobodrd6e07e72019-01-31 16:07:20 -0500158
159 return rtrn
160}
161{{end}}
162
163