Skip to content

Commit

Permalink
chore: Creates project for test execution (#2010)
Browse files Browse the repository at this point in the history
* execution project

* no need to empty these values

* simplify TestMainExecution conditions

* address feedback

* add comments

* clarify ProjectIDExecution behavior
  • Loading branch information
lantoli authored Mar 13, 2024
1 parent 2c53a4e commit e054e18
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
)
Expand Down
39 changes: 39 additions & 0 deletions internal/testutil/acc/atlas.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 0 additions & 11 deletions internal/testutil/acc/name.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package acc

import (
"context"
"fmt"
"testing"

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

const (
Expand Down Expand Up @@ -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
}
49 changes: 49 additions & 0 deletions internal/testutil/acc/shared_resource.go
Original file line number Diff line number Diff line change
@@ -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
}{}
6 changes: 5 additions & 1 deletion internal/testutil/acc/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") == ""
}

0 comments on commit e054e18

Please sign in to comment.