Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 13, 2024
1 parent 1d0fe09 commit c29b2ce
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 80 deletions.
Expand Up @@ -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) {
Expand Down
85 changes: 6 additions & 79 deletions internal/testutil/acc/atlas.go
Expand Up @@ -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")
Expand All @@ -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
}
6 changes: 6 additions & 0 deletions internal/testutil/acc/name.go
Expand Up @@ -2,6 +2,7 @@ package acc

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
)
Expand Down Expand Up @@ -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")
}
46 changes: 46 additions & 0 deletions 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
}{}

0 comments on commit c29b2ce

Please sign in to comment.