|
| 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 | +"encoding/json" |
| 23 | +"fmt" |
| 24 | +"testing" |
| 25 | + |
| 26 | +"github.com/google/uuid" |
| 27 | +"google.golang.org/grpc/internal/envconfig" |
| 28 | +"google.golang.org/grpc/internal/testutils/stats" |
| 29 | +"google.golang.org/grpc/internal/xds/bootstrap" |
| 30 | +) |
| 31 | + |
| 32 | +// TestDefaultPool_LazyLoadBootstrapConfig verifies that the DefaultPool |
| 33 | +// lazily loads the bootstrap configuration from environment variables when |
| 34 | +// an xDS client is created for the first time. |
| 35 | +func (s) TestDefaultPool_LazyLoadBootstrapConfig(t *testing.T) { |
| 36 | +bs, err := bootstrap.NewContentsForTesting(bootstrap.ConfigOptionsForTesting{ |
| 37 | +Servers: []byte(fmt.Sprintf(`[{ |
| 38 | +"server_uri": %q, |
| 39 | +"channel_creds": [{"type": "insecure"}] |
| 40 | +}]`, "non-existent-management-server")), |
| 41 | +Node: []byte(fmt.Sprintf(`{"id": "%s"}`, uuid.New().String())), |
| 42 | +CertificateProviders: map[string]json.RawMessage{ |
| 43 | +"cert-provider-instance": json.RawMessage("{}"), |
| 44 | +}, |
| 45 | +}) |
| 46 | +if err != nil { |
| 47 | +t.Fatalf("Failed to create bootstrap configuration: %v", err) |
| 48 | +} |
| 49 | + |
| 50 | +origBootstrapContent := envconfig.XDSBootstrapFileContent |
| 51 | +envconfig.XDSBootstrapFileContent = string(bs) |
| 52 | +defer func() { envconfig.XDSBootstrapFileContent = origBootstrapContent }() |
| 53 | + |
| 54 | +if DefaultPool.BootstrapConfigForTesting() != nil { |
| 55 | +t.Fatalf("DefaultPool.BootstrapConfigForTesting() = %v, want nil", DefaultPool.BootstrapConfigForTesting()) |
| 56 | +} |
| 57 | + |
| 58 | +_, closeFunc, err := DefaultPool.NewClient(t.Name(), &stats.NoopMetricsRecorder{}) |
| 59 | +if err != nil { |
| 60 | +t.Fatalf("Failed to create xDS client: %v", err) |
| 61 | +} |
| 62 | +defer func() { |
| 63 | +closeFunc() |
| 64 | +DefaultPool.UnsetBootstrapConfigForTesting() |
| 65 | +}() |
| 66 | + |
| 67 | +if DefaultPool.BootstrapConfigForTesting() == nil { |
| 68 | +t.Fatalf("DefaultPool.BootstrapConfigForTesting() = nil, want %v", DefaultPool.BootstrapConfigForTesting()) |
| 69 | +} |
| 70 | +} |
0 commit comments