blob: 3b0f6d420a957563fdd9937fcb6ccaf74d55dc2c [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 (
21 "fmt"
22 "time"
23 "errors"
24 "encoding/json"
25 "github.com/opencord/voltha-go/common/log"
26 {{range .Imports}}
27 _ "{{.}}"
28 {{end}}
29 {{range .ProtoImports}}
30 {{.Short}} "{{.Package}}"
31 {{end}}
32)
33
34func runTests() {
35 {{range $k,$v := .Tests}}
36 if err := test{{$k}}(); err == nil {
37 resFile.Write([]byte("\tTest Successful\n"))
38 } else {
39 resFile.Write([]byte("\tTest Failed\n"))
40 }
41 {{end}}
42 time.Sleep(5 * time.Second)
43}
44
45{{range $k,$v := .Tests}}
46func test{{$k}}() error {
47 var rtrn error = nil
48 // Announce the test being run
49 resFile.Write([]byte("******************** Running test case: {{$v.Name}}\n"))
50 // Acquire the client used to run the test
51 cl := clients["{{$v.Send.Client}}"]
52 // Create the server's reply data structure
53 repl := &reply{repl:&{{$v.Send.ExpectType}}{{$v.Send.Expect}}}
54 // Send the reply data structure to each of the servers
55 {{range $s := .Srvr}}
56 if servers["{{$s.Name}}"] == nil {
57 log.Error("Server '{{$s.Name}}' is nil")
58 return errors.New("GAAK")
59 }
60 servers["{{$s.Name}}"].replyData <- repl
61 {{end}}
62
63 // Now call the RPC with the data provided
64 if expct,err := json.Marshal(repl.repl); err != nil {
65 log.Errorf("Marshaling the reply for test {{$v.Name}}: %v",err)
66 } else {
67 if err := cl.send("{{$v.Send.Method}}",
68 &{{$v.Send.ParamType}}{{$v.Send.Param}},
69 string(expct)); err != nil {
70 log.Errorf("Test case {{$v.Name}} failed!: %v", err)
71
72 }
73 }
74
75 // Now read the servers' information to validate it
76 var s *serverCtl
77 var payload string
78 var i *incoming
79 if pld, err := json.Marshal(&{{$v.Send.ParamType}}{{$v.Send.Param}}); err != nil {
80 log.Errorf("Marshaling paramter for test {{$v.Name}}: %v", err)
81 } else {
82 payload = string(pld)
83 }
84 {{range $s := .Srvr}}
85 s = servers["{{$s.Name}}"]
86 i = <-s.incmg
87 if i.payload != payload {
88 log.Errorf("Mismatched payload for test {{$v.Name}}, %s:%s", i.payload, payload)
89 resFile.Write([]byte(fmt.Sprintf("Mismatched payload expected %s, got %s\n", payload, i.payload)))
90 rtrn = errors.New("Failed")
91 }
92 {{range $m := $s.Meta}}
93 if mv,ok := i.meta["{{$m.Key}}"]; ok == true {
94 if "{{$m.Val}}" != mv[0] {
95 log.Errorf("Mismatched metadata for test {{$v.Name}}, %s:%s", mv[0], "{{$m.Val}}")
96 resFile.Write([]byte(fmt.Sprintf("Mismatched metadata on server %s expected %s, got %s\n", "{{$s.Name}}", "{{$m.Val}}", mv[0])))
97 rtrn = errors.New("Failed")
98 }
99 }
100 {{end}}
101 {{end}}
102
103 return rtrn
104}
105{{end}}
106
107