Skip to content

Commit

Permalink
deprecate NewHarnessWithManager
Browse files Browse the repository at this point in the history
  • Loading branch information
gemmahou committed Aug 15, 2024
1 parent 5ea1fff commit 0691318
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 51 deletions.
47 changes: 28 additions & 19 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand Down Expand Up @@ -97,33 +98,38 @@ type Harness struct {
}

type HarnessOptions struct {
VCRPath string
VCRPath string
RunSamples bool
}

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()
Expand All @@ -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)

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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

Expand All @@ -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)
}

Expand Down
32 changes: 2 additions & 30 deletions config/tests/samples/create/samples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion pkg/test/main/testmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/unified_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 0691318

Please sign in to comment.