google.golang.org/grpc@v1.72.2/xds/internal/xdsclient/pool/pool_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 pool_test
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  	"testing"
    25  
    26  	"github.com/google/uuid"
    27  	"google.golang.org/grpc/internal/grpctest"
    28  	"google.golang.org/grpc/internal/testutils"
    29  	"google.golang.org/grpc/internal/testutils/stats"
    30  	"google.golang.org/grpc/internal/xds/bootstrap"
    31  	"google.golang.org/grpc/xds/internal/xdsclient"
    32  )
    33  
    34  type s struct {
    35  	grpctest.Tester
    36  }
    37  
    38  func Test(t *testing.T) {
    39  	grpctest.RunSubTests(t, s{})
    40  }
    41  
    42  // TestDefaultPool_LazyLoadBootstrapConfig verifies that the DefaultPool
    43  // lazily loads the bootstrap configuration from environment variables when
    44  // an xDS client is created for the first time.
    45  //
    46  // If tries to create the new client in DefaultPool at the start of test when
    47  // none of the env vars are set. This should fail.
    48  //
    49  // Then it sets the env var XDSBootstrapFileName and retry creating a client
    50  // in DefaultPool. This should succeed.
    51  func (s) TestDefaultPool_LazyLoadBootstrapConfig(t *testing.T) {
    52  	_, closeFunc, err := xdsclient.DefaultPool.NewClient(t.Name(), &stats.NoopMetricsRecorder{})
    53  	if err == nil {
    54  		t.Fatalf("xdsclient.DefaultPool.NewClient() succeeded without setting bootstrap config env vars, want failure")
    55  	}
    56  
    57  	bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{
    58  		Servers: []byte(fmt.Sprintf(`[{
    59  			 "server_uri": %q,
    60  			 "channel_creds": [{"type": "insecure"}]
    61  		 }]`, "non-existent-management-server")),
    62  		Node: []byte(fmt.Sprintf(`{"id": "%s"}`, uuid.New().String())),
    63  		CertificateProviders: map[string]json.RawMessage{
    64  			"cert-provider-instance": json.RawMessage("{}"),
    65  		},
    66  	})
    67  	if err != nil {
    68  		t.Fatalf("Failed to create bootstrap configuration: %v", err)
    69  	}
    70  
    71  	testutils.CreateBootstrapFileForTesting(t, bs)
    72  	if cfg := xdsclient.DefaultPool.BootstrapConfigForTesting(); cfg != nil {
    73  		t.Fatalf("DefaultPool.BootstrapConfigForTesting() = %v, want nil", cfg)
    74  	}
    75  
    76  	_, closeFunc, err = xdsclient.DefaultPool.NewClient(t.Name(), &stats.NoopMetricsRecorder{})
    77  	if err != nil {
    78  		t.Fatalf("Failed to create xDS client: %v", err)
    79  	}
    80  	defer func() {
    81  		closeFunc()
    82  		xdsclient.DefaultPool.UnsetBootstrapConfigForTesting()
    83  	}()
    84  
    85  	if xdsclient.DefaultPool.BootstrapConfigForTesting() == nil {
    86  		t.Fatalf("DefaultPool.BootstrapConfigForTesting() = nil, want non-nil")
    87  	}
    88  }