Skip to content

Commit

Permalink
chore: Enables tests in CI for `mongodbatlas_privatelink_endpoint_ser…
Browse files Browse the repository at this point in the history
…vice_data_federation_online_archive` resource (#2001)

* fix prechecks to fail if not met

* skip gov test

* enable basic test

* refactor PreCheckPrivateEndpoint

* move vars inside the tests

* ProjectIDPrivateEndpoint

* config vars in CI

* fail if project id is not found

* use global project

* don't run project id in uni tests

* comment SkipInUnitTest

* Update internal/testutil/acc/skip.go

Co-authored-by: Agustin Bettati <bettatiagustin@gmail.com>

* error message when project name is not found

* no need to use Sprintf

---------

Co-authored-by: Agustin Bettati <bettatiagustin@gmail.com>
  • Loading branch information
lantoli and AgustinBettati authored Mar 11, 2024
1 parent 0e99328 commit 7f8a674
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 55 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/acceptance-tests-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ on:
required: true
mongodb_atlas_ldap_ca_certificate:
required: true

mongodb_atlas_private_endpoint_id:
required: true
mongodb_atlas_private_endpoint_dns_name:
required: true

env:
TF_ACC: 1
TF_LOG: ${{ vars.LOG_LEVEL }}
Expand Down Expand Up @@ -417,6 +421,8 @@ jobs:
terraform_wrapper: false
- name: Acceptance Tests
env:
MONGODB_ATLAS_PRIVATE_ENDPOINT_ID: ${{ secrets.mongodb_atlas_private_endpoint_id }}
MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME: ${{ secrets.mongodb_atlas_private_endpoint_dns_name }}
AWS_ACCOUNT_ID: ${{ secrets.aws_account_id }}
AWS_ACCESS_KEY_ID: ${{ secrets.aws_access_key_id }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.aws_secret_access_key }}
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ jobs:
mongodb_atlas_ldap_password: ${{ secrets.MONGODB_ATLAS_LDAP_PASSWORD }}
mongodb_atlas_ldap_port: ${{ secrets.MONGODB_ATLAS_LDAP_PORT }}
mongodb_atlas_ldap_ca_certificate: ${{ secrets.MONGODB_ATLAS_LDAP_CA_CERTIFICATE }}
mongodb_atlas_private_endpoint_id: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_ID }}
mongodb_atlas_private_endpoint_dns_name: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME }}

with:
terraform_version: ${{ inputs.terraform_version || vars.TF_VERSION_LATEST }}
ref: ${{ inputs.ref }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/migration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ jobs:
terraform_wrapper: false
- name: Migration Tests
env:
MONGODB_ATLAS_PRIVATE_ENDPOINT_ID: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_ID }}
MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME: ${{ secrets.MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME }}
AWS_ACCOUNT_ID: ${{ secrets.AWS_ACCOUNT_ID }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY_ID }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package privatelinkendpointservicedatafederationonlinearchive_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -13,15 +14,19 @@ var (
)

func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { acc.PreCheckPrivateEndpoint(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: dataSourcesConfigBasic(projectID, endpointID),
Config: dataSourcesConfigBasic(projectID, endpointID, customerEndpointDNSName),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(dataSourcePrivatelinkEndpointServiceDataFederetionDataArchive, "project_id", projectID),
Expand All @@ -37,7 +42,7 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiveDS_basic
})
}

