Skip to content

Commit

Permalink
Add test cases for proving that renames are going through update
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Apr 12, 2024
1 parent 3803bd1 commit 1f80645
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 69 deletions.
54 changes: 53 additions & 1 deletion pkg/resources/file_format_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/plancheck"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -446,7 +448,45 @@ func TestAcc_FileFormat_issue1947(t *testing.T) {
})
}

// TODO: Tescik
func TestAcc_FileFormat_Rename(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
newName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
comment := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
newComment := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
resourceName := "snowflake_file_format.test"

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
Steps: []resource.TestStep{
{
Config: fileFormatConfigWithComment(name, acc.TestDatabaseName, acc.TestSchemaName, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
),
},
{
Config: fileFormatConfigWithComment(newName, acc.TestDatabaseName, acc.TestSchemaName, newComment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", newName),
resource.TestCheckResourceAttr(resourceName, "comment", newComment),
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}

func fileFormatConfigCSV(n string, databaseName string, schemaName string, fieldDelimiter string, fieldOptionallyEnclosedBy string, comment string) string {
return fmt.Sprintf(`
Expand Down Expand Up @@ -581,6 +621,18 @@ resource "snowflake_file_format" "test" {
`, n, databaseName, schemaName, formatType)
}

func fileFormatConfigWithComment(n string, databaseName string, schemaName string, comment string) string {
return fmt.Sprintf(`
resource "snowflake_file_format" "test" {
name = "%v"
database = "%s"
schema = "%s"
format_type = "XML"
comment = "%s"
}
`, n, databaseName, schemaName, comment)
}

func fileFormatConfigFullDefaultsWithAdditionalParam(n string, formatType string, databaseName string, schemaName string) string {
return fmt.Sprintf(`
resource "snowflake_file_format" "test" {
Expand Down
8 changes: 4 additions & 4 deletions pkg/resources/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,14 +658,14 @@ func UpdateContextFunction(ctx context.Context, d *schema.ResourceData, meta int
id := sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(d.Id())
if d.HasChange("name") {
name := d.Get("name").(string)
newId := sdk.NewSchemaObjectIdentifier(id.DatabaseName(), id.SchemaName(), name)
newId := sdk.NewSchemaObjectIdentifierWithArguments(id.DatabaseName(), id.SchemaName(), name, id.Arguments())

if err := client.Functions.Alter(ctx, sdk.NewAlterFunctionRequest(id.WithoutArguments(), id.Arguments()).WithRenameTo(&newId)); err != nil {
if err := client.Functions.Alter(ctx, sdk.NewAlterFunctionRequest(id.WithoutArguments(), id.Arguments()).WithRenameTo(sdk.Pointer(newId.WithoutArguments()))); err != nil {
return diag.FromErr(err)
}

d.SetId(id.FullyQualifiedName())
id = sdk.NewSchemaObjectIdentifierWithArguments(id.DatabaseName(), id.SchemaName(), name, id.Arguments())
d.SetId(newId.FullyQualifiedName())
id = newId
}

if d.HasChange("is_secure") {
Expand Down
53 changes: 48 additions & 5 deletions pkg/resources/function_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/plancheck"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
Expand Down Expand Up @@ -187,6 +189,7 @@ func TestAcc_Function_complex(t *testing.T) {
// proves issue https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2490
func TestAcc_Function_migrateFromVersion085(t *testing.T) {
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
comment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_function.f"

resource.Test(t, resource.TestCase{
Expand All @@ -208,7 +211,7 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) {
Source: "Snowflake-Labs/snowflake",
},
},
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name),
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf("%s|%s|%s|VARCHAR", acc.TestDatabaseName, acc.TestSchemaName, name)),
resource.TestCheckResourceAttr(resourceName, "name", name),
Expand All @@ -218,7 +221,7 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) {
},
{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name),
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", sdk.NewSchemaObjectIdentifierWithArguments(acc.TestDatabaseName, acc.TestSchemaName, name, []sdk.DataType{sdk.DataTypeVARCHAR}).FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "name", name),
Expand All @@ -230,14 +233,54 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) {
})
}

// TODO: Tescik
func TestAcc_Function_Rename(t *testing.T) {
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
newName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
comment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
newComment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resourceName := "snowflake_function.f"

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: testAccCheckFunctionDestroy,
Steps: []resource.TestStep{
{
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "comment", comment),
),
},
{
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, newName, newComment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", newName),
resource.TestCheckResourceAttr(resourceName, "comment", newComment),
),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
},
PostApplyPostRefresh: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}

func functionConfig(database string, schema string, name string) string {
func functionConfig(database string, schema string, name string, comment string) string {
return fmt.Sprintf(`
resource "snowflake_function" "f" {
database = "%[1]s"
schema = "%[2]s"
name = "%[3]s"
comment = "%[4]s"
return_type = "VARCHAR"
return_behavior = "IMMUTABLE"
statement = "SELECT PARAM"
Expand All @@ -247,7 +290,7 @@ resource "snowflake_function" "f" {
type = "VARCHAR"
}
}
`, database, schema, name)
`, database, schema, name, comment)
}

func testAccCheckFunctionDestroy(s *terraform.State) error {
Expand Down
1 change: 0 additions & 1 deletion pkg/resources/masking_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var maskingPolicySchema = map[string]*schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Specifies the identifier for the masking policy; must be unique for the database and schema in which the masking policy is created.",
ForceNew: true,
},
"database": {
Type: schema.TypeString,
Expand Down
19 changes: 7 additions & 12 deletions pkg/resources/masking_policy_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

Expand Down Expand Up @@ -43,19 +42,17 @@ func TestAcc_MaskingPolicy(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "signature.0.column.0.type", "VARCHAR"),
),
},
// change comment
{
Config: maskingPolicyConfig(accName, accName, comment2, acc.TestDatabaseName, acc.TestSchemaName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "name", accName),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "comment", comment2),
),
},
// rename
// rename + change comment
{
Config: maskingPolicyConfig(accName, accName2, comment2, acc.TestDatabaseName, acc.TestSchemaName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("snowflake_masking_policy.test", plancheck.ResourceActionUpdate),
},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "name", accName2),
resource.TestCheckResourceAttr("snowflake_masking_policy.test", "comment", comment2),
),
},
// change body and unset comment
Expand All @@ -76,8 +73,6 @@ func TestAcc_MaskingPolicy(t *testing.T) {
})
}

