-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
106 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters