Skip to content
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: Reuses projects in executions in some resources and rename mig tests #2007

Merged
merged 12 commits into from
Mar 15, 2024

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Mar 11, 2024

Description

Reuses projects in executions in some resources and rename mig tests.

NOTE: To minimize the use of test resources, multiple small changes across many files are done in this PR, as opposed to extend them through many PRs and having to run the test groups multiple times.

  • TestMain added to all resources so they can use share resources like project execution.
  • TestAccMigrationXXX migration test names are renamed to TestMigXXX so acc test can be run only with a regexp and they don't need the skip param. Also we make test go "cacheable-friendly" as go test with skip param can't use cache.
  • Remove unneeded "${...}" in TF tests, e.g: "${mongodbatlas_api_key.test.org_id}" changed to: mongodbatlas_api_key.test.org_id
  • ClusterInfo changed to use ProjectIDExecution so execution project is reused in the following resources:
    • cloud_backup_schedule
    • cloud_backup_snapshot
    • global_cluster_config
    • search_index
    • stream_connection
  • RandomProjectName changed in some resources to use ProjectIDExecution so execution project is reused in the following resources:
    • alert_configuration
    • database_user

Link to any related issue(s): CLOUDP-237006

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contribution guidelines
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals, I defined an isolated PR with a relevant title as it will be used in the auto-generated changelog.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@lantoli lantoli changed the title chore: Reuses project in resources using GetClusterInfo Reuses projects in executions and rename mig tests. Mar 13, 2024
@lantoli lantoli changed the title Reuses projects in executions and rename mig tests. Reuses projects in executions and rename mig tests Mar 13, 2024
@lantoli lantoli force-pushed the CLOUDP-237006_cluster_info branch from e89be08 to c45b2e2 Compare March 13, 2024 17:37
@lantoli lantoli changed the title Reuses projects in executions and rename mig tests chore: Reuses projects in executions and rename mig tests Mar 13, 2024
@lantoli lantoli changed the title chore: Reuses projects in executions and rename mig tests chore: Reuses projects in executions in some resources and rename mig tests Mar 14, 2024
@lantoli lantoli marked this pull request as ready for review March 14, 2024 12:25
@lantoli lantoli requested a review from a team as a code owner March 14, 2024 12:25
)

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this intentional?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is based on the lines below :) maybe I missed it in the description but I am not understanding why we are not making it parallel anymore

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that before, each test was creating a project, so now that the different tests use the same one, the tests will fail if run in parallel

Copy link
Member Author

@lantoli lantoli Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I changed alert_configuration and database_user test to be serial on purpose. The reason is that they are resources common to the project.
For instance one test for alert_configuration plural datasource creates 2 alerts and ask if there are two, it will fail if other tests are creating others.
alert_notification creates like 20 projects and each one takes only like 5 seconds, so now they will create only 1 project and will take 2 minutes to run all.
however tests than don't change common project elements (which are the most expensive to run like the ones using clusters) can continue working on parallel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as an example in this PR, these resources are reusing projects and running in parallel:
cloud_backup_schedule
cloud_backup_snapshot
global_cluster_config
search_index
stream_connection

Copy link
Member Author

@lantoli lantoli Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both alert_configuration and database_user are in "config" test group, in your link it takes 3m5s

in this PR it takes 2m42s.

Current times:
ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/databaseuser 37.419s
ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration 56.755s

PR times:
ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/databaseuser 39.836s
ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration 50.120s

So even in serial it takes less time now :-)

Most of time in your log is creating the project, that's why they take in the 20s.

Also notice that all packages/resources in a test group run in parallel in different processes, so serial tests in database_user are not serialized together with alert_notification, serial is only in each resource.

Neither of these two resources is in the critical path for config test group.

Anyway I'm going to try what @AgustinBettati said of only running serially the plural ds test, that should work.

Copy link
Member Author

