google.golang.org/grpc@v1.74.2/xds/internal/clients/xdsclient/xdsclient_test.go (about) 1 /* 2 * 3 * Copyright 2025 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 19 package xdsclient 20 21 import ( 22 "strings" 23 "testing" 24 25 "google.golang.org/grpc/credentials/insecure" 26 "google.golang.org/grpc/xds/internal/clients" 27 "google.golang.org/grpc/xds/internal/clients/grpctransport" 28 "google.golang.org/grpc/xds/internal/clients/xdsclient/internal/xdsresource" 29 ) 30 31 func (s) TestXDSClient_New(t *testing.T) { 32 configs := map[string]grpctransport.Config{"insecure": {Credentials: insecure.NewBundle()}} 33 34 tests := []struct { 35 name string 36 config Config 37 wantErr string 38 }{ 39 { 40 name: "empty node ID", 41 config: Config{}, 42 wantErr: "node ID is empty", 43 }, 44 { 45 name: "nil resource types", 46 config: Config{ 47 Node: clients.Node{ID: "node-id"}, 48 }, 49 wantErr: "resource types map is nil", 50 }, 51 { 52 name: "nil transport builder", 53 config: Config{ 54 Node: clients.Node{ID: "node-id"}, 55 ResourceTypes: map[string]ResourceType{xdsresource.V3ListenerURL: listenerType}, 56 }, 57 wantErr: "transport builder is nil", 58 }, 59 { 60 name: "no servers or authorities", 61 config: Config{ 62 Node: clients.Node{ID: "node-id"}, 63 ResourceTypes: map[string]ResourceType{xdsresource.V3ListenerURL: listenerType}, 64 TransportBuilder: grpctransport.NewBuilder(configs), 65 }, 66 wantErr: "no servers or authorities specified", 67 }, 68 { 69 name: "success with servers", 70 config: Config{ 71 Node: clients.Node{ID: "node-id"}, 72 ResourceTypes: map[string]ResourceType{xdsresource.V3ListenerURL: listenerType}, 73 TransportBuilder: grpctransport.NewBuilder(configs), 74 Servers: []ServerConfig{{ServerIdentifier: clients.ServerIdentifier{ServerURI: "dummy-server"}}}, 75 }, 76 wantErr: "", 77 }, 78 { 79 name: "success with authorities", 80 config: Config{ 81 Node: clients.Node{ID: "node-id"}, 82 ResourceTypes: map[string]ResourceType{xdsresource.V3ListenerURL: listenerType}, 83 TransportBuilder: grpctransport.NewBuilder(configs), 84 Authorities: map[string]Authority{"authority-name": {XDSServers: []ServerConfig{{ServerIdentifier: clients.ServerIdentifier{ServerURI: "dummy-server"}}}}}, 85 }, 86 wantErr: "", 87 }, 88 } 89 for _, tt := range tests { 90 t.Run(tt.name, func(t *testing.T) { 91 c, err := New(tt.config) 92 if tt.wantErr == "" { 93 if err != nil { 94 t.Fatalf("New(%+v) failed: %v", tt.config, err) 95 } 96 } else { 97 if err == nil || !strings.Contains(err.Error(), tt.wantErr) { 98 t.Fatalf("New(%+v) returned error %v, want error %q", tt.config, err, tt.wantErr) 99 } 100 } 101 if c != nil { 102 c.Close() 103 } 104 }) 105 } 106 } 107 108 func (s) TestXDSClient_Close(t *testing.T) { 109 configs := map[string]grpctransport.Config{"insecure": {Credentials: insecure.NewBundle()}} 110 config := Config{ 111 Node: clients.Node{ID: "node-id"}, 112 ResourceTypes: map[string]ResourceType{xdsresource.V3ListenerURL: listenerType}, 113 TransportBuilder: grpctransport.NewBuilder(configs), 114 Servers: []ServerConfig{{ServerIdentifier: clients.ServerIdentifier{ServerURI: "dummy-server"}}}, 115 } 116 c, err := New(config) 117 if err != nil { 118 t.Fatalf("New(%+v) failed: %v", config, err) 119 } 120 c.Close() 121 // Calling close again should not panic. 122 c.Close() 123 }