From 0691318c8906b47f57f97414ed3cf7a09b6803e4 Mon Sep 17 00:00:00 2001 From: Gemma Hou Date: Thu, 15 Aug 2024 21:15:11 +0000 Subject: [PATCH] deprecate NewHarnessWithManager --- config/tests/samples/create/harness.go | 47 ++++++++++++--------- config/tests/samples/create/samples_test.go | 32 +------------- pkg/test/main/testmain.go | 2 +- tests/e2e/unified_test.go | 2 +- 4 files changed, 32 insertions(+), 51 deletions(-) diff --git a/config/tests/samples/create/harness.go b/config/tests/samples/create/harness.go index 1e41528b95..4e4af9cb9b 100644 --- a/config/tests/samples/create/harness.go +++ b/config/tests/samples/create/harness.go @@ -25,6 +25,8 @@ import ( "testing" "time" + "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" + "github.com/go-logr/logr" transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" "golang.org/x/oauth2" @@ -44,7 +46,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/envtest" "sigs.k8s.io/controller-runtime/pkg/log" - "sigs.k8s.io/controller-runtime/pkg/manager" "sigs.k8s.io/controller-runtime/pkg/webhook" "sigs.k8s.io/kubebuilder-declarative-pattern/mockkubeapiserver" @@ -97,7 +98,8 @@ type Harness struct { } type HarnessOptions struct { - VCRPath string + VCRPath string + RunSamples bool } type httpRoundTripperKeyType int @@ -105,25 +107,29 @@ type httpRoundTripperKeyType int // httpRoundTripperKey is the key value for http.RoundTripper in a context.Context var httpRoundTripperKey httpRoundTripperKeyType -// NewHarnessWithManager builds a Harness for an existing manager. -// deprecated: Prefer NewHarness, which can construct a manager and mock gcp etc. -func NewHarnessWithManager(ctx context.Context, t *testing.T, mgr manager.Manager) *Harness { - h := &Harness{ - T: t, - Ctx: ctx, - client: mgr.GetClient(), +func NewHarness(ctx context.Context, t *testing.T) *Harness { + return newHarnessWithOptions(ctx, t, &HarnessOptions{}) +} + +func NewVCRHarness(ctx context.Context, t *testing.T, path string) *Harness { + opts := &HarnessOptions{ + VCRPath: path, } - return h + return newHarnessWithOptions(ctx, t, opts) } -func NewHarness(ctx context.Context, t *testing.T) *Harness { +func NewSamplesHarness(ctx context.Context, t *testing.T) *Harness { opts := &HarnessOptions{ - VCRPath: "", + RunSamples: true, + } + err := os.Setenv("E2E_KUBE_TARGET", "envtest") + if err != nil { + t.Errorf("error setting kube target: %v", err) } - return NewHarnessWithOptions(ctx, t, opts) + return newHarnessWithOptions(ctx, t, opts) } -func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptions) *Harness { +func newHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptions) *Harness { ctx, ctxCancel := context.WithCancel(ctx) t.Cleanup(func() { ctxCancel() @@ -145,6 +151,9 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio // creating multiple managers for tests will fail if more than one // manager tries to bind to the same port. kccConfig.ManagerOptions.HealthProbeBindAddress = "0" + if h.options.RunSamples { + kccConfig.StateIntoSpecDefaultValue = k8s.StateIntoSpecDefaultValueV1Beta1 + } // configure caching nocache.OnlyCacheCCAndCCC(&kccConfig.ManagerOptions) @@ -292,15 +301,13 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio h.gcpAccessToken = "dummytoken" 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) + // if E2E_GCP_TARGET is unset, default to target real GCP + t.Logf("targeting real GCP") } if os.Getenv("E2E_GCP_TARGET") == "mock" { + t.Logf("running mock test") // Some fixed-value fake org-ids for testing. // We used fixed values so that the output is predictable (for golden testing) testgcp.TestFolderID.Set("123451001") @@ -352,6 +359,7 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio testgcp.TestKCCAttachedClusterProject.Set("mock-project") h.Project = project } else if os.Getenv("E2E_GCP_TARGET") == "vcr" && os.Getenv("VCR_MODE") == "replay" { + t.Logf("running VCR test") h.gcpAccessToken = "dummytoken" kccConfig.GCPAccessToken = h.gcpAccessToken @@ -368,6 +376,7 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio testgcp.TestBillingAccountID.Set("123456-777777-000001") testgcp.TestBillingAccountIDForBillingResources.Set("123456-777777-000003") } else { + t.Logf("running default test") h.Project = testgcp.GetDefaultProject(t) } diff --git a/config/tests/samples/create/samples_test.go b/config/tests/samples/create/samples_test.go index 2bb2955dc5..70007d61d7 100644 --- a/config/tests/samples/create/samples_test.go +++ b/config/tests/samples/create/samples_test.go @@ -23,20 +23,13 @@ import ( "regexp" "testing" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/kccmanager" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/registration" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/k8s" - "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/logging" testgcp "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/gcp" testmain "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/main" + _ "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/register" "golang.org/x/sync/semaphore" klog "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" - "sigs.k8s.io/controller-runtime/pkg/manager/signals" - - _ "github.com/GoogleCloudPlatform/k8s-config-connector/pkg/controller/direct/register" ) func init() { @@ -234,7 +227,6 @@ var testDisabledList = map[string]bool{ func TestAll(t *testing.T) { project := testgcp.GetDefaultProject(t) - setup() samples := LoadMatchingSamples(t, regexp.MustCompile(runTestsRegex), project) if len(samples) == 0 { t.Fatalf("No tests to run for pattern %s", runTestsRegex) @@ -265,7 +257,7 @@ func TestAll(t *testing.T) { ctx := context.TODO() - h := NewHarnessWithManager(ctx, t, mgr) + h := NewSamplesHarness(ctx, t) SetupNamespacesAndApplyDefaults(h, s.Resources, project) networkCount := int64(networksInSampleCount(s)) @@ -282,26 +274,6 @@ func TestAll(t *testing.T) { } } -func setup() { - ctx := context.TODO() - flag.Parse() - var err error - mgr, err = kccmanager.New(ctx, unusedManager.GetConfig(), kccmanager.Config{StateIntoSpecDefaultValue: k8s.StateIntoSpecDefaultValueV1Beta1}) - if err != nil { - logging.Fatal(err, "error creating new manager") - } - // Register the deletion defender controller - if err := registration.Add(mgr, &controller.Deps{}, registration.RegisterDeletionDefenderController); err != nil { - logging.Fatal(err, "error adding registration controller for deletion defender controllers") - } - // start the manager, Start(...) is a blocking operation so it needs to be done asynchronously - go func() { - if err := mgr.Start(signals.SetupSignalHandler()); err != nil { - logging.Fatal(err, "error starting manager") - } - }() -} - func TestMain(m *testing.M) { testmain.ForIntegrationTests(m, &unusedManager) } diff --git a/pkg/test/main/testmain.go b/pkg/test/main/testmain.go index beb902902e..e1c2b9df05 100644 --- a/pkg/test/main/testmain.go +++ b/pkg/test/main/testmain.go @@ -35,7 +35,7 @@ import ( func ForIntegrationTests(m *testing.M, mgr *manager.Manager) { if os.Getenv("E2E_GCP_TARGET") != "" { - log.Fatalf("dynamic integration tests do not support variable E2E_GCP_TARGET") + log.Fatalf("integration tests do not support variable E2E_GCP_TARGET") } // Since Terraform logging defers to the Go standard logger, diff --git a/tests/e2e/unified_test.go b/tests/e2e/unified_test.go index febac6e36c..5367108d87 100644 --- a/tests/e2e/unified_test.go +++ b/tests/e2e/unified_test.go @@ -211,7 +211,7 @@ func runScenario(ctx context.Context, t *testing.T, testPause bool, fixture reso // Create test harness var h *create.Harness if os.Getenv("E2E_GCP_TARGET") == "vcr" { - h = create.NewHarnessWithOptions(ctx, t, &create.HarnessOptions{VCRPath: fixture.SourceDir}) + h = create.NewVCRHarness(ctx, t, fixture.SourceDir) hash := func(s string) uint64 { h := fnv.New64a() h.Write([]byte(s))