Skip to content

Commit

Permalink
fix: Try to reproduce 2679 and 3117 (#3124)
Browse files Browse the repository at this point in the history
- Prove #2679; documentation added, fix will be done with the stage
resource rework
- Try to prove #3117; could not reproduce the error, test left for now
  • Loading branch information
sfc-gh-asawicki authored Oct 10, 2024
1 parent 0e88e08 commit ccdbc30
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/resources/stage.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ resource "snowflake_stage" "example_stage" {
- `credentials` (String, Sensitive) Specifies the credentials for the stage.
- `directory` (String) Specifies the directory settings for the stage.
- `encryption` (String) Specifies the encryption settings for the stage.
- `file_format` (String) Specifies the file format for the stage.
- `file_format` (String) Specifies the file format for the stage. Specifying the default Snowflake value (e.g. TYPE = CSV) will currently result in a permadiff (check [#2679](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2679)). For now, omit the default values; it will be fixed in the upcoming provider versions.
- `snowflake_iam_user` (String) An AWS IAM user created for your Snowflake account. This user is the same for every external S3 stage created in your account.
- `storage_integration` (String) Specifies the name of the storage integration used to delegate authentication responsibility for external cloud storage to a Snowflake identity and access management (IAM) entity.
- `tag` (Block List, Deprecated) Definitions of a tag to associate with the resource. (see [below for nested schema](#nestedblock--tag))
Expand Down
7 changes: 7 additions & 0 deletions pkg/acceptance/helpers/stage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,10 @@ func (c *StageClient) Rename(t *testing.T, id sdk.SchemaObjectIdentifier, newId
err := c.client().Alter(ctx, sdk.NewAlterStageRequest(id).WithRenameTo(&newId))
require.NoError(t, err)
}

func (c *StageClient) Describe(t *testing.T, id sdk.SchemaObjectIdentifier) ([]sdk.StageProperty, error) {
t.Helper()
ctx := context.Background()

return c.client().Describe(ctx, id)
}
56 changes: 56 additions & 0 deletions pkg/resources/alert_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)
Expand Down Expand Up @@ -202,3 +203,58 @@ resource "snowflake_alert" "test_alert" {
}
return result.String()
}

// Can't reproduce the issue, leaving the test for now.
func TestAcc_Alert_Issue3117(t *testing.T) {
id := acc.TestClient().Ids.RandomSchemaObjectIdentifierWithPrefix("small caps with spaces")

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.Alert),
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"snowflake": {
VersionConstraint: "=0.92.0",
Source: "Snowflake-Labs/snowflake",
},
},
Config: alertIssue3117Config(id, acc.TestClient().Ids.WarehouseId()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_alert.test_alert", "name", id.Name()),
),
},
{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
Config: alertIssue3117Config(id, acc.TestClient().Ids.WarehouseId()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_alert.test_alert", "name", id.Name()),
),
},
},
})
}

func alertIssue3117Config(alertId sdk.SchemaObjectIdentifier, warehouseId sdk.AccountObjectIdentifier) string {
return fmt.Sprintf(`
resource "snowflake_alert" "test_alert" {
database = "%[1]s"
schema = "%[2]s"
name = "%[3]s"
warehouse = "%[4]s"
alert_schedule {
interval = 1 #check every minute for new alerts
}
action = "select 0 as c"
condition = "select 0 as c"
enabled = true
comment = "Alert config for GH issue 3117"
}
`, alertId.DatabaseName(), alertId.SchemaName(), alertId.Name(), warehouseId.Name())
}
2 changes: 1 addition & 1 deletion pkg/resources/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var stageSchema = map[string]*schema.Schema{
"file_format": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies the file format for the stage.",
Description: "Specifies the file format for the stage. Specifying the default Snowflake value (e.g. TYPE = CSV) will currently result in a permadiff (check [#2679](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2679)). For now, omit the default values; it will be fixed in the upcoming provider versions.",
DiffSuppressFunc: suppressQuoting,
},
"copy_options": {
Expand Down
81 changes: 80 additions & 1 deletion pkg/resources/stage_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
"testing"

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

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/ids"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

Expand Down Expand Up @@ -255,3 +257,80 @@ resource "snowflake_stage" "test" {
}
`, stageId.Name(), stageId.DatabaseName(), stageId.SchemaName())
}

// TODO [SNOW-1348110]: fix behavior with stage rework
func TestAcc_Stage_Issue2679(t *testing.T) {
integrationId := acc.TestClient().Ids.RandomAccountObjectIdentifier()
stageId := acc.TestClient().Ids.RandomSchemaObjectIdentifier()
resourceName := "snowflake_stage.test"

fileFormatWithDefaultTypeCsv := "TYPE = CSV NULL_IF = []"
fileFormatWithoutType := "NULL_IF = []"

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: acc.CheckDestroy(t, resources.Stage),
Steps: []resource.TestStep{
{
Config: stageIssue2679Config(integrationId, stageId, fileFormatWithDefaultTypeCsv),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", stageId.Name()),
),
ExpectNonEmptyPlan: true,
},
{
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
Config: stageIssue2679Config(integrationId, stageId, fileFormatWithoutType),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", stageId.Name()),
// TODO [SNOW-1348110]: use generated assertions after stage rework
func(_ *terraform.State) error {
properties, err := acc.TestClient().Stage.Describe(t, stageId)
if err != nil {
return err
}
typeProperty, err := collections.FindFirst(properties, func(property sdk.StageProperty) bool {
return property.Parent == "STAGE_FILE_FORMAT" && property.Name == "TYPE"
})
if err != nil {
return err
}
if typeProperty.Value != "CSV" {
return fmt.Errorf("expected type property 'CSV', got '%s'", typeProperty.Value)
}
return nil
},
),
},
},
})
}

func stageIssue2679Config(integrationId sdk.AccountObjectIdentifier, stageId sdk.SchemaObjectIdentifier, fileFormat string) string {
return fmt.Sprintf(`
resource "snowflake_storage_integration" "test" {
name = "%[1]s"
storage_allowed_locations = ["s3://aaaaa"]
storage_provider = "S3"
storage_aws_role_arn = "arn:aws:iam::000000000001:/role/test"
}
resource "snowflake_stage" "test" {
name = "%[2]s"
database = "%[3]s"
schema = "%[4]s"
file_format = "%[5]s"
storage_integration = snowflake_storage_integration.test.name
url = "s3://aaaaa"
}
`, integrationId.Name(), stageId.Name(), stageId.DatabaseName(), stageId.SchemaName(), fileFormat)
}
2 changes: 1 addition & 1 deletion pkg/sdk/testint/warehouses_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func TestInt_Warehouses(t *testing.T) {
returnedWarehouse, err := client.Warehouses.ShowByID(ctx, warehouse.ID())
require.NoError(t, err)
assert.Equal(t, sdk.WarehouseTypeStandard, returnedWarehouse.Type)
assert.Equal(t, sdk.WarehouseStateStarted, returnedWarehouse.State)
require.Eventually(t, func() bool { return sdk.WarehouseStateStarted == returnedWarehouse.State }, 5*time.Second, time.Second)

alterOptions := &sdk.AlterWarehouseOptions{
Set: &sdk.WarehouseSet{WarehouseType: sdk.Pointer(sdk.WarehouseTypeSnowparkOptimized)},
Expand Down
Loading

0 comments on commit ccdbc30

Please sign in to comment.