Skip to content

Commit 7304d61

Browse files
authored
chore: Supports running cloud gov tests in CI (#2302)
1 parent 9fc1189 commit 7304d61

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

.github/workflows/acceptance-tests-runner.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ on:
7373
mongodb_atlas_federated_org_id:
7474
type: string
7575
required: true
76+
mongodb_atlas_gov_org_id:
77+
type: string
78+
required: true
79+
mongodb_atlas_gov_base_url:
80+
type: string
81+
required: true
7682
mongodb_atlas_federated_settings_associated_domain:
7783
type: string
7884
required: true
@@ -107,6 +113,10 @@ on:
107113
required: true
108114
mongodb_atlas_private_endpoint_dns_name:
109115
required: true
116+
mongodb_atlas_gov_private_key:
117+
required: true
118+
mongodb_atlas_gov_public_key:
119+
required: true
110120
azure_directory_id:
111121
required: true
112122
azure_resource_group_name:
@@ -128,6 +138,11 @@ env:
128138
MONGODB_ATLAS_ORG_ID: ${{ inputs.mongodb_atlas_org_id }}
129139
MONGODB_ATLAS_PUBLIC_KEY: ${{ secrets.mongodb_atlas_public_key }}
130140
MONGODB_ATLAS_PRIVATE_KEY: ${{ secrets.mongodb_atlas_private_key }}
141+
MONGODB_ATLAS_GOV_PUBLIC_KEY: ${{ secrets.mongodb_atlas_gov_public_key }}
142+
MONGODB_ATLAS_GOV_PRIVATE_KEY: ${{ secrets.mongodb_atlas_gov_private_key }}
143+
MONGODB_ATLAS_GOV_BASE_URL: ${{ inputs.mongodb_atlas_gov_base_url }}
144+
MONGODB_ATLAS_GOV_ORG_ID: ${{ inputs.mongodb_atlas_gov_org_id }}
145+
131146

132147
jobs:
133148

.github/workflows/acceptance-tests.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ jobs:
5151
secrets:
5252
mongodb_atlas_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_QA || secrets.MONGODB_ATLAS_PUBLIC_KEY_CLOUD_DEV }}
5353
mongodb_atlas_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_QA || secrets.MONGODB_ATLAS_PRIVATE_KEY_CLOUD_DEV }}
54+
mongodb_atlas_gov_public_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_QA || secrets.MONGODB_ATLAS_GOV_PUBLIC_KEY_DEV }}
55+
mongodb_atlas_gov_private_key: ${{ inputs.atlas_cloud_env == 'qa' && secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_QA || secrets.MONGODB_ATLAS_GOV_PRIVATE_KEY_DEV }}
5456
ca_cert: ${{ secrets.CA_CERT }}
5557
aws_account_id: ${{ secrets.AWS_ACCOUNT_ID }}
5658
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
@@ -92,4 +94,6 @@ jobs:
9294
mongodb_atlas_federated_sso_url: ${{ vars.MONGODB_ATLAS_FEDERATED_SSO_URL }}
9395
mongodb_atlas_federated_issuer_uri: ${{ vars.MONGODB_ATLAS_FEDERATED_ISSUER_URI }}
9496
mongodb_atlas_federated_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_FEDERATED_ORG_ID_QA || vars.MONGODB_ATLAS_FEDERATED_ORG_ID }}
97+
mongodb_atlas_gov_base_url: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_BASE_URL_QA || vars.MONGODB_ATLAS_GOV_BASE_URL_DEV }}
98+
mongodb_atlas_gov_org_id: ${{ inputs.atlas_cloud_env == 'qa' && vars.MONGODB_ATLAS_GOV_ORG_ID_QA || vars.MONGODB_ATLAS_GOV_ORG_ID_DEV }}
9599
mongodb_atlas_federated_settings_associated_domain: ${{ vars.MONGODB_ATLAS_FEDERATED_SETTINGS_ASSOCIATED_DOMAIN }}

contributing/testing-best-practices.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
- 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).
3333
- 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).
3434

