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 | // The template for the tester. |
| 17 | // This template is filled in by the |
| 18 | // test driver based on the configuration. |
| 19 | |
| 20 | package main |
| 21 | |
| 22 | import ( |
| 23 | "fmt" |
| 24 | "net" |
| 25 | "errors" |
| 26 | "encoding/json" |
| 27 | "google.golang.org/grpc" |
| 28 | "golang.org/x/net/context" |
| 29 | "google.golang.org/grpc/metadata" |
Scott Baker | ec6e61b | 2019-10-23 10:21:27 -0700 | [diff] [blame^] | 30 | "github.com/opencord/voltha-lib-go/pkg/log" |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 31 | // Values generated by the go template |
| 32 | {{range .Imports}} |
| 33 | "{{.}}" |
| 34 | {{end}} |
| 35 | {{range .ProtoImports}} |
| 36 | {{.Short}} "{{.Package}}" |
| 37 | {{end}} |
| 38 | // End go template values |
| 39 | ) |
| 40 | |
| 41 | // The channels to get fed the expected results by the test driver. |
| 42 | //var {{.Name}}Meta <-chan string |
| 43 | ////var {{.Name}}Payload <-chan string |
| 44 | |
| 45 | {{if .Ct}}{{else}} |
| 46 | type reply struct { |
| 47 | repl interface{} |
| 48 | } |
| 49 | type incoming struct { |
| 50 | meta metadata.MD |
| 51 | payload string |
| 52 | } |
| 53 | type serverCtl struct { |
| 54 | replyData chan * reply |
| 55 | incmg chan * incoming |
| 56 | } |
| 57 | {{end}} |
| 58 | |
| 59 | type {{.Name}}TestServer struct { |
| 60 | control *serverCtl |
| 61 | srv *grpc.Server |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | func init() { |
| 66 | {{if .Ct}}{{else}} |
| 67 | if _, err := log.SetDefaultLogger(log.JSON, 0, nil); err != nil { |
| 68 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 69 | } |
| 70 | {{end}} |
| 71 | {{.Name}}ListenAndServe() |
| 72 | } |
| 73 | */ |
| 74 | |
| 75 | |
| 76 | func {{.Name}}ListenAndServe() error { |
| 77 | var s {{.Name}}TestServer |
| 78 | |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 79 | s.control = &serverCtl{replyData:make(chan *reply), incmg:make(chan *incoming)} |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 80 | servers["{{.Name}}"] = s.control |
| 81 | |
| 82 | log.Debugf("Starting server %s on port %d", "{{.Name}}", {{.Port}}) |
| 83 | // THe test head always uses localhost 127.0.0.1 |
| 84 | addr := fmt.Sprintf("127.0.0.1:%d", {{.Port}}) |
| 85 | |
| 86 | // Create the gRPC server |
| 87 | s.srv = grpc.NewServer() |
| 88 | |
| 89 | {{range .ProtoImports}} |
| 90 | // Register the handler object |
| 91 | {{.Short}}.Register{{.Service}}Server(s.srv, s) |
| 92 | {{end}} |
| 93 | |
| 94 | // Create the channel to listen on |
| 95 | lis, err := net.Listen("tcp", addr) |
| 96 | if err != nil { |
| 97 | log.Errorf("could not listen on %s: %s", addr, err) |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // Serve and Listen |
| 102 | go func() { |
| 103 | if err = s.srv.Serve(lis); err != nil { |
| 104 | log.Errorf("grpc serve error: %s", err) |
| 105 | return |
| 106 | } |
| 107 | }() |
| 108 | |
| 109 | return err |
| 110 | } |
| 111 | |
| 112 | {{range .Methods}} |
| 113 | {{if .Ss}} |
| 114 | func (ts {{$.Name}}TestServer) {{.Name}}(in *{{.Param}}, srvr {{.Pkg}}.{{.Svc}}_{{.Name}}Server) error { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 115 | log.Debug("Serving server streaming {{$.Name}}") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 116 | return nil |
| 117 | } |
| 118 | {{else if .Cs}} |
| 119 | func (ts {{$.Name}}TestServer) {{.Name}}({{.Pkg}}.{{.Svc}}_{{.Name}}Server) error { |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 120 | log.Debug("Serving client streaming {{$.Name}}") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 121 | return nil |
| 122 | } |
| 123 | {{else}} |
| 124 | func (ts {{$.Name}}TestServer) {{.Name}}(ctx context.Context, in *{{.Param}}) (*{{.Rtrn}}, error) { |
| 125 | var r * incoming = &incoming{} |
sslobodr | 1d1e50b | 2019-03-14 09:17:40 -0400 | [diff] [blame] | 126 | //log.Debug("Serving {{$.Name}}") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 127 | // Read the metadata |
| 128 | if md,ok := metadata.FromIncomingContext(ctx); ok == false { |
| 129 | log.Error("Getting matadata during call to {{.Name}}") |
| 130 | } else { |
| 131 | r.meta = md.Copy() |
| 132 | } |
| 133 | // Read the data sent to the function |
| 134 | if parm,err := json.Marshal(in); err != nil { |
| 135 | log.Error("Marshalling the {{.Param}} for {{.Name}}") |
| 136 | } else { |
| 137 | r.payload = string(parm) |
| 138 | } |
| 139 | // Send the server information back to the test framework |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 140 | go func(ctx context.Context) { |
| 141 | select { |
| 142 | case ts.control.incmg <- r: |
| 143 | return |
| 144 | case <-ctx.Done(): |
| 145 | return |
| 146 | |
| 147 | } |
| 148 | }(glCtx) |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 149 | // Read the value that needs to be returned from the channel |
sslobodr | d9daabf | 2019-02-05 13:14:21 -0500 | [diff] [blame] | 150 | select { |
| 151 | case d := <-ts.control.replyData: |
| 152 | switch r := d.repl.(type) { |
| 153 | case *{{.Rtrn}}: |
| 154 | return r, nil |
| 155 | default: |
| 156 | return nil, errors.New("Mismatched type in call to {{.Name}}") |
| 157 | } |
sslobodr | 1318284 | 2019-02-08 14:40:30 -0500 | [diff] [blame] | 158 | case <-glCtx.Done(): |
| 159 | return nil, errors.New("Timeout: nothing in the reply data channel in call to {{.Name}}, sending nil") |
sslobodr | d6e07e7 | 2019-01-31 16:07:20 -0500 | [diff] [blame] | 160 | } |
| 161 | return &{{.Rtrn}}{},nil |
| 162 | } |
| 163 | {{end}} |
| 164 | {{end}} |
| 165 | |