Skip to content

Commit

Permalink
Fix vcr when resource is enabled for direct controller
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Sep 10, 2024
1 parent 45b6174 commit 78490ff
Show file tree
Hide file tree
Showing 50 changed files with 445 additions and 388 deletions.
19 changes: 11 additions & 8 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type Harness struct {

Project testgcp.GCPProject

VCRRecorderDCL *recorder.Recorder
VCRRecorderNonTF *recorder.Recorder
VCRRecorderTF *recorder.Recorder
VCRRecorderOauth *recorder.Recorder

Expand Down Expand Up @@ -401,28 +401,30 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio
path := filepath.Join(h.options.VCRPath, "_vcr_cassettes")
// In replay mode, RealTransport is unnecessary because we simply replay existing cassettes.
opts := &recorder.Options{
CassetteName: filepath.Join(path, "dcl"),
CassetteName: filepath.Join(path, "nontf"),
Mode: vcrMode,
}
// In record mode, use the real GCP HTTP client's transport as the recorder's transport.
// This way, the recorder is able to capture the real request/response pairs.
if inputMode == "record" {
// Intercept (and log) DCL and direct(non TF) requests
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)
t.Fatalf("error creating the http client to be not used by TF: %v", err)
}
kccConfig.HTTPClient = httpClient
}
opts.RealTransport = kccConfig.HTTPClient.Transport
}
r, err := recorder.NewWithOptions(opts)
if err != nil {
t.Fatalf("[VCR] Failed create DCL vcr recorder: %v", err)
t.Fatalf("[VCR] Failed create non TF vcr recorder: %v", err)
}
h.VCRRecorderDCL = r
kccConfig.HTTPClient = &http.Client{Transport: h.VCRRecorderDCL}
h.VCRRecorderNonTF = r
kccConfig.HTTPClient = &http.Client{Transport: h.VCRRecorderNonTF}

// Intercept (and log) TF requests
transport_tpg.DefaultHTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
ret := inner
if t := ctx.Value(httpRoundTripperKey); t != nil {
Expand All @@ -441,6 +443,7 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio
ret = &http.Client{Transport: h.VCRRecorderTF}
return ret
}
// Intercept (and log) OAuth requests
transport_tpg.OAuth2HTTPClientTransformer = func(ctx context.Context, inner *http.Client) *http.Client {
ret := inner
if t := ctx.Value(httpRoundTripperKey); t != nil {
Expand Down Expand Up @@ -496,7 +499,7 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio

transport_tpg.GRPCUnaryClientInterceptor = grpcUnaryInterceptor

// Intercept (and log) DCL requests
// Intercept (and log) DCL and direct(non TF) requests
if len(eventSinks) != 0 {
if kccConfig.HTTPClient == nil {
httpClient, err := google.DefaultClient(ctx, gcp.ClientScopes...)
Expand Down Expand Up @@ -793,7 +796,7 @@ func MaybeSkip(t *testing.T, name string, resources []*unstructured.Unstructured
case "networkconnectivityhub":
case "networkservicesgrpcroute":
case "osconfigguestpolicy":
case "pubsubsubscription":
case "basicpubsubsubscription":
case "pubsublitereservation":
case "androidrecaptchaenterprisekey":
case "redisinstance":
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/kccmanager/kccmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Config struct {
// but UserProjectOverride is set to true, resource project will be used.
BillingProject string

// HTTPClient is the http client to use for DCL.
// HTTPClient is the http client to use by KCC.
// Currently only used in tests.
HTTPClient *http.Client

Expand Down

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/README.VCRTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ E2E_KUBE_TARGET=envtest RUN_E2E=1 E2E_GCP_TARGET=vcr \
Test name `service` is a common substring that can be used by many tests, regex
`^service$` ensures that only the specified test will run.

Three files will be generated: tf.yaml, dcl.yaml and oauth.yaml. Requests from
Three files will be generated: tf.yaml, nontf.yaml and oauth.yaml. Requests from
different http clients will be saved into different files.

## Replay Mode
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ func runScenario(ctx context.Context, t *testing.T, testPause bool, fixture reso
uniqueID = strconv.FormatUint(hash(t.Name()), 36)
// Stop recording after tests finish and write to cassette
t.Cleanup(func() {
err := h.VCRRecorderDCL.Stop()
err := h.VCRRecorderNonTF.Stop()
if err != nil {
t.Errorf("[VCR] Failed stop DCL vcr recorder: %v", err)
t.Errorf("[VCR] Failed stop non TF vcr recorder: %v", err)
}
err = h.VCRRecorderTF.Stop()
if err != nil {
Expand Down Expand Up @@ -1033,7 +1033,7 @@ func configureVCR(t *testing.T, h *create.Harness) {

return nil
}
h.VCRRecorderDCL.AddHook(hook, recorder.BeforeSaveHook)
h.VCRRecorderNonTF.AddHook(hook, recorder.BeforeSaveHook)
h.VCRRecorderTF.AddHook(hook, recorder.BeforeSaveHook)
h.VCRRecorderOauth.AddHook(hook, recorder.BeforeSaveHook)

Expand Down Expand Up @@ -1073,7 +1073,7 @@ func configureVCR(t *testing.T, h *create.Harness) {
}
return true
}
h.VCRRecorderDCL.SetMatcher(matcher)
h.VCRRecorderNonTF.SetMatcher(matcher)
h.VCRRecorderTF.SetMatcher(matcher)
h.VCRRecorderOauth.SetMatcher(matcher)
}
Expand Down

0 comments on commit 78490ff

Please sign in to comment.