From 0fcb3f989436d03a21fd849e440750e66a474b56 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:28:18 -0600 Subject: [PATCH 01/14] Parameterize acceptance tests for local runs --- .gitignore | 3 +- README.md | 13 ++++- .../acctest_helper/acctest_helper.go | 53 +++++++++++++++++++ .../data_source_all_acceptance_test.go | 2 +- .../data_source_acceptance_test.go | 11 ++-- .../notification/resource_acceptance_test.go | 25 +++++---- .../resource_acceptance_test.go | 30 ++++++----- .../user/data_source_acceptance_test.go | 7 +-- .../user_groups_acceptance_test.go | 11 +--- .../resources/user_groups_acceptance_test.go | 16 +----- 10 files changed, 104 insertions(+), 67 deletions(-) diff --git a/.gitignore b/.gitignore index 611e48c6..4c48cab2 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ terraform-provider* autogen TODO.md NOTES.md -.DS_Store \ No newline at end of file +.DS_Store +.env \ No newline at end of file diff --git a/README.md b/README.md index acb48c9b..26f63020 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,18 @@ The CLI [dbtcloud-terraforming](https://github.com/dbt-labs/dbtcloud-terraformin ## Running Acceptance Tests Currently, acceptance tests, run via `make test-acceptance` must be done on your -own account +own account. + +To run the acceptance tests locally, you will need to set the following environment variables to appropriate values +for your dbt Cloud account: +``` +DBT_CLOUD_ACCOUNT_ID=1 +DBT_CLOUD_USER_ID=1 +DBT_CLOUD_USER_EMAIL= +DBT_CLOUD_TOKEN= +DBT_CLOUD_HOST_URL=https:///api +DBT_CLOUD_GROUP_IDS=1,2,3 +``` ## Acknowledgement diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index 1cf88f56..cb6268ba 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -2,6 +2,7 @@ package acctest_helper import ( "context" + "fmt" "log" "net/http" "os" @@ -77,6 +78,58 @@ func IsDbtCloudPR() bool { return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" } +// GetDbtCloudUserId returns the user ID to use for acceptance tests. +// Currently, this utilizes some legacy logic to determine the user ID. +// If the DBT_CLOUD_USER_ID environment variable is fully adopted, this +// function could be simplified to require that environment variable. +func GetDbtCloudUserId() int { + if IsDbtCloudPR() { + return 1 + } else if os.Getenv("CI") != "" { + return 54461 + } else { + id, err := strconv.Atoi(os.Getenv("DBT_CLOUD_USER_ID")) + if err != nil { + log.Fatalf("Unable to determine UserID for test: %v", err) + } + return id + } +} + +// GetDbtCloudUserEmail returns the user email to use for acceptance tests. +func GetDbtCloudUserEmail() string { + if IsDbtCloudPR() { + return "d" + "ev@" + "db" + "tla" + "bs.c" + "om" + } else if os.Getenv("CI") != "" { + return "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" + } else { + email := os.Getenv("DBT_CLOUD_USER_EMAIL") + if email == "" { + log.Fatalf("Unable to determine GroupIds for test") + } + return email + } +} + +// GetDbtCloudGroupIds returns the group IDs to use for acceptance tests. +// Currently, this utilizes some legacy logic to determine the group IDs. +// If the DBT_CLOUD_GROUP_IDS environment variable is fully adopted, this +// function could be simplified to require that environment variable. +func GetDbtCloudGroupIds() string { + var groupIds string + if IsDbtCloudPR() { + groupIds = "1,2,3" + } else if os.Getenv("CI") != "" { + groupIds = "531585,531584,531583" + } else { + groupIds = os.Getenv("DBT_CLOUD_GROUP_IDS") + if groupIds == "" { + log.Fatalf("Unable to determine GroupIds for test") + } + } + return fmt.Sprintf("[%s]", groupIds) +} + func HelperTestResourceSchema[R resource.Resource](t *testing.T, r R) { ctx := context.Background() diff --git a/pkg/framework/objects/environment/data_source_all_acceptance_test.go b/pkg/framework/objects/environment/data_source_all_acceptance_test.go index e9f9f6cc..6d4fbe71 100644 --- a/pkg/framework/objects/environment/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_all_acceptance_test.go @@ -2,10 +2,10 @@ package environment_test import ( "fmt" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" - "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) diff --git a/pkg/framework/objects/notification/data_source_acceptance_test.go b/pkg/framework/objects/notification/data_source_acceptance_test.go index 613ccb6c..f0c1f13f 100644 --- a/pkg/framework/objects/notification/data_source_acceptance_test.go +++ b/pkg/framework/objects/notification/data_source_acceptance_test.go @@ -16,12 +16,7 @@ func TestAccDbtCloudNotificationDataSource(t *testing.T) { t.Skip("Skipping notifications in dbt Cloud CI for now") } - var userID string - if acctest_helper.IsDbtCloudPR() { - userID = "1" - } else { - userID = "100" - } + userID := acctest_helper.GetDbtCloudUserId() currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-datasource@nomail.com", currentTime) @@ -58,7 +53,7 @@ func TestAccDbtCloudNotificationDataSource(t *testing.T) { }) } -func notification(projectName, userID, notificationEmail string) string { +func notification(projectName string, userID int, notificationEmail string) string { return fmt.Sprintf(` resource "dbtcloud_project" "test_notification_project" { name = "%s" @@ -86,7 +81,7 @@ func notification(projectName, userID, notificationEmail string) string { } resource "dbtcloud_notification" "test_notification_external" { - user_id = %s + user_id = %d on_failure = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 external_email = "%s" diff --git a/pkg/framework/objects/notification/resource_acceptance_test.go b/pkg/framework/objects/notification/resource_acceptance_test.go index 09db07f8..d20c8c4b 100644 --- a/pkg/framework/objects/notification/resource_acceptance_test.go +++ b/pkg/framework/objects/notification/resource_acceptance_test.go @@ -2,13 +2,13 @@ package notification_test import ( "fmt" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "regexp" "strings" "testing" "time" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" - "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" ) @@ -19,12 +19,7 @@ func TestAccDbtCloudNotificationResource(t *testing.T) { t.Skip("Skipping notifications in dbt Cloud CI for now") } - var userID string - if acctest_helper.IsDbtCloudPR() { - userID = "1" - } else { - userID = "100" - } + userID := acctest_helper.GetDbtCloudUserId() currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-resource@nomail.com", currentTime) @@ -185,12 +180,14 @@ resource "dbtcloud_job" "test_notification_job_2" { } func testAccDbtCloudNotificationResourceCreateNotifications( - projectName, userID, notificationEmail string, + projectName string, + userID int, + notificationEmail string, ) string { notificationsConfig := fmt.Sprintf(` resource "dbtcloud_notification" "test_notification_internal" { - user_id = %s + user_id = %d on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id, dbtcloud_job.test_notification_job_2.id] @@ -198,7 +195,7 @@ resource "dbtcloud_notification" "test_notification_internal" { } resource "dbtcloud_notification" "test_notification_external" { - user_id = %s + user_id = %d on_warning = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] notification_type = 4 @@ -209,12 +206,14 @@ resource "dbtcloud_notification" "test_notification_external" { } func testAccDbtCloudNotificationResourceModifyNotifications( - projectName, userID, notificationEmail string, + projectName string, + userID int, + notificationEmail string, ) string { notificationsConfig := fmt.Sprintf(` resource "dbtcloud_notification" "test_notification_internal" { - user_id = %s + user_id = %d on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [] @@ -223,7 +222,7 @@ resource "dbtcloud_notification" "test_notification_internal" { } resource "dbtcloud_notification" "test_notification_external" { - user_id = %s + user_id = %d on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 diff --git a/pkg/framework/objects/partial_notification/resource_acceptance_test.go b/pkg/framework/objects/partial_notification/resource_acceptance_test.go index cf694cb7..1e17d7e0 100644 --- a/pkg/framework/objects/partial_notification/resource_acceptance_test.go +++ b/pkg/framework/objects/partial_notification/resource_acceptance_test.go @@ -19,12 +19,12 @@ func TestAccDbtCloudPartialNotificationResource(t *testing.T) { t.Skip("Skipping notifications in dbt Cloud CI for now") } - var userID string - if acctest_helper.IsDbtCloudPR() { - userID = "1" - } else { - userID = "100" - } + userID := acctest_helper.GetDbtCloudUserId() + //if acctest_helper.IsDbtCloudPR() { + // userID = "1" + //} else { + // userID = "100" + //} currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-partial-resource@nomail.com", currentTime) @@ -180,12 +180,14 @@ resource "dbtcloud_job" "test_notification_job_2" { } func testAccDbtCloudPartialNotificationResourceCreatePartialNotifications( - projectName, userID, notificationEmail string, + projectName string, + userID int, + notificationEmail string, ) string { notificationsConfig := fmt.Sprintf(` resource "dbtcloud_partial_notification" "test_notification_internal" { - user_id = %s + user_id = %d on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id, dbtcloud_job.test_notification_job_2.id] @@ -193,7 +195,7 @@ resource "dbtcloud_partial_notification" "test_notification_internal" { } resource "dbtcloud_partial_notification" "test_notification_external" { - user_id = %s + user_id = %d on_warning = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] notification_type = 4 @@ -206,12 +208,14 @@ resource "dbtcloud_partial_notification" "test_notification_external" { } func testAccDbtCloudPartialNotificationResourceModifyPartialNotifications( - projectName, userID, notificationEmail string, + projectName string, + userID int, + notificationEmail string, ) string { notificationsConfig := fmt.Sprintf(` resource "dbtcloud_partial_notification" "test_notification_internal" { - user_id = %s + user_id = %d on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [] @@ -220,7 +224,7 @@ resource "dbtcloud_partial_notification" "test_notification_internal" { } resource "dbtcloud_partial_notification" "test_notification_external" { - user_id = %s + user_id = %d on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 @@ -228,7 +232,7 @@ resource "dbtcloud_partial_notification" "test_notification_external" { } resource "dbtcloud_partial_notification" "test_notification_external2" { - user_id = %s + user_id = %d on_success = [dbtcloud_job.test_notification_job_1.id, dbtcloud_job.test_notification_job_2.id] notification_type = 4 external_email = "%s" diff --git a/pkg/framework/objects/user/data_source_acceptance_test.go b/pkg/framework/objects/user/data_source_acceptance_test.go index 2b572b41..e8ec58a1 100644 --- a/pkg/framework/objects/user/data_source_acceptance_test.go +++ b/pkg/framework/objects/user/data_source_acceptance_test.go @@ -10,12 +10,7 @@ import ( func TestAccDbtCloudUserDataSource(t *testing.T) { - var userEmail string - if acctest_helper.IsDbtCloudPR() { - userEmail = "d" + "ev@" + "db" + "tla" + "bs.c" + "om" - } else { - userEmail = "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" - } + userEmail := acctest_helper.GetDbtCloudUserEmail() config := user(userEmail) diff --git a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go index f8d8f2f6..1934f643 100644 --- a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go @@ -2,7 +2,6 @@ package data_sources_test import ( "fmt" - "os" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -11,15 +10,7 @@ import ( func TestDbtCloudUserGroupsDataSource(t *testing.T) { - var userID int - if isDbtCloudPR() { - userID = 1 - } else if os.Getenv("CI") != "" { - userID = 54461 - } else { - userID = 32 - } - + userID := acctest_helper.GetDbtCloudUserId() config := userGroups(userID) check := resource.ComposeAggregateTestCheckFunc( diff --git a/pkg/sdkv2/resources/user_groups_acceptance_test.go b/pkg/sdkv2/resources/user_groups_acceptance_test.go index 2e7c9647..6cbb1c66 100644 --- a/pkg/sdkv2/resources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/resources/user_groups_acceptance_test.go @@ -2,7 +2,6 @@ package resources_test import ( "fmt" - "os" "strconv" "testing" @@ -13,19 +12,8 @@ import ( func TestAccDbtCloudUserGroupsResource(t *testing.T) { - var userID int - var groupIDs string - if isDbtCloudPR() { - userID = 1 - groupIDs = "[1,2,3]" - } else if value := os.Getenv("CI"); value != "" { - userID = 54461 - groupIDs = "[531585, 531584, 531583]" - } else { - userID = 4 - groupIDs = "[104, 105, 106]" - } - + userID := acctest_helper.GetDbtCloudUserId() + groupIDs := acctest_helper.GetDbtCloudGroupIds() GroupName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) resource.Test(t, resource.TestCase{ From f228b288350c9325be81afa92ffea7f735f10c09 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:32:17 -0600 Subject: [PATCH 02/14] Parameterize acceptance tests for local runs --- README.md | 4 ++-- pkg/framework/acctest_helper/acctest_helper.go | 7 +++++-- .../objects/environment/data_source_all_acceptance_test.go | 2 +- .../partial_notification/resource_acceptance_test.go | 5 ----- 4 files changed, 8 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 26f63020..0af68f2f 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ own account. To run the acceptance tests locally, you will need to set the following environment variables to appropriate values for your dbt Cloud account: ``` -DBT_CLOUD_ACCOUNT_ID=1 -DBT_CLOUD_USER_ID=1 +DBT_CLOUD_ACCOUNT_ID=1234 +DBT_CLOUD_USER_ID=4321 DBT_CLOUD_USER_EMAIL= DBT_CLOUD_TOKEN= DBT_CLOUD_HOST_URL=https:///api diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index cb6268ba..7a487c21 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -81,7 +81,7 @@ func IsDbtCloudPR() bool { // GetDbtCloudUserId returns the user ID to use for acceptance tests. // Currently, this utilizes some legacy logic to determine the user ID. // If the DBT_CLOUD_USER_ID environment variable is fully adopted, this -// function could be simplified to require that environment variable. +// function could be simplified. func GetDbtCloudUserId() int { if IsDbtCloudPR() { return 1 @@ -97,6 +97,9 @@ func GetDbtCloudUserId() int { } // GetDbtCloudUserEmail returns the user email to use for acceptance tests. +// Currently, this utilizes some legacy logic to determine the user email. +// If the DBT_CLOUD_USER_EMAIL environment variable is fully adopted, this +// function could be simplified. func GetDbtCloudUserEmail() string { if IsDbtCloudPR() { return "d" + "ev@" + "db" + "tla" + "bs.c" + "om" @@ -114,7 +117,7 @@ func GetDbtCloudUserEmail() string { // GetDbtCloudGroupIds returns the group IDs to use for acceptance tests. // Currently, this utilizes some legacy logic to determine the group IDs. // If the DBT_CLOUD_GROUP_IDS environment variable is fully adopted, this -// function could be simplified to require that environment variable. +// function could be simplified. func GetDbtCloudGroupIds() string { var groupIds string if IsDbtCloudPR() { diff --git a/pkg/framework/objects/environment/data_source_all_acceptance_test.go b/pkg/framework/objects/environment/data_source_all_acceptance_test.go index 6d4fbe71..e9f9f6cc 100644 --- a/pkg/framework/objects/environment/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_all_acceptance_test.go @@ -2,10 +2,10 @@ package environment_test import ( "fmt" - "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" ) diff --git a/pkg/framework/objects/partial_notification/resource_acceptance_test.go b/pkg/framework/objects/partial_notification/resource_acceptance_test.go index 1e17d7e0..c1723150 100644 --- a/pkg/framework/objects/partial_notification/resource_acceptance_test.go +++ b/pkg/framework/objects/partial_notification/resource_acceptance_test.go @@ -20,11 +20,6 @@ func TestAccDbtCloudPartialNotificationResource(t *testing.T) { } userID := acctest_helper.GetDbtCloudUserId() - //if acctest_helper.IsDbtCloudPR() { - // userID = "1" - //} else { - // userID = "100" - //} currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-partial-resource@nomail.com", currentTime) From dc2265b08e97e3deabe397e59fa7faf1276fd403 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 09:38:04 -0600 Subject: [PATCH 03/14] Parameterize acceptance tests for local runs --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0af68f2f..5cd456f0 100644 --- a/README.md +++ b/README.md @@ -59,11 +59,10 @@ The CLI [dbtcloud-terraforming](https://github.com/dbt-labs/dbtcloud-terraformin ## Running Acceptance Tests -Currently, acceptance tests, run via `make test-acceptance` must be done on your -own account. +Acceptance tests are executed by running the `make test-acceptance` command. -To run the acceptance tests locally, you will need to set the following environment variables to appropriate values -for your dbt Cloud account: +For the acceptance tests to work locally, the following environment variables must be set to appropriate values +for a dbt Cloud account the tests can interact with: ``` DBT_CLOUD_ACCOUNT_ID=1234 DBT_CLOUD_USER_ID=4321 From 5b8f67bc5b78cd9ef8c9450039aead56b109fdb4 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:40:55 -0600 Subject: [PATCH 04/14] Parameterize acceptance tests for local runs --- .env.example | 8 ++++ README.md | 20 +++++++++- .../acctest_helper/acctest_helper.go | 38 +++++++++++++++++-- .../resources/repository_acceptance_test.go | 14 +++++-- 4 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..36252f8a --- /dev/null +++ b/.env.example @@ -0,0 +1,8 @@ +export DBT_CLOUD_ACCOUNT_ID=1234 +export DBT_CLOUD_USER_ID=4321 +export DBT_CLOUD_USER_EMAIL=user@example.com +export DBT_CLOUD_TOKEN= +export DBT_CLOUD_HOST_URL=https:///api +export DBT_CLOUD_GROUP_IDS=1,2,3 +export ACC_TEST_GITHUB_REPO_URL=git@github.com:my-org/dbt-fundamentals.git +export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678 \ No newline at end of file diff --git a/README.md b/README.md index 5cd456f0..46b50ce0 100644 --- a/README.md +++ b/README.md @@ -62,16 +62,32 @@ The CLI [dbtcloud-terraforming](https://github.com/dbt-labs/dbtcloud-terraformin Acceptance tests are executed by running the `make test-acceptance` command. For the acceptance tests to work locally, the following environment variables must be set to appropriate values -for a dbt Cloud account the tests can interact with: +for a dbt Cloud account the tests can interact with. All dbt Cloud resources referenced by the environment variables +(e.g. user id, email address, and group ids) must exist in the dbt Cloud account. ``` DBT_CLOUD_ACCOUNT_ID=1234 DBT_CLOUD_USER_ID=4321 -DBT_CLOUD_USER_EMAIL= +DBT_CLOUD_USER_EMAIL= DBT_CLOUD_TOKEN= DBT_CLOUD_HOST_URL=https:///api DBT_CLOUD_GROUP_IDS=1,2,3 +ACC_TEST_GITHUB_REPO_URL=git@github.com:my-org/dbt-fundamentals.git +ACC_TEST_GITHUB_APP_INSTALLATION_ID=1234 ``` +To assist with setting the environment variables, the `.env.example` file can be copied to `.env` and the values updated. +The variables can then be loaded into the environment by running `source .env` on Mac or Linux. + +**A note on the Repository Acceptance Tests** +The Repository Acceptance Tests require a GitHub repository to be set up and the dbt Cloud GitHub App installed. + +`ACC_TEST_GITHUB_REPO_URL` must be set to the SSH URL of a repository + +`ACC_TEST_GITHUB_APP_INSTALLATION_ID` must be set to the installation ID of the GitHub App. +The installation ID can be found by navigating to `Settings` -> `Applications`, +and clicking `Configure` on the dbt Cloud GitHub App. The installation ID can be found in the url, for example, +`https://github.com/settings/installations/` + ## Acknowledgement Thanks to Gary James [[GtheSheep](https://github.com/GtheSheep)], for all the effort put in creating this provider originally diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index 7a487c21..5569af05 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -81,7 +81,7 @@ func IsDbtCloudPR() bool { // GetDbtCloudUserId returns the user ID to use for acceptance tests. // Currently, this utilizes some legacy logic to determine the user ID. // If the DBT_CLOUD_USER_ID environment variable is fully adopted, this -// function could be simplified. +// function can be simplified. func GetDbtCloudUserId() int { if IsDbtCloudPR() { return 1 @@ -99,7 +99,7 @@ func GetDbtCloudUserId() int { // GetDbtCloudUserEmail returns the user email to use for acceptance tests. // Currently, this utilizes some legacy logic to determine the user email. // If the DBT_CLOUD_USER_EMAIL environment variable is fully adopted, this -// function could be simplified. +// function can be simplified. func GetDbtCloudUserEmail() string { if IsDbtCloudPR() { return "d" + "ev@" + "db" + "tla" + "bs.c" + "om" @@ -117,7 +117,7 @@ func GetDbtCloudUserEmail() string { // GetDbtCloudGroupIds returns the group IDs to use for acceptance tests. // Currently, this utilizes some legacy logic to determine the group IDs. // If the DBT_CLOUD_GROUP_IDS environment variable is fully adopted, this -// function could be simplified. +// function can be simplified. func GetDbtCloudGroupIds() string { var groupIds string if IsDbtCloudPR() { @@ -133,6 +133,38 @@ func GetDbtCloudGroupIds() string { return fmt.Sprintf("[%s]", groupIds) } +// GetGitHubRepoUrl returns the GitHub repository URL to use for acceptance tests. +// Currently, this utilizes some legacy logic to determine the GitHub repository URL. +// If the ACC_TEST_GITHUB_REPO_URL environment variable is fully adopted, this +// function can be simplified. +func GetGitHubRepoUrl() string { + if IsDbtCloudPR() || os.Getenv("CI") != "" { + return "git://github.com/dbt-labs/jaffle_shop.git" + } else { + url := os.Getenv("ACC_TEST_GITHUB_REPO_URL") + if url == "" { + log.Fatalf("Unable to determine GitHub repository url for test") + } + return url + } +} + +// GetGitHubAppInstallationId returns the GitHub app installation ID to use for acceptance tests. +// Currently, this utilizes some legacy logic to determine the GitHub app installation ID. +// If the ACC_TEST_GITHUB_APP_INSTALLATION_ID environment variable is fully adopted, this +// function can be simplified. +func GetGitHubAppInstallationId() int { + if IsDbtCloudPR() || os.Getenv("CI") != "" { + return 28374841 + } else { + id, err := strconv.Atoi(os.Getenv("ACC_TEST_GITHUB_APP_INSTALLATION_ID")) + if err != nil { + log.Fatalf("Unable to determine GitHub app installation id for test: %v", err) + } + return id + } +} + func HelperTestResourceSchema[R resource.Resource](t *testing.T, r R) { ctx := context.Background() diff --git a/pkg/sdkv2/resources/repository_acceptance_test.go b/pkg/sdkv2/resources/repository_acceptance_test.go index d327413d..9b907ad1 100644 --- a/pkg/sdkv2/resources/repository_acceptance_test.go +++ b/pkg/sdkv2/resources/repository_acceptance_test.go @@ -56,7 +56,8 @@ func TestAccDbtCloudRepositoryResource(t *testing.T) { }, }) - repoUrlGithubApplication := "git://github.com/dbt-labs/jaffle_shop.git" + repoUrlGithubApplication := acctest_helper.GetGitHubRepoUrl() + githubAppInstallationId := acctest_helper.GetGitHubAppInstallationId() projectNameGithubApplication := strings.ToUpper( acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), ) @@ -71,6 +72,7 @@ func TestAccDbtCloudRepositoryResource(t *testing.T) { Config: testAccDbtCloudRepositoryResourceGithubApplicationConfig( repoUrlGithubApplication, projectNameGithubApplication, + githubAppInstallationId, ), Check: resource.ComposeTestCheckFunc( testAccCheckDbtCloudRepositoryExists( @@ -115,7 +117,11 @@ resource "dbtcloud_repository" "test_repository_github" { `, projectName, repoUrl) } -func testAccDbtCloudRepositoryResourceGithubApplicationConfig(repoUrl, projectName string) string { +func testAccDbtCloudRepositoryResourceGithubApplicationConfig( + repoUrl string, + projectName string, + githubAppInstallationId int, +) string { return fmt.Sprintf(` resource "dbtcloud_project" "test_project" { name = "%s" @@ -124,11 +130,11 @@ resource "dbtcloud_project" "test_project" { resource "dbtcloud_repository" "test_repository_github_application" { remote_url = "%s" project_id = dbtcloud_project.test_project.id - github_installation_id = 28374841 + github_installation_id = %d git_clone_strategy = "github_app" pull_request_url_template = "https://github.com/my-org/my-repo/compare/qa...{{source}}" } -`, projectName, repoUrl) +`, projectName, repoUrl, githubAppInstallationId) } // From 0f0c0c493aeaa894aab760114edefc85ac8312be Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:45:21 -0600 Subject: [PATCH 05/14] Parameterize acceptance tests for local runs --- .env.example | 2 +- Makefile | 2 +- README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 36252f8a..28a31a65 100644 --- a/.env.example +++ b/.env.example @@ -4,5 +4,5 @@ export DBT_CLOUD_USER_EMAIL=user@example.com export DBT_CLOUD_TOKEN= export DBT_CLOUD_HOST_URL=https:///api export DBT_CLOUD_GROUP_IDS=1,2,3 -export ACC_TEST_GITHUB_REPO_URL=git@github.com:my-org/dbt-fundamentals.git +export ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678 \ No newline at end of file diff --git a/Makefile b/Makefile index df662955..ecd21771 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ test: deps go test -mod=readonly -count=1 ./... test-acceptance: deps - TF_ACC=1 go test -v -mod=readonly -count=1 -parallel 10 ./... + TF_ACC=1 go test -v -mod=readonly -count=1 -parallel 1 ./... check-docs: docs git diff --exit-code -- docs diff --git a/README.md b/README.md index 46b50ce0..dcd6e649 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ DBT_CLOUD_USER_EMAIL= DBT_CLOUD_TOKEN= DBT_CLOUD_HOST_URL=https:///api DBT_CLOUD_GROUP_IDS=1,2,3 -ACC_TEST_GITHUB_REPO_URL=git@github.com:my-org/dbt-fundamentals.git +ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git ACC_TEST_GITHUB_APP_INSTALLATION_ID=1234 ``` From acc769775bb56fa2f4208683132cd226ed6af180 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Tue, 10 Dec 2024 10:48:39 -0600 Subject: [PATCH 06/14] Parameterize acceptance tests for local runs --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ecd21771..df662955 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ test: deps go test -mod=readonly -count=1 ./... test-acceptance: deps - TF_ACC=1 go test -v -mod=readonly -count=1 -parallel 1 ./... + TF_ACC=1 go test -v -mod=readonly -count=1 -parallel 10 ./... check-docs: docs git diff --exit-code -- docs From 8ce29bb60a9e2070c1c5061c9185d8fd10570746 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:57:00 -0600 Subject: [PATCH 07/14] Refactor to use global config object for env vars --- .../acctest_config/acctest_config.go | 91 ++++++++++++++++++ .../acctest_helper/acctest_helper.go | 92 ------------------- .../data_source_acceptance_test.go | 2 +- .../data_source_all_acceptance_test.go | 2 +- .../job/data_source_all_acceptance_test.go | 3 +- .../data_source_acceptance_test.go | 7 +- .../notification/resource_acceptance_test.go | 7 +- .../resource_acceptance_test.go | 7 +- .../user/data_source_acceptance_test.go | 3 +- .../user/data_source_all_acceptance_test.go | 8 +- .../user_groups_acceptance_test.go | 3 +- .../resources/repository_acceptance_test.go | 5 +- .../resources/user_groups_acceptance_test.go | 5 +- 13 files changed, 119 insertions(+), 116 deletions(-) create mode 100644 pkg/framework/acctest_config/acctest_config.go diff --git a/pkg/framework/acctest_config/acctest_config.go b/pkg/framework/acctest_config/acctest_config.go new file mode 100644 index 00000000..4bc6fd71 --- /dev/null +++ b/pkg/framework/acctest_config/acctest_config.go @@ -0,0 +1,91 @@ +package acctest_config + +import ( + "log" + "os" + "strconv" +) + +var AcceptanceTestConfig = buildAcctestConfig() + +func buildAcctestConfig() AcctestConfig { + return AcctestConfig{ + DbtCloudAccountId: determineIntValue("DBT_CLOUD_ACCOUNT_ID", 1, 1), + DbtCloudServiceToken: os.Getenv("DBT_CLOUD_TOKEN"), + DbtCloudHostUrl: determineStringValue("DBT_CLOUD_HOST_URL", "", ""), + DbtCloudVersion: "versionless", + DbtCloudUserId: determineIntValue("DBT_CLOUD_USER_ID", 1, 54461), + DbtCloudUserEmail: determineStringValue( + "DBT_CLOUD_USER_EMAIL", + "d"+"ev@"+"db"+"tla"+"bs.c"+"om", + "beno"+"it"+".per"+"igaud"+"@"+"fisht"+"ownanalytics"+"."+"com", + ), + DbtCloudGroupIds: determineStringValue( + "DBT_CLOUD_GROUP_IDS", + "1,2,3", + "531585,531584,531583", + ), + GitHubRepoUrl: determineStringValue( + "ACC_TEST_GITHUB_REPO_URL", + "git://github.com/dbt-labs/jaffle_shop.git", + "git://github.com/dbt-labs/jaffle_shop.git", + ), + GitHubAppInstallationId: determineIntValue( + "ACC_TEST_GITHUB_APP_INSTALLATION_ID", + 28374841, + 28374841, + ), + } +} + +type AcctestConfig struct { + DbtCloudAccountId int + DbtCloudServiceToken string + DbtCloudHostUrl string + DbtCloudVersion string + DbtCloudUserId int + DbtCloudUserEmail string + DbtCloudGroupIds string + GitHubRepoUrl string + GitHubAppInstallationId int +} + +func IsDbtCloudPR() bool { + return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" +} + +func IsCI() bool { + return os.Getenv("CI") != "" +} + +func determineStringValue(envVarKey string, dbtCloudPRValue string, ciValue string) string { + val := os.Getenv(envVarKey) + if val != "" { + return val + } else if IsDbtCloudPR() { + return dbtCloudPRValue + } else if IsCI() { + return ciValue + } else { + log.Printf("Unable to determine %s value, tests may fail", envVarKey) + return "" + } +} + +func determineIntValue(envVarKey string, dbtCloudPRValue int, ciValue int) int { + val := os.Getenv(envVarKey) + if val != "" { + intVal, err := strconv.Atoi(val) + if err != nil { + log.Fatalf("Unable to determine %s value for test: %v", envVarKey, err) + } + return intVal + } else if IsDbtCloudPR() { + return dbtCloudPRValue + } else if IsCI() { + return ciValue + } else { + log.Printf("Unable to determine %s value, tests may fail", envVarKey) + return -1 + } +} diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index 5569af05..8ec477ac 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -2,7 +2,6 @@ package acctest_helper import ( "context" - "fmt" "log" "net/http" "os" @@ -74,97 +73,6 @@ func TestAccPreCheck(t *testing.T) { } } -func IsDbtCloudPR() bool { - return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" -} - -// GetDbtCloudUserId returns the user ID to use for acceptance tests. -// Currently, this utilizes some legacy logic to determine the user ID. -// If the DBT_CLOUD_USER_ID environment variable is fully adopted, this -// function can be simplified. -func GetDbtCloudUserId() int { - if IsDbtCloudPR() { - return 1 - } else if os.Getenv("CI") != "" { - return 54461 - } else { - id, err := strconv.Atoi(os.Getenv("DBT_CLOUD_USER_ID")) - if err != nil { - log.Fatalf("Unable to determine UserID for test: %v", err) - } - return id - } -} - -// GetDbtCloudUserEmail returns the user email to use for acceptance tests. -// Currently, this utilizes some legacy logic to determine the user email. -// If the DBT_CLOUD_USER_EMAIL environment variable is fully adopted, this -// function can be simplified. -func GetDbtCloudUserEmail() string { - if IsDbtCloudPR() { - return "d" + "ev@" + "db" + "tla" + "bs.c" + "om" - } else if os.Getenv("CI") != "" { - return "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" - } else { - email := os.Getenv("DBT_CLOUD_USER_EMAIL") - if email == "" { - log.Fatalf("Unable to determine GroupIds for test") - } - return email - } -} - -// GetDbtCloudGroupIds returns the group IDs to use for acceptance tests. -// Currently, this utilizes some legacy logic to determine the group IDs. -// If the DBT_CLOUD_GROUP_IDS environment variable is fully adopted, this -// function can be simplified. -func GetDbtCloudGroupIds() string { - var groupIds string - if IsDbtCloudPR() { - groupIds = "1,2,3" - } else if os.Getenv("CI") != "" { - groupIds = "531585,531584,531583" - } else { - groupIds = os.Getenv("DBT_CLOUD_GROUP_IDS") - if groupIds == "" { - log.Fatalf("Unable to determine GroupIds for test") - } - } - return fmt.Sprintf("[%s]", groupIds) -} - -// GetGitHubRepoUrl returns the GitHub repository URL to use for acceptance tests. -// Currently, this utilizes some legacy logic to determine the GitHub repository URL. -// If the ACC_TEST_GITHUB_REPO_URL environment variable is fully adopted, this -// function can be simplified. -func GetGitHubRepoUrl() string { - if IsDbtCloudPR() || os.Getenv("CI") != "" { - return "git://github.com/dbt-labs/jaffle_shop.git" - } else { - url := os.Getenv("ACC_TEST_GITHUB_REPO_URL") - if url == "" { - log.Fatalf("Unable to determine GitHub repository url for test") - } - return url - } -} - -// GetGitHubAppInstallationId returns the GitHub app installation ID to use for acceptance tests. -// Currently, this utilizes some legacy logic to determine the GitHub app installation ID. -// If the ACC_TEST_GITHUB_APP_INSTALLATION_ID environment variable is fully adopted, this -// function can be simplified. -func GetGitHubAppInstallationId() int { - if IsDbtCloudPR() || os.Getenv("CI") != "" { - return 28374841 - } else { - id, err := strconv.Atoi(os.Getenv("ACC_TEST_GITHUB_APP_INSTALLATION_ID")) - if err != nil { - log.Fatalf("Unable to determine GitHub app installation id for test: %v", err) - } - return id - } -} - func HelperTestResourceSchema[R resource.Resource](t *testing.T, r R) { ctx := context.Background() diff --git a/pkg/framework/objects/environment/data_source_acceptance_test.go b/pkg/framework/objects/environment/data_source_acceptance_test.go index aa1a78f4..6d04de33 100644 --- a/pkg/framework/objects/environment/data_source_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_acceptance_test.go @@ -70,5 +70,5 @@ func environment(projectName, environmentName string) string { project_id = dbtcloud_project.test_project.id environment_id = dbtcloud_environment.test_environment.environment_id } - `, projectName, environmentName, acctest_helper.DBT_CLOUD_VERSION) + `, projectName, environmentName, acctest_config.AcceptanceTestConfig.DbtCloudVersion) } diff --git a/pkg/framework/objects/environment/data_source_all_acceptance_test.go b/pkg/framework/objects/environment/data_source_all_acceptance_test.go index e9f9f6cc..475eda43 100644 --- a/pkg/framework/objects/environment/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_all_acceptance_test.go @@ -95,5 +95,5 @@ func environments( project_id = dbtcloud_project.test_project2.id depends_on = [dbtcloud_environment.test_environment1, dbtcloud_environment.test_environment2] } - `, randomProjectName1, randomProjectName2, randomEnvironmentName1, acctest_helper.DBT_CLOUD_VERSION, randomEnvironmentName2, acctest_helper.DBT_CLOUD_VERSION) + `, randomProjectName1, randomProjectName2, randomEnvironmentName1, acctest_config.AcceptanceTestConfig.DbtCloudVersion, randomEnvironmentName2, acctest_helper.DBT_CLOUD_VERSION) } diff --git a/pkg/framework/objects/job/data_source_all_acceptance_test.go b/pkg/framework/objects/job/data_source_all_acceptance_test.go index 28d38834..95bf3afa 100644 --- a/pkg/framework/objects/job/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/job/data_source_all_acceptance_test.go @@ -2,6 +2,7 @@ package job_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -137,5 +138,5 @@ func jobs(jobName string, jobName2 string) string { dbtcloud_job.test_job2, ] } - `, acctest_helper.DBT_CLOUD_VERSION, acctest_helper.DBT_CLOUD_VERSION, jobName, jobName2) + `, acctest_config.AcceptanceTestConfig.DbtCloudVersion, acctest_config.AcceptanceTestConfig.DbtCloudVersion, jobName, jobName2) } diff --git a/pkg/framework/objects/notification/data_source_acceptance_test.go b/pkg/framework/objects/notification/data_source_acceptance_test.go index f0c1f13f..6e73d098 100644 --- a/pkg/framework/objects/notification/data_source_acceptance_test.go +++ b/pkg/framework/objects/notification/data_source_acceptance_test.go @@ -2,6 +2,7 @@ package notification_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "time" @@ -12,11 +13,11 @@ import ( func TestAccDbtCloudNotificationDataSource(t *testing.T) { - if acctest_helper.IsDbtCloudPR() { + if acctest_config.IsDbtCloudPR() { t.Skip("Skipping notifications in dbt Cloud CI for now") } - userID := acctest_helper.GetDbtCloudUserId() + userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-datasource@nomail.com", currentTime) @@ -90,5 +91,5 @@ func notification(projectName string, userID int, notificationEmail string) stri data "dbtcloud_notification" "test_notification_external" { notification_id = dbtcloud_notification.test_notification_external.id } - `, projectName, acctest_helper.DBT_CLOUD_VERSION, userID, notificationEmail) + `, projectName, acctest_config.AcceptanceTestConfig.DbtCloudVersion, userID, notificationEmail) } diff --git a/pkg/framework/objects/notification/resource_acceptance_test.go b/pkg/framework/objects/notification/resource_acceptance_test.go index d20c8c4b..91b0151b 100644 --- a/pkg/framework/objects/notification/resource_acceptance_test.go +++ b/pkg/framework/objects/notification/resource_acceptance_test.go @@ -2,6 +2,7 @@ package notification_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "regexp" "strings" @@ -15,11 +16,11 @@ import ( func TestAccDbtCloudNotificationResource(t *testing.T) { - if acctest_helper.IsDbtCloudPR() { + if acctest_config.IsDbtCloudPR() { t.Skip("Skipping notifications in dbt Cloud CI for now") } - userID := acctest_helper.GetDbtCloudUserId() + userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-resource@nomail.com", currentTime) @@ -176,7 +177,7 @@ resource "dbtcloud_job" "test_notification_job_2" { "schedule" : false, } } -`, projectName, acctest_helper.DBT_CLOUD_VERSION) +`, projectName, acctest_config.AcceptanceTestConfig.DbtCloudVersion) } func testAccDbtCloudNotificationResourceCreateNotifications( diff --git a/pkg/framework/objects/partial_notification/resource_acceptance_test.go b/pkg/framework/objects/partial_notification/resource_acceptance_test.go index c1723150..640eb145 100644 --- a/pkg/framework/objects/partial_notification/resource_acceptance_test.go +++ b/pkg/framework/objects/partial_notification/resource_acceptance_test.go @@ -2,6 +2,7 @@ package partial_notification_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "regexp" "strings" "testing" @@ -15,11 +16,11 @@ import ( func TestAccDbtCloudPartialNotificationResource(t *testing.T) { - if acctest_helper.IsDbtCloudPR() { + if acctest_config.IsDbtCloudPR() { t.Skip("Skipping notifications in dbt Cloud CI for now") } - userID := acctest_helper.GetDbtCloudUserId() + userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId currentTime := time.Now().Unix() notificationEmail := fmt.Sprintf("%d-partial-resource@nomail.com", currentTime) @@ -171,7 +172,7 @@ resource "dbtcloud_job" "test_notification_job_2" { "schedule" : false, } } -`, projectName, acctest_helper.DBT_CLOUD_VERSION) +`, projectName, acctest_config.AcceptanceTestConfig.DbtCloudVersion) } func testAccDbtCloudPartialNotificationResourceCreatePartialNotifications( diff --git a/pkg/framework/objects/user/data_source_acceptance_test.go b/pkg/framework/objects/user/data_source_acceptance_test.go index e8ec58a1..f41a1949 100644 --- a/pkg/framework/objects/user/data_source_acceptance_test.go +++ b/pkg/framework/objects/user/data_source_acceptance_test.go @@ -2,6 +2,7 @@ package user_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -10,7 +11,7 @@ import ( func TestAccDbtCloudUserDataSource(t *testing.T) { - userEmail := acctest_helper.GetDbtCloudUserEmail() + userEmail := acctest_config.AcceptanceTestConfig.DbtCloudUserEmail config := user(userEmail) diff --git a/pkg/framework/objects/user/data_source_all_acceptance_test.go b/pkg/framework/objects/user/data_source_all_acceptance_test.go index c00ab6ec..6b480a4c 100644 --- a/pkg/framework/objects/user/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/user/data_source_all_acceptance_test.go @@ -1,6 +1,7 @@ package user_test import ( + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -9,12 +10,7 @@ import ( func TestAccDbtCloudUsersDataSource(t *testing.T) { - var userEmail string - if acctest_helper.IsDbtCloudPR() { - userEmail = "d" + "ev@" + "db" + "tla" + "bs.c" + "om" - } else { - userEmail = "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" - } + userEmail := acctest_config.AcceptanceTestConfig.DbtCloudUserEmail _ = userEmail config := users() diff --git a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go index 1934f643..bc0b7b24 100644 --- a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go @@ -2,6 +2,7 @@ package data_sources_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -10,7 +11,7 @@ import ( func TestDbtCloudUserGroupsDataSource(t *testing.T) { - userID := acctest_helper.GetDbtCloudUserId() + userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId config := userGroups(userID) check := resource.ComposeAggregateTestCheckFunc( diff --git a/pkg/sdkv2/resources/repository_acceptance_test.go b/pkg/sdkv2/resources/repository_acceptance_test.go index 9b907ad1..5cac503b 100644 --- a/pkg/sdkv2/resources/repository_acceptance_test.go +++ b/pkg/sdkv2/resources/repository_acceptance_test.go @@ -2,6 +2,7 @@ package resources_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "regexp" "strings" "testing" @@ -56,8 +57,8 @@ func TestAccDbtCloudRepositoryResource(t *testing.T) { }, }) - repoUrlGithubApplication := acctest_helper.GetGitHubRepoUrl() - githubAppInstallationId := acctest_helper.GetGitHubAppInstallationId() + repoUrlGithubApplication := acctest_config.AcceptanceTestConfig.GitHubRepoUrl + githubAppInstallationId := acctest_config.AcceptanceTestConfig.GitHubAppInstallationId projectNameGithubApplication := strings.ToUpper( acctest.RandStringFromCharSet(10, acctest.CharSetAlpha), ) diff --git a/pkg/sdkv2/resources/user_groups_acceptance_test.go b/pkg/sdkv2/resources/user_groups_acceptance_test.go index 6cbb1c66..51c87ecb 100644 --- a/pkg/sdkv2/resources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/resources/user_groups_acceptance_test.go @@ -2,6 +2,7 @@ package resources_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "strconv" "testing" @@ -12,8 +13,8 @@ import ( func TestAccDbtCloudUserGroupsResource(t *testing.T) { - userID := acctest_helper.GetDbtCloudUserId() - groupIDs := acctest_helper.GetDbtCloudGroupIds() + userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId + groupIDs := acctest_config.AcceptanceTestConfig.DbtCloudGroupIds GroupName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) resource.Test(t, resource.TestCase{ From 500ed01a16e3bfb6c0912cd78ba5db13bc1fce56 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:59:40 -0600 Subject: [PATCH 08/14] Missed import --- pkg/framework/objects/environment/data_source_acceptance_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/framework/objects/environment/data_source_acceptance_test.go b/pkg/framework/objects/environment/data_source_acceptance_test.go index 6d04de33..468f0583 100644 --- a/pkg/framework/objects/environment/data_source_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_acceptance_test.go @@ -2,6 +2,7 @@ package environment_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" From a4fd892a3b0b545cfdecad6395a64b9db2463c32 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:02:29 -0600 Subject: [PATCH 09/14] Missed import --- .../environment/data_source_all_acceptance_test.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/framework/objects/environment/data_source_all_acceptance_test.go b/pkg/framework/objects/environment/data_source_all_acceptance_test.go index 475eda43..d4480798 100644 --- a/pkg/framework/objects/environment/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_all_acceptance_test.go @@ -2,6 +2,7 @@ package environment_test import ( "fmt" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "testing" "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper" @@ -95,5 +96,11 @@ func environments( project_id = dbtcloud_project.test_project2.id depends_on = [dbtcloud_environment.test_environment1, dbtcloud_environment.test_environment2] } - `, randomProjectName1, randomProjectName2, randomEnvironmentName1, acctest_config.AcceptanceTestConfig.DbtCloudVersion, randomEnvironmentName2, acctest_helper.DBT_CLOUD_VERSION) + `, randomProjectName1, + randomProjectName2, + randomEnvironmentName1, + acctest_config.AcceptanceTestConfig.DbtCloudVersion, + randomEnvironmentName2, + acctest_config.AcceptanceTestConfig.DbtCloudVersion, + ) } From e38a8767a28b6cf3c2678c53f80064edb2c1e07b Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:03:25 -0600 Subject: [PATCH 10/14] gofmt --- .../objects/environment/data_source_all_acceptance_test.go | 3 ++- pkg/sdkv2/resources/databricks_credential.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/framework/objects/environment/data_source_all_acceptance_test.go b/pkg/framework/objects/environment/data_source_all_acceptance_test.go index d4480798..ff937217 100644 --- a/pkg/framework/objects/environment/data_source_all_acceptance_test.go +++ b/pkg/framework/objects/environment/data_source_all_acceptance_test.go @@ -96,7 +96,8 @@ func environments( project_id = dbtcloud_project.test_project2.id depends_on = [dbtcloud_environment.test_environment1, dbtcloud_environment.test_environment2] } - `, randomProjectName1, + `, + randomProjectName1, randomProjectName2, randomEnvironmentName1, acctest_config.AcceptanceTestConfig.DbtCloudVersion, diff --git a/pkg/sdkv2/resources/databricks_credential.go b/pkg/sdkv2/resources/databricks_credential.go index 1df531a6..52a21a15 100644 --- a/pkg/sdkv2/resources/databricks_credential.go +++ b/pkg/sdkv2/resources/databricks_credential.go @@ -410,7 +410,7 @@ func resourceDatabricksCredentialUpdateGlobConn( d.Get("catalog").(string), ) - for key, _ := range patchCredentialsDetails.Fields { + for key := range patchCredentialsDetails.Fields { if d.Get(key) == nil || !d.HasChange(key) { delete(patchCredentialsDetails.Fields, key) } From bc78ce33e764013eec38866b1c98247ac1bb7c42 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Wed, 11 Dec 2024 10:17:00 -0600 Subject: [PATCH 11/14] Fix test --- pkg/sdkv2/resources/user_groups_acceptance_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdkv2/resources/user_groups_acceptance_test.go b/pkg/sdkv2/resources/user_groups_acceptance_test.go index 51c87ecb..8007588e 100644 --- a/pkg/sdkv2/resources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/resources/user_groups_acceptance_test.go @@ -14,7 +14,7 @@ import ( func TestAccDbtCloudUserGroupsResource(t *testing.T) { userID := acctest_config.AcceptanceTestConfig.DbtCloudUserId - groupIDs := acctest_config.AcceptanceTestConfig.DbtCloudGroupIds + groupIDs := fmt.Sprintf("[%s]", acctest_config.AcceptanceTestConfig.DbtCloudGroupIds) GroupName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) resource.Test(t, resource.TestCase{ From d34ebec76e04800cc6b25003e0279bfa351533e4 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:15:46 -0600 Subject: [PATCH 12/14] Add Azure DevOps Project datasource test config --- .env.example | 7 ++-- README.md | 6 ++-- .../acctest_config/acctest_config.go | 36 +++++++++++++------ .../data_source_acceptance_test.go | 10 +++--- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/.env.example b/.env.example index 28a31a65..dc07b018 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,9 @@ export DBT_CLOUD_ACCOUNT_ID=1234 -export DBT_CLOUD_USER_ID=4321 -export DBT_CLOUD_USER_EMAIL=user@example.com export DBT_CLOUD_TOKEN= +export DBT_CLOUD_PERSONAL_ACCESS_TOKEN= export DBT_CLOUD_HOST_URL=https:///api -export DBT_CLOUD_GROUP_IDS=1,2,3 +export ACC_TEST_DBT_CLOUD_USER_ID=4321 +export ACC_TEST_DBT_CLOUD_USER_EMAIL=user@example.com +export ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3 export ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678 \ No newline at end of file diff --git a/README.md b/README.md index dcd6e649..3f850a5d 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,11 @@ for a dbt Cloud account the tests can interact with. All dbt Cloud resources ref (e.g. user id, email address, and group ids) must exist in the dbt Cloud account. ``` DBT_CLOUD_ACCOUNT_ID=1234 -DBT_CLOUD_USER_ID=4321 -DBT_CLOUD_USER_EMAIL= +ACC_TEST_DBT_CLOUD_USER_ID=4321 +ACC_TEST_DBT_CLOUD_USER_EMAIL= DBT_CLOUD_TOKEN= DBT_CLOUD_HOST_URL=https:///api -DBT_CLOUD_GROUP_IDS=1,2,3 +ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3 ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git ACC_TEST_GITHUB_APP_INSTALLATION_ID=1234 ``` diff --git a/pkg/framework/acctest_config/acctest_config.go b/pkg/framework/acctest_config/acctest_config.go index 4bc6fd71..02b92c2c 100644 --- a/pkg/framework/acctest_config/acctest_config.go +++ b/pkg/framework/acctest_config/acctest_config.go @@ -10,21 +10,32 @@ var AcceptanceTestConfig = buildAcctestConfig() func buildAcctestConfig() AcctestConfig { return AcctestConfig{ - DbtCloudAccountId: determineIntValue("DBT_CLOUD_ACCOUNT_ID", 1, 1), - DbtCloudServiceToken: os.Getenv("DBT_CLOUD_TOKEN"), - DbtCloudHostUrl: determineStringValue("DBT_CLOUD_HOST_URL", "", ""), - DbtCloudVersion: "versionless", - DbtCloudUserId: determineIntValue("DBT_CLOUD_USER_ID", 1, 54461), + DbtCloudAccountId: determineIntValue("DBT_CLOUD_ACCOUNT_ID", 1, 1), + DbtCloudServiceToken: os.Getenv("DBT_CLOUD_TOKEN"), + DbtCloudPersonalAccessToken: os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN"), + DbtCloudHostUrl: determineStringValue("DBT_CLOUD_HOST_URL", "", ""), + DbtCloudVersion: "versionless", + + DbtCloudUserId: determineIntValue( + "ACC_TEST_DBT_CLOUD_USER_ID", + 1, + 54461, + ), DbtCloudUserEmail: determineStringValue( - "DBT_CLOUD_USER_EMAIL", + "ACC_TEST_DBT_CLOUD_USER_EMAIL", "d"+"ev@"+"db"+"tla"+"bs.c"+"om", "beno"+"it"+".per"+"igaud"+"@"+"fisht"+"ownanalytics"+"."+"com", ), DbtCloudGroupIds: determineStringValue( - "DBT_CLOUD_GROUP_IDS", + "ACC_TEST_DBT_CLOUD_GROUP_IDS", "1,2,3", "531585,531584,531583", ), + AzureDevOpsProjectName: determineStringValue( + "ACC_TEST_AZURE_DEVOPS_PROJECT_NAME", + "dbt-cloud-ado-project", + "dbt-cloud-ado-project", + ), GitHubRepoUrl: determineStringValue( "ACC_TEST_GITHUB_REPO_URL", "git://github.com/dbt-labs/jaffle_shop.git", @@ -39,13 +50,16 @@ func buildAcctestConfig() AcctestConfig { } type AcctestConfig struct { - DbtCloudAccountId int - DbtCloudServiceToken string - DbtCloudHostUrl string - DbtCloudVersion string + DbtCloudAccountId int + DbtCloudServiceToken string + DbtCloudPersonalAccessToken string + DbtCloudHostUrl string + DbtCloudVersion string + DbtCloudUserId int DbtCloudUserEmail string DbtCloudGroupIds string + AzureDevOpsProjectName string GitHubRepoUrl string GitHubAppInstallationId int } diff --git a/pkg/framework/objects/azure_dev_ops_project/data_source_acceptance_test.go b/pkg/framework/objects/azure_dev_ops_project/data_source_acceptance_test.go index 6e9b9317..08deeef0 100644 --- a/pkg/framework/objects/azure_dev_ops_project/data_source_acceptance_test.go +++ b/pkg/framework/objects/azure_dev_ops_project/data_source_acceptance_test.go @@ -1,6 +1,7 @@ package azure_dev_ops_project_test import ( + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_config" "os" "testing" @@ -10,19 +11,18 @@ import ( ) func TestAccDbtCloudAzureDevOpsProject(t *testing.T) { - //TODO: Remove both env var checks when this gets configurted in CI and the variables are parameterized + //TODO: Remove both env var checks when this gets configured in CI and the variables are parameterized if os.Getenv("CI") != "" { t.Skip("Skipping Azure DevOps Project datasource test in CI " + "until Azure integration and a personal access token are available") } - if os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN") == "" { + personalAccessToken := acctest_config.AcceptanceTestConfig.DbtCloudPersonalAccessToken + if personalAccessToken == "" { t.Skip("Skipping Azure DevOps Project datasource because no personal access token is available") } - //TODO: Parameterize these values when a standard method is available for parameterization - adoProjectName := "dbt-cloud-ado-project" - personalAccessToken := os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN") + adoProjectName := acctest_config.AcceptanceTestConfig.AzureDevOpsProjectName resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest_helper.TestAccPreCheck(t) }, From 8acfe200a2890fe1006c157b423b6f2739469ef9 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Thu, 12 Dec 2024 09:23:48 -0600 Subject: [PATCH 13/14] Update readme --- .env.example | 3 ++- README.md | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index dc07b018..ad5e6430 100644 --- a/.env.example +++ b/.env.example @@ -1,9 +1,10 @@ export DBT_CLOUD_ACCOUNT_ID=1234 +export DBT_CLOUD_HOST_URL=https:///api export DBT_CLOUD_TOKEN= export DBT_CLOUD_PERSONAL_ACCESS_TOKEN= -export DBT_CLOUD_HOST_URL=https:///api export ACC_TEST_DBT_CLOUD_USER_ID=4321 export ACC_TEST_DBT_CLOUD_USER_EMAIL=user@example.com export ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3 +export ACC_TEST_AZURE_DEVOPS_PROJECT_NAME=test-project export ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git export ACC_TEST_GITHUB_APP_INSTALLATION_ID=5678 \ No newline at end of file diff --git a/README.md b/README.md index 3f850a5d..af56d313 100644 --- a/README.md +++ b/README.md @@ -66,11 +66,13 @@ for a dbt Cloud account the tests can interact with. All dbt Cloud resources ref (e.g. user id, email address, and group ids) must exist in the dbt Cloud account. ``` DBT_CLOUD_ACCOUNT_ID=1234 -ACC_TEST_DBT_CLOUD_USER_ID=4321 -ACC_TEST_DBT_CLOUD_USER_EMAIL= -DBT_CLOUD_TOKEN= DBT_CLOUD_HOST_URL=https:///api +DBT_CLOUD_TOKEN= +DBT_CLOUD_PERSONAL_ACCESS_TOKEN= +ACC_TEST_DBT_CLOUD_USER_EMAIL= +ACC_TEST_DBT_CLOUD_USER_ID=4321 ACC_TEST_DBT_CLOUD_GROUP_IDS=1,2,3 +ACC_TEST_AZURE_DEVOPS_PROJECT_NAME=test-project ACC_TEST_GITHUB_REPO_URL=git@github.com:/.git ACC_TEST_GITHUB_APP_INSTALLATION_ID=1234 ``` From cf6e7bf0b9b5eebd3027515eee92a6d0921c6b85 Mon Sep 17 00:00:00 2001 From: Craig Squire <677724+csquire@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:05:37 -0600 Subject: [PATCH 14/14] Update tests to use dbt_version latest --- pkg/framework/acctest_config/acctest_config.go | 2 +- pkg/framework/acctest_helper/acctest_helper.go | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/pkg/framework/acctest_config/acctest_config.go b/pkg/framework/acctest_config/acctest_config.go index 02b92c2c..de3afa0f 100644 --- a/pkg/framework/acctest_config/acctest_config.go +++ b/pkg/framework/acctest_config/acctest_config.go @@ -14,7 +14,7 @@ func buildAcctestConfig() AcctestConfig { DbtCloudServiceToken: os.Getenv("DBT_CLOUD_TOKEN"), DbtCloudPersonalAccessToken: os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN"), DbtCloudHostUrl: determineStringValue("DBT_CLOUD_HOST_URL", "", ""), - DbtCloudVersion: "versionless", + DbtCloudVersion: "latest", DbtCloudUserId: determineIntValue( "ACC_TEST_DBT_CLOUD_USER_ID", diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index 8ec477ac..3acc3562 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -40,10 +40,6 @@ func SharedClient() (*dbt_cloud.Client, error) { return &client, nil } -const ( - DBT_CLOUD_VERSION = "latest" -) - var TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ "dbtcloud": func() (tfprotov6.ProviderServer, error) { upgradedSdkProvider, err := tf5to6server.UpgradeServer(