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: use go-vcr package to setup vcr recorder #1346

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions go.sum

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

Loading
Loading