google.golang.org/grpc@v1.62.1/xds/internal/xdsclient/transport/transport_new_test.go (about) 1 /* 2 * 3 * Copyright 2022 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package transport_test 19 20 import ( 21 "strings" 22 "testing" 23 24 "google.golang.org/grpc/xds/internal/testutils" 25 "google.golang.org/grpc/xds/internal/xdsclient/bootstrap" 26 "google.golang.org/grpc/xds/internal/xdsclient/transport" 27 28 v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" 29 ) 30 31 // TestNew covers that New() returns an error if the input *ServerConfig 32 // contains invalid content. 33 func (s) TestNew(t *testing.T) { 34 tests := []struct { 35 name string 36 opts transport.Options 37 wantErrStr string 38 }{ 39 { 40 name: "missing server URI", 41 opts: transport.Options{ServerCfg: bootstrap.ServerConfig{}}, 42 wantErrStr: "missing server URI when creating a new transport", 43 }, 44 { 45 name: "missing credentials", 46 opts: transport.Options{ServerCfg: bootstrap.ServerConfig{ServerURI: "server-address"}}, 47 wantErrStr: "missing credentials when creating a new transport", 48 }, 49 { 50 name: "missing onRecv handler", 51 opts: transport.Options{ 52 ServerCfg: *testutils.ServerConfigForAddress(t, "server-address"), 53 NodeProto: &v3corepb.Node{}, 54 }, 55 wantErrStr: "missing OnRecv callback handler when creating a new transport", 56 }, 57 { 58 name: "missing onError handler", 59 opts: transport.Options{ 60 ServerCfg: *testutils.ServerConfigForAddress(t, "server-address"), 61 NodeProto: &v3corepb.Node{}, 62 OnRecvHandler: func(transport.ResourceUpdate) error { return nil }, 63 OnSendHandler: func(*transport.ResourceSendInfo) {}, 64 }, 65 wantErrStr: "missing OnError callback handler when creating a new transport", 66 }, 67 68 { 69 name: "missing onSend handler", 70 opts: transport.Options{ 71 ServerCfg: *testutils.ServerConfigForAddress(t, "server-address"), 72 NodeProto: &v3corepb.Node{}, 73 OnRecvHandler: func(transport.ResourceUpdate) error { return nil }, 74 OnErrorHandler: func(error) {}, 75 }, 76 wantErrStr: "missing OnSend callback handler when creating a new transport", 77 }, 78 { 79 name: "happy case", 80 opts: transport.Options{ 81 ServerCfg: *testutils.ServerConfigForAddress(t, "server-address"), 82 NodeProto: &v3corepb.Node{}, 83 OnRecvHandler: func(transport.ResourceUpdate) error { return nil }, 84 OnErrorHandler: func(error) {}, 85 OnSendHandler: func(*transport.ResourceSendInfo) {}, 86 }, 87 }, 88 } 89 90 for _, test := range tests { 91 t.Run(test.name, func(t *testing.T) { 92 c, err := transport.New(test.opts) 93 defer func() { 94 if c != nil { 95 c.Close() 96 } 97 }() 98 if (err != nil) != (test.wantErrStr != "") { 99 t.Fatalf("New(%+v) = %v, wantErr: %v", test.opts, err, test.wantErrStr) 100 } 101 if err != nil && !strings.Contains(err.Error(), test.wantErrStr) { 102 t.Fatalf("New(%+v) = %v, wantErr: %v", test.opts, err, test.wantErrStr) 103 } 104 }) 105 } 106 }