From fee0f8c587862efccbf9520b5593591d5977b757 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 11:33:51 +0100 Subject: [PATCH 1/8] Introduce acceptance test context --- pkg/acceptance/testing.go | 45 ++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/pkg/acceptance/testing.go b/pkg/acceptance/testing.go index 49eb8bd7f6..d600249420 100644 --- a/pkg/acceptance/testing.go +++ b/pkg/acceptance/testing.go @@ -8,6 +8,7 @@ import ( "sync" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-go/tfprotov5" @@ -15,6 +16,7 @@ import ( "github.com/hashicorp/terraform-plugin-mux/tf5to6server" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-testing/config" + "github.com/snowflakedb/gosnowflake" ) const ( @@ -28,6 +30,7 @@ var ( TestAccProvider *schema.Provider v5Server tfprotov5.ProviderServer v6Server tfprotov6.ProviderServer + atc acceptanceTestContext ) func init() { @@ -44,6 +47,26 @@ func init() { log.Panicf("Cannot upgrade server from proto v5 to proto v6, failing, err: %v", err) } _ = testAccProtoV6ProviderFactoriesNew + + defaultConfig, err := sdk.ProfileConfig(testprofiles.Default) + if err != nil { + log.Panicf("Cannot load default config, err: %v", err) + } + if defaultConfig == nil { + log.Panic("Config is required to run acceptance tests") + } + atc.config = defaultConfig + + client, err := sdk.NewClient(defaultConfig) + if err != nil { + log.Panicf("Cannot instantiate new client, err: %v", err) + } + atc.client = client +} + +type acceptanceTestContext struct { + config *gosnowflake.Config + client *sdk.Client } var TestAccProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){ @@ -68,35 +91,31 @@ var once sync.Once func TestAccPreCheck(t *testing.T) { // use singleton design pattern to ensure we only create these resources once once.Do(func() { - client, err := sdk.NewDefaultClient() - if err != nil { - t.Fatal(err) - } ctx := context.Background() dbId := sdk.NewAccountObjectIdentifier(TestDatabaseName) - if err := client.Databases.Create(ctx, dbId, &sdk.CreateDatabaseOptions{ + if err := atc.client.Databases.Create(ctx, dbId, &sdk.CreateDatabaseOptions{ IfNotExists: sdk.Bool(true), }); err != nil { t.Fatal(err) } schemaId := sdk.NewDatabaseObjectIdentifier(TestDatabaseName, TestSchemaName) - if err := client.Schemas.Create(ctx, schemaId, &sdk.CreateSchemaOptions{ + if err := atc.client.Schemas.Create(ctx, schemaId, &sdk.CreateSchemaOptions{ IfNotExists: sdk.Bool(true), }); err != nil { t.Fatal(err) } warehouseId := sdk.NewAccountObjectIdentifier(TestWarehouseName) - if err := client.Warehouses.Create(ctx, warehouseId, &sdk.CreateWarehouseOptions{ + if err := atc.client.Warehouses.Create(ctx, warehouseId, &sdk.CreateWarehouseOptions{ IfNotExists: sdk.Bool(true), }); err != nil { t.Fatal(err) } warehouseId2 := sdk.NewAccountObjectIdentifier(TestWarehouseName2) - if err := client.Warehouses.Create(ctx, warehouseId2, &sdk.CreateWarehouseOptions{ + if err := atc.client.Warehouses.Create(ctx, warehouseId2, &sdk.CreateWarehouseOptions{ IfNotExists: sdk.Bool(true), }); err != nil { t.Fatal(err) @@ -119,3 +138,13 @@ func ConfigurationDirectory(directory string) func(config.TestStepConfigRequest) return filepath.Join("testdata", directory) } } + +func Client(t *testing.T) *sdk.Client { + t.Helper() + return atc.client +} + +func DefaultConfig(t *testing.T) *gosnowflake.Config { + t.Helper() + return atc.config +} From fe761174ee5946ead563b8caf101f1c6d7394aaa Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 11:34:19 +0100 Subject: [PATCH 2/8] Expose default config in integration tests --- pkg/sdk/testint/setup_test.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/sdk/testint/setup_test.go b/pkg/sdk/testint/setup_test.go index 945073ff69..fc0b20abf3 100644 --- a/pkg/sdk/testint/setup_test.go +++ b/pkg/sdk/testint/setup_test.go @@ -2,6 +2,7 @@ package testint import ( "context" + "errors" "log" "os" "testing" @@ -10,6 +11,7 @@ import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/internal/random" + "github.com/snowflakedb/gosnowflake" ) var ( @@ -61,6 +63,7 @@ func cleanup() { } type integrationTestContext struct { + config *gosnowflake.Config client *sdk.Client ctx context.Context @@ -85,7 +88,16 @@ type integrationTestContext struct { func (itc *integrationTestContext) initialize() error { log.Println("Initializing integration test context") - c, err := sdk.NewDefaultClient() + defaultConfig, err := sdk.ProfileConfig(testprofiles.Default) + if err != nil { + return err + } + if defaultConfig == nil { + return errors.New("config is required to run integration tests") + } + itc.config = defaultConfig + + c, err := sdk.NewClient(defaultConfig) if err != nil { return err } @@ -245,3 +257,8 @@ func testSecondaryWarehouse(t *testing.T) *sdk.Warehouse { t.Helper() return itc.secondaryWarehouse } + +func testConfig(t *testing.T) *gosnowflake.Config { + t.Helper() + return itc.config +} From 4e3a793d0e82cadeb7ee40dcf9cb4cc11b4f9491 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 11:34:34 +0100 Subject: [PATCH 3/8] Remove not needed test envs --- pkg/acceptance/testenvs/testenvs_test.go | 26 +++++++++---------- .../testenvs/testing_environment_variables.go | 6 ----- pkg/provider/provider_acceptance_test.go | 10 +++---- pkg/sdk/client_integration_test.go | 9 ++++--- 4 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pkg/acceptance/testenvs/testenvs_test.go b/pkg/acceptance/testenvs/testenvs_test.go index 1a82f3d3af..1a2a97492f 100644 --- a/pkg/acceptance/testenvs/testenvs_test.go +++ b/pkg/acceptance/testenvs/testenvs_test.go @@ -18,14 +18,14 @@ func Test_GetOrSkipTest(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - env = testenvs.GetOrSkipTest(t, testenvs.User) + env = testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) }() wg.Wait() return env } t.Run("skip test if missing", func(t *testing.T) { - t.Setenv(string(testenvs.User), "") + t.Setenv(string(testenvs.BusinessCriticalAccount), "") tut := &testing.T{} env := runGetOrSkipInGoroutineAndWaitForCompletion(tut) @@ -35,7 +35,7 @@ func Test_GetOrSkipTest(t *testing.T) { }) t.Run("get env if exists", func(t *testing.T) { - t.Setenv(string(testenvs.User), "user") + t.Setenv(string(testenvs.BusinessCriticalAccount), "user") tut := &testing.T{} env := runGetOrSkipInGoroutineAndWaitForCompletion(tut) @@ -60,19 +60,19 @@ func Test_SkipTestIfSet(t *testing.T) { } t.Run("skip test if env is set", func(t *testing.T) { - t.Setenv(string(testenvs.User), "1") + t.Setenv(string(testenvs.BusinessCriticalAccount), "1") tut := &testing.T{} - runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.User) + runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.BusinessCriticalAccount) require.True(t, tut.Skipped()) }) t.Run("do not skip if env not set", func(t *testing.T) { - t.Setenv(string(testenvs.User), "") + t.Setenv(string(testenvs.BusinessCriticalAccount), "") tut := &testing.T{} - runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.User) + runSkipTestIfSetInGoroutineAndWaitForCompletion(tut, testenvs.BusinessCriticalAccount) require.False(t, tut.Skipped()) }) @@ -92,26 +92,26 @@ func Test_Assertions(t *testing.T) { } t.Run("test if env does not exist", func(t *testing.T) { - t.Setenv(string(testenvs.User), "") + t.Setenv(string(testenvs.BusinessCriticalAccount), "") tut1 := &testing.T{} - runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvNotSet(tut1, string(testenvs.User)) }) + runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvNotSet(tut1, string(testenvs.BusinessCriticalAccount)) }) tut2 := &testing.T{} - runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvSet(tut2, string(testenvs.User)) }) + runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvSet(tut2, string(testenvs.BusinessCriticalAccount)) }) require.False(t, tut1.Failed()) require.True(t, tut2.Failed()) }) t.Run("test if env exists", func(t *testing.T) { - t.Setenv(string(testenvs.User), "user") + t.Setenv(string(testenvs.BusinessCriticalAccount), "user") tut1 := &testing.T{} - runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvNotSet(tut1, string(testenvs.User)) }) + runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvNotSet(tut1, string(testenvs.BusinessCriticalAccount)) }) tut2 := &testing.T{} - runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvSet(tut2, string(testenvs.User)) }) + runAssertionInGoroutineAndWaitForCompletion(func() { testenvs.AssertEnvSet(tut2, string(testenvs.BusinessCriticalAccount)) }) require.True(t, tut1.Failed()) require.False(t, tut2.Failed()) diff --git a/pkg/acceptance/testenvs/testing_environment_variables.go b/pkg/acceptance/testenvs/testing_environment_variables.go index 2a9ddf461e..b96a41e66e 100644 --- a/pkg/acceptance/testenvs/testing_environment_variables.go +++ b/pkg/acceptance/testenvs/testing_environment_variables.go @@ -9,12 +9,6 @@ import ( type env string const ( - User env = "TEST_SF_TF_USER" - Password env = "TEST_SF_TF_PASSWORD" // #nosec G101 - Account env = "TEST_SF_TF_ACCOUNT" - Role env = "TEST_SF_TF_ROLE" - Host env = "TEST_SF_TF_HOST" - BusinessCriticalAccount env = "SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT" TestAccountCreate env = "TEST_SF_TF_TEST_ACCOUNT_CREATE" diff --git a/pkg/provider/provider_acceptance_test.go b/pkg/provider/provider_acceptance_test.go index ef4ce1bf93..36cdf3ddb7 100644 --- a/pkg/provider/provider_acceptance_test.go +++ b/pkg/provider/provider_acceptance_test.go @@ -17,11 +17,11 @@ import ( ) func TestAcc_Provider_configHierarchy(t *testing.T) { - user := testenvs.GetOrSkipTest(t, testenvs.User) - pass := testenvs.GetOrSkipTest(t, testenvs.Password) - account := testenvs.GetOrSkipTest(t, testenvs.Account) - role := testenvs.GetOrSkipTest(t, testenvs.Role) - host := testenvs.GetOrSkipTest(t, testenvs.Host) + user := acc.DefaultConfig(t).User + pass := acc.DefaultConfig(t).Password + account := acc.DefaultConfig(t).Account + role := acc.DefaultConfig(t).Role + host := acc.DefaultConfig(t).Host nonExistingUser := "non-existing-user" diff --git a/pkg/sdk/client_integration_test.go b/pkg/sdk/client_integration_test.go index 9498d6b2d7..c7192517c5 100644 --- a/pkg/sdk/client_integration_test.go +++ b/pkg/sdk/client_integration_test.go @@ -6,7 +6,6 @@ import ( "os" "testing" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testprofiles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/snowflakeenvs" "github.com/snowflakedb/gosnowflake" @@ -41,14 +40,18 @@ func TestClient_NewClient(t *testing.T) { }) t.Run("with missing config - should not care about correct env variables", func(t *testing.T) { - account := testenvs.GetOrSkipTest(t, testenvs.Account) + config, err := ProfileConfig(testprofiles.Default) + require.NoError(t, err) + require.NotNil(t, config) + + account := config.Account t.Setenv(snowflakeenvs.Account, account) dir, err := os.UserHomeDir() require.NoError(t, err) t.Setenv(snowflakeenvs.ConfigPath, dir) - config := DefaultConfig() + config = DefaultConfig() _, err = NewClient(config) require.ErrorContains(t, err, "260000: account is empty") }) From fb47ef4a82db5ec56531e2529470027eda533afe Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 11:34:50 +0100 Subject: [PATCH 4/8] Expose envs to test jobs --- .github/workflows/tests.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 76ec115312..5c6c6b61cd 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -43,6 +43,16 @@ jobs: - run: make test if: steps.create_config.conclusion == 'success' + env: + SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT: ${{ secrets.SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT }} + TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL }} + TEST_SF_TF_AWS_EXTERNAL_KEY_ID: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_KEY_ID }} + TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN }} + TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY }} + TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL }} + TEST_SF_TF_AZURE_EXTERNAL_SAS_TOKEN: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_SAS_TOKEN }} + TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID }} + TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL }} - name: Setup Terraform if: steps.create_config.conclusion == 'success' @@ -53,6 +63,16 @@ jobs: - run: make test-acceptance if: steps.create_config.conclusion == 'success' + env: + SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT: ${{ secrets.SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT }} + TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL }} + TEST_SF_TF_AWS_EXTERNAL_KEY_ID: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_KEY_ID }} + TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_ROLE_ARN }} + TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_SECRET_KEY }} + TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_BUCKET_URL }} + TEST_SF_TF_AZURE_EXTERNAL_SAS_TOKEN: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_SAS_TOKEN }} + TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID: ${{ secrets.TEST_SF_TF_AZURE_EXTERNAL_TENANT_ID }} + TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL }} - name: sweepers cleanup if: ${{ always() }} From cf7be5541bb1bbfc55082799380fe55060a4cf83 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 12:09:30 +0100 Subject: [PATCH 5/8] Skip tests requiring business critical account --- .../failover_groups_acceptance_test.go | 3 +++ pkg/resources/failover_group_acceptance_test.go | 15 +++++++++++++++ .../failover_group_grant_acceptance_test.go | 3 +++ .../testint/failover_groups_integration_test.go | 6 ++++++ 4 files changed, 27 insertions(+) diff --git a/pkg/datasources/failover_groups_acceptance_test.go b/pkg/datasources/failover_groups_acceptance_test.go index 05e23d47df..3eb6d666d0 100644 --- a/pkg/datasources/failover_groups_acceptance_test.go +++ b/pkg/datasources/failover_groups_acceptance_test.go @@ -14,6 +14,9 @@ import ( ) func TestAcc_FailoverGroups(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) diff --git a/pkg/resources/failover_group_acceptance_test.go b/pkg/resources/failover_group_acceptance_test.go index e55fb6e3c5..74a37fecb2 100644 --- a/pkg/resources/failover_group_acceptance_test.go +++ b/pkg/resources/failover_group_acceptance_test.go @@ -14,6 +14,9 @@ import ( ) func TestAcc_FailoverGroupBasic(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + randomCharacters := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) @@ -49,6 +52,9 @@ func TestAcc_FailoverGroupBasic(t *testing.T) { } func TestAcc_FailoverGroupRemoveObjectTypes(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + randomCharacters := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) @@ -87,6 +93,9 @@ func TestAcc_FailoverGroupRemoveObjectTypes(t *testing.T) { } func TestAcc_FailoverGroupInterval(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + randomCharacters := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) @@ -183,6 +192,9 @@ func TestAcc_FailoverGroupInterval(t *testing.T) { } func TestAcc_FailoverGroup_issue2517(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + randomCharacters := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) @@ -211,6 +223,9 @@ func TestAcc_FailoverGroup_issue2517(t *testing.T) { } func TestAcc_FailoverGroup_issue2544(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + randomCharacters := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) diff --git a/pkg/resources/failover_group_grant_acceptance_test.go b/pkg/resources/failover_group_grant_acceptance_test.go index 7a8ba2c410..9bf303ad88 100644 --- a/pkg/resources/failover_group_grant_acceptance_test.go +++ b/pkg/resources/failover_group_grant_acceptance_test.go @@ -14,6 +14,9 @@ import ( ) func TestAcc_FailoverGroupGrant(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + accountName := testenvs.GetOrSkipTest(t, testenvs.BusinessCriticalAccount) name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)) diff --git a/pkg/sdk/testint/failover_groups_integration_test.go b/pkg/sdk/testint/failover_groups_integration_test.go index f52e46133b..01b36fb116 100644 --- a/pkg/sdk/testint/failover_groups_integration_test.go +++ b/pkg/sdk/testint/failover_groups_integration_test.go @@ -15,6 +15,9 @@ import ( ) func TestInt_FailoverGroupsCreate(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + client := testClient(t) ctx := testContext(t) shareTest, shareCleanup := createShare(t, client) @@ -138,6 +141,9 @@ func TestInt_FailoverGroupsCreate(t *testing.T) { } func TestInt_Issue2544(t *testing.T) { + // TODO [SNOW-1002023]: Unskip; Business Critical Snowflake Edition needed + _ = testenvs.GetOrSkipTest(t, testenvs.TestFailoverGroups) + client := testClient(t) ctx := testContext(t) From 991fbf7ddd5074d714b187d69d2852c44197d660 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 12:44:32 +0100 Subject: [PATCH 6/8] Set up client everytime in provider setup test --- pkg/acceptance/testenvs/testing_environment_variables.go | 3 ++- pkg/provider/provider_acceptance_test.go | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/acceptance/testenvs/testing_environment_variables.go b/pkg/acceptance/testenvs/testing_environment_variables.go index b96a41e66e..e97f2e27e9 100644 --- a/pkg/acceptance/testenvs/testing_environment_variables.go +++ b/pkg/acceptance/testenvs/testing_environment_variables.go @@ -27,7 +27,8 @@ const ( SkipManagedAccountTest env = "TEST_SF_TF_SKIP_MANAGED_ACCOUNT_TEST" SkipSamlIntegrationTest env = "TEST_SF_TF_SKIP_SAML_INTEGRATION_TEST" - EnableSweep env = "TEST_SF_TF_ENABLE_SWEEP" + EnableSweep env = "TEST_SF_TF_ENABLE_SWEEP" + ConfigureClientOnce env = "SF_TF_ACC_TEST_CONFIGURE_CLIENT_ONCE" ) func GetOrSkipTest(t *testing.T, envName Env) string { diff --git a/pkg/provider/provider_acceptance_test.go b/pkg/provider/provider_acceptance_test.go index 36cdf3ddb7..eeabe48813 100644 --- a/pkg/provider/provider_acceptance_test.go +++ b/pkg/provider/provider_acceptance_test.go @@ -17,6 +17,8 @@ import ( ) func TestAcc_Provider_configHierarchy(t *testing.T) { + t.Setenv(string(testenvs.ConfigureClientOnce), "") + user := acc.DefaultConfig(t).User pass := acc.DefaultConfig(t).Password account := acc.DefaultConfig(t).Account From f4037d6b0ef067199ae6e89f466797250a8ec7a2 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 13:06:39 +0100 Subject: [PATCH 7/8] Fix situation with switching SF_TF_ACC_TEST_CONFIGURE_CLIENT_ONCE on/off --- pkg/provider/provider.go | 17 ++++++++++--- pkg/provider/provider_acceptance_test.go | 31 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pkg/provider/provider.go b/pkg/provider/provider.go index 4ff770346f..f11d1512c7 100644 --- a/pkg/provider/provider.go +++ b/pkg/provider/provider.go @@ -782,11 +782,20 @@ func ConfigureProvider(s *schema.ResourceData) (interface{}, error) { } } - configuredClient, configureClientError = sdk.NewClient(config) + cl, clErr := sdk.NewClient(config) - if configureClientError != nil { - return nil, configureClientError + // needed for tests verifying different provider setups + if os.Getenv("TF_ACC") != "" && os.Getenv("SF_TF_ACC_TEST_CONFIGURE_CLIENT_ONCE") == "true" { + configuredClient = cl + configureClientError = clErr + } else { + configuredClient = nil + configureClientError = nil + } + + if clErr != nil { + return nil, clErr } - return &provider.Context{Client: configuredClient}, nil + return &provider.Context{Client: cl}, nil } diff --git a/pkg/provider/provider_acceptance_test.go b/pkg/provider/provider_acceptance_test.go index eeabe48813..590e45fec5 100644 --- a/pkg/provider/provider_acceptance_test.go +++ b/pkg/provider/provider_acceptance_test.go @@ -121,6 +121,37 @@ func TestAcc_Provider_configHierarchy(t *testing.T) { }) } +func TestAcc_Provider_(t *testing.T) { + t.Setenv(string(testenvs.ConfigureClientOnce), "") + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + PreCheck: func() { + acc.TestAccPreCheck(t) + testenvs.AssertEnvNotSet(t, snowflakeenvs.User) + testenvs.AssertEnvNotSet(t, snowflakeenvs.Password) + }, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + Steps: []resource.TestStep{ + { + Config: providerConfig(testprofiles.IncorrectUserAndPassword), + ExpectError: regexp.MustCompile("Incorrect username or password was specified"), + }, + { + PreConfig: func() { + t.Setenv(string(testenvs.ConfigureClientOnce), "true") + }, + Config: emptyProviderConfig(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.snowflake_database.t", "name", acc.TestDatabaseName), + ), + }, + }, + }) +} + func emptyProviderConfig() string { return ` provider "snowflake" { From 4dc2e5e1a070f257254b214c254d50c1d2698008 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 7 Mar 2024 14:04:21 +0100 Subject: [PATCH 8/8] Added test name and comments --- pkg/provider/provider_acceptance_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/provider/provider_acceptance_test.go b/pkg/provider/provider_acceptance_test.go index 590e45fec5..e9d05169c0 100644 --- a/pkg/provider/provider_acceptance_test.go +++ b/pkg/provider/provider_acceptance_test.go @@ -121,7 +121,7 @@ func TestAcc_Provider_configHierarchy(t *testing.T) { }) } -func TestAcc_Provider_(t *testing.T) { +func TestAcc_Provider_configureClientOnceSwitching(t *testing.T) { t.Setenv(string(testenvs.ConfigureClientOnce), "") resource.Test(t, resource.TestCase{ @@ -135,10 +135,12 @@ func TestAcc_Provider_(t *testing.T) { tfversion.RequireAbove(tfversion.Version1_5_0), }, Steps: []resource.TestStep{ + // client setup is incorrect { Config: providerConfig(testprofiles.IncorrectUserAndPassword), ExpectError: regexp.MustCompile("Incorrect username or password was specified"), }, + // in this step we simulate the situation when we want to use client configured once, but it was faulty last time { PreConfig: func() { t.Setenv(string(testenvs.ConfigureClientOnce), "true")