Skip to content

Commit

Permalink
Copy the existing gcloud config directory if it exists. (#1761)
Browse files Browse the repository at this point in the history
  • Loading branch information
igorpeshansky authored Aug 21, 2024
1 parent 0f03a99 commit 7760f01
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
6 changes: 5 additions & 1 deletion integration_test/agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,11 @@ func CommonSetupWithExtraCreateArgumentsAndMetadata(t *testing.T, imageSpec stri
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), gce.SuggestedTimeout)
t.Cleanup(cancel)
ctx = gce.WithGcloudConfigDir(ctx, t.TempDir())
gcloudConfigDir := t.TempDir()
if err := gce.SetupGcloudConfigDir(ctx, gcloudConfigDir); err != nil {
t.Fatalf("Unable to set up a gcloud config directory: %v", err)
}
ctx = gce.WithGcloudConfigDir(ctx, gcloudConfigDir)

logger := gce.SetupLogger(t)
logger.ToMainLog().Println("Calling SetupVM(). For details, see VM_initialization.txt.")
Expand Down
25 changes: 25 additions & 0 deletions integration_test/gce/gce_testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,31 @@ func runCommand(ctx context.Context, logger *log.Logger, stdin io.Reader, args [
return output, err
}

// getGcloudConfigDir returns the current gcloud configuration directory.
func getGcloudConfigDir(ctx context.Context) (string, error) {
out, err := RunGcloud(ctx, log.New(io.Discard, "", 0), "", []string{"info", "--format=value[terminator=''](config.paths.global_config_dir)"})
if err != nil {
return "", err
}
return out.Stdout, nil
}

// SetupGcloudConfigDir sets up a new gcloud configuration directory.
// This copies the contents of the context-specified configuration directory
// into the new directory.
// This only works on Linux.
func SetupGcloudConfigDir(ctx context.Context, directory string) error {
currentConfigDir, err := getGcloudConfigDir(ctx)
if err != nil {
return err
}
// TODO: Replace with os.CopyFS() once available.
if _, err := runCommand(ctx, log.New(io.Discard, "", 0), nil, []string{"cp", "-r", filepath.Join(currentConfigDir, "."), filepath.Join(directory, ".")}, nil); err != nil {
return err
}
return nil
}

const (
gcloudConfigDirKey = "__gcloud_config_dir__"
)
Expand Down
6 changes: 5 additions & 1 deletion integration_test/third_party_apps_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,11 @@ func TestThirdPartyApps(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), gce.SuggestedTimeout)
defer cancel()
ctx = gce.WithGcloudConfigDir(ctx, t.TempDir())
gcloudConfigDir := t.TempDir()
if err := gce.SetupGcloudConfigDir(ctx, gcloudConfigDir); err != nil {
t.Fatalf("Unable to set up a gcloud config directory: %v", err)
}
ctx = gce.WithGcloudConfigDir(ctx, gcloudConfigDir)

var err error
for attempt := 1; attempt <= 4; attempt++ {
Expand Down

0 comments on commit 7760f01

Please sign in to comment.