-
Notifications
You must be signed in to change notification settings - Fork 208
chore: Add project_settings_api acceptance tests #3751
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package projectsettingsapi_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
cleanup := acc.SetupSharedResources() | ||
exitCode := m.Run() | ||
cleanup() | ||
os.Exit(exitCode) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package projectsettingsapi_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/terraform" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
) | ||
|
||
const resourceName = "mongodbatlas_project_settings_api.test" | ||
|
||
func TestAccProjectSettingsAPI_basic(t *testing.T) { | ||
var ( | ||
projectID = acc.ProjectIDExecution(t) | ||
) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(t) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config(projectID, false), | ||
Check: check(projectID, false), | ||
}, | ||
{ | ||
Config: config(projectID, true), | ||
Check: check(projectID, true), | ||
}, | ||
{ | ||
Config: emptyConfig(projectID), | ||
Check: check(projectID, true), | ||
}, | ||
{ | ||
Config: config(projectID, false), | ||
Check: check(projectID, false), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportStateIdFunc: importStateIDFunc(resourceName), | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ImportStateVerifyIdentifierAttribute: "group_id", | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func config(projectID string, enabled bool) string { | ||
return fmt.Sprintf(` | ||
resource "mongodbatlas_project_settings_api" "test" { | ||
group_id = %[1]q | ||
is_collect_database_specifics_statistics_enabled = %[2]t | ||
is_data_explorer_enabled = %[2]t | ||
is_data_explorer_gen_ai_features_enabled = %[2]t | ||
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. q: Noticed this is the only attribute that is optional-only, does the API behave differently or we simply missed this additional config? 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. Good catch, adding config for this one. |
||
is_data_explorer_gen_ai_sample_document_passing_enabled = %[2]t | ||
is_extended_storage_sizes_enabled = %[2]t | ||
is_performance_advisor_enabled = %[2]t | ||
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: passing the same value to attributes could hide some assignment errors (e.g. we put in is_performance_advisor_enabled the value of is_extended_storage_sizes_enabled) 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 considered this but decided against testing each value individually as of now. |
||
is_realtime_performance_panel_enabled = %[2]t | ||
is_schema_advisor_enabled = %[2]t | ||
} | ||
`, projectID, enabled) | ||
} | ||
|
||
func emptyConfig(projectID string) string { | ||
return fmt.Sprintf(` | ||
resource "mongodbatlas_project_settings_api" "test" { | ||
group_id = %[1]q | ||
} | ||
`, projectID) | ||
} | ||
|
||
func check(projectID string, enabled bool) resource.TestCheckFunc { | ||
expected := strconv.FormatBool(enabled) | ||
attrChecks := map[string]string{ | ||
"group_id": projectID, | ||
"is_collect_database_specifics_statistics_enabled": expected, | ||
"is_data_explorer_enabled": expected, | ||
"is_data_explorer_gen_ai_features_enabled": expected, | ||
"is_data_explorer_gen_ai_sample_document_passing_enabled": expected, | ||
"is_extended_storage_sizes_enabled": expected, | ||
"is_performance_advisor_enabled": expected, | ||
"is_realtime_performance_panel_enabled": expected, | ||
"is_schema_advisor_enabled": expected, | ||
} | ||
checks := acc.AddAttrChecks(resourceName, nil, attrChecks) | ||
checks = append(checks, checkExists(resourceName)) | ||
return resource.ComposeAggregateTestCheckFunc(checks...) | ||
} | ||
|
||
func checkExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("not found: %s", resourceName) | ||
} | ||
groupID := rs.Primary.Attributes["group_id"] | ||
if groupID == "" { | ||
return fmt.Errorf("no group_id is set") | ||
} | ||
if _, _, err := acc.ConnV2().ProjectsApi.GetGroupSettings(context.Background(), groupID).Execute(); err == nil { | ||
return nil | ||
} | ||
return fmt.Errorf("project settings for project(%s) do not exist", groupID) | ||
} | ||
} | ||
|
||
func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { | ||
return func(s *terraform.State) (string, error) { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return "", fmt.Errorf("not found: %s", resourceName) | ||
} | ||
groupID := rs.Primary.Attributes["group_id"] | ||
return groupID, nil | ||
} | ||
} |
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.
nit: Could be could to add a step where some or all attributes are not defined, given they are O+C values in state would not change.
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.
👌 Added an empty config step.