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 12, 2024
1 parent b2efd1b commit e69df2b
Show file tree
Hide file tree
Showing 5 changed files with 577 additions and 9 deletions.
44 changes: 35 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"
"github.com/google/go-cmp/cmp"
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
Expand Down Expand Up @@ -75,6 +77,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

Rec *recorder.Recorder
}

type httpRoundTripperKeyType int
Expand Down Expand Up @@ -237,6 +241,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 @@ -306,17 +312,37 @@ 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 := "vcr/fixtures/cassette/"
testName := strings.ReplaceAll(t.Name(), "/", "_")
opts := &recorder.Options{
CassetteName: dir + testName,
Mode: recorder.ModeRecordOnly,
RealTransport: ret.Transport,
}
r, _ := recorder.NewWithOptions(opts)
h.Rec = r
ret = &http.Client{Transport: h.Rec}
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.32.0
gopkg.in/dnaeon/go-vcr.v3 v3.1.2
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.

10 changes: 10 additions & 0 deletions tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func TestAllInSeries(t *testing.T) {
h := create.NewHarness(ctx, t)
project := h.Project

if os.Getenv("E2E_GCP_TARGET") == "vcr" {
// Stop recording after tests finish and write to cassette
defer func() {
err := h.Rec.Stop()
if err != nil {
t.Errorf("[VCR] Failed stop vcr recorder: %v", err)
}
}()
}

primaryResource, opt := loadFixture(project)

exportResources := []*unstructured.Unstructured{primaryResource}
Expand Down
Loading

0 comments on commit e69df2b

Please sign in to comment.