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
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
37bd3a6
test fail if precondition for MONGODB_ATLAS_ORG_ID is not met
lantoli Nov 27, 2023
38e9bab
refactor teamsIds
lantoli Nov 27, 2023
98d87b6
remove unused var MONGODB_ATLAS_API_KEYS_IDS
lantoli Nov 27, 2023
f021848
make an explicit dependency
lantoli Nov 27, 2023
72539d0
Revert "make an explicit dependency"
lantoli Nov 28, 2023
571fa00
enable TF logs
lantoli Nov 28, 2023
260afbb
Revert "enable TF logs"
lantoli Nov 28, 2023
1f8e81d
check TF versions in GH action
lantoli Nov 28, 2023
8c546b4
TEMPORARY - run only flaky test
lantoli Nov 28, 2023
f1f0a82
debug terraform file
lantoli Nov 28, 2023
43aae57
try 2 tests
lantoli Nov 28, 2023
3291160
Revert "try 2 tests"
lantoli Nov 28, 2023
95e0854
debug statements
lantoli Nov 28, 2023
1c96572
more debug
lantoli Nov 28, 2023
698adeb
debug delete project
lantoli Nov 28, 2023
d6d391f
TEMPORARY - don't run project migration tests
lantoli Nov 28, 2023
a30ccce
Revert "debug delete project"
lantoli Nov 28, 2023
c76b692
Revert "more debug"
lantoli Nov 28, 2023
f2a16ae
Revert "debug statements"
lantoli Nov 28, 2023
1ee3627
Revert "debug terraform file"
lantoli Nov 28, 2023
2b2c8da
Revert "TEMPORARY - run only flaky test"
lantoli Nov 28, 2023
9266a49
Revert "check TF versions in GH action"
lantoli Nov 28, 2023
89b0ea3
Revert "TEMPORARY - don't run project migration tests"
lantoli Nov 28, 2023
fc43b97
fix data source plural so it doesn't fail if a project is deleted aft…
lantoli Nov 28, 2023
be185bc
rename minTeamsCount and PreCheckProjectTeamsIdsWithMinCount
lantoli Nov 29, 2023
817acb0
rename GetProjectTeamsIdsWithPos
lantoli Nov 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

MONGODB_ATLAS_TEAMS_IDS: ${{ vars.MONGODB_ATLAS_TEAMS_IDS }}
SKIP_TEST_EXTERNAL_CREDENTIALS: ${{ vars.SKIP_TEST_EXTERNAL_CREDENTIALS }}
ACCTEST_TIMEOUT: ${{ vars.ACCTEST_TIMEOUT }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/migration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,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 }}
MONGODB_ATLAS_TEAMS_IDS: ${{ vars.MONGODB_ATLAS_TEAMS_IDS }}
SKIP_TEST_EXTERNAL_CREDENTIALS: ${{ vars.SKIP_TEST_EXTERNAL_CREDENTIALS }}
ACCTEST_TIMEOUT: ${{ vars.ACCTEST_TIMEOUT }}
Expand Down
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ You must also configure the following environment variables before running the t
- For `Project(s)` resource configuration:
```bash
$ export MONGODB_ATLAS_TEAMS_IDS=<YOUR_TEAMS_IDS>
$ export MONGODB_ATLAS_API_KEYS_IDS=<API_KEYS_IDS>
```
~> **Notice:** It should be at least one team id up to 3 teams ids depending of acceptance testing using separator comma like this `teamId1,teamdId2,teamId3`.

