Skip to content

Commit

Permalink
Capture VCR record
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Apr 1, 2024
1 parent c60188b commit a1ec9c0
Show file tree
Hide file tree
Showing 9 changed files with 16,670 additions and 88 deletions.
87 changes: 47 additions & 40 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
t.Logf("targeting real GCP")
} else if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "vcr" {
t.Logf("creating vcr test")
h.gcpAccessToken = "dummytoken"
kccConfig.GCPAccessToken = h.gcpAccessToken
} else {
t.Fatalf("E2E_GCP_TARGET=%q not supported", targetGCP)
}
Expand Down Expand Up @@ -297,39 +299,15 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
}
testgcp.TestKCCAttachedClusterProject.Set("mock-project")
h.Project = project
} else if os.Getenv("E2E_GCP_TARGET") == "vcr" && os.Getenv("VCR_MODE") == "replay" {
h.Project = testgcp.GCPProject{
ProjectID: "example-project",
ProjectNumber: 12345678,
}
} else {
h.Project = testgcp.GetDefaultProject(t)
}

eventSink := test.NewMemoryEventSink()
ctx = test.AddSinkToContext(ctx, eventSink)
h.Ctx = ctx

h.Events = eventSink

eventSinks := test.EventSinksFromContext(ctx)

// Set up event sink for logging to a file, if ARTIFACTS env var is set
if artifacts := os.Getenv("ARTIFACTS"); artifacts != "" {
outputDir := filepath.Join(artifacts, "http-logs")
eventSinks = append(eventSinks, test.NewDirectoryEventSink(outputDir))
} else {
log.Info("env var ARTIFACTS is not set; will not record http log")
}

// Intercept (and log) DCL requests
if len(eventSinks) != 0 {
if kccConfig.HTTPClient == nil {
httpClient, err := google.DefaultClient(ctx, gcp.ClientScopes...)
if err != nil {
t.Fatalf("error creating the http client to be used by DCL: %v", err)
}
kccConfig.HTTPClient = httpClient
}
t := test.NewHTTPRecorder(kccConfig.HTTPClient.Transport, eventSinks...)
kccConfig.HTTPClient = &http.Client{Transport: t}
}

if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "vcr" {
// Initialize VCR for TF requests
input := os.Getenv("VCR_MODE")
Expand Down Expand Up @@ -362,6 +340,35 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
return ret
}
} else {
eventSink := test.NewMemoryEventSink()
ctx = test.AddSinkToContext(ctx, eventSink)
h.Ctx = ctx

h.Events = eventSink

eventSinks := test.EventSinksFromContext(ctx)

// Set up event sink for logging to a file, if ARTIFACTS env var is set
if artifacts := os.Getenv("ARTIFACTS"); artifacts != "" {
outputDir := filepath.Join(artifacts, "http-logs")
eventSinks = append(eventSinks, test.NewDirectoryEventSink(outputDir))
} else {
log.Info("env var ARTIFACTS is not set; will not record http log")
}

// Intercept (and log) DCL requests
if len(eventSinks) != 0 {
if kccConfig.HTTPClient == nil {
httpClient, err := google.DefaultClient(ctx, gcp.ClientScopes...)
if err != nil {
t.Fatalf("error creating the http client to be used by DCL: %v", err)
}
kccConfig.HTTPClient = httpClient
}
t := test.NewHTTPRecorder(kccConfig.HTTPClient.Transport, eventSinks...)
kccConfig.HTTPClient = &http.Client{Transport: t}
}

// Intercept (and log) TF requests
transport_tpg.DefaultHTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
ret := inner
Expand All @@ -374,19 +381,19 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
}
return ret
}
}

// Intercept (and log) TF oauth requests
transport_tpg.OAuth2HTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
ret := inner
if t := ctx.Value(httpRoundTripperKey); t != nil {
ret = &http.Client{Transport: t.(http.RoundTripper)}
}
if len(eventSinks) != 0 {
t := test.NewHTTPRecorder(ret.Transport, eventSinks...)
ret = &http.Client{Transport: t}
// Intercept (and log) TF oauth requests
transport_tpg.OAuth2HTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
ret := inner
if t := ctx.Value(httpRoundTripperKey); t != nil {
ret = &http.Client{Transport: t.(http.RoundTripper)}
}
if len(eventSinks) != 0 {
t := test.NewHTTPRecorder(ret.Transport, eventSinks...)
ret = &http.Client{Transport: t}
}
return ret
}
return ret
}

h.kccConfig = kccConfig
Expand Down
Loading

0 comments on commit a1ec9c0

Please sign in to comment.