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

fix: Fixes project flaky tests #1669

Merged
merged 26 commits into from
Nov 29, 2023
Merged

fix: Fixes project flaky tests #1669

merged 26 commits into from
Nov 29, 2023

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Nov 27, 2023

Description

Jira ticket: INTMDB-1341

Fixes project flaky tests

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.

Further comments

@lantoli lantoli changed the title bug: Fixes project flaky tests fix: Fixes project flaky tests Nov 27, 2023
@github-actions github-actions bot added the bug label Nov 27, 2023
@@ -335,7 +335,6 @@ jobs:
MONGODB_ATLAS_ORG_ID: ${{ vars.MONGODB_ATLAS_ORG_ID_CLOUD_DEV }}
MONGODB_ATLAS_BASE_URL: ${{ vars.MONGODB_ATLAS_BASE_URL }}
MONGODB_ATLAS_PROJECT_OWNER_ID: ${{ vars.MONGODB_ATLAS_PROJECT_OWNER_ID }}
MONGODB_ATLAS_API_KEYS_IDS: ${{ vars.MONGODB_ATLAS_API_KEYS_IDS }}
Copy link
Member Author

Choose a reason for hiding this comment

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

not used

func PreCheckTeamsIds(tb testing.TB) {
if os.Getenv("MONGODB_ATLAS_TEAMS_IDS") == "" {
tb.Skip("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing")
func PreCheckProjectTeamsIds(tb testing.TB, min int) {
Copy link
Member Author

Choose a reason for hiding this comment

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

improve teamsIds checks, and fail tests if pre-check is not met

@@ -171,17 +171,14 @@ func (d *ProjectsDS) Read(ctx context.Context, req datasource.ReadRequest, resp
}

func populateProjectsDataSourceModel(ctx context.Context, conn *matlas.Client, connV2 *admin.APIClient, stateModel *tfProjectsDSModel, projectsRes *matlas.Projects) error {
Copy link
Member Author

Choose a reason for hiding this comment

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

the fix is in this func

@lantoli lantoli marked this pull request as ready for review November 28, 2023 21:26
@lantoli lantoli requested a review from a team as a code owner November 28, 2023 21:26
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
teamsIds := strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",")
if len(teamsIds) < 2 {
t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 2 team ids for this acceptance testing")
Copy link
Member Author

Choose a reason for hiding this comment

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

improve pre-checks and fail if they're not met instead of just skipping the test

atlasTeams, atlasLimits, atlasProjectSettings, err := getProjectPropsFromAPI(ctx, conn, connV2, project.ID)
if err != nil {
return fmt.Errorf("error while getting project properties for project %s: %v", project.ID, err.Error())
if err == nil { // if the project is still valid, e.g. could have just been deleted
Copy link
Collaborator

Choose a reason for hiding this comment

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

should we fail if err != nil ?

Copy link
Member Author

@lantoli lantoli Nov 29, 2023

Choose a reason for hiding this comment

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

no, that was the problem before. Imagine we get the list of projects for the org, but then some of them are deleted. It's not really a failure, so we just ignore it if it's deleted after it was in the list. This was the reason for project flaky test.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok noted, but then I guess we're ok to ignore 5xx errors?
What I am trying to understand here is: if there is a 5xx error and we ignore that and we move on, will we get a different test behaviour that might hide some problem?

Copy link
Member Author

@lantoli lantoli Nov 29, 2023

Choose a reason for hiding this comment

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

I've got 500 sometimes when doing something in a project that is being closed, so we could get also a 500 and it doesn't mean a real error but that the project is closing. So I think it's hard in this case to differentiate a "real" error from an error caused by the project being closed. Also chances are that if the project is in the list and it's not closed we'll get the details without any problem

Copy link
Member

Choose a reason for hiding this comment

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

Understand this an edge case that is likely only appearing in our acceptance tests execution: fetching and creating/deleting project in parallel. Curious if a potential fix to this problem could be defining the data source test to run sequentially (resource.Test( over resource.ParallelTest() to reduce the chances of this scenario. (just to keep in mind for other flaky tests that we might have)

Copy link
Member Author

Choose a reason for hiding this comment

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

that would probably fix the tests, also to use the concurrency facility in GH actions so project acc and migration tests don't run in parallel. But at the end I thought this is a realistic use case that can happen sometimes and we don't want the DS to fail if that happens, that's why I focused on improving the prod. code.

}

func GetProjectTeamsIds(pos int) string {
envVar := os.Getenv("MONGODB_ATLAS_TEAMS_IDS")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can you please pass the name of the environment variable as input of the function?

Copy link
Member Author

@lantoli lantoli Nov 29, 2023

Choose a reason for hiding this comment

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

I think it's better in this way as PreCheckProjectTeamsIds and GetProjectTeamsIds work together for teamsIds and they both use the same env.var

Copy link
Collaborator

@marcosuma marcosuma Nov 29, 2023

Choose a reason for hiding this comment

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

work together

I think this is an assumption that might not be always true, unless you call the precheck... in the get... method right?

Copy link
Member Author

@lantoli lantoli Nov 29, 2023

Choose a reason for hiding this comment

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

if you don't call precheck you won't check that you have the min teams you need but it will still work (returns "" when you don't have that pos).
but if you don't call the precheck method, i don't see how passing the env.var can help there.

(They have the same name part ProjectTeamsIds and are located together in the file to make it easier to see that they are intended to work together)

func PreCheckTeamsIds(tb testing.TB) {
if os.Getenv("MONGODB_ATLAS_TEAMS_IDS") == "" {
tb.Skip("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing")
func PreCheckProjectTeamsIds(tb testing.TB, min int) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can you please pass the name of the environment variable as input of the function?

Copy link
Member Author

Choose a reason for hiding this comment

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

same, I think it's better to have the env.var in both methods as they're used only for teamsIds, these methods are not intended to be generic for other purporses.

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIdsWithMinSize(t, 2) },

nit: for clarity I would suggest a more specific method name

Copy link
Member Author

Choose a reason for hiding this comment

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

I can do that but I normally like to say "with..." when there are multiple related methods, there is only one method in this case

Copy link
Member Author

Choose a reason for hiding this comment

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

renamed to: func PreCheckProjectTeamsIdsWithMinCount(tb testing.TB, minTeamsCount int)

func PreCheckTeamsIds(tb testing.TB) {
if os.Getenv("MONGODB_ATLAS_TEAMS_IDS") == "" {
tb.Skip("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing")
func PreCheckProjectTeamsIds(tb testing.TB, min int) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
func PreCheckProjectTeamsIds(tb testing.TB, min int) {
func PreCheckProjectTeamsIds(tb testing.TB, minProjectCount int) {

Copy link
Member Author

Choose a reason for hiding this comment

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

I can change it, although I tried to follow "go idiomatic" in the sense the I think "min" in this context is clear, but I agree I can make it more specific here.

Copy link
Member Author

@lantoli lantoli Nov 29, 2023

Choose a reason for hiding this comment

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

renamed to minTeamsCount (although it looks a bit redudant to me as Teams is already in function name)

Copy link
Collaborator

@marcosuma marcosuma left a comment

Choose a reason for hiding this comment

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

few minor comments, LGTM overall

Copy link
Contributor

Code Coverage

Package Line Rate Health
github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion 31%
github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate 68%
github.com/mongodb/terraform-provider-mongodbatlas/internal/provider 5%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster 3%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/alertconfiguration 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/atlasuser 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/cluster 1%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/databaseuser 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectipaccesslist 0%
github.com/mongodb/terraform-provider-mongodbatlas/internal/service/searchdeployment 23%
github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc 5%
github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas 2%
Summary 3% (315 / 10714)

@lantoli lantoli merged commit 62ae4e1 into master Nov 29, 2023
28 checks passed
@lantoli lantoli deleted the INTMDB-1341_project_fix branch November 29, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants