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

Conversation

igorpeshansky
Copy link
Member

@igorpeshansky igorpeshansky commented Jul 25, 2024

Description

Copy the existing gcloud config directory if it exists. This will avoid errors in situations where testing relies on this configuration, e.g., when running integration tests locally with cached credentials. A follow-on to #1750.

Related issue

b/352054649

How has this been tested?

Integration tests.

Checklist:

  • Unit tests
    • Unit tests do not apply.
    • Unit tests have been added/modified and passed for this PR.
  • Integration tests
    • Integration tests do not apply.
    • Integration tests have been added/modified and passed for this PR.
  • Documentation
    • This PR introduces no user visible changes.
    • This PR introduces user visible changes and the corresponding documentation change has been made.
  • Minor version bump
    • This PR introduces no new features.
    • This PR introduces new features, and there is a separate PR to bump the minor version since the last release already.
    • This PR bumps the version.

@igorpeshansky igorpeshansky force-pushed the igorpeshansky-gcloud-config-context branch 2 times, most recently from 59f822c to dd962fb Compare July 25, 2024 16:23
This is needed to run integration tests locally.
@igorpeshansky igorpeshansky force-pushed the igorpeshansky-gcloud-config-context branch from dd962fb to 47be625 Compare July 25, 2024 23:56
@igorpeshansky igorpeshansky marked this pull request as ready for review July 25, 2024 23:59
if err != nil {
return err
}
sfi, err := os.Stat(currentConfigDir)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a lot of code! Stackoverflow says that go 1.23 will likely get a function to do (approximately) this, but in the meantime, why not just runCommand() a "cp --recursive" command? Do we really need to copy only these relevantFiles or something?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I saw golang issue 62484, but we're not on 1.23 yet… The good news is that, when we do upgrade, it'll look like this:

func SetupGcloudConfigDir(ctx context.Context, directory string) error {
        currentConfigDir, err := getGcloudConfigDir(ctx)
        if err != nil {
                return err
        }
        err = os.CopyFS(directory, os.DirFS(currentConfigDir))
        if os.IsNotExist(err) {
                return nil
        }
        return err
}

Regarding using cp, this was my first thought too, but, sadly, this needs to work on Windows as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

why does this need to work on windows as well? I'm pretty sure it doesn't... even when testing Windows VMs, the kokoro worker is still a Linux machine, described here: https://github.com/GoogleCloudPlatform/ops-agent/blob/6cbd2f752094bdeb44a54ac7b96300dc82774e05/kokoro/scripts/test/go_test.Dockerfile

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, it doesn't need to work on Windows after all. I didn't think of that initially. That said, once we've upgraded to Go 1.23, the code to invoke os.CopyFS (above) would be ~same as the code to invoke runCommand(…, []string{"cp", "--recursive"}, …), and, IMO, less hacky. But I'll rewrite using "cp -r" if you insist.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I was proposing:

  • cp -r for now, with a TODO I guess
  • replace that with os.CopyFS when it becomes available, because it's less hacky

This saves us about 50+ lines of code of hand-rolled directory copying code. Like for example, defer in.Close() drops errors from closing files, but we don't need to hash that out if you are just calling cp -r. So yeah I think cp -r is worth doing, even if only to save time going back and forth in reviews.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
@igorpeshansky igorpeshansky force-pushed the igorpeshansky-gcloud-config-context branch from 8e2e5e1 to 69bde8e Compare August 7, 2024 01:17
Copy link
Contributor

@martijnvans martijnvans left a comment

Choose a reason for hiding this comment

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

some nits left, nothing much

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).

integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
integration_test/gce/gce_testing.go Outdated Show resolved Hide resolved
@igorpeshansky igorpeshansky merged commit 7760f01 into master Aug 21, 2024
20 of 34 checks passed
@igorpeshansky igorpeshansky deleted the igorpeshansky-gcloud-config-context branch August 21, 2024 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants