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

vcr: Apply hook in vcr recorder #1349

Merged
merged 3 commits into from
Mar 25, 2024

Conversation

gemmahou
Copy link
Collaborator

@gemmahou gemmahou commented Mar 12, 2024

Change description

ref: https://github.com/dnaeon/go-vcr?tab=readme-ov-file#hooks

The hook will replace values in request/response pairs before we save them to file.

Note: Applying hook to replace data that we don't want to expose to public. Some data, like timestamp, or id, does not contain sensitive information, so it's fine to leave them for now.

If we can migrate VCR testing as a part of prow job, and save cassette file to GCS bucket, then we may not need hooks since it's a private storage.

Tests you have done

  • Run make ready-pr to ensure this PR is ready for review.
  • Perform necessary E2E testing for changed resources.

config/tests/samples/create/harness.go Outdated Show resolved Hide resolved
tests/e2e/unified_test.go Show resolved Hide resolved
@gemmahou gemmahou changed the title Apply hook in vcr recorder vcr: Apply hook in vcr recorder Mar 13, 2024
@justinsb justinsb added this to the Future milestone Mar 14, 2024
@gemmahou gemmahou force-pushed the vcr-hook branch 3 times, most recently from b048a7b to 9eef7ca Compare March 15, 2024 02:58
@gemmahou gemmahou modified the milestones: Future, 1.115 Mar 19, 2024
Copy link
Collaborator

@maqiuyujoyce maqiuyujoyce left a comment

Choose a reason for hiding this comment

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

/lgtm

All comments are non-blocking. Would be great to see the demo!

config/tests/samples/create/harness.go Outdated Show resolved Hide resolved
@@ -236,6 +240,8 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
kccConfig.GCPAccessToken = h.gcpAccessToken
} else if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "real" {
t.Logf("targeting real GCP")
} else if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "vcr" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Non-blocking future improvement idea: Can we have all the environment variables and valid values used by integration tests documented and maintained somewhere? As someone who rarely touches the code here, it's hard for me to figure out what environment variables I should set and why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I created b/332719149 to address this comment.

ret := inner
if t := ctx.Value(httpRoundTripperKey); t != nil {
ret = &http.Client{Transport: t.(http.RoundTripper)}
if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "vcr" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Non-blocking refactoring idea:
Can we do one targetGCP := os.Getenv("E2E_GCP_TARGET") in the beginning of the function, and then do switch case/if else for different conditions in line233-247, line314 and one, etc?

Repetitively retrieving the same environment seems to reduce the readability.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this will be done in b/332719149 as well.

if t := ctx.Value(httpRoundTripperKey); t != nil {
ret = &http.Client{Transport: t.(http.RoundTripper)}
}
dir := "pkg/test/resourcefixture/testdata/vcr/cassette/"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious: Is cassette a special code name? I personally don't know much about VCR and its concepts so would like to learn more context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

recorder and cassette are both VCR concepts. Similar to the VCR recorder in the real world, this test package simulates the workflow, recording HTTP interactions instead of audio (😂), and saving them in a file, which is used to replay them later.
TBH, the folder name can be anything (tape, episode, etc., 😂), I just borrowed the name as it's a known concept for storing recorded logs.

tests/e2e/unified_test.go Outdated Show resolved Hide resolved
tests/e2e/unified_test.go Show resolved Hide resolved
@google-oss-prow google-oss-prow bot added the lgtm label Mar 21, 2024
@justinsb
Copy link
Collaborator

Thanks @gemmahou

/approve
/lgtm

@google-oss-prow google-oss-prow bot added the lgtm label Mar 25, 2024
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-prow google-oss-prow bot merged commit 0dc8eef into GoogleCloudPlatform:master Mar 25, 2024
8 checks passed
@gemmahou gemmahou deleted the vcr-hook branch March 25, 2024 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants