From c29b2cee4b5f87016c0d9c93a5e9fe9953f5cfe4 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 13 Mar 2024 15:41:14 +0100 Subject: [PATCH] address feedback --- ...ice_data_federation_online_archive_test.go | 5 +- internal/testutil/acc/atlas.go | 85 ++----------------- internal/testutil/acc/name.go | 6 ++ internal/testutil/acc/shared_resource.go | 46 ++++++++++ 4 files changed, 62 insertions(+), 80 deletions(-) create mode 100644 internal/testutil/acc/shared_resource.go diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go index 34ead6fbca..ca1457ebcb 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go @@ -19,7 +19,10 @@ var ( ) func TestMain(m *testing.M) { - acc.TestMainExecution(m) + acc.SetupSharedResources() + exitCode := m.Run() + acc.CleanupSharedResources() + os.Exit(exitCode) } func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) { diff --git a/internal/testutil/acc/atlas.go b/internal/testutil/acc/atlas.go index c388521290..1024910500 100644 --- a/internal/testutil/acc/atlas.go +++ b/internal/testutil/acc/atlas.go @@ -4,89 +4,12 @@ import ( "context" "fmt" "os" - "regexp" - "runtime" - "sync" "testing" "github.com/stretchr/testify/require" "go.mongodb.org/atlas-sdk/v20231115007/admin" ) -// TestMainExecution must be called from TestMain in the test package if ProjectIDExecution is going to be used. -func TestMainExecution(m *testing.M) { - atlasInfo.init = true - atlasInfo.resourceName = resourceName() - - exitCode := m.Run() - - if atlasInfo.needsDeletion { - fmt.Printf("Deleting execution project: %s, resource: %s\n", atlasInfo.projectName, atlasInfo.resourceName) - deleteProject(atlasInfo.projectID) - } - - os.Exit(exitCode) -} - -// ProjectIDExecution returns a project id created for the execution of the resource tests. -func ProjectIDExecution(tb testing.TB) string { - tb.Helper() - SkipInUnitTest(tb) - require.True(tb, atlasInfo.init, "TestMainExecution must called to be able to use ProjectIDExecution") - - atlasInfo.mu.Lock() - defer atlasInfo.mu.Unlock() - - // lazy creation so it's only done if really needed - if atlasInfo.projectName == "" { - var globalName, globalID string - if atlasInfo.resourceName != "" { - globalName = (prefixProjectKeep + "-" + atlasInfo.resourceName)[:projectNameMaxLen] - globalID = projectID(globalName) - } - - if globalID == "" { - atlasInfo.projectName = RandomProjectName() - tb.Logf("Creating execution project: %s, resource: %s, global project (not found): %s\n", atlasInfo.projectName, atlasInfo.resourceName, globalName) - atlasInfo.projectID = createProject(tb, atlasInfo.projectName) - atlasInfo.needsDeletion = true - } else { - atlasInfo.projectName = globalName - tb.Logf("Reusing global project: %s, resource: %s\n", atlasInfo.projectName, atlasInfo.resourceName) - atlasInfo.projectID = globalID - } - } - - return atlasInfo.projectID -} - -var atlasInfo = struct { - projectID string - projectName string - resourceName string - mu sync.Mutex - init bool - needsDeletion bool -}{} - -const ( - projectNameMaxLen = 64 -) - -func resourceName() string { - pc, _, _, ok := runtime.Caller(2) - if !ok { - return "" - } - pattern := `([^/]+)_test\.TestMain$` - re := regexp.MustCompile(pattern) - matches := re.FindStringSubmatch(runtime.FuncForPC(pc).Name()) - if len(matches) <= 1 { - return "" - } - return matches[1] -} - func createProject(tb testing.TB, name string) string { tb.Helper() orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") @@ -106,7 +29,11 @@ func deleteProject(id string) { } } -func projectID(name string) string { +func projectID(tb testing.TB, name string) string { + tb.Helper() + SkipInUnitTest(tb) resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute() - return resp.GetId() + id := resp.GetId() + require.NotEmpty(tb, id, "Project name not found: %s", name) + return id } diff --git a/internal/testutil/acc/name.go b/internal/testutil/acc/name.go index 95054c6e40..36a5d3a71c 100644 --- a/internal/testutil/acc/name.go +++ b/internal/testutil/acc/name.go @@ -2,6 +2,7 @@ package acc import ( "fmt" + "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" ) @@ -37,3 +38,8 @@ func RandomIP(a, b, c byte) string { func RandomEmail() string { return fmt.Sprintf("%s-%s@mongodb.com", prefixName, acctest.RandString(10)) } + +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 new file mode 100644 index 0000000000..f008fb22ef --- /dev/null +++ b/internal/testutil/acc/shared_resource.go @@ -0,0 +1,46 @@ +package acc + +import ( + "fmt" + "sync" + "testing" + + "github.com/stretchr/testify/require" +) + +func SetupSharedResources() { + sharedInfo.init = true +} + +func CleanupSharedResources() { + if sharedInfo.projectID != "" { + fmt.Printf("Deleting execution project: %s, id: %s\n", sharedInfo.projectName, sharedInfo.projectID) + deleteProject(sharedInfo.projectID) + } +} + +// ProjectIDExecution returns a project id created for the execution of the tests in the resource package. +func ProjectIDExecution(tb testing.TB) string { + tb.Helper() + SkipInUnitTest(tb) + require.True(tb, sharedInfo.init, "SetupSharedResources must called from TestMain test package") + + sharedInfo.mu.Lock() + defer sharedInfo.mu.Unlock() + + // lazy creation so it's only done if really needed + if sharedInfo.projectID == "" { + sharedInfo.projectName = RandomProjectName() + tb.Logf("Creating execution project: %s, id: %s\n", sharedInfo.projectName, sharedInfo.projectID) + sharedInfo.projectID = createProject(tb, sharedInfo.projectName) + } + + return sharedInfo.projectID +} + +var sharedInfo = struct { + projectID string + projectName string + mu sync.Mutex + init bool +}{}