From 62f73ecd84b14e5092e0ec0ea956a3be35b9faac Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 8 Dec 2021 10:53:49 -0800 Subject: [PATCH] xds/client: move xdsclient.New tests to controller.New (#5037) --- .../xdsclient/controller/controller_test.go | 102 ++++++++++++++++++ xds/internal/xdsclient/xdsclient_test.go | 87 --------------- 2 files changed, 102 insertions(+), 87 deletions(-) create mode 100644 xds/internal/xdsclient/controller/controller_test.go diff --git a/xds/internal/xdsclient/controller/controller_test.go b/xds/internal/xdsclient/controller/controller_test.go new file mode 100644 index 00000000000..b2d9c225d8e --- /dev/null +++ b/xds/internal/xdsclient/controller/controller_test.go @@ -0,0 +1,102 @@ +/* + * + * Copyright 2021 gRPC authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package controller + +import ( + "testing" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/xds/internal/testutils" + "google.golang.org/grpc/xds/internal/xdsclient/bootstrap" + "google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version" +) + +const testXDSServer = "xds-server" + +// TestNew covers that New() returns an error if the input *ServerConfig +// contains invalid content. +func (s) TestNew(t *testing.T) { + tests := []struct { + name string + config *bootstrap.ServerConfig + wantErr bool + }{ + { + name: "empty-opts", + config: &bootstrap.ServerConfig{}, + wantErr: true, + }, + { + name: "empty-balancer-name", + config: &bootstrap.ServerConfig{ + Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), + NodeProto: testutils.EmptyNodeProtoV2, + }, + wantErr: true, + }, + { + name: "empty-dial-creds", + config: &bootstrap.ServerConfig{ + ServerURI: testXDSServer, + NodeProto: testutils.EmptyNodeProtoV2, + }, + wantErr: true, + }, + { + name: "empty-node-proto", + config: &bootstrap.ServerConfig{ + ServerURI: testXDSServer, + Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), + }, + wantErr: true, + }, + { + name: "node-proto-version-mismatch", + config: &bootstrap.ServerConfig{ + ServerURI: testXDSServer, + Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), + TransportAPI: version.TransportV2, + NodeProto: testutils.EmptyNodeProtoV3, + }, + wantErr: true, + }, + { + name: "happy-case", + config: &bootstrap.ServerConfig{ + ServerURI: testXDSServer, + Creds: grpc.WithInsecure(), + NodeProto: testutils.EmptyNodeProtoV2, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + c, err := New(test.config, nil, nil, nil) // Only testing the config, other inputs are left as nil. + defer func() { + if c != nil { + c.Close() + } + }() + if (err != nil) != test.wantErr { + t.Fatalf("New(%+v) = %v, wantErr: %v", test.config, err, test.wantErr) + } + }) + } +} diff --git a/xds/internal/xdsclient/xdsclient_test.go b/xds/internal/xdsclient/xdsclient_test.go index 92423a59f29..68ecd88acc4 100644 --- a/xds/internal/xdsclient/xdsclient_test.go +++ b/xds/internal/xdsclient/xdsclient_test.go @@ -20,16 +20,8 @@ package xdsclient_test import ( "testing" - "time" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/internal/grpctest" - "google.golang.org/grpc/xds/internal/testutils" - "google.golang.org/grpc/xds/internal/xdsclient" - "google.golang.org/grpc/xds/internal/xdsclient/bootstrap" - "google.golang.org/grpc/xds/internal/xdsclient/xdsresource/version" - _ "google.golang.org/grpc/xds/internal/xdsclient/controller/version/v2" // Register the v2 API client. ) @@ -42,82 +34,3 @@ func Test(t *testing.T) { } const testXDSServer = "xds-server" - -func (s) TestNew(t *testing.T) { - tests := []struct { - name string - config *bootstrap.Config - wantErr bool - }{ - { - name: "empty-opts", - config: &bootstrap.Config{}, - wantErr: true, - }, - { - name: "empty-balancer-name", - config: &bootstrap.Config{ - XDSServer: &bootstrap.ServerConfig{ - Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), - NodeProto: testutils.EmptyNodeProtoV2, - }, - }, - wantErr: true, - }, - { - name: "empty-dial-creds", - config: &bootstrap.Config{ - XDSServer: &bootstrap.ServerConfig{ - ServerURI: testXDSServer, - NodeProto: testutils.EmptyNodeProtoV2, - }, - }, - wantErr: true, - }, - { - name: "empty-node-proto", - config: &bootstrap.Config{ - XDSServer: &bootstrap.ServerConfig{ - ServerURI: testXDSServer, - Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), - }, - }, - wantErr: true, - }, - { - name: "node-proto-version-mismatch", - config: &bootstrap.Config{ - XDSServer: &bootstrap.ServerConfig{ - ServerURI: testXDSServer, - Creds: grpc.WithTransportCredentials(insecure.NewCredentials()), - TransportAPI: version.TransportV2, - NodeProto: testutils.EmptyNodeProtoV3, - }, - }, - wantErr: true, - }, - // TODO(easwars): Add cases for v3 API client. - { - name: "happy-case", - config: &bootstrap.Config{ - XDSServer: &bootstrap.ServerConfig{ - ServerURI: testXDSServer, - Creds: grpc.WithInsecure(), - NodeProto: testutils.EmptyNodeProtoV2, - }, - }, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - c, err := xdsclient.NewWithConfigForTesting(test.config, 15*time.Second) - if (err != nil) != test.wantErr { - t.Fatalf("New(%+v) = %v, wantErr: %v", test.config, err, test.wantErr) - } - if c != nil { - c.Close() - } - }) - } -}