-
Notifications
You must be signed in to change notification settings - Fork 178
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
test: Refactors mongodbatlas_private_endpoint_regional_mode
to use cluster info
#2403
Merged
EspenAlbert
merged 10 commits into
master
from
CLOUDP-260920_get_cluster_info_privateendpointregionalmode
Jul 12, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
438c6d2
test: refactor to use cluster info
EspenAlbert 358ebfa
test: enable test in CI and fix duplicate zone name
EspenAlbert 20c36b2
test: use AWS_REGION_UPPERCASE and add pre-checks
EspenAlbert 2c7f73f
fix: use clusterResourceName
EspenAlbert c6424b5
test: fix GetClusterInfo call
EspenAlbert b02bc02
fix: pre check call
EspenAlbert ab532c2
fix: add UPPERCASE/LOWERCASE to network test suite
EspenAlbert ea473c5
test: Skip in ci since it is slow and use new GetClusterInfo api
EspenAlbert ad01de7
test: Fix the broken test and simpify assert statements
EspenAlbert 61f0ad7
test: enable in CI, after refactorings ~1230s
EspenAlbert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ import ( | |
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -18,51 +17,49 @@ func TestAccPrivateEndpointRegionalMode_basic(t *testing.T) { | |
} | ||
|
||
func TestAccPrivateEndpointRegionalMode_conn(t *testing.T) { | ||
acc.SkipTestForCI(t) // needs AWS configuration | ||
|
||
var ( | ||
endpointResourceSuffix = "atlasple" | ||
resourceSuffix = "atlasrm" | ||
resourceName = fmt.Sprintf("mongodbatlas_private_endpoint_regional_mode.%s", resourceSuffix) | ||
awsAccessKey = os.Getenv("AWS_ACCESS_KEY_ID") | ||
awsSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") | ||
providerName = "AWS" | ||
region = os.Getenv("AWS_REGION") | ||
projectID = acc.ProjectIDExecution(t) | ||
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") | ||
projectName = acc.RandomProjectName() | ||
clusterName = acc.RandomClusterName() | ||
clusterResourceName = "test" | ||
clusterResource = acc.ConfigClusterGlobal(orgID, projectName, clusterName) | ||
clusterDataSource = modeClusterData(clusterResourceName, resourceSuffix, endpointResourceSuffix) | ||
endpointResources = testConfigUnmanagedAWS( | ||
endpointResourceSuffix = "atlasple" | ||
resourceSuffix = "atlasrm" | ||
resourceName = fmt.Sprintf("mongodbatlas_private_endpoint_regional_mode.%s", resourceSuffix) | ||
awsAccessKey = os.Getenv("AWS_ACCESS_KEY_ID") | ||
awsSecretKey = os.Getenv("AWS_SECRET_ACCESS_KEY") | ||
providerName = "AWS" | ||
region = os.Getenv("AWS_REGION_LOWERCASE") | ||
privatelinkEndpointServiceResourceName = fmt.Sprintf("mongodbatlas_privatelink_endpoint_service.%s", endpointResourceSuffix) | ||
spec1 = acc.ReplicationSpecRequest{Region: os.Getenv("AWS_REGION_UPPERCASE"), ProviderName: providerName, ZoneName: "Zone 1"} | ||
spec2 = acc.ReplicationSpecRequest{Region: "US_WEST_2", ProviderName: providerName, ZoneName: "Zone 2"} | ||
clusterInfo = acc.GetClusterInfo(t, &acc.ClusterRequest{Geosharded: true, DiskSizeGb: 80, ReplicationSpecs: []acc.ReplicationSpecRequest{spec1, spec2}}) | ||
projectID = clusterInfo.ProjectID | ||
clusterResourceName = clusterInfo.ClusterResourceName | ||
clusterDataName = "data.mongodbatlas_advanced_cluster.test" | ||
endpointResources = testConfigUnmanagedAWS( | ||
awsAccessKey, awsSecretKey, projectID, providerName, region, endpointResourceSuffix, | ||
) | ||
dependencies = []string{clusterResource, clusterDataSource, endpointResources} | ||
clusterDataSource = modeClusterData(clusterResourceName, resourceName, privatelinkEndpointServiceResourceName) | ||
dependencies = []string{clusterInfo.ClusterTerraformStr, clusterDataSource, endpointResources} | ||
) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheck(t) }, | ||
PreCheck: func() { acc.PreCheckAwsEnvBasic(t); acc.PreCheckAwsRegionCases(t) }, | ||
ExternalProviders: acc.ExternalProvidersOnlyAWS(), | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: configWithDependencies(resourceSuffix, projectID, false, dependencies), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
checkExists(resourceName), | ||
checkModeClustersUpToDate(projectID, clusterName, clusterResourceName), | ||
resource.TestCheckResourceAttr(clusterDataName, "connection_strings.0.private_endpoint.#", "0"), | ||
resource.TestCheckResourceAttrSet(resourceName, "project_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "enabled"), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "false"), | ||
), | ||
}, | ||
{ | ||
Config: configWithDependencies(resourceSuffix, projectID, true, dependencies), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
checkExists(resourceName), | ||
checkModeClustersUpToDate(projectID, clusterName, clusterResourceName), | ||
resource.TestCheckResourceAttr(clusterDataName, "connection_strings.0.private_endpoint.#", "1"), | ||
resource.TestCheckResourceAttrSet(resourceName, "project_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "enabled"), | ||
resource.TestCheckResourceAttr(resourceName, "enabled", "true"), | ||
), | ||
}, | ||
|
@@ -113,12 +110,12 @@ func basicTestCase(tb testing.TB) *resource.TestCase { | |
|
||
func modeClusterData(clusterResourceName, regionalModeResourceName, privateLinkResourceName string) string { | ||
return fmt.Sprintf(` | ||
data "mongodbatlas_cluster" %[1]q { | ||
project_id = mongodbatlas_cluster.%[1]s.project_id | ||
name = mongodbatlas_cluster.%[1]s.name | ||
data "mongodbatlas_advanced_cluster" "test" { | ||
project_id = %[1]s.project_id | ||
name = %[1]s.name | ||
depends_on = [ | ||
mongodbatlas_privatelink_endpoint_service.%[3]s, | ||
mongodbatlas_private_endpoint_regional_mode.%[2]s | ||
%[2]s, | ||
%[3]s | ||
] | ||
} | ||
`, clusterResourceName, regionalModeResourceName, privateLinkResourceName) | ||
|
@@ -179,32 +176,6 @@ func checkExists(resourceName string) resource.TestCheckFunc { | |
} | ||
} | ||
|
||
func checkModeClustersUpToDate(projectID, clusterName, clusterResourceName string) resource.TestCheckFunc { | ||
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. not useful? 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. Super verbose and fails to report error when no PrivateEndpoint is set. (Only compares to whatever the sdk returns for the cluster) |
||
resourceName := strings.Join([]string{"data", "mongodbatlas_cluster", clusterResourceName}, ".") | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Could not find resource state for cluster (%s) on project (%s)", clusterName, projectID) | ||
} | ||
var rsPrivateEndpointCount int | ||
var err error | ||
if rsPrivateEndpointCount, err = strconv.Atoi(rs.Primary.Attributes["connection_strings.0.private_endpoint.#"]); err != nil { | ||
return fmt.Errorf("Connection strings private endpoint count is not a number") | ||
} | ||
c, _, _ := acc.Conn().Clusters.Get(context.Background(), projectID, clusterName) | ||
if rsPrivateEndpointCount != len(c.ConnectionStrings.PrivateEndpoint) { | ||
return fmt.Errorf("Cluster PrivateEndpoint count does not match resource") | ||
} | ||
if rs.Primary.Attributes["connection_strings.0.standard"] != c.ConnectionStrings.Standard { | ||
return fmt.Errorf("Cluster standard connection_string does not match resource") | ||
} | ||
if rs.Primary.Attributes["connection_strings.0.standard_srv"] != c.ConnectionStrings.StandardSrv { | ||
return fmt.Errorf("Cluster standard connection_string does not match resource") | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
func checkDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "mongodbatlas_private_endpoint_regional_mode" { | ||
|
@@ -221,14 +192,14 @@ func checkDestroy(s *terraform.State) error { | |
func testConfigUnmanagedAWS(awsAccessKey, awsSecretKey, projectID, providerName, region, serviceResourceName string) string { | ||
return fmt.Sprintf(` | ||
provider "aws" { | ||
region = "%[5]s" | ||
access_key = "%[1]s" | ||
secret_key = "%[2]s" | ||
region = %[5]q | ||
access_key = %[1]q | ||
secret_key = %[2]q | ||
} | ||
resource "mongodbatlas_privatelink_endpoint" "test" { | ||
project_id = "%[3]s" | ||
provider_name = "%[4]s" | ||
region = "%[5]s" | ||
project_id = %[3]q | ||
provider_name = %[4]q | ||
region = %[5]q | ||
} | ||
resource "aws_vpc_endpoint" "ptfe_service" { | ||
vpc_id = aws_vpc.primary.id | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
question: was there a reason for adding AWS_REGION_UPPERCASE and AWS_REGION_LOWERCASE, vs using AWS_REGION and using existing function
conversion.MongoDBRegionToAWSRegion
?