diff --git a/internal/testutil/acc/atlas.go b/internal/testutil/acc/atlas.go index 4276d57623..6fc2976c89 100644 --- a/internal/testutil/acc/atlas.go +++ b/internal/testutil/acc/atlas.go @@ -29,11 +29,31 @@ func deleteProject(id string) { } } +// ProjectID returns the id for a project name. +// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions. func ProjectID(tb testing.TB, name string) string { tb.Helper() SkipInUnitTest(tb) + + if id := projectIDLocal(tb); id != "" { + return id + } + resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute() id := resp.GetId() require.NotEmpty(tb, id, "Project name not found: %s", name) return id } + +func projectIDLocal(tb testing.TB) string { + tb.Helper() + id := os.Getenv("MONGODB_ATLAS_PROJECT_ID") + if id == "" { + return "" + } + if InCI() { + tb.Fatal("MONGODB_ATLAS_PROJECT_ID can't be used in CI") + } + tb.Logf("Using MONGODB_ATLAS_PROJECT_ID: %s", id) + return id +} diff --git a/internal/testutil/acc/name.go b/internal/testutil/acc/name.go index 5b0b269e23..423f1de3e3 100644 --- a/internal/testutil/acc/name.go +++ b/internal/testutil/acc/name.go @@ -50,6 +50,7 @@ func RandomLDAPName() string { // ProjectIDGlobal returns a common global project. // Consider mig.ProjectIDGlobal for mig tests. +// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions. func ProjectIDGlobal(tb testing.TB) string { tb.Helper() return ProjectID(tb, PrefixProjectKeep+"-global") diff --git a/internal/testutil/acc/shared_resource.go b/internal/testutil/acc/shared_resource.go index 4121c1efba..ee772fbcf2 100644 --- a/internal/testutil/acc/shared_resource.go +++ b/internal/testutil/acc/shared_resource.go @@ -24,6 +24,7 @@ func cleanupSharedResources() { // ProjectIDExecution returns a project id created for the execution of the tests in the resource package. // Even if a GH test group is run, every resource/package will create its own project, not a shared project for all the test group. +// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions. func ProjectIDExecution(tb testing.TB) string { tb.Helper() SkipInUnitTest(tb) @@ -32,6 +33,10 @@ func ProjectIDExecution(tb testing.TB) string { sharedInfo.mu.Lock() defer sharedInfo.mu.Unlock() + if id := projectIDLocal(tb); id != "" { + return id + } + // lazy creation so it's only done if really needed if sharedInfo.projectID == "" { sharedInfo.projectName = RandomProjectName() diff --git a/internal/testutil/acc/skip.go b/internal/testutil/acc/skip.go index 62bd7aea27..29a89710f2 100644 --- a/internal/testutil/acc/skip.go +++ b/internal/testutil/acc/skip.go @@ -9,11 +9,15 @@ import ( // SkipTestForCI is added to tests that cannot run as part of CI, e.g. in Github actions. func SkipTestForCI(tb testing.TB) { tb.Helper() - if strings.EqualFold(os.Getenv("CI"), "true") { + if InCI() { tb.Skip() } } +func InCI() bool { + return strings.EqualFold(os.Getenv("CI"), "true") +} + // SkipInUnitTest allows skipping a test entirely when TF_ACC=1 is not defined. // This can be useful for acceptance tests that define logic prior to resource.Test/resource.ParallelTest functions as this code would always be run. func SkipInUnitTest(tb testing.TB) { diff --git a/internal/testutil/mig/name.go b/internal/testutil/mig/name.go index 37f6e9966f..9f5242e595 100644 --- a/internal/testutil/mig/name.go +++ b/internal/testutil/mig/name.go @@ -8,6 +8,7 @@ import ( // ProjectIDGlobal returns a common global project to be used by migration tests. // As there is a small number of mig tests this project won't hit limits. +// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions. func ProjectIDGlobal(tb testing.TB) string { tb.Helper() return acc.ProjectID(tb, acc.PrefixProjectKeep+"-global-mig")