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

test: Refactors mongodbatlas_private_endpoint_regional_mode to use cluster info #2403

Merged
2 changes: 2 additions & 0 deletions .github/workflows/acceptance-tests-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ jobs:
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }}
AWS_REGION: ${{ vars.AWS_REGION }}
AWS_REGION_UPPERCASE: ${{ vars.AWS_REGION_UPPERCASE }}
AWS_REGION_LOWERCASE: ${{ vars.AWS_REGION_LOWERCASE }}
Comment on lines 650 to +652
Copy link
Member

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?

AWS_SECURITY_GROUP_1: ${{ vars.AWS_SECURITY_GROUP_1 }}
AWS_SECURITY_GROUP_2: ${{ vars.AWS_SECURITY_GROUP_2 }}
AWS_VPC_CIDR_BLOCK: ${{ vars.AWS_VPC_CIDR_BLOCK }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"strconv"
"strings"
"testing"

Expand All @@ -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"),
),
},
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -179,32 +176,6 @@ func checkExists(resourceName string) resource.TestCheckFunc {
}
}

func checkModeClustersUpToDate(projectID, clusterName, clusterResourceName 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.

not useful?

Copy link
Collaborator Author

@EspenAlbert EspenAlbert Jul 11, 2024

Choose a reason for hiding this comment

The 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" {
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions internal/testutil/acc/pre_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ func PreCheckAwsEnv(tb testing.TB) {
}
}

func PreCheckAwsRegionCases(tb testing.TB) {
tb.Helper()
if os.Getenv("AWS_REGION_UPPERCASE") == "" ||
os.Getenv("AWS_REGION_LOWERCASE") == "" {
tb.Fatal("`AWS_REGION_UPPERCASE`, `AWS_REGION_LOWERCASE` must be set for acceptance testing")
}
}

func PreCheckAwsEnvPrivateLinkEndpointService(tb testing.TB) {
tb.Helper()
if os.Getenv("AWS_ACCESS_KEY_ID") == "" ||
Expand Down