@lantoli lantoli Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcosuma @andreaangiolillo after @AgustinBettati idea to run all in parallel except plural ds these are the new PR times 🎉

ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/databaseuser 23.636s
ok github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration 33.800s

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok it actually did improve and your rationale seems right to me: avoiding creating the project all the times actually contributes to saving time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two follow-ups coming from this PR and conversation (not urgent, but worth a discussion):

  1. couldn't we also convert plural tests to be parallel by changing the expectations? (e.g. remove the "check number of alerts equal n" in favour of "check alert with 'this' and 'this' name exists)
  2. In n months (just random time in the future) when someone will see the code base and see some tests as running in parallel and some sequential, what do you think it could go wrong? Is there a risk that without proper clarification on the code base they might think it's an error and start investigating why?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - yes, that is a good idea, let me think about it
2 - we already do have a mix of parallel and serial (before this PR). let me see 1 if we can do the ds plural serial so we minimize exceptions.

@@ -1,130 +1,54 @@
package acc

import (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not moving all these methods to the databaseuser_test package? Seems like they are only used there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason apart from trying to minimize the changes in this PR as it's getting very big. it's true that I moved checkExists and checkDestroy, I'll move all then, thx for the comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry at the end I think i'm not going to do this refactor in this PR as it's not related to the main topic of the PR and will increase changes still more

Comment on lines 50 to 48
resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithThreshold(orgID, projectName, true, 1),
Config: configWithThreshold(projectID, true, 1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[q] taking this test as an example the terraform configuration is creating a single alert configuration and running a data source for that specific alert, could we preserve running this in parallel?
I would asume the only test cases that needs to be run sequentially are those that rely on asserting the plural data source, and effectively check a certain amount of resources. This also applies for database user tests that where adjusted from ParallelTest to Test

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AgustinBettati great callout!!! i've changed and it worked

Comment on lines 17 to 22
func TestMain(m *testing.M) {
acc.SetupSharedResources()
exitCode := m.Run()
acc.CleanupSharedResources()
os.Exit(exitCode)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regarding where TestMain should be placed within the test package: I do not see a clear convention other than place the TestMain function wherever it makes the most sense.
With this in mind I believe having a main_test.go file (mentioned by @maastha) could be the most transparent option so it is clear the function impacts the entire test package. This is an example I found from the terraform repository: e2etest/main_test.go

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AgustinBettati @maastha ok, changed to their own main_test.go, also I like the idea of setup returning the cleanup function so cleanup doesn't need to be a public function

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@@ -14,6 +14,13 @@ import (
"go.mongodb.org/atlas-sdk/v20231115007/admin"
)

func TestMain(m *testing.M) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is really hard to review 😢 Just an FYI that people in cloud were discussing this interesting blog post The ideal PR is 50 lines long

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx for the comment. Sorry, I know and I normally try to do small PRs. In this case I try to explain in the PR description why I chose to go for a bigger PR here.

.github/workflows/migration-tests.yml Show resolved Hide resolved
Comment on lines 50 to 48
resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: configWithThreshold(orgID, projectName, true, 1),
Config: configWithThreshold(projectID, true, 1),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

)

resource.ParallelTest(t, resource.TestCase{
resource.Test(t, resource.TestCase{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to show in the PR description the impact of running these tests sequentially vs in parallel: this info should be easy to get, just check the time needed to run the tests in this PR vs in the last nightly run

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as I commented to Marco the "config" test group that runs database_user and alert_notification is taking less time now than before even being in serial. anyway i'm going to follow Agustin idea of just running in serial the plural ds tests.

Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the follow up adjustments

Comment on lines +12 to +13
// It returns the cleanup function that must be called at the end of TestMain.
func SetupSharedResources() func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this approach of having the setup and cleanup coupled together to avoid any potential strange use case

@lantoli
Copy link
Member Author

lantoli commented Mar 15, 2024

I'm going to merge as I think the general idea is fine, if you have any comment about some specific area let me know and will address in a following PR. In that way we don't have to run all test groups because a change in a specific area.

@lantoli lantoli merged commit 09357b9 into master Mar 15, 2024
45 checks passed
@lantoli lantoli deleted the CLOUDP-237006_cluster_info branch March 15, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants