Skip to content

Commit

Permalink
use MONGODB_ATLAS_PROJECT_ID (#2060)
Browse files Browse the repository at this point in the history
  • Loading branch information
lantoli committed Mar 22, 2024
1 parent 39ace4e commit 5bce7c8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
20 changes: 20 additions & 0 deletions internal/testutil/acc/atlas.go
Expand Up @@ -29,11 +29,31 @@ func deleteProject(id string) {
}
}

// ProjectID returns the id for a project name.
// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions.
func ProjectID(tb testing.TB, name string) string {
tb.Helper()
SkipInUnitTest(tb)

if id := projectIDLocal(tb); id != "" {
return id
}

resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute()
id := resp.GetId()
require.NotEmpty(tb, id, "Project name not found: %s", name)
return id
}

func projectIDLocal(tb testing.TB) string {
tb.Helper()
id := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
if id == "" {
return ""
}
if InCI() {
tb.Fatal("MONGODB_ATLAS_PROJECT_ID can't be used in CI")
}
tb.Logf("Using MONGODB_ATLAS_PROJECT_ID: %s", id)
return id
}
1 change: 1 addition & 0 deletions internal/testutil/acc/name.go
Expand Up @@ -50,6 +50,7 @@ func RandomLDAPName() string {

// ProjectIDGlobal returns a common global project.
// Consider mig.ProjectIDGlobal for mig tests.
// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions.
func ProjectIDGlobal(tb testing.TB) string {
tb.Helper()
return ProjectID(tb, PrefixProjectKeep+"-global")
Expand Down
5 changes: 5 additions & 0 deletions internal/testutil/acc/shared_resource.go
Expand Up @@ -24,6 +24,7 @@ func cleanupSharedResources() {

// 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.
// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions.
func ProjectIDExecution(tb testing.TB) string {
tb.Helper()
SkipInUnitTest(tb)
Expand All @@ -32,6 +33,10 @@ func ProjectIDExecution(tb testing.TB) string {
sharedInfo.mu.Lock()
defer sharedInfo.mu.Unlock()

if id := projectIDLocal(tb); id != "" {
return id
}

// lazy creation so it's only done if really needed
if sharedInfo.projectID == "" {
sharedInfo.projectName = RandomProjectName()
Expand Down
6 changes: 5 additions & 1 deletion internal/testutil/acc/skip.go
Expand Up @@ -9,11 +9,15 @@ import (
// SkipTestForCI is added to tests that cannot run as part of CI, e.g. in Github actions.
func SkipTestForCI(tb testing.TB) {
tb.Helper()
if strings.EqualFold(os.Getenv("CI"), "true") {
if InCI() {
tb.Skip()
}
}

func InCI() bool {
return strings.EqualFold(os.Getenv("CI"), "true")
}

// SkipInUnitTest allows skipping a test entirely when TF_ACC=1 is not defined.
// 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) {
Expand Down
1 change: 1 addition & 0 deletions internal/testutil/mig/name.go
Expand Up @@ -8,6 +8,7 @@ import (

// ProjectIDGlobal returns a common global project to be used by migration tests.
// As there is a small number of mig tests this project won't hit limits.
// When `MONGODB_ATLAS_PROJECT_ID` is defined, it is used instead of creating a project. This is useful for local execution but not intended for CI executions.
func ProjectIDGlobal(tb testing.TB) string {
tb.Helper()
return acc.ProjectID(tb, acc.PrefixProjectKeep+"-global-mig")
Expand Down

0 comments on commit 5bce7c8

Please sign in to comment.