35+
### Cloud Gov tests
36+
37+
1. Use [`PreCheck: PreCheckGovBasic`](https://github.com/mongodb/terraform-provider-mongodbatlas/blob/CLOUDP-250271_cloud_gov/internal/testutil/acc/pre_check.go#L98)
38+
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
39+
3. Modify the `checkExist` and `CheckDestroy` to use `acc.ConnV2UsingGov`
40+
4. Follow naming convention:
41+
1. `TestAccGovProject_withProjectOwner`, note prefix: `TestAccGov`
42+
2. `TestMigGovProject_regionUsageRestrictionsDefault`, note prefix: `TestMigGov`
43+
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
44+
3545
## Migration tests
3646

3747
- 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).

internal/testutil/acc/factory.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ func ConnV2UsingProxy(proxyPort *int) *admin.APIClient {
5151
return client.(*config.MongoDBClient).AtlasV2
5252
}
5353

54+
func ConnV2UsingGov() *admin.APIClient {
55+
cfg := config.Config{
56+
PublicKey: os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"),
57+
PrivateKey: os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"),
58+
BaseURL: os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"),
59+
}
60+
client, _ := cfg.NewClient(context.Background())
61+
return client.(*config.MongoDBClient).AtlasV2
62+
}
63+
5464
func init() {
5565
TestAccProviderV6Factories = map[string]func() (tfprotov6.ProviderServer, error){
5666
ProviderNameMongoDBAtlas: func() (tfprotov6.ProviderServer, error) {

internal/testutil/acc/pre_check.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,12 @@ func GetProjectTeamsIDsWithPos(pos int) string {
9797

9898
func PreCheckGovBasic(tb testing.TB) {
9999
tb.Helper()
100-
if os.Getenv("MONGODB_ATLAS_PUBLIC_KEY") == "" ||
101-
os.Getenv("MONGODB_ATLAS_PRIVATE_KEY") == "" ||
102-
os.Getenv("MONGODB_ATLAS_ORG_ID_GOV") == "" {
103-
tb.Fatal("`MONGODB_ATLAS_PUBLIC_KEY`, `MONGODB_ATLAS_PRIVATE_KEY`and `MONGODB_ATLAS_ORG_ID_GOV` must be set for acceptance testing")
104-
}
105-
}
106-
107-
func PreCheckGov(tb testing.TB) {
108-
tb.Helper()
109-
if os.Getenv("MONGODB_ATLAS_PROJECT_ID_GOV") == "" {
110-
tb.Fatal("`MONGODB_ATLAS_PROJECT_ID_GOV` must be set for acceptance testing")
100+
if os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY") == "" ||
101+
os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY") == "" ||
102+
os.Getenv("MONGODB_ATLAS_GOV_BASE_URL") == "" ||
103+
os.Getenv("MONGODB_ATLAS_GOV_ORG_ID") == "" {
104+
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")
111105
}
112-
PreCheckGovBasic(tb)
113106
}
114107

115108
func PreCheckGPCEnv(tb testing.TB) {

internal/testutil/acc/provider.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package acc
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
58
)
69

@@ -38,3 +41,23 @@ func providerAWS() *resource.ExternalProvider {
3841
Source: "hashicorp/aws",
3942
}
4043
}
44+
45+
// configProvider creates a new provider with credentials explicit in config.
46+
//
47+
// This can be used when you want credentials different from the default env-vars.
48+
func configProvider(publicKey, privateKey, baseURL string) string {
49+
return fmt.Sprintf(`
50+
provider %[1]q {
51+
public_key = %[2]q
52+
private_key = %[3]q
53+
base_url = %[4]q
54+
}
55+
`, ProviderNameMongoDBAtlas, publicKey, privateKey, baseURL)
56+
}
57+
58+
// ConfigGovProvider creates provider using MONGODB_ATLAS_GOV_* env vars.
59+
//
60+
// Remember to use PreCheckGovBasic when using this.
61+
func ConfigGovProvider() string {
62+
return configProvider(os.Getenv("MONGODB_ATLAS_GOV_PUBLIC_KEY"), os.Getenv("MONGODB_ATLAS_GOV_PRIVATE_KEY"), os.Getenv("MONGODB_ATLAS_GOV_BASE_URL"))
63+
}

0 commit comments

Comments
 (0)