From 7f8a6749154b914010507279d96ffd1014fed3ee Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Mon, 11 Mar 2024 12:14:51 +0100 Subject: [PATCH] chore: Enables tests in CI for `mongodbatlas_privatelink_endpoint_service_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 * error message when project name is not found * no need to use Sprintf --------- Co-authored-by: Agustin Bettati --- .github/workflows/acceptance-tests-runner.yml | 8 +++- .github/workflows/acceptance-tests.yml | 3 ++ .github/workflows/migration-tests.yml | 2 + ...ice_data_federation_online_archive_test.go | 15 ++++--- ...ce_data_federation_online_archives_test.go | 10 +++-- ...ederation_online_archive_migration_test.go | 25 +++++------ ...ice_data_federation_online_archive_test.go | 43 +++++++++++-------- .../service/project/resource_project_test.go | 1 + internal/testutil/acc/name.go | 26 +++++++++-- internal/testutil/acc/pre_check.go | 14 +++--- internal/testutil/acc/skip.go | 3 +- internal/testutil/mig/pre_check.go | 4 +- 12 files changed, 99 insertions(+), 55 deletions(-) diff --git a/.github/workflows/acceptance-tests-runner.yml b/.github/workflows/acceptance-tests-runner.yml index 6f65eb614c..0619f0616b 100644 --- a/.github/workflows/acceptance-tests-runner.yml +++ b/.github/workflows/acceptance-tests-runner.yml @@ -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 }} @@ -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 }} diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index f8f9684374..be780f6a61 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -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 }} diff --git a/.github/workflows/migration-tests.yml b/.github/workflows/migration-tests.yml index acfd808d45..27d434de9d 100644 --- a/.github/workflows/migration-tests.yml +++ b/.github/workflows/migration-tests.yml @@ -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 }} diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go index f991823cf1..d4e7c705f6 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archive_test.go @@ -2,6 +2,7 @@ package privatelinkendpointservicedatafederationonlinearchive_test import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -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), @@ -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 diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go index 1116732697..79ed6d5c94 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/data_source_privatelink_endpoint_service_data_federation_online_archives_test.go @@ -2,6 +2,7 @@ package privatelinkendpointservicedatafederationonlinearchive_test import ( "fmt" + "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -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{ diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go index 63b753c92b..7829bfb736 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_migration_test.go @@ -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), @@ -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), }, }) } diff --git a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go index 7ba277eff7..1d9c553b61 100644 --- a/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go +++ b/internal/service/privatelinkendpointservicedatafederationonlinearchive/resource_privatelink_endpoint_service_data_federation_online_archive_test.go @@ -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{ @@ -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{ @@ -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), @@ -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 diff --git a/internal/service/project/resource_project_test.go b/internal/service/project/resource_project_test.go index 2b9194ff91..e0498a09d8 100644 --- a/internal/service/project/resource_project_test.go +++ b/internal/service/project/resource_project_test.go @@ -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") diff --git a/internal/testutil/acc/name.go b/internal/testutil/acc/name.go index 7b50a59e5e..e739efd4e1 100644 --- a/internal/testutil/acc/name.go +++ b/internal/testutil/acc/name.go @@ -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 { @@ -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 +} diff --git a/internal/testutil/acc/pre_check.go b/internal/testutil/acc/pre_check.go index fd4a5d2060..3675147ac6 100644 --- a/internal/testutil/acc/pre_check.go +++ b/internal/testutil/acc/pre_check.go @@ -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") } } @@ -202,7 +202,7 @@ 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) } @@ -210,7 +210,7 @@ func PreCheckDataLakePipelineRun(tb testing.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) } @@ -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) { diff --git a/internal/testutil/acc/skip.go b/internal/testutil/acc/skip.go index 801f95ce72..86c413da33 100644 --- a/internal/testutil/acc/skip.go +++ b/internal/testutil/acc/skip.go @@ -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") == "" { diff --git a/internal/testutil/mig/pre_check.go b/internal/testutil/mig/pre_check.go index add602d2d2..1154d08505 100644 --- a/internal/testutil/mig/pre_check.go +++ b/internal/testutil/mig/pre_check.go @@ -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) }