google.golang.org/grpc@v1.62.1/test/xds/xds_client_integration_test.go (about) 1 /* 2 * 3 * Copyright 2021 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 xds_test 20 21 import ( 22 "context" 23 "fmt" 24 "testing" 25 "time" 26 27 "google.golang.org/grpc" 28 "google.golang.org/grpc/credentials/insecure" 29 "google.golang.org/grpc/internal/grpctest" 30 "google.golang.org/grpc/internal/stubserver" 31 "google.golang.org/grpc/internal/testutils" 32 "google.golang.org/grpc/internal/testutils/xds/e2e" 33 34 testgrpc "google.golang.org/grpc/interop/grpc_testing" 35 testpb "google.golang.org/grpc/interop/grpc_testing" 36 ) 37 38 type s struct { 39 grpctest.Tester 40 } 41 42 func Test(t *testing.T) { 43 grpctest.RunSubTests(t, s{}) 44 } 45 46 const ( 47 defaultTestTimeout = 10 * time.Second 48 defaultTestShortTimeout = 10 * time.Millisecond // For events expected to *not* happen. 49 ) 50 51 func (s) TestClientSideXDS(t *testing.T) { 52 managementServer, nodeID, _, resolver, cleanup1 := e2e.SetupManagementServer(t, e2e.ManagementServerOptions{}) 53 defer cleanup1() 54 55 server := stubserver.StartTestService(t, nil) 56 defer server.Stop() 57 58 const serviceName = "my-service-client-side-xds" 59 resources := e2e.DefaultClientResources(e2e.ResourceParams{ 60 DialTarget: serviceName, 61 NodeID: nodeID, 62 Host: "localhost", 63 Port: testutils.ParsePort(t, server.Address), 64 SecLevel: e2e.SecurityLevelNone, 65 }) 66 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 67 defer cancel() 68 if err := managementServer.Update(ctx, resources); err != nil { 69 t.Fatal(err) 70 } 71 72 // Create a ClientConn and make a successful RPC. 73 cc, err := grpc.Dial(fmt.Sprintf("xds:///%s", serviceName), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithResolvers(resolver)) 74 if err != nil { 75 t.Fatalf("failed to dial local test server: %v", err) 76 } 77 defer cc.Close() 78 79 client := testgrpc.NewTestServiceClient(cc) 80 if _, err := client.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil { 81 t.Fatalf("rpc EmptyCall() failed: %v", err) 82 } 83 }