diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go index d4e7c705f6..c1f5c70fc1 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go @@ -15,7 +15,7 @@ var ( func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME") ) diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go index 79ed6d5c94..6c2433308c 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go @@ -15,7 +15,7 @@ var ( func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchivesDSPlural_basic(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") ) diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go index 7829bfb736..33616a5427 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go @@ -11,7 +11,7 @@ import ( func TestAccMigrationNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") config = resourceConfigBasic(projectID, endpointID, comment) ) 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 1d9c553b61..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 @@ -18,9 +18,16 @@ var ( atlasRegion = "US_EAST_1" ) +func TestMain(m *testing.M) { + acc.SetupSharedResources() + exitCode := m.Run() + acc.CleanupSharedResources() + os.Exit(exitCode) +} + func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") ) @@ -51,7 +58,7 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t } func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateComment(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") commentUpdated = "Terraform Acceptance Test Updated" ) @@ -98,7 +105,7 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateC func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basicWithRegionDnsName(t *testing.T) { var ( - projectID = acc.ProjectIDGlobal(t) + projectID = acc.ProjectIDExecution(t) endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME") ) diff --git a/internal/testutil/acc/atlas.go b/internal/testutil/acc/atlas.go new file mode 100644 index 0000000000..1024910500 --- /dev/null +++ b/internal/testutil/acc/atlas.go @@ -0,0 +1,39 @@ +package acc + +import ( + "context" + "fmt" + "os" + "testing" + + "github.com/stretchr/testify/require" + "go.mongodb.org/atlas-sdk/v20231115007/admin" +) + +func createProject(tb testing.TB, name string) string { + tb.Helper() + orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") + require.NotNil(tb, "Project creation failed: %s, org not set", name) + params := &admin.Group{Name: name, OrgId: orgID} + resp, _, err := ConnV2().ProjectsApi.CreateProject(context.Background(), params).Execute() + require.NoError(tb, err, "Project creation failed: %s, err: %s", name, err) + id := resp.GetId() + require.NotEmpty(tb, id, "Project creation failed: %s", name) + return id +} + +func deleteProject(id string) { + _, _, err := ConnV2().ProjectsApi.DeleteProject(context.Background(), id).Execute() + if err != nil { + fmt.Printf("Project deletion failed: %s, error: %s", id, err) + } +} + +func projectID(tb testing.TB, name string) string { + tb.Helper() + SkipInUnitTest(tb) + resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute() + 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 e739efd4e1..36a5d3a71c 100644 --- a/internal/testutil/acc/name.go +++ b/internal/testutil/acc/name.go @@ -1,12 +1,10 @@ package acc import ( - "context" "fmt" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/stretchr/testify/require" ) const ( @@ -45,12 +43,3 @@ func ProjectIDGlobal(tb testing.TB) string { tb.Helper() return projectID(tb, prefixProjectKeep+"-global") } - -func projectID(tb testing.TB, name string) string { - tb.Helper() - SkipInUnitTest(tb) - resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute() - id := resp.GetId() - require.NotEmpty(tb, id, "Project name not found: %s", name) - return id -} diff --git a/internal/testutil/acc/shared_resource.go b/internal/testutil/acc/shared_resource.go new file mode 100644 index 0000000000..21d8005591 --- /dev/null +++ b/internal/testutil/acc/shared_resource.go @@ -0,0 +1,49 @@ +package acc + +import ( + "fmt" + "sync" + "testing" + + "github.com/stretchr/testify/require" +) + +// SetupSharedResources must be called from TestMain test package in order to use ProjectIDExecution. +func SetupSharedResources() { + sharedInfo.init = true +} + +// CleanupSharedResources must be called from TestMain test package in order to use ProjectIDExecution. +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. +// 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. +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\n", sharedInfo.projectName) + sharedInfo.projectID = createProject(tb, sharedInfo.projectName) + } + + return sharedInfo.projectID +} + +var sharedInfo = struct { + projectID string + projectName string + mu sync.Mutex + init bool +}{} diff --git a/internal/testutil/acc/skip.go b/internal/testutil/acc/skip.go index 86c413da33..62bd7aea27 100644 --- a/internal/testutil/acc/skip.go +++ b/internal/testutil/acc/skip.go @@ -18,7 +18,11 @@ func SkipTestForCI(tb testing.TB) { // 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) { tb.Helper() - if os.Getenv("TF_ACC") == "" { + if InUnitTest() { tb.Skip() } } + +func InUnitTest() bool { + return os.Getenv("TF_ACC") == "" +}