From 29ca0e9c2075d3b24dd8f94c778742bd29652af0 Mon Sep 17 00:00:00 2001 From: Benoit Perigaud <8754100+b-per@users.noreply.github.com> Date: Tue, 28 May 2024 19:47:47 +0200 Subject: [PATCH] Update testing for dbt Cloud CI --- .../acctest_helper/acctest_helper.go | 4 + .../data_source_acceptance_test.go | 15 +++- .../notification/resource_acceptance_test.go | 31 ++++--- .../resource_acceptance_test.go | 31 ++++--- .../user/data_source_acceptance_test.go | 7 +- pkg/sdkv2/data_sources/helpers_test.go | 6 ++ .../privatelink_endpoint_acceptance_test.go | 90 +++++++++---------- .../user_groups_acceptance_test.go | 4 +- .../data_sources/webhook_acceptance_test.go | 5 ++ .../resources/connection_acceptance_test.go | 76 ++++++++-------- pkg/sdkv2/resources/helpers_test.go | 4 + .../resources/user_groups_acceptance_test.go | 5 +- .../resources/webhook_acceptance_test.go | 4 + 13 files changed, 169 insertions(+), 113 deletions(-) diff --git a/pkg/framework/acctest_helper/acctest_helper.go b/pkg/framework/acctest_helper/acctest_helper.go index 6e447928..7a9a15e8 100644 --- a/pkg/framework/acctest_helper/acctest_helper.go +++ b/pkg/framework/acctest_helper/acctest_helper.go @@ -71,3 +71,7 @@ func TestAccPreCheck(t *testing.T) { t.Fatal("DBT_CLOUD_TOKEN must be set for acceptance tests") } } + +func IsDbtCloudPR() bool { + return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" +} diff --git a/pkg/framework/objects/notification/data_source_acceptance_test.go b/pkg/framework/objects/notification/data_source_acceptance_test.go index 04fd0a7b..161c74f6 100644 --- a/pkg/framework/objects/notification/data_source_acceptance_test.go +++ b/pkg/framework/objects/notification/data_source_acceptance_test.go @@ -11,9 +11,16 @@ import ( func TestAccDbtCloudNotificationDataSource(t *testing.T) { + var userID string + if acctest_helper.IsDbtCloudPR() { + userID = "1" + } else { + userID = "100" + } + randomProjectName := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - config := notification(randomProjectName) + config := notification(randomProjectName, userID) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -43,7 +50,7 @@ func TestAccDbtCloudNotificationDataSource(t *testing.T) { }) } -func notification(projectName string) string { +func notification(projectName, userID string) string { return fmt.Sprintf(` resource "dbtcloud_project" "test_notification_project" { name = "%s" @@ -71,7 +78,7 @@ func notification(projectName string) string { } resource "dbtcloud_notification" "test_notification_external" { - user_id = 100 + user_id = %s on_failure = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 external_email = "nomail@mail.com" @@ -80,5 +87,5 @@ func notification(projectName string) string { data "dbtcloud_notification" "test_notification_external" { notification_id = dbtcloud_notification.test_notification_external.id } - `, projectName, acctest_helper.DBT_CLOUD_VERSION) + `, projectName, acctest_helper.DBT_CLOUD_VERSION, userID) } diff --git a/pkg/framework/objects/notification/resource_acceptance_test.go b/pkg/framework/objects/notification/resource_acceptance_test.go index a7186e2d..c6123e17 100644 --- a/pkg/framework/objects/notification/resource_acceptance_test.go +++ b/pkg/framework/objects/notification/resource_acceptance_test.go @@ -15,6 +15,13 @@ import ( func TestAccDbtCloudNotificationResource(t *testing.T) { + var userID string + if acctest_helper.IsDbtCloudPR() { + userID = "1" + } else { + userID = "100" + } + projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) resource.Test(t, resource.TestCase{ @@ -23,7 +30,7 @@ func TestAccDbtCloudNotificationResource(t *testing.T) { CheckDestroy: testAccCheckDbtCloudNotificationDestroy, Steps: []resource.TestStep{ { - Config: testAccDbtCloudNotificationResourceCreateNotifications(projectName), + Config: testAccDbtCloudNotificationResourceCreateNotifications(projectName, userID), Check: resource.ComposeTestCheckFunc( testAccCheckDbtCloudNotificationExists( "dbtcloud_notification.test_notification_internal", @@ -71,7 +78,7 @@ func TestAccDbtCloudNotificationResource(t *testing.T) { }, // MODIFY { - Config: testAccDbtCloudNotificationResourceModifyNotifications(projectName), + Config: testAccDbtCloudNotificationResourceModifyNotifications(projectName, userID), Check: resource.ComposeTestCheckFunc( testAccCheckDbtCloudNotificationExists( "dbtcloud_notification.test_notification_internal", @@ -150,11 +157,11 @@ resource "dbtcloud_job" "test_notification_job_2" { `, projectName, acctest_helper.DBT_CLOUD_VERSION) } -func testAccDbtCloudNotificationResourceCreateNotifications(projectName string) string { +func testAccDbtCloudNotificationResourceCreateNotifications(projectName, userID string) string { - notificationsConfig := ` + notificationsConfig := fmt.Sprintf(` resource "dbtcloud_notification" "test_notification_internal" { - user_id = 100 + user_id = %s 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] @@ -162,20 +169,20 @@ resource "dbtcloud_notification" "test_notification_internal" { } resource "dbtcloud_notification" "test_notification_external" { - user_id = 100 + user_id = %s on_failure = [dbtcloud_job.test_notification_job_2.id] notification_type = 4 external_email = "nomail@mail.com" } -` +`, userID, userID) return testAccDbtCloudNotificationResourceBasicConfig(projectName) + "\n" + notificationsConfig } -func testAccDbtCloudNotificationResourceModifyNotifications(projectName string) string { +func testAccDbtCloudNotificationResourceModifyNotifications(projectName, userID string) string { - notificationsConfig := ` + notificationsConfig := fmt.Sprintf(` resource "dbtcloud_notification" "test_notification_internal" { - user_id = 100 + user_id = %s on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [] @@ -183,13 +190,13 @@ resource "dbtcloud_notification" "test_notification_internal" { } resource "dbtcloud_notification" "test_notification_external" { - user_id = 100 + user_id = %s on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 external_email = "nomail@mail.com" } -` +`, userID, userID) return testAccDbtCloudNotificationResourceBasicConfig(projectName) + "\n" + notificationsConfig } diff --git a/pkg/framework/objects/partial_notification/resource_acceptance_test.go b/pkg/framework/objects/partial_notification/resource_acceptance_test.go index dcfed295..0b2ffc60 100644 --- a/pkg/framework/objects/partial_notification/resource_acceptance_test.go +++ b/pkg/framework/objects/partial_notification/resource_acceptance_test.go @@ -14,6 +14,13 @@ import ( func TestAccDbtCloudPartialNotificationResource(t *testing.T) { + var userID string + if acctest_helper.IsDbtCloudPR() { + userID = "1" + } else { + userID = "100" + } + projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) resource.Test(t, resource.TestCase{ @@ -24,6 +31,7 @@ func TestAccDbtCloudPartialNotificationResource(t *testing.T) { { Config: testAccDbtCloudPartialNotificationResourceCreatePartialNotifications( projectName, + userID, ), Check: resource.ComposeTestCheckFunc( testAccCheckDbtCloudPartialNotificationExists( @@ -74,6 +82,7 @@ func TestAccDbtCloudPartialNotificationResource(t *testing.T) { { Config: testAccDbtCloudPartialNotificationResourceModifyPartialNotifications( projectName, + userID, ), Check: resource.ComposeTestCheckFunc( testAccCheckDbtCloudPartialNotificationExists( @@ -150,12 +159,12 @@ resource "dbtcloud_job" "test_notification_job_2" { } func testAccDbtCloudPartialNotificationResourceCreatePartialNotifications( - projectName string, + projectName, userID string, ) string { - notificationsConfig := ` + notificationsConfig := fmt.Sprintf(` resource "dbtcloud_partial_notification" "test_notification_internal" { - user_id = 100 + user_id = %s 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] @@ -163,24 +172,24 @@ resource "dbtcloud_partial_notification" "test_notification_internal" { } resource "dbtcloud_partial_notification" "test_notification_external" { - user_id = 100 + user_id = %s on_failure = [dbtcloud_job.test_notification_job_2.id] notification_type = 4 external_email = "nomail@mail.com" } -` +`, userID, userID) return testAccDbtCloudPartialNotificationResourceBasicConfig( projectName, ) + "\n" + notificationsConfig } func testAccDbtCloudPartialNotificationResourceModifyPartialNotifications( - projectName string, + projectName, userID string, ) string { - notificationsConfig := ` + notificationsConfig := fmt.Sprintf(` resource "dbtcloud_partial_notification" "test_notification_internal" { - user_id = 100 + user_id = %s on_success = [dbtcloud_job.test_notification_job_1.id] on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [] @@ -188,7 +197,7 @@ resource "dbtcloud_partial_notification" "test_notification_internal" { } resource "dbtcloud_partial_notification" "test_notification_external" { - user_id = 100 + user_id = %s on_failure = [dbtcloud_job.test_notification_job_2.id] on_cancel = [dbtcloud_job.test_notification_job_1.id] notification_type = 4 @@ -196,13 +205,13 @@ resource "dbtcloud_partial_notification" "test_notification_external" { } resource "dbtcloud_partial_notification" "test_notification_external2" { - user_id = 100 + user_id = %s on_success = [dbtcloud_job.test_notification_job_1.id, dbtcloud_job.test_notification_job_2.id] notification_type = 4 external_email = "nomail@mail.com" depends_on = [dbtcloud_partial_notification.test_notification_external] } -` +`, userID, userID, userID) return testAccDbtCloudPartialNotificationResourceBasicConfig( projectName, ) + "\n" + notificationsConfig diff --git a/pkg/framework/objects/user/data_source_acceptance_test.go b/pkg/framework/objects/user/data_source_acceptance_test.go index 20030492..2b572b41 100644 --- a/pkg/framework/objects/user/data_source_acceptance_test.go +++ b/pkg/framework/objects/user/data_source_acceptance_test.go @@ -10,7 +10,12 @@ import ( func TestAccDbtCloudUserDataSource(t *testing.T) { - userEmail := "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" + var userEmail string + if acctest_helper.IsDbtCloudPR() { + userEmail = "d" + "ev@" + "db" + "tla" + "bs.c" + "om" + } else { + userEmail = "beno" + "it" + ".per" + "igaud" + "@" + "fisht" + "ownanalytics" + "." + "com" + } config := user(userEmail) diff --git a/pkg/sdkv2/data_sources/helpers_test.go b/pkg/sdkv2/data_sources/helpers_test.go index 2c28725c..0d1c4e6e 100644 --- a/pkg/sdkv2/data_sources/helpers_test.go +++ b/pkg/sdkv2/data_sources/helpers_test.go @@ -1,6 +1,8 @@ package data_sources_test import ( + "os" + "github.com/dbt-labs/terraform-provider-dbtcloud/pkg/provider" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) @@ -15,3 +17,7 @@ func providers() map[string]*schema.Provider { "dbtcloud": p, } } + +func isDbtCloudPR() bool { + return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" +} diff --git a/pkg/sdkv2/data_sources/privatelink_endpoint_acceptance_test.go b/pkg/sdkv2/data_sources/privatelink_endpoint_acceptance_test.go index 3052a02c..da7dfeac 100644 --- a/pkg/sdkv2/data_sources/privatelink_endpoint_acceptance_test.go +++ b/pkg/sdkv2/data_sources/privatelink_endpoint_acceptance_test.go @@ -2,7 +2,6 @@ package data_sources_test import ( "fmt" - "log" "os" "testing" @@ -12,57 +11,56 @@ import ( func TestAccDbtCloudPrivatelinkEndpointDataSource(t *testing.T) { // we only test this explicitly as we can't create resources and need to read from existing ones - if os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK") != "" { + if os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK") == "" { + t.Skip("Skipping acceptance tests as DBT_ACCEPTANCE_TEST_PRIVATE_LINK is not set") + } - endpointName := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_NAME") - endpointURL := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_URL") + endpointName := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_NAME") + endpointURL := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_URL") - // different configurations whether we provide the endpoint name and/or url - config := privatelinkEndpoint(endpointName, endpointURL) - configNoURL := privatelinkEndpoint(endpointName, "") + // different configurations whether we provide the endpoint name and/or url + config := privatelinkEndpoint(endpointName, endpointURL) + configNoURL := privatelinkEndpoint(endpointName, "") - configNoName := privatelinkEndpoint("", endpointURL) + configNoName := privatelinkEndpoint("", endpointURL) - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr( - "data.dbtcloud_privatelink_endpoint.test", - "name", - endpointName, - ), - resource.TestCheckResourceAttr( - "data.dbtcloud_privatelink_endpoint.test", - "private_link_endpoint_url", - endpointURL, - ), - resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "id"), - resource.TestCheckResourceAttrSet( - "data.dbtcloud_privatelink_endpoint.test", - "cidr_range", - ), - resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "type"), - resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "state"), - ) + check := resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr( + "data.dbtcloud_privatelink_endpoint.test", + "name", + endpointName, + ), + resource.TestCheckResourceAttr( + "data.dbtcloud_privatelink_endpoint.test", + "private_link_endpoint_url", + endpointURL, + ), + resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "id"), + resource.TestCheckResourceAttrSet( + "data.dbtcloud_privatelink_endpoint.test", + "cidr_range", + ), + resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "type"), + resource.TestCheckResourceAttrSet("data.dbtcloud_privatelink_endpoint.test", "state"), + ) - resource.ParallelTest(t, resource.TestCase{ - Providers: providers(), - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - Config: configNoURL, - Check: check, - }, - { - Config: configNoName, - Check: check, - }, + resource.ParallelTest(t, resource.TestCase{ + Providers: providers(), + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } else { - log.Println("WARNING: The test is skipped as DBT_TEST_PRIVATE_LINK is not set") - } + { + Config: configNoURL, + Check: check, + }, + { + Config: configNoName, + Check: check, + }, + }, + }) } func privatelinkEndpoint(endpointName, endpointURL string) string { diff --git a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go index 145354fb..480fb3de 100644 --- a/pkg/sdkv2/data_sources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/data_sources/user_groups_acceptance_test.go @@ -11,7 +11,9 @@ import ( func TestDbtCloudUserGroupsDataSource(t *testing.T) { var userID int - if value := os.Getenv("CI"); value != "" { + if isDbtCloudPR() { + userID = 1 + } else if os.Getenv("CI") != "" { userID = 54461 } else { userID = 32 diff --git a/pkg/sdkv2/data_sources/webhook_acceptance_test.go b/pkg/sdkv2/data_sources/webhook_acceptance_test.go index 62d54bb9..2a1b394f 100644 --- a/pkg/sdkv2/data_sources/webhook_acceptance_test.go +++ b/pkg/sdkv2/data_sources/webhook_acceptance_test.go @@ -10,6 +10,10 @@ import ( func TestDbtCloudWebhookDataSource(t *testing.T) { + if isDbtCloudPR() { + t.Skip("Skipping webhooks acceptance in dbt Cloud CI for now") + } + randomWebhookName := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) randomWebhookDescription := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -39,6 +43,7 @@ func TestDbtCloudWebhookDataSource(t *testing.T) { }, }, }) + } func webhooks(webhookName string, webhookDesc string) string { diff --git a/pkg/sdkv2/resources/connection_acceptance_test.go b/pkg/sdkv2/resources/connection_acceptance_test.go index 2729c82f..676c5cea 100644 --- a/pkg/sdkv2/resources/connection_acceptance_test.go +++ b/pkg/sdkv2/resources/connection_acceptance_test.go @@ -385,48 +385,50 @@ func TestAccDbtCloudDatabricksConnectionResource(t *testing.T) { func TestAccDbtCloudConnectionPrivateLinkResource(t *testing.T) { // we only test this explicitly as we can't create a PL connection and need to read from existing ones - if os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK") != "" { + if os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK") == "" { + t.Skip("Skipping acceptance tests as DBT_ACCEPTANCE_TEST_PRIVATE_LINK is not set") + } - endpointName := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_NAME") - endpointURL := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_URL") + endpointName := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_NAME") + endpointURL := os.Getenv("DBT_ACCEPTANCE_TEST_PRIVATE_LINK_URL") - connectionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + connectionName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) + projectName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckDbtCloudConnectionDestroy, - Steps: []resource.TestStep{ - { - Config: testAccDbtCloudConnectionResourcePrivateLinkConfig( + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckDbtCloudConnectionDestroy, + Steps: []resource.TestStep{ + { + Config: testAccDbtCloudConnectionResourcePrivateLinkConfig( + connectionName, + projectName, + endpointName, + endpointURL, + ), + Check: resource.ComposeTestCheckFunc( + testAccCheckDbtCloudConnectionExists("dbtcloud_connection.test_connection"), + resource.TestCheckResourceAttr( + "dbtcloud_connection.test_connection", + "name", connectionName, - projectName, - endpointName, - endpointURL, - ), - Check: resource.ComposeTestCheckFunc( - testAccCheckDbtCloudConnectionExists("dbtcloud_connection.test_connection"), - resource.TestCheckResourceAttr( - "dbtcloud_connection.test_connection", - "name", - connectionName, - ), - resource.TestCheckResourceAttrSet( - "dbtcloud_connection.test_connection", - "private_link_endpoint_id", - ), - ), - }, - // IMPORT - { - ResourceName: "dbtcloud_connection.test_connection", - ImportState: true, - ImportStateVerifyIgnore: []string{}, - }, + ), + resource.TestCheckResourceAttrSet( + "dbtcloud_connection.test_connection", + "private_link_endpoint_id", + ), + ), }, - }) - } + // IMPORT + { + ResourceName: "dbtcloud_connection.test_connection", + ImportState: true, + ImportStateVerifyIgnore: []string{}, + }, + }, + }) + } func testAccDbtCloudConnectionResourceBasicConfig( diff --git a/pkg/sdkv2/resources/helpers_test.go b/pkg/sdkv2/resources/helpers_test.go index a68190c5..a2bdeea2 100644 --- a/pkg/sdkv2/resources/helpers_test.go +++ b/pkg/sdkv2/resources/helpers_test.go @@ -30,3 +30,7 @@ func testAccPreCheck(t *testing.T) { t.Fatal("DBT_CLOUD_TOKEN must be set for acceptance tests") } } + +func isDbtCloudPR() bool { + return os.Getenv("DBT_CLOUD_ACCOUNT_ID") == "1" +} diff --git a/pkg/sdkv2/resources/user_groups_acceptance_test.go b/pkg/sdkv2/resources/user_groups_acceptance_test.go index ff681d68..910ed947 100644 --- a/pkg/sdkv2/resources/user_groups_acceptance_test.go +++ b/pkg/sdkv2/resources/user_groups_acceptance_test.go @@ -14,7 +14,10 @@ func TestAccDbtCloudUserGroupsResource(t *testing.T) { var userID int var groupIDs string - if value := os.Getenv("CI"); value != "" { + if isDbtCloudPR() { + userID = 1 + groupIDs = "[]" + } else if value := os.Getenv("CI"); value != "" { userID = 54461 groupIDs = "[531585, 531584, 531583]" } else { diff --git a/pkg/sdkv2/resources/webhook_acceptance_test.go b/pkg/sdkv2/resources/webhook_acceptance_test.go index 8c61ea83..cce32e43 100644 --- a/pkg/sdkv2/resources/webhook_acceptance_test.go +++ b/pkg/sdkv2/resources/webhook_acceptance_test.go @@ -13,6 +13,10 @@ import ( func TestAccDbtCloudWebhookResource(t *testing.T) { + if isDbtCloudPR() { + t.Skip("Skipping webhooks acceptance in dbt Cloud CI for now") + } + webhookName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) webhookName2 := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) projectName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)