blob: 2c65f094136f4601e2593fc7c9bc51c7e87307bb [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 (
sslobodrd6e07e72019-01-31 16:07:20 -050021 "errors"
22 "encoding/json"
sslobodrd9daabf2019-02-05 13:14:21 -050023 "golang.org/x/net/context"
24 "google.golang.org/grpc/metadata"
sslobodrd6e07e72019-01-31 16:07:20 -050025 "github.com/opencord/voltha-go/common/log"
26 {{range .Imports}}
27 _ "{{.}}"
28 {{end}}
29 {{range .ProtoImports}}
30 {{.Short}} "{{.Package}}"
31 {{end}}
32)
33
sslobodrd9daabf2019-02-05 13:14:21 -050034func 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
sslobodrd6e07e72019-01-31 16:07:20 -050052func runTests() {
53 {{range $k,$v := .Tests}}
54 if err := test{{$k}}(); err == nil {
sslobodrd9daabf2019-02-05 13:14:21 -050055 resFile.testLog("\tTest Successful\n")
sslobodrd6e07e72019-01-31 16:07:20 -050056 } else {
sslobodrd9daabf2019-02-05 13:14:21 -050057 resFile.testLog("\tTest Failed\n")
sslobodrd6e07e72019-01-31 16:07:20 -050058 }
sslobodrd9daabf2019-02-05 13:14:21 -050059 resetChannels()
sslobodrd6e07e72019-01-31 16:07:20 -050060 {{end}}
sslobodrd6e07e72019-01-31 16:07:20 -050061}
62
63{{range $k,$v := .Tests}}
64func test{{$k}}() error {
65 var rtrn error = nil
66 // Announce the test being run
sslobodrd9daabf2019-02-05 13:14:21 -050067 resFile.testLog("******************** Running test case: {{$v.Name}}\n")
sslobodrd6e07e72019-01-31 16:07:20 -050068 // 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 {
sslobodrd9daabf2019-02-05 13:14:21 -050075 err := errors.New("Server '{{$s.Name}}' is nil")
76 log.Error(err)
77 return err
sslobodrd6e07e72019-01-31 16:07:20 -050078 }
sslobodrd9daabf2019-02-05 13:14:21 -050079 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 }
sslobodrd6e07e72019-01-31 16:07:20 -050086 {{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 {
sslobodrd9daabf2019-02-05 13:14:21 -050092 // 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,
sslobodrd6e07e72019-01-31 16:07:20 -0500103 &{{$v.Send.ParamType}}{{$v.Send.Param}},
sslobodrd9daabf2019-02-05 13:14:21 -0500104 string(expct), expectMd); err != nil {
sslobodrd6e07e72019-01-31 16:07:20 -0500105 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}}"]
sslobodrd9daabf2019-02-05 13:14:21 -0500121 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)
sslobodrd6e07e72019-01-31 16:07:20 -0500126 rtrn = errors.New("Failed")
127 }
sslobodrd9daabf2019-02-05 13:14:21 -0500128 {{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)
sslobodrd6e07e72019-01-31 16:07:20 -0500140 }
141 {{end}}
sslobodrd6e07e72019-01-31 16:07:20 -0500142
143 return rtrn
144}
145{{end}}
146
147