Skip to content

Commit

Permalink
use go-vcr package
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Mar 19, 2024
1 parent 2946446 commit 5b0da2d
Show file tree
Hide file tree
Showing 5 changed files with 596 additions and 9 deletions.
47 changes: 38 additions & 9 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"testing"
"time"

"gopkg.in/dnaeon/go-vcr.v3/recorder"

"github.com/go-logr/logr"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -74,6 +76,8 @@ type Harness struct {
// gcpAccessToken is set to the oauth2 token to use for GCP, primarily when GCP is mocked.
gcpAccessToken string
kccConfig kccmanager.Config

VCRRecorder *recorder.Recorder
}

type httpRoundTripperKeyType int
Expand Down Expand Up @@ -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" {
t.Logf("creating vcr test")
} else {
t.Fatalf("E2E_GCP_TARGET=%q not supported", targetGCP)
}
Expand Down Expand Up @@ -305,17 +311,40 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
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
if t := ctx.Value(httpRoundTripperKey); t != nil {
ret = &http.Client{Transport: t.(http.RoundTripper)}
if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "vcr" {
transport_tpg.DefaultHTTPClientTransformer = 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)}
}
dir := "pkg/test/resourcefixture/testdata/vcr/cassette/"
testName := strings.ReplaceAll(t.Name(), "/", "_")
opts := &recorder.Options{
CassetteName: filepath.Join(dir, testName),
Mode: recorder.ModeRecordOnly,
RealTransport: ret.Transport,
}
r, err := recorder.NewWithOptions(opts)
if err != nil {
t.Fatalf("[VCR] Failed create vcr recorder: %v", err)
}
h.VCRRecorder = r
ret = &http.Client{Transport: h.VCRRecorder}
return ret
}
if len(eventSinks) != 0 {
t := test.NewHTTPRecorder(ret.Transport, eventSinks...)
ret = &http.Client{Transport: t}
} else {
// 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 {
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
}

// Intercept (and log) TF oauth requests
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
golang.org/x/time v0.5.0
google.golang.org/api v0.160.0
google.golang.org/protobuf v1.33.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.27.11
k8s.io/apiextensions-apiserver v0.27.9
Expand Down
4 changes: 4 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b0da2d

Please sign in to comment.