Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy the existing gcloud config directory if it exists. #1761

Merged
merged 11 commits into from
Aug 21, 2024
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 "configurations" subdirectory of the context-specified
igorpeshansky marked this conversation as resolved.
Show resolved Hide resolved
// 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 out, err := runCommand(ctx, log.New(io.Discard, "", 0), nil, []string{"cp", "-r", filepath.Join(currentConfigDir, "."), filepath.Join(directory, ".")}, nil); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you adding a /. on the end of currentConfigDir and directory?

Seems unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /. on the source directory is to ensure that only the contents are copied (https://askubuntu.com/a/1151667). The /. on the target directory is to ensure that it exists (and is a directory).

return fmt.Errorf("error copying directory contents: %s (%w)", out.Stderr, err)
igorpeshansky marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -983,7 +983,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
Loading