Skip to content

Commit e054e18

Browse files
authored
chore: Creates project for test execution (#2010)
* execution project * no need to empty these values * simplify TestMainExecution conditions * address feedback * add comments * clarify ProjectIDExecution behavior
1 parent 2c53a4e commit e054e18

8 files changed

+106
-18
lines changed

internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515

1616
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic(t *testing.T) {
1717
var (
18-
projectID = acc.ProjectIDGlobal(t)
18+
projectID = acc.ProjectIDExecution(t)
1919
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
2020
customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME")
2121
)

internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var (
1515

1616
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchivesDSPlural_basic(t *testing.T) {
1717
var (
18-
projectID = acc.ProjectIDGlobal(t)
18+
projectID = acc.ProjectIDExecution(t)
1919
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
2020
)
2121

internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestAccMigrationNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) {
1313
var (
14-
projectID = acc.ProjectIDGlobal(t)
14+
projectID = acc.ProjectIDExecution(t)
1515
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
1616
config = resourceConfigBasic(projectID, endpointID, comment)
1717
)

internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ var (
1818
atlasRegion = "US_EAST_1"
1919
)
2020

21+
func TestMain(m *testing.M) {
22+
acc.SetupSharedResources()
23+
exitCode := m.Run()
24+
acc.CleanupSharedResources()
25+
os.Exit(exitCode)
26+
}
27+
2128
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) {
2229
var (
23-
projectID = acc.ProjectIDGlobal(t)
30+
projectID = acc.ProjectIDExecution(t)
2431
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
2532
)
2633

@@ -51,7 +58,7 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t
5158
}
5259
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateComment(t *testing.T) {
5360
var (
54-
projectID = acc.ProjectIDGlobal(t)
61+
projectID = acc.ProjectIDExecution(t)
5562
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
5663
commentUpdated = "Terraform Acceptance Test Updated"
5764
)
@@ -98,7 +105,7 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateC
98105

99106
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basicWithRegionDnsName(t *testing.T) {
100107
var (
101-
projectID = acc.ProjectIDGlobal(t)
108+
projectID = acc.ProjectIDExecution(t)
102109
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
103110
customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME")
104111
)

internal/testutil/acc/atlas.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package acc
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
"go.mongodb.org/atlas-sdk/v20231115007/admin"
11+
)
12+
13+
func createProject(tb testing.TB, name string) string {
14+
tb.Helper()
15+
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
16+
require.NotNil(tb, "Project creation failed: %s, org not set", name)
17+
params := &admin.Group{Name: name, OrgId: orgID}
18+
resp, _, err := ConnV2().ProjectsApi.CreateProject(context.Background(), params).Execute()
19+
require.NoError(tb, err, "Project creation failed: %s, err: %s", name, err)
20+
id := resp.GetId()
21+
require.NotEmpty(tb, id, "Project creation failed: %s", name)
22+
return id
23+
}
24+
25+
func deleteProject(id string) {
26+
_, _, err := ConnV2().ProjectsApi.DeleteProject(context.Background(), id).Execute()
27+
if err != nil {
28+
fmt.Printf("Project deletion failed: %s, error: %s", id, err)
29+
}
30+
}
31+
32+
func projectID(tb testing.TB, name string) string {
33+
tb.Helper()
34+
SkipInUnitTest(tb)
35+
resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute()
36+
id := resp.GetId()
37+
require.NotEmpty(tb, id, "Project name not found: %s", name)
38+
return id
39+
}

internal/testutil/acc/name.go

-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package acc
22

33
import (
4-
"context"
54
"fmt"
65
"testing"
76

87
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9-
"github.com/stretchr/testify/require"
108
)
119

1210
const (
@@ -45,12 +43,3 @@ func ProjectIDGlobal(tb testing.TB) string {
4543
tb.Helper()
4644
return projectID(tb, prefixProjectKeep+"-global")
4745
}
48-
49-
func projectID(tb testing.TB, name string) string {
50-
tb.Helper()
51-
SkipInUnitTest(tb)
52-
resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute()
53-
id := resp.GetId()
54-
require.NotEmpty(tb, id, "Project name not found: %s", name)
55-
return id
56-
}
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package acc
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// SetupSharedResources must be called from TestMain test package in order to use ProjectIDExecution.
12+
func SetupSharedResources() {
13+
sharedInfo.init = true
14+
}
15+
16+
// CleanupSharedResources must be called from TestMain test package in order to use ProjectIDExecution.
17+
func CleanupSharedResources() {
18+
if sharedInfo.projectID != "" {
19+
fmt.Printf("Deleting execution project: %s, id: %s\n", sharedInfo.projectName, sharedInfo.projectID)
20+
deleteProject(sharedInfo.projectID)
21+
}
22+
}
23+
24+
// ProjectIDExecution returns a project id created for the execution of the tests in the resource package.
25+
// 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.
26+
func ProjectIDExecution(tb testing.TB) string {
27+
tb.Helper()
28+
SkipInUnitTest(tb)
29+
require.True(tb, sharedInfo.init, "SetupSharedResources must called from TestMain test package")
30+
31+
sharedInfo.mu.Lock()
32+
defer sharedInfo.mu.Unlock()
33+
34+
// lazy creation so it's only done if really needed
35+
if sharedInfo.projectID == "" {
36+
sharedInfo.projectName = RandomProjectName()
37+
tb.Logf("Creating execution project: %s\n", sharedInfo.projectName)
38+
sharedInfo.projectID = createProject(tb, sharedInfo.projectName)
39+
}
40+
41+
return sharedInfo.projectID
42+
}
43+
44+
var sharedInfo = struct {
45+
projectID string
46+
projectName string
47+
mu sync.Mutex
48+
init bool
49+
}{}

internal/testutil/acc/skip.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ func SkipTestForCI(tb testing.TB) {
1818
// This can be useful for acceptance tests that define logic prior to resource.Test/resource.ParallelTest functions as this code would always be run.
1919
func SkipInUnitTest(tb testing.TB) {
2020
tb.Helper()
21-
if os.Getenv("TF_ACC") == "" {
21+
if InUnitTest() {
2222
tb.Skip()
2323
}
2424
}
25+
26+
func InUnitTest() bool {
27+
return os.Getenv("TF_ACC") == ""
28+
}

0 commit comments

Comments
 (0)