google.golang.org/grpc@v1.72.2/internal/testutils/xds_bootstrap.go (about) 1 /* 2 * 3 * Copyright 2024 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 testutils 20 21 import ( 22 "os" 23 "testing" 24 25 "google.golang.org/grpc/internal/envconfig" 26 ) 27 28 // CreateBootstrapFileForTesting creates a temporary file with the provided 29 // bootstrap contents, and updates the bootstrap environment variable to point 30 // to this file. 31 // 32 // Registers a cleanup function on the provided testing.T, that deletes the 33 // temporary file and resets the bootstrap environment variable. 34 func CreateBootstrapFileForTesting(t *testing.T, bootstrapContents []byte) { 35 t.Helper() 36 37 f, err := os.CreateTemp("", "test_xds_bootstrap_*") 38 if err != nil { 39 t.Fatalf("Failed to created bootstrap file: %v", err) 40 } 41 42 if err := os.WriteFile(f.Name(), bootstrapContents, 0644); err != nil { 43 t.Fatalf("Failed to created bootstrap file: %v", err) 44 } 45 46 origBootstrapFileName := envconfig.XDSBootstrapFileName 47 envconfig.XDSBootstrapFileName = f.Name() 48 t.Cleanup(func() { 49 os.Remove(f.Name()) 50 envconfig.XDSBootstrapFileName = origBootstrapFileName 51 }) 52 }