-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Creates project for test execution #2010
Changes from all commits
d515f8b
68030d6
1d0fe09
c29b2ce
1a1f21c
d6bb0d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,16 @@ var ( | |
atlasRegion = "US_EAST_1" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go runs this function before any test in the package |
||
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") | ||
) | ||
|
||
|
@@ -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" | ||
) | ||
|
@@ -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") | ||
) | ||
|
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 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nit] this looks like a more generic util method to be put outside this file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. moved |
||||||
tb.Helper() | ||||||
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID") | ||||||
require.NotNil(tb, "Project creation failed: %s, org not set", name) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
id := resp.GetId() | ||||||
require.NotEmpty(tb, id, "Project creation failed: %s", name) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return id | ||||||
} | ||||||
|
||||||
func deleteProject(id string) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. out of curiosity why create is a helper but delete is not? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the creation is done lazily inside a test so we have the T, but deletion is done in TestMain when all test have finished and we don't have test context anymore |
||||||
_, _, err := ConnV2().ProjectsApi.DeleteProject(context.Background(), id).Execute() | ||||||
if err != nil { | ||||||
fmt.Printf("Project deletion failed: %s, error: %s", id, err) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which is delete is a helper you can then do
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't be because it's called from TestMain not from a test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, this may make this approach a bit noisy on the logs since you usually only want logs when running on verbose mode (-v) and right now with printf is harder to control There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're running with -v in GH Actions, but you have a point, will consider in next PRs if delete it, thanks! |
||||||
} | ||||||
} | ||||||
|
||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you make sure all assertions where you do string formatting end with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think they do the same, e.g.:
the signature is: func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) { example of implementation:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think it's just to make sure that the first param is a string (with the msg) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah I see internally does |
||||||
return id | ||||||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how are we running tests? normally you want logs when running in verbose mode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we use verbose flag: TF_ACC=1 go test |
||
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 == "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that is a good idea, but i'm going to do in a next PR so this doesn't get more complicated, thx! |
||
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 | ||
}{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all functions like this will start with ProjectID, we have ProjectIDExecution, ProjectIDGlobal and might have ProjectIDTrigger, etc.