gitee.com/zhaochuninhefei/gmgo@v0.0.31-0.20240209061119-069254a02979/grpc/xds/internal/test/xds_integration_test.go (about)

     1  //go:build !386
     2  // +build !386
     3  
     4  /*
     5   *
     6   * Copyright 2020 gRPC authors.
     7   *
     8   * Licensed under the Apache License, Version 2.0 (the "License");
     9   * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   *     http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   */
    21  
    22  // Package xds_test contains e2e tests for xDS use.
    23  package xds_test
    24  
    25  import (
    26  	"context"
    27  	"encoding/json"
    28  	"fmt"
    29  	"io/ioutil"
    30  	"os"
    31  	"path"
    32  	"testing"
    33  	"time"
    34  
    35  	"gitee.com/zhaochuninhefei/gmgo/x509"
    36  
    37  	tls "gitee.com/zhaochuninhefei/gmgo/gmtls"
    38  
    39  	"github.com/google/uuid"
    40  
    41  	"gitee.com/zhaochuninhefei/gmgo/grpc/credentials"
    42  	"gitee.com/zhaochuninhefei/gmgo/grpc/internal/grpctest"
    43  	"gitee.com/zhaochuninhefei/gmgo/grpc/resolver"
    44  	"gitee.com/zhaochuninhefei/gmgo/grpc/testdata"
    45  	"gitee.com/zhaochuninhefei/gmgo/grpc/xds"
    46  	"gitee.com/zhaochuninhefei/gmgo/grpc/xds/internal/testutils/e2e"
    47  
    48  	xdsinternal "gitee.com/zhaochuninhefei/gmgo/grpc/internal/xds"
    49  	testpb "gitee.com/zhaochuninhefei/gmgo/grpc/test/grpc_testing"
    50  )
    51  
    52  const (
    53  	defaultTestTimeout      = 10 * time.Second
    54  	defaultTestShortTimeout = 100 * time.Millisecond
    55  )
    56  
    57  type s struct {
    58  	grpctest.Tester
    59  }
    60  
    61  func Test(t *testing.T) {
    62  	grpctest.RunSubTests(t, s{})
    63  }
    64  
    65  type testService struct {
    66  	testpb.TestServiceServer
    67  }
    68  
    69  func (*testService) EmptyCall(context.Context, *testpb.Empty) (*testpb.Empty, error) {
    70  	return &testpb.Empty{}, nil
    71  }
    72  
    73  func (*testService) UnaryCall(context.Context, *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
    74  	return &testpb.SimpleResponse{}, nil
    75  }
    76  
    77  func createTmpFile(src, dst string) error {
    78  	data, err := ioutil.ReadFile(src)
    79  	if err != nil {
    80  		return fmt.Errorf("ioutil.ReadFile(%q) failed: %v", src, err)
    81  	}
    82  	if err := ioutil.WriteFile(dst, data, os.ModePerm); err != nil {
    83  		return fmt.Errorf("ioutil.WriteFile(%q) failed: %v", dst, err)
    84  	}
    85  	return nil
    86  }
    87  
    88  // createTempDirWithFiles creates a temporary directory under the system default
    89  // tempDir with the given dirSuffix. It also reads from certSrc, keySrc and
    90  // rootSrc files are creates appropriate files under the newly create tempDir.
    91  // Returns the name of the created tempDir.
    92  func createTmpDirWithFiles(dirSuffix, certSrc, keySrc, rootSrc string) (string, error) {
    93  	// Create a temp directory. Passing an empty string for the first argument
    94  	// uses the system temp directory.
    95  	dir, err := ioutil.TempDir("", dirSuffix)
    96  	if err != nil {
    97  		return "", fmt.Errorf("ioutil.TempDir() failed: %v", err)
    98  	}
    99  
   100  	if err := createTmpFile(testdata.Path(certSrc), path.Join(dir, certFile)); err != nil {
   101  		return "", err
   102  	}
   103  	if err := createTmpFile(testdata.Path(keySrc), path.Join(dir, keyFile)); err != nil {
   104  		return "", err
   105  	}
   106  	if err := createTmpFile(testdata.Path(rootSrc), path.Join(dir, rootFile)); err != nil {
   107  		return "", err
   108  	}
   109  	return dir, nil
   110  }
   111  
   112  // createClientTLSCredentials creates client-side TLS transport credentials.
   113  func createClientTLSCredentials(t *testing.T) credentials.TransportCredentials {
   114  	t.Helper()
   115  
   116  	cert, err := tls.LoadX509KeyPair(testdata.Path("x509/client1_cert.pem"), testdata.Path("x509/client1_key.pem"))
   117  	if err != nil {
   118  		t.Fatalf("tls.LoadX509KeyPair(x509/client1_cert.pem, x509/client1_key.pem) failed: %v", err)
   119  	}
   120  	b, err := ioutil.ReadFile(testdata.Path("x509/server_ca_cert.pem"))
   121  	if err != nil {
   122  		t.Fatalf("ioutil.ReadFile(x509/server_ca_cert.pem) failed: %v", err)
   123  	}
   124  	roots := x509.NewCertPool()
   125  	if !roots.AppendCertsFromPEM(b) {
   126  		t.Fatal("failed to append certificates")
   127  	}
   128  	return credentials.NewTLS(&tls.Config{
   129  		Certificates: []tls.Certificate{cert},
   130  		RootCAs:      roots,
   131  		ServerName:   "x.test.example.com",
   132  	})
   133  }
   134  
   135  // setupManagement server performs the following:
   136  // - spin up an xDS management server on a local port
   137  // - set up certificates for consumption by the file_watcher plugin
   138  // - creates a bootstrap file in a temporary location
   139  // - creates an xDS resolver using the above bootstrap contents
   140  //
   141  // Returns the following:
   142  // - management server
   143  // - nodeID to be used by the client when connecting to the management server
   144  // - bootstrap contents to be used by the client
   145  // - xDS resolver builder to be used by the client
   146  // - a cleanup function to be invoked at the end of the test
   147  func setupManagementServer(t *testing.T) (*e2e.ManagementServer, string, []byte, resolver.Builder, func()) {
   148  	t.Helper()
   149  
   150  	// Spin up an xDS management server on a local port.
   151  	server, err := e2e.StartManagementServer()
   152  	if err != nil {
   153  		t.Fatalf("Failed to spin up the xDS management server: %v", err)
   154  	}
   155  	defer func() {
   156  		if err != nil {
   157  			server.Stop()
   158  		}
   159  	}()
   160  
   161  	// Create a directory to hold certs and key files used on the server side.
   162  	serverDir, err := createTmpDirWithFiles("testServerSideXDS*", "x509/server1_cert.pem", "x509/server1_key.pem", "x509/client_ca_cert.pem")
   163  	if err != nil {
   164  		server.Stop()
   165  		t.Fatal(err)
   166  	}
   167  
   168  	// Create a directory to hold certs and key files used on the client side.
   169  	clientDir, err := createTmpDirWithFiles("testClientSideXDS*", "x509/client1_cert.pem", "x509/client1_key.pem", "x509/server_ca_cert.pem")
   170  	if err != nil {
   171  		server.Stop()
   172  		t.Fatal(err)
   173  	}
   174  
   175  	// Create certificate providers section of the bootstrap config with entries
   176  	// for both the client and server sides.
   177  	cpc := map[string]json.RawMessage{
   178  		e2e.ServerSideCertProviderInstance: e2e.DefaultFileWatcherConfig(path.Join(serverDir, certFile), path.Join(serverDir, keyFile), path.Join(serverDir, rootFile)),
   179  		e2e.ClientSideCertProviderInstance: e2e.DefaultFileWatcherConfig(path.Join(clientDir, certFile), path.Join(clientDir, keyFile), path.Join(clientDir, rootFile)),
   180  	}
   181  
   182  	// Create a bootstrap file in a temporary directory.
   183  	nodeID := uuid.New().String()
   184  	bootstrapContents, err := xdsinternal.BootstrapContents(xdsinternal.BootstrapOptions{
   185  		Version:                            xdsinternal.TransportV3,
   186  		NodeID:                             nodeID,
   187  		ServerURI:                          server.Address,
   188  		CertificateProviders:               cpc,
   189  		ServerListenerResourceNameTemplate: e2e.ServerListenerResourceNameTemplate,
   190  	})
   191  	if err != nil {
   192  		server.Stop()
   193  		t.Fatalf("Failed to create bootstrap file: %v", err)
   194  	}
   195  	resolver, err := xds.NewXDSResolverWithConfigForTesting(bootstrapContents)
   196  	if err != nil {
   197  		server.Stop()
   198  		t.Fatalf("Failed to create xDS resolver for testing: %v", err)
   199  	}
   200  
   201  	return server, nodeID, bootstrapContents, resolver, func() { server.Stop() }
   202  }