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: Adds mig tests and refactor - searchindex #2065

Merged
merged 7 commits into from
Mar 25, 2024

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Mar 22, 2024

Description

Adds mig tests and refactor - searchindex

See also comment below

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

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

},
},
})
}
func commonChecks(indexName, indexType, mappingsDynamic, databaseName string, clusterInfo acc.ClusterInfo) []resource.TestCheckFunc {
Copy link
Collaborator

@EspenAlbert EspenAlbert Mar 25, 2024

Choose a reason for hiding this comment

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

NOTE: adds AddAttrChecks and AddAttrSetChecks to acc package

These functions are meant to simplify the reusing of checks.
This function is used by almost all of the tests to simplify the checking.
commonChecks defines:

  1. attributesused by AddAttrChecks the common fields to be checked on datasource and resource for all tests (checking both field and value)
  2. AddAttrSetChecks only checks that the field is defined
  3. any differences between resourceName and datasourceName, e.g., index_id is also checked on the datasourceName

In this case, I add two more helpers to simplify adding checks to both datasource and resource: addAttrChecks and addAttrSetChecks

@EspenAlbert EspenAlbert marked this pull request as ready for review March 25, 2024 11:15
@EspenAlbert EspenAlbert requested a review from a team as a code owner March 25, 2024 11:15
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
)

func TestMigSearchIndexRS_basic(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

remove RS from test names if we're testing resource and datasource together

func AddAttrSetChecks(targetName string, checks []resource.TestCheckFunc, attrNames ...string) []resource.TestCheckFunc {
// avoids accidentally modifying existing slice
newChecks := []resource.TestCheckFunc{}
copy(newChecks, checks)
Copy link
Member Author

@lantoli lantoli Mar 25, 2024

Choose a reason for hiding this comment

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

not sure this copy is doing anything as destination is empty, e.g: https://yourbasic.org/golang/copy-explained/
" It returns the number of elements copied, which will be the minimum of len(dst) and len(src) "

If destination is empty nothing is copied. you should need:

newChecks := make(resource.TestCheckFunc, len(checks))

Copy link
Collaborator

Choose a reason for hiding this comment

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

Great. Thank you for the reference as well 🙏

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note: I had to add the capacity argument too, otherwise ci-lint complains about append to slice newChecks with non-zero initialized length (makezero)

@lantoli
Copy link
Member Author

lantoli commented Mar 25, 2024

LGTM, just minor comments

@@ -54,3 +54,23 @@ func JSONEquals(expected string) resource.CheckResourceAttrWithFunc {
return nil
}
}

func AddAttrSetChecks(targetName string, checks []resource.TestCheckFunc, attrNames ...string) []resource.TestCheckFunc {
Copy link
Member

Choose a reason for hiding this comment

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

Name was a little confusing to me at first (thought Set referred to the collection type), does AddAttrIsDefinedChecks sound better?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm. It comes from the underlying resource.TestCheckResourceAttrSet from the terraform-plugin-testing.
I thought it would be smart to keep the names the same. The relevant calls are:

  1. acc.AddAttrSetChecks(datasourceName, checks, "project_id", "index_id")
  2. acc.AddAttrSetChecks(resourceName, checks, "project_id")

WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

makes sense, agree with that approach 👍

return acc.AddAttrChecks(datasourceName, checks, mapChecks)
}

func addAttrSetChecks(checks []resource.TestCheckFunc, attrNames ...string) []resource.TestCheckFunc {
Copy link
Member

Choose a reason for hiding this comment

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

relevant to comment made below regarding naming

@EspenAlbert EspenAlbert merged commit 5ef7248 into master Mar 25, 2024
46 checks passed
@EspenAlbert EspenAlbert deleted the CLOUDP-237030_search_index branch March 25, 2024 15:07
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.

3 participants