Expand Down
59 changes: 25 additions & 34 deletions internal/service/project/data_source_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package project_test
import (
"fmt"
"os"
"strings"
"testing"

"go.mongodb.org/atlas-sdk/v20231115001/admin"
Expand All @@ -15,26 +14,23 @@ import (
)

func TestAccProjectDSProject_byID(t *testing.T) {
projectName := acctest.RandomWithPrefix("test-acc")
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

}

var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
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)

ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectDSByIDUsingRS(acc.ConfigProject(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand All @@ -50,27 +46,24 @@ func TestAccProjectDSProject_byID(t *testing.T) {
}

func TestAccProjectDSProject_byName(t *testing.T) {
projectName := acctest.RandomWithPrefix("test-acc")
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")
}

var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectDSByNameUsingRS(acc.ConfigProject(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{

TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand All @@ -86,27 +79,24 @@ func TestAccProjectDSProject_byName(t *testing.T) {
}

func TestAccProjectDSProject_defaultFlags(t *testing.T) {
projectName := acctest.RandomWithPrefix("test-acc")
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")
}

var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectDSByNameUsingRS(acc.ConfigProject(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{

TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand All @@ -128,9 +118,10 @@ func TestAccProjectDSProject_defaultFlags(t *testing.T) {
}

func TestAccProjectDSProject_limits(t *testing.T) {
projectName := acctest.RandomWithPrefix("test-acc")
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")

var (
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Expand Down
13 changes: 5 additions & 8 deletions internal/service/project/data_source_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

results := make([]*tfProjectDSModel, len(projectsRes.Results))

for i, project := range projectsRes.Results {
results := make([]*tfProjectDSModel, 0, len(projectsRes.Results))
for _, project := range projectsRes.Results {
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.

projectModel := newTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits)
results = append(results, &projectModel)
}
projectModel := newTFProjectDataSourceModel(ctx, project, atlasTeams, atlasProjectSettings, atlasLimits)
results[i] = &projectModel
}

stateModel.Results = results
stateModel.TotalCount = types.Int64Value(int64(projectsRes.TotalCount))
stateModel.ID = types.StringValue(id.UniqueId())
Expand Down
35 changes: 14 additions & 21 deletions internal/service/project/data_source_projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package project_test
import (
"fmt"
"os"
"strings"
"testing"

matlas "go.mongodb.org/atlas/mongodbatlas"
Expand All @@ -14,26 +13,23 @@ import (
)

func TestAccProjectDSProjects_basic(t *testing.T) {
projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
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")
}

var (
projectName = fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectsConfigWithDS(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand All @@ -52,26 +48,23 @@ func TestAccProjectDSProjects_basic(t *testing.T) {
}

func TestAccProjectDSProjects_withPagination(t *testing.T) {
projectName := fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
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")
}

var (
projectName = fmt.Sprintf("test-datasource-project-%s", acctest.RandString(10))
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
)
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 2) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasProjectsConfigWithPagination(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand Down
30 changes: 11 additions & 19 deletions internal/service/project/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package project_test
import (
"os"
"regexp"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -20,26 +19,21 @@ func TestAccProjectRSProject_basic(t *testing.T) {
projectName = acctest.RandomWithPrefix("test-acc")
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
clusterCount = "0"
teamsIds = strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",")
)
if len(teamsIds) < 3 {
t.Skip("`MONGODB_ATLAS_TEAMS_IDS` must have 3 team ids for this acceptance testing")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 3) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProject,
Steps: []resource.TestStep{
{
Config: acc.ConfigProject(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_ADMIN", "GROUP_OWNER"},
},
},
Expand All @@ -57,15 +51,15 @@ func TestAccProjectRSProject_basic(t *testing.T) {
Config: acc.ConfigProject(projectName, orgID,
[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_OWNER"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_DATA_ACCESS_READ_WRITE"},
},
{
TeamID: teamsIds[2],
TeamID: acc.GetProjectTeamsIds(2),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_DATA_ACCESS_ADMIN"},
},
},
Expand All @@ -84,11 +78,11 @@ func TestAccProjectRSProject_basic(t *testing.T) {

[]*matlas.ProjectTeam{
{
TeamID: teamsIds[0],
TeamID: acc.GetProjectTeamsIds(0),
RoleNames: []string{"GROUP_READ_ONLY", "GROUP_READ_ONLY"},
},
{
TeamID: teamsIds[1],
TeamID: acc.GetProjectTeamsIds(1),
RoleNames: []string{"GROUP_OWNER", "GROUP_DATA_ACCESS_ADMIN"},
},
},
Expand Down Expand Up @@ -232,24 +226,22 @@ func TestAccProjectRSProject_withUpdatedRole(t *testing.T) {
roleName = "GROUP_DATA_ACCESS_ADMIN"
roleNameUpdated = "GROUP_READ_ONLY"
clusterCount = "0"
teamsIds = strings.Split(os.Getenv("MONGODB_ATLAS_TEAMS_IDS"), ",")
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckTeamsIds(t) },
PreCheck: func() { acc.PreCheckBasic(t); acc.PreCheckProjectTeamsIds(t, 1) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroyProject,
Steps: []resource.TestStep{
{
Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, teamsIds[0], roleName),
Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, acc.GetProjectTeamsIds(0), roleName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", projectName),
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
resource.TestCheckResourceAttr(resourceName, "cluster_count", clusterCount),
),
},
{
Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, teamsIds[0], roleNameUpdated),
Config: acc.ConfigProjectWithUpdatedRole(projectName, orgID, acc.GetProjectTeamsIds(0), roleNameUpdated),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", projectName),
resource.TestCheckResourceAttr(resourceName, "org_id", orgID),
Expand Down
23 changes: 20 additions & 3 deletions internal/testutil/acc/pre_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acc

import (
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -45,10 +46,26 @@ func PreCheckAtlasUsername(tb testing.TB) {
}
}

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

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.

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)

envVar := os.Getenv("MONGODB_ATLAS_TEAMS_IDS")
if envVar == "" {
tb.Fatal("`MONGODB_ATLAS_TEAMS_IDS` must be set for Projects acceptance testing")
return
}
teamsIds := strings.Split(envVar, ",")
if count := len(teamsIds); count < min {
tb.Fatalf("`MONGODB_ATLAS_TEAMS_IDS` must have at least %d team ids for this acceptance testing, has %d", min, count)
}
}

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)

teamsIds := strings.Split(envVar, ",")
count := len(teamsIds)
if envVar == "" || pos >= count {
return ""
}
return teamsIds[pos]
}

func PreCheckGov(tb testing.TB) {
Expand Down