func dataSourcesConfigBasic(projectID, endpointID string) string {
func dataSourcesConfigBasic(projectID, endpointID, customerEndpointDNSName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive" "test" {
project_id = %[1]q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package privatelinkendpointservicedatafederationonlinearchive_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -13,10 +14,13 @@ var (
)

func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchivesDSPlural_basic(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { acc.PreCheckPrivateEndpoint(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package privatelinkendpointservicedatafederationonlinearchive_test

import (
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/mig"
)

func TestAccMigrationNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
config = resourceConfigBasic(projectID, endpointID, comment)
)

resource.Test(t, resource.TestCase{
PreCheck: func() { mig.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { mig.PreCheckPrivateEndpoint(t) },
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
ExternalProviders: mig.ExternalProviders(),
Config: resourceConfigBasic(projectID, endpointID, comment),
Config: config,
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
Expand All @@ -28,16 +32,7 @@ func TestAccMigrationNetworkPrivatelinkEndpointServiceDataFederationOnlineArchiv
resource.TestCheckResourceAttrSet(resourceName, "provider_name"),
),
},
{
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
Config: resourceConfigBasic(projectID, endpointID, comment),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
acc.DebugPlan(),
plancheck.ExpectEmptyPlan(),
},
},
},
mig.TestStepCheckEmptyPlan(config),
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (
)

var (
resourceName = "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive.test"
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_ID")
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME")
comment = "Terraform Acceptance Test"
atlasRegion = "US_EAST_1"
resourceName = "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive.test"
comment = "Terraform Acceptance Test"
atlasRegion = "US_EAST_1"
)

func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { acc.PreCheckPrivateEndpoint(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
Expand All @@ -50,11 +50,14 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basic(t
})
}
func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateComment(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
commentUpdated := "Terraform Acceptance Test Updated"
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
commentUpdated = "Terraform Acceptance Test Updated"
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { acc.PreCheckPrivateEndpoint(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -94,15 +97,19 @@ func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_updateC
}

func TestAccNetworkPrivatelinkEndpointServiceDataFederationOnlineArchive_basicWithRegionDnsName(t *testing.T) {
// Skip because private endpoints are deleted daily from dev environment
acc.SkipTestForCI(t)
var (
projectID = acc.ProjectIDGlobal(t)
endpointID = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID")
customerEndpointDNSName = os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.PreCheck(t); acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(t) },
PreCheck: func() { acc.PreCheckPrivateEndpoint(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: checkDestroy,
Steps: []resource.TestStep{
{
Config: resourceConfigBasicWithRegionDNSName(projectID, endpointID, comment),
Config: resourceConfigBasicWithRegionDNSName(projectID, endpointID, comment, customerEndpointDNSName),
Check: resource.ComposeTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
Expand Down Expand Up @@ -180,7 +187,7 @@ func resourceConfigBasic(projectID, endpointID, comment string) string {
`, projectID, endpointID, comment)
}

func resourceConfigBasicWithRegionDNSName(projectID, endpointID, comment string) string {
func resourceConfigBasicWithRegionDNSName(projectID, endpointID, comment, customerEndpointDNSName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_privatelink_endpoint_service_data_federation_online_archive" "test" {
project_id = %[1]q
Expand Down
1 change: 1 addition & 0 deletions internal/service/project/resource_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ func TestAccProjectRSProject_withProjectOwner(t *testing.T) {
}

func TestAccProjectRSGovProject_withProjectOwner(t *testing.T) {
acc.SkipTestForCI(t) // Gov test config not set
var (
group admin.Group
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID_GOV")
Expand Down
26 changes: 22 additions & 4 deletions internal/testutil/acc/name.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package acc

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/stretchr/testify/require"
)

const (
prefixName = "test-acc-tf"
prefixProject = prefixName + "-p"
prefixCluster = prefixName + "-c"
prefixIAMRole = "mongodb-atlas-" + prefixName
prefixName = "test-acc-tf"
prefixProject = prefixName + "-p"
prefixProjectKeep = prefixProject + "-keep"
prefixCluster = prefixName + "-c"
prefixIAMRole = "mongodb-atlas-" + prefixName
)

func RandomName() string {
Expand All @@ -36,3 +40,17 @@ func RandomIP(a, b, c byte) string {
func RandomEmail() string {
return fmt.Sprintf("%s-%s@mongodb.com", prefixName, acctest.RandString(10))
}

func ProjectIDGlobal(tb testing.TB) string {
tb.Helper()
return projectID(tb, prefixProjectKeep+"-global")
}

func projectID(tb testing.TB, name string) string {
tb.Helper()
SkipInUnitTest(tb)
resp, _, _ := ConnV2().ProjectsApi.GetProjectByName(context.Background(), name).Execute()
id := resp.GetId()
require.NotEmpty(tb, id, "Project name not found: %s", name)
return id
}
14 changes: 8 additions & 6 deletions internal/testutil/acc/pre_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func PreCheckGov(tb testing.TB) {
os.Getenv("MONGODB_ATLAS_PRIVATE_KEY") == "" ||
os.Getenv("MONGODB_ATLAS_PROJECT_ID_GOV") == "" ||
os.Getenv("MONGODB_ATLAS_ORG_ID_GOV") == "" {
tb.Skip()
tb.Fatal("`MONGODB_ATLAS_PUBLIC_KEY`, `MONGODB_ATLAS_PRIVATE_KEY`, `MONGODB_ATLAS_PROJECT_ID_GOV` and `MONGODB_ATLAS_ORG_ID_GOV` must be set for acceptance testing")
}
}

Expand Down Expand Up @@ -202,15 +202,15 @@ func PreCheckSTSAssumeRole(tb testing.TB) {
func PreCheckDataLakePipelineRun(tb testing.TB) {
tb.Helper()
if os.Getenv("MONGODB_ATLAS_DATA_LAKE_PIPELINE_RUN_ID") == "" {
tb.Skip("`MONGODB_ATLAS_DATA_LAKE_PIPELINE_RUN_ID` must be set for Projects acceptance testing")
tb.Fatal("`MONGODB_ATLAS_DATA_LAKE_PIPELINE_RUN_ID` must be set for Projects acceptance testing")
}
PreCheckDataLakePipelineRuns(tb)
}

func PreCheckDataLakePipelineRuns(tb testing.TB) {
tb.Helper()
if os.Getenv("MONGODB_ATLAS_DATA_LAKE_PIPELINE_NAME") == "" {
tb.Skip("`MONGODB_ATLAS_DATA_LAKE_PIPELINE_NAME` must be set for Projects acceptance testing")
tb.Fatal("`MONGODB_ATLAS_DATA_LAKE_PIPELINE_NAME` must be set for Projects acceptance testing")
}
PreCheck(tb)
}
Expand Down Expand Up @@ -242,11 +242,13 @@ func PreCheckFederatedSettings(tb testing.TB) {
}
}

func PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(tb testing.TB) {
func PreCheckPrivateEndpoint(tb testing.TB) {
tb.Helper()
if os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") == "" {
tb.Skip("`MONGODB_ATLAS_PRIVATE_ENDPOINT_ID` must be set for Private Endpoint Service Data Federation and Online Archive acceptance testing")
if os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_ID") == "" ||
os.Getenv("MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME") == "" {
tb.Fatal("`MONGODB_ATLAS_PRIVATE_ENDPOINT_ID` and `MONGODB_ATLAS_PRIVATE_ENDPOINT_DNS_NAME`must be set for Private Endpoint Service Data Federation and Online Archive acceptance testing")
}
PreCheckBasic(tb)
}

func PreCheckS3Bucket(tb testing.TB) {
Expand Down
3 changes: 2 additions & 1 deletion internal/testutil/acc/skip.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func SkipTestForCI(tb testing.TB) {
}
}

// SkipInUnitTest is rarely needed, it is used in acc and mig tests to make sure that they don't run in unit test mode.
// SkipInUnitTest allows skipping a test entirely when TF_ACC=1 is not defined.
// This can be useful for acceptance tests that define logic prior to resource.Test/resource.ParallelTest functions as this code would always be run.
func SkipInUnitTest(tb testing.TB) {
tb.Helper()
if os.Getenv("TF_ACC") == "" {
Expand Down
4 changes: 2 additions & 2 deletions internal/testutil/mig/pre_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func PreCheckAtlasUsername(tb testing.TB) {
acc.PreCheckAtlasUsername(tb)
}

func PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(tb testing.TB) {
func PreCheckPrivateEndpoint(tb testing.TB) {
tb.Helper()
checkLastVersion(tb)
acc.PreCheckPrivateEndpointServiceDataFederationOnlineArchiveRun(tb)
acc.PreCheckPrivateEndpoint(tb)
}

0 comments on commit 7f8a674

Please sign in to comment.