// TODO: Tescik

func maskingPolicyConfig(n string, name string, comment string, databaseName string, schemaName string) string {
return fmt.Sprintf(`
resource "snowflake_masking_policy" "test" {
Expand Down
23 changes: 18 additions & 5 deletions pkg/resources/password_policy_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/plancheck"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -100,16 +102,21 @@ func TestAcc_PasswordPolicy(t *testing.T) {
}

func TestAcc_PasswordPolicyMaxAgeDays(t *testing.T) {
accName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
newName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

m := func(maxAgeDays int) map[string]config.Variable {
return map[string]config.Variable{
"name": config.StringVariable(accName),
"name": config.StringVariable(name),
"database": config.StringVariable(acc.TestDatabaseName),
"schema": config.StringVariable(acc.TestSchemaName),
"max_age_days": config.IntegerVariable(maxAgeDays),
}
}

configValueWithNewName := m(10)
configValueWithNewName["name"] = config.StringVariable(newName)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
Expand Down Expand Up @@ -141,22 +148,28 @@ func TestAcc_PasswordPolicyMaxAgeDays(t *testing.T) {
resource.TestCheckResourceAttr("snowflake_password_policy.pa", "max_age_days", "0"),
),
},
// Unsets properly
// Rename + Unsets properly
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_PasswordPolicy_noOptionals"),
ConfigVariables: map[string]config.Variable{
"name": config.StringVariable(accName),
"name": config.StringVariable(newName),
"database": config.StringVariable(acc.TestDatabaseName),
"schema": config.StringVariable(acc.TestSchemaName),
},
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("snowflake_password_policy.pa", plancheck.ResourceActionUpdate),
},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_password_policy.pa", "name", newName),
resource.TestCheckResourceAttr("snowflake_password_policy.pa", "max_age_days", "90"),
),
},
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_PasswordPolicy_noOptionals"),
ConfigVariables: map[string]config.Variable{
"name": config.StringVariable(accName),
"name": config.StringVariable(name),
"database": config.StringVariable(acc.TestDatabaseName),
"schema": config.StringVariable(acc.TestSchemaName),
},
Expand Down
8 changes: 6 additions & 2 deletions pkg/resources/schema_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ func TestAcc_Schema(t *testing.T) {
})
}

// TODO: Rename + param change
func TestAcc_Schema_Rename(t *testing.T) {
oldSchemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
newSchemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
Expand Down Expand Up @@ -105,6 +104,11 @@ func TestAcc_Schema_Rename(t *testing.T) {
"database": config.StringVariable(acc.TestDatabaseName),
"comment": config.StringVariable(comment),
},
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("snowflake_schema.test", plancheck.ResourceActionUpdate),
},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_schema.test", "name", newSchemaName),
resource.TestCheckResourceAttr("snowflake_schema.test", "database", acc.TestDatabaseName),
Expand Down Expand Up @@ -337,7 +341,7 @@ func TestAcc_Schema_RemoveDatabaseOutsideOfTerraform(t *testing.T) {
ExpectNonEmptyPlan: true,
RefreshPlanChecks: resource.RefreshPlanChecks{
PostRefresh: []plancheck.PlanCheck{
expectsCreatePlan("snowflake_schema.test"),
plancheck.ExpectResourceAction("snowflake_schema.test", plancheck.ResourceActionCreate),
},
},
},
Expand Down
Loading

0 comments on commit 1f80645

Please sign in to comment.