diff --git a/pkg/acceptance/helpers/common.go b/pkg/acceptance/helpers/common.go index c3a5d0c811d..4c4aa95fb63 100644 --- a/pkg/acceptance/helpers/common.go +++ b/pkg/acceptance/helpers/common.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "os" "regexp" "strings" "testing" @@ -82,12 +81,3 @@ func AssertErrorContainsPartsFunc(t *testing.T, parts []string) resource.ErrorCh return nil } } - -// TfAccFunc wraps a function and executes it whenever TF_ACC environment variable is set. -// Its main purpose is to run setup code before the actual terraform acceptance test. -func TfAccFunc(t *testing.T, f func()) { - t.Helper() - if os.Getenv(resource.EnvTfAcc) != "" { - f() - } -} diff --git a/pkg/acceptance/helpers/common_test.go b/pkg/acceptance/helpers/common_test.go index 9e80f2b92f9..ccad8dcb512 100644 --- a/pkg/acceptance/helpers/common_test.go +++ b/pkg/acceptance/helpers/common_test.go @@ -3,8 +3,6 @@ package helpers import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) @@ -58,23 +56,3 @@ func TestMatchAllStringsInOrderNonOverlapping(t *testing.T) { }) } } - -func TestTfAccFunc(t *testing.T) { - t.Run("TF_ACC enabled", func(t *testing.T) { - t.Setenv("TF_ACC", "true") - value := new(bool) - TfAccFunc(t, func() { - *value = true - }) - assert.True(t, *value) - }) - - t.Run("TF_ACC disabled", func(t *testing.T) { - t.Setenv("TF_ACC", "") - value := new(bool) - TfAccFunc(t, func() { - *value = true - }) - assert.False(t, *value) - }) -} diff --git a/pkg/acceptance/testenvs/testing_environment_variables.go b/pkg/acceptance/testenvs/testing_environment_variables.go index 01ca81eaccf..a8b6f9e0213 100644 --- a/pkg/acceptance/testenvs/testing_environment_variables.go +++ b/pkg/acceptance/testenvs/testing_environment_variables.go @@ -2,6 +2,7 @@ package testenvs import ( "fmt" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" "os" "testing" ) @@ -27,6 +28,7 @@ const ( SkipManagedAccountTest env = "TEST_SF_TF_SKIP_MANAGED_ACCOUNT_TEST" SkipSamlIntegrationTest env = "TEST_SF_TF_SKIP_SAML_INTEGRATION_TEST" + EnableAcceptance env = resource.EnvTfAcc EnableSweep env = "TEST_SF_TF_ENABLE_SWEEP" ConfigureClientOnce env = "SF_TF_ACC_TEST_CONFIGURE_CLIENT_ONCE" TestObjectsSuffix env = "TEST_SF_TF_TEST_OBJECT_SUFFIX" diff --git a/pkg/datasources/databases_acceptance_test.go b/pkg/datasources/databases_acceptance_test.go index 2da4fa3d319..9989a1ad779 100644 --- a/pkg/datasources/databases_acceptance_test.go +++ b/pkg/datasources/databases_acceptance_test.go @@ -43,7 +43,8 @@ func TestAcc_Databases_Complete(t *testing.T) { resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.kind", "STANDARD"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_transient", "false"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_default", "false"), - resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_current", "true"), + // Commenting as this value depends on the currently used database, which is different when running as a single test and multiple tests (e.g., on CI) + // resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_current", "true"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.origin", ""), resource.TestCheckResourceAttrSet("data.snowflake_databases.test", "databases.0.owner"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.comment", comment), @@ -75,7 +76,8 @@ func TestAcc_Databases_Complete(t *testing.T) { resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.kind", "STANDARD"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_transient", "false"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_default", "false"), - resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_current", "false"), + // Commenting for the same reason as above + // resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.is_current", "false"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.origin", ""), resource.TestCheckResourceAttrSet("data.snowflake_databases.test", "databases.0.owner"), resource.TestCheckResourceAttr("data.snowflake_databases.test", "databases.0.comment", comment), diff --git a/pkg/resources/custom_diffs.go b/pkg/resources/custom_diffs.go index 6dac5f3ed3a..a90f50b085f 100644 --- a/pkg/resources/custom_diffs.go +++ b/pkg/resources/custom_diffs.go @@ -41,7 +41,8 @@ func ParameterValueComputedIf(key string, parameters []*sdk.Parameter, objectPar // For cases where currently set value (in the config) is equal to the parameter, but not set on the right level. // The parameter is set somewhere higher in the hierarchy, and we need to "forcefully" set the value to // perform the actual set on Snowflake (and set the parameter on the correct level). - if ok && !configValue.IsNull() && parameter.Level != objectParameterLevel { + // TODO: + if ok && !configValue.IsNull() && parameter.Level != objectParameterLevel && parameter.Value == valueToString(d.Get(key)) { return d.SetNewComputed(key) } diff --git a/pkg/resources/database_acceptance_test.go b/pkg/resources/database_acceptance_test.go index d3ca14d703b..7d18e310430 100644 --- a/pkg/resources/database_acceptance_test.go +++ b/pkg/resources/database_acceptance_test.go @@ -2,6 +2,7 @@ package resources_test import ( "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" "strconv" "testing" @@ -276,7 +277,6 @@ func TestAcc_Database_ComputedValues(t *testing.T) { resource.TestCheckResourceAttr("snowflake_database.test", "external_volume", externalVolumeId.Name()), resource.TestCheckResourceAttr("snowflake_database.test", "catalog", catalogId.Name()), resource.TestCheckResourceAttr("snowflake_database.test", "replace_invalid_characters", "true"), - resource.TestCheckResourceAttr("snowflake_database.test", "default_ddl_collation", "en_US"), resource.TestCheckResourceAttr("snowflake_database.test", "storage_serialization_policy", string(sdk.StorageSerializationPolicyCompatible)), resource.TestCheckResourceAttr("snowflake_database.test", "log_level", string(sdk.LogLevelInfo)), resource.TestCheckResourceAttr("snowflake_database.test", "trace_level", string(sdk.TraceLevelOnEvent)), @@ -1105,13 +1105,12 @@ resource "snowflake_database" "test" { } func TestAcc_Database_UpgradeFromShare(t *testing.T) { + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + id := acc.TestClient().Ids.RandomAccountObjectIdentifier() secondaryClientLocator := acc.SecondaryClient(t).GetAccountLocator() - shareExternalId := sdk.Pointer(sdk.NewExternalObjectIdentifierFromFullyQualifiedName("")) - helpers.TfAccFunc(t, func() { - *shareExternalId = createShareableDatabase(t) - }) + shareExternalId := createShareableDatabase(t) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, @@ -1153,7 +1152,7 @@ func TestAcc_Database_UpgradeFromShare(t *testing.T) { }) } -func databaseStateUpgraderFromShareOld(id sdk.AccountObjectIdentifier, secondaryClientLocator string, externalShare *sdk.ExternalObjectIdentifier) string { +func databaseStateUpgraderFromShareOld(id sdk.AccountObjectIdentifier, secondaryClientLocator string, externalShare sdk.ExternalObjectIdentifier) string { return fmt.Sprintf(` resource "snowflake_database" "test" { name = "%s" @@ -1176,17 +1175,13 @@ resource "snowflake_database" "test" { } func TestAcc_Database_UpgradeFromReplica(t *testing.T) { - id := acc.TestClient().Ids.RandomAccountObjectIdentifier() + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) - // This couldn't be done in any other way (except creating this setup with terraform configuration), because primaryDatabaseId is part of the configuration which is "static". - primaryDatabaseId := sdk.Pointer(sdk.NewExternalObjectIdentifierFromFullyQualifiedName("")) - helpers.TfAccFunc(t, func() { - _, externalId, databaseCleanup := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ - acc.TestClient().Account.GetAccountIdentifier(t), - }) - t.Cleanup(databaseCleanup) - *primaryDatabaseId = externalId + id := acc.TestClient().Ids.RandomAccountObjectIdentifier() + _, primaryDatabaseId, databaseCleanup := acc.SecondaryTestClient().Database.CreatePrimaryDatabase(t, []sdk.AccountIdentifier{ + acc.TestClient().Account.GetAccountIdentifier(t), }) + t.Cleanup(databaseCleanup) resource.Test(t, resource.TestCase{ PreCheck: func() { acc.TestAccPreCheck(t) }, @@ -1202,7 +1197,7 @@ func TestAcc_Database_UpgradeFromReplica(t *testing.T) { Source: "Snowflake-Labs/snowflake", }, }, - Config: databaseStateUpgraderFromReplicaOld(id, *primaryDatabaseId), + Config: databaseStateUpgraderFromReplicaOld(id, primaryDatabaseId), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("snowflake_database.test", "id", id.Name()), resource.TestCheckResourceAttr("snowflake_database.test", "name", id.Name()), diff --git a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go index 1d98b760e8f..2699b0744d2 100644 --- a/pkg/resources/grant_privileges_to_account_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_account_role_acceptance_test.go @@ -1004,14 +1004,14 @@ func TestAcc_GrantPrivilegesToAccountRole_ImportedPrivileges(t *testing.T) { sharedDatabaseId := acc.TestClient().Ids.RandomAccountObjectIdentifier() sharedDatabaseName := sharedDatabaseId.Name() shareId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - shareName := shareId.Name() roleName := acc.TestClient().Ids.Alpha() - secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) configVariables := config.Variables{ "role_name": config.StringVariable(roleName), "shared_database_name": config.StringVariable(sharedDatabaseName), - "share_name": config.StringVariable(shareName), - "account_name": config.StringVariable(secondaryAccountName), + "external_share_name": config.StringVariable(sdk.NewExternalObjectIdentifier( + acc.SecondaryTestClient().Account.GetAccountIdentifier(t), + shareId, + ).FullyQualifiedName()), "privileges": config.ListVariable( config.StringVariable(sdk.AccountObjectPrivilegeImportedPrivileges.String()), ), diff --git a/pkg/resources/grant_privileges_to_role_acceptance_test.go b/pkg/resources/grant_privileges_to_role_acceptance_test.go index b09a9747b9b..121f1484a2a 100644 --- a/pkg/resources/grant_privileges_to_role_acceptance_test.go +++ b/pkg/resources/grant_privileges_to_role_acceptance_test.go @@ -1081,14 +1081,14 @@ func TestAcc_GrantPrivilegesToRole_ImportedPrivileges(t *testing.T) { sharedDatabaseId := acc.TestClient().Ids.RandomAccountObjectIdentifier() sharedDatabaseName := sharedDatabaseId.Name() shareId := acc.TestClient().Ids.RandomAccountObjectIdentifier() - shareName := shareId.Name() roleName := acc.TestClient().Ids.Alpha() - secondaryAccountName := acc.SecondaryTestClient().Context.CurrentAccount(t) configVariables := config.Variables{ "role_name": config.StringVariable(roleName), "shared_database_name": config.StringVariable(sharedDatabaseName), - "share_name": config.StringVariable(shareName), - "account_name": config.StringVariable(secondaryAccountName), + "external_share_name": config.StringVariable(sdk.NewExternalObjectIdentifier( + acc.SecondaryTestClient().Account.GetAccountIdentifier(t), + shareId, + ).FullyQualifiedName()), "privileges": config.ListVariable( config.StringVariable(sdk.AccountObjectPrivilegeImportedPrivileges.String()), ), diff --git a/pkg/resources/secondary_database_acceptance_test.go b/pkg/resources/secondary_database_acceptance_test.go index fccc1683f13..921025ef90b 100644 --- a/pkg/resources/secondary_database_acceptance_test.go +++ b/pkg/resources/secondary_database_acceptance_test.go @@ -326,13 +326,13 @@ func TestAcc_CreateSecondaryDatabase_complete(t *testing.T) { ConfigVariables: unsetConfigVariables, ConfigDirectory: acc.ConfigurationDirectory("TestAcc_SecondaryDatabase/complete-optionals-unset"), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_secondary_database.test", "name", newId.Name()), + resource.TestCheckResourceAttr("snowflake_secondary_database.test", "name", id.Name()), resource.TestCheckResourceAttr("snowflake_secondary_database.test", "is_transient", "false"), resource.TestCheckResourceAttr("snowflake_secondary_database.test", "as_replica_of", externalPrimaryId.FullyQualifiedName()), - resource.TestCheckResourceAttr("snowflake_secondary_database.test", "comment", newComment), + resource.TestCheckResourceAttr("snowflake_secondary_database.test", "comment", ""), - resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "data_retention_time_in_days.0.value", accountDataRetentionTimeInDays), - resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "max_data_extension_time_in_days.0.value", accountMaxDataExtensionTimeInDays), + resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "data_retention_time_in_days", accountDataRetentionTimeInDays), + resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "max_data_extension_time_in_days", accountMaxDataExtensionTimeInDays), resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "external_volume", accountExternalVolume), resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "catalog", accountCatalog), resource.TestCheckResourceAttrPtr("snowflake_secondary_database.test", "replace_invalid_characters", accountReplaceInvalidCharacters), @@ -399,27 +399,41 @@ func TestAcc_CreateSecondaryDatabase_DataRetentionTimeInDays(t *testing.T) { accountDataRetentionTimeInDays, err := acc.Client(t).Parameters.ShowAccountParameter(context.Background(), sdk.AccountParameterDataRetentionTimeInDays) require.NoError(t, err) + externalVolumeId, externalVolumeCleanup := acc.TestClient().ExternalVolume.Create(t) + t.Cleanup(externalVolumeCleanup) + + catalogId, catalogCleanup := acc.TestClient().CatalogIntegration.Create(t) + t.Cleanup(catalogCleanup) + configVariables := func( id sdk.AccountObjectIdentifier, primaryDatabaseName sdk.ExternalObjectIdentifier, dataRetentionTimeInDays *int, ) config.Variables { variables := config.Variables{ - "name": config.StringVariable(id.Name()), - "as_replica_of": config.StringVariable(primaryDatabaseName.FullyQualifiedName()), - "transient": config.BoolVariable(false), - "external_volume": config.StringVariable(""), - "catalog": config.StringVariable(""), - "replace_invalid_characters": config.StringVariable("false"), - "default_ddl_collation": config.StringVariable(""), - "storage_serialization_policy": config.StringVariable("OPTIMIZED"), - "log_level": config.StringVariable("OFF"), - "trace_level": config.StringVariable("OFF"), - "comment": config.StringVariable(""), + "name": config.StringVariable(id.Name()), + "as_replica_of": config.StringVariable(primaryDatabaseName.FullyQualifiedName()), + "transient": config.BoolVariable(false), + "comment": config.StringVariable(""), + + "max_data_extension_time_in_days": config.IntegerVariable(10), + "external_volume": config.StringVariable(externalVolumeId.Name()), + "catalog": config.StringVariable(catalogId.Name()), + "replace_invalid_characters": config.BoolVariable(true), + "default_ddl_collation": config.StringVariable("en_US"), + "storage_serialization_policy": config.StringVariable("OPTIMIZED"), + "log_level": config.StringVariable("OFF"), + "trace_level": config.StringVariable("OFF"), + "suspend_task_after_num_failures": config.IntegerVariable(10), + "task_auto_retry_attempts": config.IntegerVariable(10), + "user_task_managed_initial_warehouse_size": config.StringVariable(string(sdk.WarehouseSizeSmall)), + "user_task_timeout_ms": config.IntegerVariable(120000), + "user_task_minimum_trigger_interval_in_seconds": config.IntegerVariable(120), + "quoted_identifiers_ignore_case": config.BoolVariable(true), + "enable_console_output": config.BoolVariable(true), } if dataRetentionTimeInDays != nil { variables["data_retention_time_in_days"] = config.IntegerVariable(*dataRetentionTimeInDays) - variables["max_data_extension_time_in_days"] = config.IntegerVariable(10) } return variables } diff --git a/pkg/resources/testdata/TestAcc_DatabaseRemovedOutsideOfTerraform/test.tf b/pkg/resources/testdata/TestAcc_DatabaseRemovedOutsideOfTerraform/test.tf index f0d3470e099..180a0c22bd1 100644 --- a/pkg/resources/testdata/TestAcc_DatabaseRemovedOutsideOfTerraform/test.tf +++ b/pkg/resources/testdata/TestAcc_DatabaseRemovedOutsideOfTerraform/test.tf @@ -1,4 +1,4 @@ -resource "snowflake_database" "db" { +resource "snowflake_database_old" "db" { name = var.db comment = "test comment" } diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf index 12a910c7d28..2f9535a0f12 100644 --- a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithDataRetentionSet/test.tf @@ -1,4 +1,4 @@ -resource "snowflake_database" "test" { +resource "snowflake_database_old" "test" { name = var.database data_retention_time_in_days = var.database_data_retention_time } diff --git a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf index 894e86ac200..c3386f300ae 100644 --- a/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf +++ b/pkg/resources/testdata/TestAcc_Database_DefaultDataRetentionTime/WithoutDataRetentionSet/test.tf @@ -1,3 +1,3 @@ -resource "snowflake_database" "test" { +resource "snowflake_database_old" "test" { name = var.database } diff --git a/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/test.tf b/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/test.tf index f0ab03829d6..c53ebc83cbc 100644 --- a/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/test.tf +++ b/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/test.tf @@ -1,10 +1,6 @@ -resource "snowflake_database" "test" { +resource "snowflake_shared_database" "test" { name = var.shared_database_name - data_retention_time_in_days = 0 - from_share = { - provider = var.account_name - share = var.share_name - } + from_share = var.external_share_name } resource "snowflake_role" "test" { @@ -12,7 +8,7 @@ resource "snowflake_role" "test" { } resource "snowflake_grant_privileges_to_account_role" "test" { - depends_on = [snowflake_database.test, snowflake_role.test] + depends_on = [snowflake_shared_database.test, snowflake_role.test] account_role_name = "\"${var.role_name}\"" privileges = var.privileges on_account_object { diff --git a/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/variables.tf b/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/variables.tf index 9e948b415ff..3f5de98393b 100644 --- a/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/variables.tf +++ b/pkg/resources/testdata/TestAcc_GrantPrivilegesToAccountRole/ImportedPrivileges/variables.tf @@ -10,10 +10,6 @@ variable "shared_database_name" { type = string } -variable "share_name" { - type = string -} - -variable "account_name" { +variable "external_share_name" { type = string } diff --git a/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/test.tf b/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/test.tf index 593ba8b1e21..c64d762da3a 100644 --- a/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/test.tf +++ b/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/test.tf @@ -1,10 +1,6 @@ -resource "snowflake_database" "test" { +resource "snowflake_shared_database" "test" { name = var.shared_database_name - data_retention_time_in_days = 0 - from_share = { - provider = var.account_name - share = var.share_name - } + from_share = var.external_share_name } resource "snowflake_role" "test" { @@ -12,7 +8,7 @@ resource "snowflake_role" "test" { } resource "snowflake_grant_privileges_to_role" "test" { - depends_on = [snowflake_database.test, snowflake_role.test] + depends_on = [snowflake_shared_database.test, snowflake_role.test] role_name = "\"${var.role_name}\"" privileges = var.privileges on_account_object { diff --git a/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/variables.tf b/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/variables.tf index 9e948b415ff..3f5de98393b 100644 --- a/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/variables.tf +++ b/pkg/resources/testdata/TestAcc_GrantPrivilegesToRole/ImportedPrivileges/variables.tf @@ -10,10 +10,6 @@ variable "shared_database_name" { type = string } -variable "share_name" { - type = string -} - -variable "account_name" { +variable "external_share_name" { type = string }