From bda1b02c0374f87b7004891a0d2cd2579f2a4bf5 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Fri, 24 May 2024 10:30:44 +0100 Subject: [PATCH 1/7] chore: Adds support for cloud-gov testing --- internal/testutil/acc/factory.go | 11 +++++++++++ internal/testutil/acc/pre_check.go | 17 +++++------------ internal/testutil/acc/provider.go | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/internal/testutil/acc/factory.go b/internal/testutil/acc/factory.go index 5b78bec254..c352b4998c 100644 --- a/internal/testutil/acc/factory.go +++ b/internal/testutil/acc/factory.go @@ -51,6 +51,17 @@ func ConnV2UsingProxy(proxyPort *int) *admin.APIClient { return client.(*config.MongoDBClient).AtlasV2 } +func ConnV2UsingGov() *admin.APIClient { + cfg := config.Config{ + PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), + PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), + BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"), + RealmBaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"), + } + client, _ := cfg.NewClient(context.Background()) + return client.(*config.MongoDBClient).AtlasV2 +} + func init() { TestAccProviderV6Factories = map[string]func() (tfprotov6.ProviderServer, error){ ProviderNameMongoDBAtlas: func() (tfprotov6.ProviderServer, error) { diff --git a/internal/testutil/acc/pre_check.go b/internal/testutil/acc/pre_check.go index 23e52636ce..49a6322379 100644 --- a/internal/testutil/acc/pre_check.go +++ b/internal/testutil/acc/pre_check.go @@ -97,19 +97,12 @@ func GetProjectTeamsIDsWithPos(pos int) string { func PreCheckGovBasic(tb testing.TB) { tb.Helper() - if os.Getenv("MONGODB_ATLAS_PUBLIC_KEY") == "" || - os.Getenv("MONGODB_ATLAS_PRIVATE_KEY") == "" || - os.Getenv("MONGODB_ATLAS_ORG_ID_GOV") == "" { - tb.Fatal("`MONGODB_ATLAS_PUBLIC_KEY`, `MONGODB_ATLAS_PRIVATE_KEY`and `MONGODB_ATLAS_ORG_ID_GOV` must be set for acceptance testing") - } -} - -func PreCheckGov(tb testing.TB) { - tb.Helper() - if os.Getenv("MONGODB_ATLAS_PROJECT_ID_GOV") == "" { - tb.Fatal("`MONGODB_ATLAS_PROJECT_ID_GOV` must be set for acceptance testing") + if os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY") == "" || + os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY") == "" || + os.Getenv("MONGODB_ATLAS_GOV_BASE_URL") == "" || + os.Getenv("MONGODB_ATLAS_GOV_ORG_ID") == "" { + tb.Fatal("`MONGODB_ATLAS_GOV_BASE_URL`, `MONGODB_ATLAS_GOV_PUBLIC_KEY`, `MONGODB_ATLAS_GOV_PRIVATE_KEY`and `MONGODB_ATLAS_GOV_ORG_ID` must be set for acceptance testing") } - PreCheckGovBasic(tb) } func PreCheckGPCEnv(tb testing.TB) { diff --git a/internal/testutil/acc/provider.go b/internal/testutil/acc/provider.go index 948b913e77..0867ce6aba 100644 --- a/internal/testutil/acc/provider.go +++ b/internal/testutil/acc/provider.go @@ -1,6 +1,9 @@ package acc import ( + "fmt" + "os" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) @@ -38,3 +41,23 @@ func providerAWS() *resource.ExternalProvider { Source: "hashicorp/aws", } } + +// ConfigProvider creates a new provider with credentials explicit in config. +// +// This can be used when you want credentials different from the default env-vars. +func ConfigProvider(publicKey, privateKey, baseURL string) string { + return fmt.Sprintf(` +provider %[1]q { + public_key = %[2]q + private_key = %[3]q + base_url = %[4]q +} +`, ProviderNameMongoDBAtlas, publicKey, privateKey, baseURL) +} + +// ConfigGovProvider creates provider using MONGODB_ATLAS_GOV_* env vars. +// +// Remember to use PreCheckGovBasic when using this. +func ConfigGovProvider() string { + return ConfigProvider(os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), os.Getenv("MONGODB_ATLAS_GOV_BASE_URL")) +} From 7dfb513c319a2066bf295efaa5e30b7dd67d3c72 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Fri, 24 May 2024 14:47:57 +0100 Subject: [PATCH 2/7] ci: update workflow files with new variables --- .github/workflows/acceptance-tests-runner.yml | 15 +++++++++++++++ .github/workflows/acceptance-tests.yml | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/.github/workflows/acceptance-tests-runner.yml b/.github/workflows/acceptance-tests-runner.yml index fd50f51b0a..0b1b2dc5fc 100644 --- a/.github/workflows/acceptance-tests-runner.yml +++ b/.github/workflows/acceptance-tests-runner.yml @@ -73,6 +73,12 @@ on: mongodb_atlas_federated_org_id: type: string required: true + mongodb_atlas_gov_org_id: + type: string + required: true + mongodb_atlas_gov_base_url: + type: string + required: true secrets: # all secrets are passed explicitly in this workflow mongodb_atlas_public_key: required: true @@ -104,6 +110,10 @@ on: required: true mongodb_atlas_private_endpoint_dns_name: required: true + mongodb_atlas_gov_private_key: + required: true + mongodb_atlas_gov_public_key: + required: true env: TF_ACC: 1 @@ -115,6 +125,11 @@ env: MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }} MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }} MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }} + MONGODB_ATLAS_GOV_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }} + MONGODB_ATLAS_GOV_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }} + MONGODB_ATLAS_GOV_BASE_URL: ${{ inputs.mongodb_atlas_gov_base_url }} + MONGODB_ATLAS_GOV_ORG_ID: ${{ inputs.mongodb_atlas_gov_org_id }} + jobs: diff --git a/.github/workflows/acceptance-tests.yml b/.github/workflows/acceptance-tests.yml index 28272bedf4..67f2bdf557 100644 --- a/.github/workflows/acceptance-tests.yml +++ b/.github/workflows/acceptance-tests.yml @@ -51,6 +51,8 @@ jobs: secrets: mongodb_atlas_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_QA || secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }} mongodb_atlas_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_QA || secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }} + mongodb_atlas_gov_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_QA || secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_DEV }} + mongodb_atlas_gov_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_QA || secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_DEV }} ca_cert: ${{ secrets.CA_CERT }} aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }} aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }} @@ -87,3 +89,5 @@ jobs: mongodb_atlas_federated_sso_url: ${{ vars.MONGODB_ATLAS_FEDERATED_SSO_URL }} mongodb_atlas_federated_issuer_uri: ${{ vars.MONGODB_ATLAS_FEDERATED_ISSUER_URI }} mongodb_atlas_federated_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_FEDERATED_ORG_ID_QA || vars.MONGODB_ATLAS_FEDERATED_ORG_ID }} + mongodb_atlas_gov_base_url: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_BASE_URL_QA || vars.MONGODB_ATLAS_GOV_BASE_URL_DEV }} + mongodb_atlas_gov_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_ORG_ID_QA || vars.MONGODB_ATLAS_GOV_ORG_ID_DEV }} From c48ce6e4e429a5c1e4d8ed2b1042b45a743682a6 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Fri, 24 May 2024 15:01:54 +0100 Subject: [PATCH 3/7] docs: Show how to write Cloud Gov tests --- contributing/testing-best-practices.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contributing/testing-best-practices.md b/contributing/testing-best-practices.md index fdd2140381..045fcf8693 100644 --- a/contributing/testing-best-practices.md +++ b/contributing/testing-best-practices.md @@ -32,6 +32,12 @@ - Data sources are tested in the same tests as the resources, e.g. [commonChecks](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/66c44e62c9afe04ffe8be0dbccaec682bab830e6/internal/service/searchindex/resource_search_index_test.go#L262-L263). - Helper functions such as `resource.TestCheckTypeSetElemNestedAttrs` or `resource.TestCheckTypeSetElemAttr` can be used to check resource and data source attributes more easily, e.g. [resource_serverless_instance_test.go](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/66c44e62c9afe04ffe8be0dbccaec682bab830e6/internal/service/serverlessinstance/resource_serverless_instance_test.go#L61). +### Cloud Gov tests + +1. Use [`PreCheck: PreCheckGovBasic`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-250271_cloud_gov/internal/testutil/acc/pre_check.go#L98) +2. Use the [`acc.ConfigGovProvider`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-250271_cloud_gov/internal/testutil/acc/provider.go#L61) together with your normal terraform config +3. Modify the `checkExist` and `CheckDestroy` to use `acc.ConnV2UsingGov` + ## Migration tests - There must be at least one `basic migration test` for each resource that leverages on the `basic acceptance tests` using helper test functions such as `CreateAndRunTest`, e.g. [TestMigServerlessInstance_basic](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/66c44e62c9afe04ffe8be0dbccaec682bab830e6/internal/service/serverlessinstance/resource_serverless_instance_migration_test.go#L10). From 9e01d6928230bc5c97a6f58951f7d6cc95da087b Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Fri, 24 May 2024 15:19:09 +0100 Subject: [PATCH 4/7] docs: add naming convention --- contributing/testing-best-practices.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contributing/testing-best-practices.md b/contributing/testing-best-practices.md index 045fcf8693..cdca2f2287 100644 --- a/contributing/testing-best-practices.md +++ b/contributing/testing-best-practices.md @@ -37,6 +37,9 @@ 1. Use [`PreCheck: PreCheckGovBasic`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-250271_cloud_gov/internal/testutil/acc/pre_check.go#L98) 2. Use the [`acc.ConfigGovProvider`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-250271_cloud_gov/internal/testutil/acc/provider.go#L61) together with your normal terraform config 3. Modify the `checkExist` and `CheckDestroy` to use `acc.ConnV2UsingGov` +4. Follow naming convention: + 1. `TestAccGovProject_withProjectOwner`, note prefix: `TestAccGov` + 2. `TestMigGovProject_regionUsageRestrictionsDefault`, note prefix: `TestMigGov` ## Migration tests From ee41811ef819d9255bf7f29db6c75f45e3014500 Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Fri, 24 May 2024 15:27:12 +0100 Subject: [PATCH 5/7] ci: fix, referencing the wrong gov private/public keys --- .github/workflows/acceptance-tests-runner.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/acceptance-tests-runner.yml b/.github/workflows/acceptance-tests-runner.yml index 0b1b2dc5fc..ec6eda42ef 100644 --- a/.github/workflows/acceptance-tests-runner.yml +++ b/.github/workflows/acceptance-tests-runner.yml @@ -125,8 +125,8 @@ env: MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }} MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }} MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }} - MONGODB_ATLAS_GOV_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }} - MONGODB_ATLAS_GOV_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }} + MONGODB_ATLAS_GOV_PUBLIC_KEY: ${{ secrets.mongodb_atlas_gov_public_key }} + MONGODB_ATLAS_GOV_PRIVATE_KEY: ${{ secrets.mongodb_atlas_gov_private_key }} MONGODB_ATLAS_GOV_BASE_URL: ${{ inputs.mongodb_atlas_gov_base_url }} MONGODB_ATLAS_GOV_ORG_ID: ${{ inputs.mongodb_atlas_gov_org_id }} From 71a3e277b7df33c37ff534801f219b7308d62cfc Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 27 May 2024 07:38:58 +0100 Subject: [PATCH 6/7] refactor: address PR comments --- internal/testutil/acc/factory.go | 7 +++---- internal/testutil/acc/provider.go | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/internal/testutil/acc/factory.go b/internal/testutil/acc/factory.go index c352b4998c..b2d6cc3b3f 100644 --- a/internal/testutil/acc/factory.go +++ b/internal/testutil/acc/factory.go @@ -53,10 +53,9 @@ func ConnV2UsingProxy(proxyPort *int) *admin.APIClient { func ConnV2UsingGov() *admin.APIClient { cfg := config.Config{ - PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), - PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), - BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"), - RealmBaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"), + PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), + PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), + BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"), } client, _ := cfg.NewClient(context.Background()) return client.(*config.MongoDBClient).AtlasV2 diff --git a/internal/testutil/acc/provider.go b/internal/testutil/acc/provider.go index 0867ce6aba..3c57702afd 100644 --- a/internal/testutil/acc/provider.go +++ b/internal/testutil/acc/provider.go @@ -42,10 +42,10 @@ func providerAWS() *resource.ExternalProvider { } } -// ConfigProvider creates a new provider with credentials explicit in config. +// configProvider creates a new provider with credentials explicit in config. // // This can be used when you want credentials different from the default env-vars. -func ConfigProvider(publicKey, privateKey, baseURL string) string { +func configProvider(publicKey, privateKey, baseURL string) string { return fmt.Sprintf(` provider %[1]q { public_key = %[2]q @@ -59,5 +59,5 @@ provider %[1]q { // // Remember to use PreCheckGovBasic when using this. func ConfigGovProvider() string { - return ConfigProvider(os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), os.Getenv("MONGODB_ATLAS_GOV_BASE_URL")) + return configProvider(os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), os.Getenv("MONGODB_ATLAS_GOV_BASE_URL")) } From c888aff4e28c166c2782c4420607c895f6c93edc Mon Sep 17 00:00:00 2001 From: EspenAlbert Date: Mon, 27 May 2024 08:57:44 +0100 Subject: [PATCH 7/7] docs: add more context around naming convention --- contributing/testing-best-practices.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contributing/testing-best-practices.md b/contributing/testing-best-practices.md index cdca2f2287..13f84a88cf 100644 --- a/contributing/testing-best-practices.md +++ b/contributing/testing-best-practices.md @@ -40,6 +40,7 @@ 4. Follow naming convention: 1. `TestAccGovProject_withProjectOwner`, note prefix: `TestAccGov` 2. `TestMigGovProject_regionUsageRestrictionsDefault`, note prefix: `TestMigGov` + 3. Although `Gov` tests can be run together with other acceptance tests, using the `Test(Acc|Mig)Gov` makes it easier to run *only* gov tests or find similar gov tests ## Migration tests