From a3cec3f1dfa155ca8c7ff6c82f3c6ad3a8c7f338 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Fri, 5 Jul 2024 14:12:00 +0200 Subject: [PATCH 01/17] Prepare assertions for resources, imports, and objects --- pkg/acceptance/bettertestspoc/commons.go | 76 ++++++ .../bettertestspoc/resource_assertions.go | 120 ++++++++++ .../bettertestspoc/snowflake_assertions.go | 62 +++++ .../bettertestspoc/warehouse_parameters.go | 60 +++++ .../warehouse_parameters_ext.go | 19 ++ .../bettertestspoc/warehouse_resource.go | 185 +++++++++++++++ .../bettertestspoc/warehouse_resource_ext.go | 16 ++ .../bettertestspoc/warehouse_show_output.go | 85 +++++++ .../bettertestspoc/warehouse_snowflake.go | 164 +++++++++++++ pkg/acceptance/importchecks/import_checks.go | 2 +- pkg/resources/warehouse.go | 4 +- pkg/resources/warehouse_acceptance_test.go | 222 +++++++++++++----- pkg/sdk/warehouses.go | 6 +- 13 files changed, 954 insertions(+), 67 deletions(-) create mode 100644 pkg/acceptance/bettertestspoc/commons.go create mode 100644 pkg/acceptance/bettertestspoc/resource_assertions.go create mode 100644 pkg/acceptance/bettertestspoc/snowflake_assertions.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_parameters.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_resource.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_resource_ext.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_show_output.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_snowflake.go diff --git a/pkg/acceptance/bettertestspoc/commons.go b/pkg/acceptance/bettertestspoc/commons.go new file mode 100644 index 0000000000..30f5ef312a --- /dev/null +++ b/pkg/acceptance/bettertestspoc/commons.go @@ -0,0 +1,76 @@ +package bettertestspoc + +import ( + "errors" + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +type TestCheckFuncProvider interface { + ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc +} + +func AssertThat(t *testing.T, fs ...TestCheckFuncProvider) resource.TestCheckFunc { + t.Helper() + return func(s *terraform.State) error { + var result []error + + for i, f := range fs { + if err := f.ToTerraformTestCheckFunc(t)(s); err != nil { + result = append(result, fmt.Errorf("check %d/%d error:\n%w", i+1, len(fs), err)) + } + } + + return errors.Join(result...) + } +} + +var _ TestCheckFuncProvider = (*testCheckFuncWrapper)(nil) + +type testCheckFuncWrapper struct { + f resource.TestCheckFunc +} + +func (w *testCheckFuncWrapper) ToTerraformTestCheckFunc(_ *testing.T) resource.TestCheckFunc { + return w.f +} + +func Check(f resource.TestCheckFunc) TestCheckFuncProvider { + return &testCheckFuncWrapper{f} +} + +type ImportStateCheckFuncProvider interface { + ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc +} + +func AssertThatImport(t *testing.T, fs ...ImportStateCheckFuncProvider) resource.ImportStateCheckFunc { + t.Helper() + return func(s []*terraform.InstanceState) error { + var result []error + + for i, f := range fs { + if err := f.ToTerraformImportStateCheckFunc(t)(s); err != nil { + result = append(result, fmt.Errorf("check %d/%d error:\n%w", i+1, len(fs), err)) + } + } + + return errors.Join(result...) + } +} + +var _ ImportStateCheckFuncProvider = (*importStateCheckFuncWrapper)(nil) + +type importStateCheckFuncWrapper struct { + f resource.ImportStateCheckFunc +} + +func (w *importStateCheckFuncWrapper) ToTerraformImportStateCheckFunc(_ *testing.T) resource.ImportStateCheckFunc { + return w.f +} + +func CheckImport(f resource.ImportStateCheckFunc) ImportStateCheckFuncProvider { + return &importStateCheckFuncWrapper{f} +} diff --git a/pkg/acceptance/bettertestspoc/resource_assertions.go b/pkg/acceptance/bettertestspoc/resource_assertions.go new file mode 100644 index 0000000000..1584fd0c9b --- /dev/null +++ b/pkg/acceptance/bettertestspoc/resource_assertions.go @@ -0,0 +1,120 @@ +package bettertestspoc + +import ( + "errors" + "fmt" + "strings" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +var _ TestCheckFuncProvider = (*ResourceAssert)(nil) +var _ ImportStateCheckFuncProvider = (*ResourceAssert)(nil) + +type ResourceAssert struct { + name string + id string + prefix string + assertions []resourceAssertion + TestCheckFuncProvider +} + +func NewResourceAssert(name string, prefix string) *ResourceAssert { + return &ResourceAssert{ + name: name, + prefix: prefix, + assertions: make([]resourceAssertion, 0), + } +} + +func NewImportedResourceAssert(id string, prefix string) *ResourceAssert { + return &ResourceAssert{ + id: id, + prefix: prefix, + assertions: make([]resourceAssertion, 0), + } +} + +type resourceAssertionType string + +const resourceAssertionTypeValueSet = "VALUE_SET" +const resourceAssertionTypeValueNotSet = "VALUE_NOT_SET" + +type resourceAssertion struct { + fieldName string + expectedValue string + resourceAssertionType resourceAssertionType +} + +func valueSet(fieldName string, expected string) resourceAssertion { + return resourceAssertion{fieldName: fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +func valueNotSet(fieldName string) resourceAssertion { + return resourceAssertion{fieldName: fieldName, resourceAssertionType: resourceAssertionTypeValueNotSet} +} + +const showOutputPrefix = "show_output.0." + +func showOutputValueSet(fieldName string, expected string) resourceAssertion { + return resourceAssertion{fieldName: showOutputPrefix + fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +const parametersPrefix = "parameters.0." +const parametersValueSuffix = ".0.value" +const parametersLevelSuffix = ".0.level" + +func parameterValueSet(fieldName string, expected string) resourceAssertion { + return resourceAssertion{fieldName: parametersPrefix + fieldName + parametersValueSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +func parameterLevelSet(fieldName string, expected string) resourceAssertion { + return resourceAssertion{fieldName: parametersPrefix + fieldName + parametersLevelSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} +} + +func (r *ResourceAssert) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { + t.Helper() + return func(s *terraform.State) error { + var result []error + + for i, a := range r.assertions { + switch a.resourceAssertionType { + case resourceAssertionTypeValueSet: + if err := resource.TestCheckResourceAttr(r.name, a.fieldName, a.expectedValue)(s); err != nil { + errCut, _ := strings.CutPrefix(err.Error(), fmt.Sprintf("%s: ", r.name)) + result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %s", r.name, r.prefix, i+1, len(r.assertions), errCut)) + } + case resourceAssertionTypeValueNotSet: + if err := resource.TestCheckNoResourceAttr(r.name, a.fieldName)(s); err != nil { + errCut, _ := strings.CutPrefix(err.Error(), fmt.Sprintf("%s: ", r.name)) + result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %s", r.name, r.prefix, i+1, len(r.assertions), errCut)) + } + } + } + + return errors.Join(result...) + } +} + +func (r *ResourceAssert) ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc { + t.Helper() + return func(s []*terraform.InstanceState) error { + var result []error + + for i, a := range r.assertions { + switch a.resourceAssertionType { + case resourceAssertionTypeValueSet: + if err := importchecks.TestCheckResourceAttrInstanceState(r.id, a.fieldName, a.expectedValue)(s); err != nil { + result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %s", r.id, r.prefix, i+1, len(r.assertions), err)) + } + case resourceAssertionTypeValueNotSet: + panic("implement") + } + } + + return errors.Join(result...) + } +} diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go new file mode 100644 index 0000000000..b8009e489a --- /dev/null +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -0,0 +1,62 @@ +package bettertestspoc + +import ( + "errors" + "fmt" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +type assertSdk[T any] func(*testing.T, T) error +type objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (T, error) + +type SnowflakeObjectAssert[T any, I sdk.ObjectIdentifier] struct { + assertions []assertSdk[T] + id I + provider objectProvider[T, I] + objectType sdk.ObjectType + TestCheckFuncProvider +} + +func NewSnowflakeObjectAssert[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, provider objectProvider[T, I]) *SnowflakeObjectAssert[T, I] { + return &SnowflakeObjectAssert[T, I]{ + assertions: make([]assertSdk[T], 0), + id: id, + provider: provider, + objectType: objectType, + } +} + +func (s *SnowflakeObjectAssert[_, _]) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { + t.Helper() + return func(_ *terraform.State) error { + return s.commonTerraformCheckFuncProvider(t) + } +} + +func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc { + t.Helper() + return func(_ []*terraform.InstanceState) error { + return s.commonTerraformCheckFuncProvider(t) + } +} + +func (s *SnowflakeObjectAssert[_, _]) commonTerraformCheckFuncProvider(t *testing.T) error { + sdkObject, err := s.provider(t, s.id) + if err != nil { + return err + } + + var result []error + + for i, assertion := range s.assertions { + if err = assertion(t, sdkObject); err != nil { + result = append(result, fmt.Errorf("object %s[%s] assertion [%d/%d]: failed with error: %w", s.objectType, s.id.FullyQualifiedName(), i+1, len(s.assertions), err)) + } + } + + return errors.Join(result...) +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_parameters.go b/pkg/acceptance/bettertestspoc/warehouse_parameters.go new file mode 100644 index 0000000000..5d7394990c --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_parameters.go @@ -0,0 +1,60 @@ +package bettertestspoc + +import ( + "strconv" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +type WarehouseParametersAssert struct { + *ResourceAssert +} + +func WarehouseParameters(t *testing.T, name string) *WarehouseParametersAssert { + t.Helper() + w := WarehouseParametersAssert{ + NewResourceAssert(name, "parameters"), + } + w.assertions = append(w.assertions, valueSet("parameters.#", "1")) + return &w +} + +func ImportedWarehouseParameters(t *testing.T, id string) *WarehouseParametersAssert { + t.Helper() + w := WarehouseParametersAssert{ + NewImportedResourceAssert(id, "imported parameters"), + } + w.assertions = append(w.assertions, valueSet("parameters.#", "1")) + return &w +} + +func (w *WarehouseParametersAssert) HasMaxConcurrencyLevel(expected int) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterValueSet("max_concurrency_level", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseParametersAssert) HasStatementQueuedTimeoutInSeconds(expected int) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterValueSet("statement_queued_timeout_in_seconds", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseParametersAssert) HasStatementTimeoutInSeconds(expected int) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterValueSet("statement_timeout_in_seconds", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseParametersAssert) HasMaxConcurrencyLevelLevel(expected sdk.ParameterType) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterLevelSet("max_concurrency_level", string(expected))) + return w +} + +func (w *WarehouseParametersAssert) HasStatementQueuedTimeoutInSecondsLevel(expected sdk.ParameterType) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterLevelSet("statement_queued_timeout_in_seconds", string(expected))) + return w +} + +func (w *WarehouseParametersAssert) HasStatementTimeoutInSecondsLevel(expected sdk.ParameterType) *WarehouseParametersAssert { + w.assertions = append(w.assertions, parameterLevelSet("statement_timeout_in_seconds", string(expected))) + return w +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go b/pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go new file mode 100644 index 0000000000..8798b9711b --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go @@ -0,0 +1,19 @@ +package bettertestspoc + +func (w *WarehouseParametersAssert) HasDefaultMaxConcurrencyLevel() *WarehouseParametersAssert { + return w. + HasMaxConcurrencyLevel(8). + HasMaxConcurrencyLevelLevel("") +} + +func (w *WarehouseParametersAssert) HasDefaultStatementQueuedTimeoutInSeconds() *WarehouseParametersAssert { + return w. + HasStatementQueuedTimeoutInSeconds(0). + HasStatementQueuedTimeoutInSecondsLevel("") +} + +func (w *WarehouseParametersAssert) HasDefaultStatementTimeoutInSeconds() *WarehouseParametersAssert { + return w. + HasStatementTimeoutInSeconds(172800). + HasStatementTimeoutInSecondsLevel("") +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_resource.go b/pkg/acceptance/bettertestspoc/warehouse_resource.go new file mode 100644 index 0000000000..7b49060a84 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_resource.go @@ -0,0 +1,185 @@ +package bettertestspoc + +import ( + "testing" +) + +type WarehouseResourceAssert struct { + *ResourceAssert +} + +func WarehouseResource(t *testing.T, name string) *WarehouseResourceAssert { + t.Helper() + + return &WarehouseResourceAssert{ + ResourceAssert: NewResourceAssert(name, "resource"), + } +} + +func ImportedWarehouseResource(t *testing.T, id string) *WarehouseResourceAssert { + t.Helper() + + return &WarehouseResourceAssert{ + ResourceAssert: NewImportedResourceAssert(id, "imported resource"), + } +} + +func (w *WarehouseResourceAssert) HasName(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("name", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasType(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("warehouse_type", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasSize(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("warehouse_size", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasMaxClusterCount(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("max_cluster_count", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasMinClusterCount(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("min_cluster_count", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasScalingPolicy(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("scaling_policy", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasAutoSuspend(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("auto_suspend", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasAutoResume(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("auto_resume", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasInitiallySuspended(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("initially_suspended", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasResourceMonitor(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("resource_monitor", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasComment(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("comment", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasEnableQueryAcceleration(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("enable_query_acceleration", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasQueryAccelerationMaxScaleFactor(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("query_acceleration_max_scale_factor", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasMaxConcurrencyLevel(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("max_concurrency_level", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasStatementQueuedTimeoutInSeconds(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_queued_timeout_in_seconds", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasStatementTimeoutInSeconds(expected string) *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_timeout_in_seconds", expected)) + return w +} + +func (w *WarehouseResourceAssert) HasNoName() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("name")) + return w +} + +func (w *WarehouseResourceAssert) HasNoType() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("warehouse_type")) + return w +} + +func (w *WarehouseResourceAssert) HasNoSize() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("warehouse_size")) + return w +} + +func (w *WarehouseResourceAssert) HasNoMaxClusterCount() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("max_cluster_count")) + return w +} + +func (w *WarehouseResourceAssert) HasNoMinClusterCount() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("min_cluster_count")) + return w +} + +func (w *WarehouseResourceAssert) HasNoScalingPolicy() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("scaling_policy")) + return w +} + +func (w *WarehouseResourceAssert) HasNoAutoSuspend() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("auto_suspend")) + return w +} + +func (w *WarehouseResourceAssert) HasNoAutoResume() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("auto_resume")) + return w +} + +func (w *WarehouseResourceAssert) HasNoInitiallySuspended() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("initially_suspended")) + return w +} + +func (w *WarehouseResourceAssert) HasNoResourceMonitor() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("resource_monitor")) + return w +} + +func (w *WarehouseResourceAssert) HasNoComment() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("comment")) + return w +} + +func (w *WarehouseResourceAssert) HasNoEnableQueryAcceleration() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("enable_query_acceleration")) + return w +} + +func (w *WarehouseResourceAssert) HasNoQueryAccelerationMaxScaleFactor() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("query_acceleration_max_scale_factor")) + return w +} + +func (w *WarehouseResourceAssert) HasNoMaxConcurrencyLevel() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("max_concurrency_level")) + return w +} + +func (w *WarehouseResourceAssert) HasNoStatementQueuedTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("statement_queued_timeout_in_seconds")) + return w +} + +func (w *WarehouseResourceAssert) HasNoStatementTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueNotSet("statement_timeout_in_seconds")) + return w +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go b/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go new file mode 100644 index 0000000000..fb16890608 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go @@ -0,0 +1,16 @@ +package bettertestspoc + +func (w *WarehouseResourceAssert) HasDefaultMaxConcurrencyLevel() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("max_concurrency_level", "8")) + return w +} + +func (w *WarehouseResourceAssert) HasDefaultStatementQueuedTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_queued_timeout_in_seconds", "0")) + return w +} + +func (w *WarehouseResourceAssert) HasDefaultStatementTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_timeout_in_seconds", "172800")) + return w +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_show_output.go b/pkg/acceptance/bettertestspoc/warehouse_show_output.go new file mode 100644 index 0000000000..073850497d --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_show_output.go @@ -0,0 +1,85 @@ +package bettertestspoc + +import ( + "strconv" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +type WarehouseShowOutputAssert struct { + *ResourceAssert +} + +func WarehouseShowOutput(t *testing.T, name string) *WarehouseShowOutputAssert { + t.Helper() + w := WarehouseShowOutputAssert{ + NewResourceAssert(name, "show_output"), + } + w.assertions = append(w.assertions, valueSet("show_output.#", "1")) + return &w +} + +func ImportedWarehouseShowOutput(t *testing.T, id string) *WarehouseShowOutputAssert { + t.Helper() + w := WarehouseShowOutputAssert{ + NewImportedResourceAssert(id, "show_output"), + } + w.assertions = append(w.assertions, valueSet("show_output.#", "1")) + return &w +} + +func (w *WarehouseShowOutputAssert) HasType(expected sdk.WarehouseType) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("type", string(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasSize(expected sdk.WarehouseSize) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("size", string(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasMaxClusterCount(expected int) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("max_cluster_count", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasMinClusterCount(expected int) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("min_cluster_count", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasScalingPolicy(expected sdk.ScalingPolicy) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("scaling_policy", string(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasAutoSuspend(expected int) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("auto_suspend", strconv.Itoa(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasAutoResume(expected bool) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("auto_resume", strconv.FormatBool(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasResourceMonitor(expected string) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("resource_monitor", expected)) + return w +} + +func (w *WarehouseShowOutputAssert) HasComment(expected string) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("comment", expected)) + return w +} + +func (w *WarehouseShowOutputAssert) HasEnableQueryAcceleration(expected bool) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("enable_query_acceleration", strconv.FormatBool(expected))) + return w +} + +func (w *WarehouseShowOutputAssert) HasQueryAccelerationMaxScaleFactor(expected int) *WarehouseShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("query_acceleration_max_scale_factor", strconv.Itoa(expected))) + return w +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_snowflake.go b/pkg/acceptance/bettertestspoc/warehouse_snowflake.go new file mode 100644 index 0000000000..fecff6ef70 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_snowflake.go @@ -0,0 +1,164 @@ +package bettertestspoc + +import ( + "fmt" + "testing" + + acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +type WarehouseAssert struct { + *SnowflakeObjectAssert[*sdk.Warehouse, sdk.AccountObjectIdentifier] +} + +func Warehouse(t *testing.T, id sdk.AccountObjectIdentifier) *WarehouseAssert { + t.Helper() + return &WarehouseAssert{ + NewSnowflakeObjectAssert(sdk.ObjectTypeWarehouse, id, acc.TestClient().Warehouse.Show), + } +} + +func (w *WarehouseAssert) HasName(expected string) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.Name != expected { + return fmt.Errorf("expected name: %v; got: %v", expected, o.Name) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasState(expected sdk.WarehouseState) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.State != expected { + return fmt.Errorf("expected state: %v; got: %v", expected, string(o.State)) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasType(expected sdk.WarehouseType) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.Type != expected { + return fmt.Errorf("expected type: %v; got: %v", expected, string(o.Type)) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasSize(expected sdk.WarehouseSize) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.Size != expected { + return fmt.Errorf("expected size: %v; got: %v", expected, string(o.Size)) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasMinClusterCount(expected int) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.MinClusterCount != expected { + return fmt.Errorf("expected min cluster count: %v; got: %v", expected, o.MinClusterCount) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasMaxClusterCount(expected int) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.MaxClusterCount != expected { + return fmt.Errorf("expected max cluster count: %v; got: %v", expected, o.MaxClusterCount) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasAutoSuspend(expected int) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.AutoSuspend != expected { + return fmt.Errorf("expected auto suspend: %v; got: %v", expected, o.AutoSuspend) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasAutoResume(expected bool) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.AutoResume != expected { + return fmt.Errorf("expected auto resume: %v; got: %v", expected, o.AutoResume) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasComment(expected string) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.Comment != expected { + return fmt.Errorf("expected comment: %v; got: %v", expected, o.Comment) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasEnableQueryAcceleration(expected bool) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.EnableQueryAcceleration != expected { + return fmt.Errorf("expected enable query acceleration: %v; got: %v", expected, o.EnableQueryAcceleration) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasQueryAccelerationMaxScaleFactor(expected int) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.QueryAccelerationMaxScaleFactor != expected { + return fmt.Errorf("expected query acceleration max scale factor: %v; got: %v", expected, o.QueryAccelerationMaxScaleFactor) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasResourceMonitor(expected sdk.AccountObjectIdentifier) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.ResourceMonitor.Name() != expected.Name() { + return fmt.Errorf("expected resource monitor: %v; got: %v", expected.Name(), o.ResourceMonitor.Name()) + } + return nil + }) + return w +} + +func (w *WarehouseAssert) HasScalingPolicy(expected sdk.ScalingPolicy) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if o.ScalingPolicy != expected { + return fmt.Errorf("expected type: %v; got: %v", expected, string(o.ScalingPolicy)) + } + return nil + }) + return w +} diff --git a/pkg/acceptance/importchecks/import_checks.go b/pkg/acceptance/importchecks/import_checks.go index a04eecf8ec..e71c6c86bf 100644 --- a/pkg/acceptance/importchecks/import_checks.go +++ b/pkg/acceptance/importchecks/import_checks.go @@ -46,7 +46,7 @@ func TestCheckResourceAttrInstanceState(id string, attributeName, attributeValue if attrVal, ok := v.Attributes[attributeName]; ok { if attrVal != attributeValue { - return fmt.Errorf("expected: %s got: %s", attributeValue, attrVal) + return fmt.Errorf("expected: %s, got: %s", attributeValue, attrVal) } return nil diff --git a/pkg/resources/warehouse.go b/pkg/resources/warehouse.go index 22d4a863ed..3641df2272 100644 --- a/pkg/resources/warehouse.go +++ b/pkg/resources/warehouse.go @@ -482,7 +482,7 @@ func UpdateWarehouse(ctx context.Context, d *schema.ResourceData, meta any) diag } else { // TODO [SNOW-1473453]: UNSET of type does not work // unset.WarehouseType = sdk.Bool(true) - set.WarehouseType = &sdk.WarehouseTypeStandard + set.WarehouseType = sdk.Pointer(sdk.WarehouseTypeStandard) } } if d.HasChange("warehouse_size") { @@ -519,7 +519,7 @@ func UpdateWarehouse(ctx context.Context, d *schema.ResourceData, meta any) diag } else { // TODO [SNOW-1473453]: UNSET of scaling policy does not work // unset.ScalingPolicy = sdk.Bool(true) - set.ScalingPolicy = &sdk.ScalingPolicyStandard + set.ScalingPolicy = sdk.Pointer(sdk.ScalingPolicyStandard) } } if d.HasChange("auto_suspend") { diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 41c520c5bc..58bb87478a 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -7,6 +7,7 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + poc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc" r "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" tfjson "github.com/hashicorp/terraform-json" @@ -46,73 +47,172 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { Steps: []resource.TestStep{ { Config: warehouseBasicConfigWithComment(name, comment), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("snowflake_warehouse.w", "name", name), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "warehouse_type"), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "warehouse_size"), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "max_cluster_count"), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "min_cluster_count"), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "scaling_policy"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "auto_suspend", r.IntDefaultString), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "auto_resume", r.BooleanDefault), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "initially_suspended"), - resource.TestCheckNoResourceAttr("snowflake_warehouse.w", "resource_monitor"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "comment", comment), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "enable_query_acceleration", r.BooleanDefault), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "query_acceleration_max_scale_factor", r.IntDefaultString), - - resource.TestCheckResourceAttr("snowflake_warehouse.w", "max_concurrency_level", "8"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "statement_queued_timeout_in_seconds", "0"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "statement_timeout_in_seconds", "172800"), - - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.#", "1"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.type", string(sdk.WarehouseTypeStandard)), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.size", string(sdk.WarehouseSizeXSmall)), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.max_cluster_count", "1"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.min_cluster_count", "1"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.scaling_policy", string(sdk.ScalingPolicyStandard)), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.auto_suspend", "600"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.auto_resume", "true"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.resource_monitor", ""), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.comment", comment), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.enable_query_acceleration", "false"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "show_output.0.query_acceleration_max_scale_factor", "8"), - - resource.TestCheckResourceAttr("snowflake_warehouse.w", "parameters.#", "1"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "parameters.0.max_concurrency_level.0.value", "8"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "parameters.0.statement_queued_timeout_in_seconds.0.value", "0"), - resource.TestCheckResourceAttr("snowflake_warehouse.w", "parameters.0.statement_timeout_in_seconds.0.value", "172800"), + Check: poc.AssertThat(t, + poc.WarehouseResource(t, "snowflake_warehouse.w"). + HasName(name). + HasNoType(). + HasNoSize(). + HasNoMaxClusterCount(). + HasNoMinClusterCount(). + HasNoScalingPolicy(). + HasAutoSuspend(r.IntDefaultString). + HasAutoResume(r.BooleanDefault). + HasNoInitiallySuspended(). + HasNoResourceMonitor(). + HasComment(comment). + HasEnableQueryAcceleration(r.BooleanDefault). + HasQueryAccelerationMaxScaleFactor(r.IntDefaultString). + HasMaxConcurrencyLevel("8"). + HasStatementQueuedTimeoutInSeconds("0"). + HasStatementTimeoutInSeconds("172800"). + // alternatively extensions possible: + HasDefaultMaxConcurrencyLevel(). + HasDefaultStatementQueuedTimeoutInSeconds(). + HasDefaultStatementTimeoutInSeconds(), + poc.WarehouseShowOutput(t, "snowflake_warehouse.w"). + HasType(sdk.WarehouseTypeStandard). + HasSize(sdk.WarehouseSizeXSmall). + HasMaxClusterCount(1). + HasMinClusterCount(1). + HasScalingPolicy(sdk.ScalingPolicyStandard). + HasAutoSuspend(600). + HasAutoResume(true). + HasResourceMonitor(""). + HasComment(comment). + HasEnableQueryAcceleration(false). + HasQueryAccelerationMaxScaleFactor(8), + poc.WarehouseParameters(t, "snowflake_warehouse.w"). + HasMaxConcurrencyLevel(8). + HasStatementQueuedTimeoutInSeconds(0). + HasStatementTimeoutInSeconds(172800). + // alternatively extensions possible: + HasDefaultMaxConcurrencyLevel(). + HasDefaultStatementQueuedTimeoutInSeconds(). + HasDefaultStatementTimeoutInSeconds(), + poc.Warehouse(t, warehouseId). + HasName(warehouseId.Name()). + HasState(sdk.WarehouseStateStarted). + HasType(sdk.WarehouseTypeStandard). + HasSize(sdk.WarehouseSizeXSmall). + HasMaxClusterCount(1). + HasMinClusterCount(1). + HasScalingPolicy(sdk.ScalingPolicyStandard). + HasAutoSuspend(600). + HasAutoResume(true). + HasResourceMonitor(sdk.AccountObjectIdentifier{}). + HasComment(comment). + HasEnableQueryAcceleration(false). + HasQueryAccelerationMaxScaleFactor(8), + // we can still use normal checks + poc.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "name", warehouseId.Name())), + + // bad checks below + // poc.WarehouseResource(t, "snowflake_warehouse.w"). + // HasType(string(sdk.WarehouseTypeSnowparkOptimized)). + // HasSize(string(sdk.WarehouseSizeMedium)), + // poc.WarehouseShowOutput(t, "snowflake_warehouse.w"). + // HasType(sdk.WarehouseTypeSnowparkOptimized), + // poc.WarehouseParameters(t, "snowflake_warehouse.w"). + // HasMaxConcurrencyLevel(16). + // HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse), + // poc.Warehouse(t, warehouseId). + // HasName("bad name"). + // HasState(sdk.WarehouseStateSuspended). + // HasType(sdk.WarehouseTypeSnowparkOptimized). + // HasSize(sdk.WarehouseSizeMedium). + // HasMaxClusterCount(12). + // HasMinClusterCount(13). + // HasScalingPolicy(sdk.ScalingPolicyEconomy). + // HasAutoSuspend(123). + // HasAutoResume(false). + // HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + // HasComment("bad comment"). + // HasEnableQueryAcceleration(true). + // HasQueryAccelerationMaxScaleFactor(12), + // poc.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "warehouse_type", string(sdk.WarehouseTypeSnowparkOptimized))), ), }, // IMPORT after empty config (in this method, most of the attributes will be filled with the defaults acquired from Snowflake) { ResourceName: "snowflake_warehouse.w", ImportState: true, - ImportStateCheck: importchecks.ComposeImportStateCheck( - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "name", name), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "warehouse_type", string(sdk.WarehouseTypeStandard)), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "warehouse_size", string(sdk.WarehouseSizeXSmall)), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "max_cluster_count", "1"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "min_cluster_count", "1"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "scaling_policy", string(sdk.ScalingPolicyStandard)), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "auto_suspend", "600"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "auto_resume", "true"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "resource_monitor", ""), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "comment", comment), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "enable_query_acceleration", "false"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "query_acceleration_max_scale_factor", "8"), - - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "max_concurrency_level", "8"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "statement_queued_timeout_in_seconds", "0"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "statement_timeout_in_seconds", "172800"), - - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.#", "1"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.max_concurrency_level.0.value", "8"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.max_concurrency_level.0.level", ""), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.statement_queued_timeout_in_seconds.0.value", "0"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.statement_queued_timeout_in_seconds.0.level", ""), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.statement_timeout_in_seconds.0.value", "172800"), - importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "parameters.0.statement_timeout_in_seconds.0.level", ""), + ImportStateCheck: poc.AssertThatImport(t, + poc.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "name", name)), + poc.ImportedWarehouseResource(t, warehouseId.Name()). + HasName(name). + HasType(string(sdk.WarehouseTypeStandard)). + HasSize(string(sdk.WarehouseSizeXSmall)). + HasMaxClusterCount("1"). + HasMinClusterCount("1"). + HasScalingPolicy(string(sdk.ScalingPolicyStandard)). + HasAutoSuspend("600"). + HasAutoResume("true"). + HasResourceMonitor(""). + HasComment(comment). + HasEnableQueryAcceleration("false"). + HasQueryAccelerationMaxScaleFactor("8"). + HasDefaultMaxConcurrencyLevel(). + HasDefaultStatementQueuedTimeoutInSeconds(). + HasDefaultStatementTimeoutInSeconds(), + poc.ImportedWarehouseShowOutput(t, warehouseId.Name()), + poc.ImportedWarehouseParameters(t, warehouseId.Name()). + HasMaxConcurrencyLevel(8). + HasMaxConcurrencyLevelLevel(""). + HasStatementQueuedTimeoutInSeconds(0). + HasStatementQueuedTimeoutInSecondsLevel(""). + HasStatementTimeoutInSeconds(172800). + HasStatementTimeoutInSecondsLevel(""), + poc.Warehouse(t, warehouseId). + HasName(warehouseId.Name()). + HasState(sdk.WarehouseStateStarted). + HasType(sdk.WarehouseTypeStandard). + HasSize(sdk.WarehouseSizeXSmall). + HasMaxClusterCount(1). + HasMinClusterCount(1). + HasScalingPolicy(sdk.ScalingPolicyStandard). + HasAutoSuspend(600). + HasAutoResume(true). + HasResourceMonitor(sdk.AccountObjectIdentifier{}). + HasComment(comment). + HasEnableQueryAcceleration(false). + HasQueryAccelerationMaxScaleFactor(8), + + // bad checks below + poc.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "bad name", name)), + poc.ImportedWarehouseResource(t, warehouseId.Name()). + HasName("bad name"). + HasType(string(sdk.WarehouseTypeSnowparkOptimized)). + HasSize(string(sdk.WarehouseSizeMedium)). + HasMaxClusterCount("2"). + HasMinClusterCount("3"). + HasScalingPolicy(string(sdk.ScalingPolicyEconomy)). + HasAutoSuspend("123"). + HasAutoResume("false"). + HasResourceMonitor("abc"). + HasComment("bad comment"). + HasEnableQueryAcceleration("true"). + HasQueryAccelerationMaxScaleFactor("16"), + poc.ImportedWarehouseParameters(t, warehouseId.Name()). + HasMaxConcurrencyLevel(1). + HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse). + HasStatementQueuedTimeoutInSeconds(23). + HasStatementQueuedTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse). + HasStatementTimeoutInSeconds(1232). + HasStatementTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse), + poc.Warehouse(t, warehouseId). + HasName("bad name"). + HasState(sdk.WarehouseStateSuspended). + HasType(sdk.WarehouseTypeSnowparkOptimized). + HasSize(sdk.WarehouseSizeMedium). + HasMaxClusterCount(12). + HasMinClusterCount(13). + HasScalingPolicy(sdk.ScalingPolicyEconomy). + HasAutoSuspend(123). + HasAutoResume(false). + HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + HasComment("bad comment"). + HasEnableQueryAcceleration(true). + HasQueryAccelerationMaxScaleFactor(12), ), }, // RENAME diff --git a/pkg/sdk/warehouses.go b/pkg/sdk/warehouses.go index 0d0c2c0e83..330361ff07 100644 --- a/pkg/sdk/warehouses.go +++ b/pkg/sdk/warehouses.go @@ -38,7 +38,7 @@ type warehouses struct { type WarehouseType string -var ( +const ( WarehouseTypeStandard WarehouseType = "STANDARD" WarehouseTypeSnowparkOptimized WarehouseType = "SNOWPARK-OPTIMIZED" ) @@ -56,7 +56,7 @@ func ToWarehouseType(s string) (WarehouseType, error) { type WarehouseSize string -var ( +const ( WarehouseSizeXSmall WarehouseSize = "XSMALL" WarehouseSizeSmall WarehouseSize = "SMALL" WarehouseSizeMedium WarehouseSize = "MEDIUM" @@ -98,7 +98,7 @@ func ToWarehouseSize(s string) (WarehouseSize, error) { type ScalingPolicy string -var ( +const ( ScalingPolicyStandard ScalingPolicy = "STANDARD" ScalingPolicyEconomy ScalingPolicy = "ECONOMY" ) From 31273365bbe996a6eff9ea23cae45636aa4b415c Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 14:24:20 +0200 Subject: [PATCH 02/17] Add possible extension --- .../bettertestspoc/warehouse_resource_ext.go | 20 +++++++++++++++++++ pkg/resources/warehouse_acceptance_test.go | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go b/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go index fb16890608..b825249584 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go +++ b/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go @@ -1,5 +1,7 @@ package bettertestspoc +import r "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" + func (w *WarehouseResourceAssert) HasDefaultMaxConcurrencyLevel() *WarehouseResourceAssert { w.assertions = append(w.assertions, valueSet("max_concurrency_level", "8")) return w @@ -14,3 +16,21 @@ func (w *WarehouseResourceAssert) HasDefaultStatementTimeoutInSeconds() *Warehou w.assertions = append(w.assertions, valueSet("statement_timeout_in_seconds", "172800")) return w } + +func (w *WarehouseResourceAssert) HasAllDefault() *WarehouseResourceAssert { + return w.HasDefaultMaxConcurrencyLevel(). + HasNoType(). + HasNoSize(). + HasNoMaxClusterCount(). + HasNoMinClusterCount(). + HasNoScalingPolicy(). + HasAutoSuspend(r.IntDefaultString). + HasAutoResume(r.BooleanDefault). + HasNoInitiallySuspended(). + HasNoResourceMonitor(). + HasEnableQueryAcceleration(r.BooleanDefault). + HasQueryAccelerationMaxScaleFactor(r.IntDefaultString). + HasDefaultMaxConcurrencyLevel(). + HasDefaultStatementQueuedTimeoutInSeconds(). + HasDefaultStatementTimeoutInSeconds() +} diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 58bb87478a..949118865a 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -68,7 +68,9 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { // alternatively extensions possible: HasDefaultMaxConcurrencyLevel(). HasDefaultStatementQueuedTimeoutInSeconds(). - HasDefaultStatementTimeoutInSeconds(), + HasDefaultStatementTimeoutInSeconds(). + // alternatively extension possible + HasAllDefault(), poc.WarehouseShowOutput(t, "snowflake_warehouse.w"). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeXSmall). From 8b1cf017935dc0c63d1d321fd169e6896fe9aab5 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 15:10:17 +0200 Subject: [PATCH 03/17] Allow using in integration tests --- .../bettertestspoc/snowflake_assertions.go | 19 +++++-- .../bettertestspoc/warehouse_snowflake_ext.go | 20 ++++++++ pkg/sdk/testint/tasks_gen_integration_test.go | 2 +- .../testint/warehouses_integration_test.go | 50 +++++++++++++++++-- 4 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go index b8009e489a..1845ca9fcb 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -8,6 +8,7 @@ import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/stretchr/testify/require" ) type assertSdk[T any] func(*testing.T, T) error @@ -33,18 +34,18 @@ func NewSnowflakeObjectAssert[T any, I sdk.ObjectIdentifier](objectType sdk.Obje func (s *SnowflakeObjectAssert[_, _]) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { t.Helper() return func(_ *terraform.State) error { - return s.commonTerraformCheckFuncProvider(t) + return s.runSnowflakeObjectsAssertions(t) } } func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc { t.Helper() return func(_ []*terraform.InstanceState) error { - return s.commonTerraformCheckFuncProvider(t) + return s.runSnowflakeObjectsAssertions(t) } } -func (s *SnowflakeObjectAssert[_, _]) commonTerraformCheckFuncProvider(t *testing.T) error { +func (s *SnowflakeObjectAssert[_, _]) runSnowflakeObjectsAssertions(t *testing.T) error { sdkObject, err := s.provider(t, s.id) if err != nil { return err @@ -60,3 +61,15 @@ func (s *SnowflakeObjectAssert[_, _]) commonTerraformCheckFuncProvider(t *testin return errors.Join(result...) } + +func AssertThatObject[T any, I sdk.ObjectIdentifier](t *testing.T, objectAssert *SnowflakeObjectAssert[T, I]) { + t.Helper() + err := objectAssert.runSnowflakeObjectsAssertions(t) + require.NoError(t, err) +} + +func (s *SnowflakeObjectAssert[_, _]) CheckAll(t *testing.T) { + t.Helper() + err := s.runSnowflakeObjectsAssertions(t) + require.NoError(t, err) +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go b/pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go new file mode 100644 index 0000000000..95c0e72d38 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go @@ -0,0 +1,20 @@ +package bettertestspoc + +import ( + "fmt" + "slices" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" +) + +func (w *WarehouseAssert) HasStateOneOf(expected ...sdk.WarehouseState) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if !slices.Contains(expected, o.State) { + return fmt.Errorf("expected state one of: %v; got: %v", expected, string(o.State)) + } + return nil + }) + return w +} diff --git a/pkg/sdk/testint/tasks_gen_integration_test.go b/pkg/sdk/testint/tasks_gen_integration_test.go index 759faa0e1d..bb336de0cf 100644 --- a/pkg/sdk/testint/tasks_gen_integration_test.go +++ b/pkg/sdk/testint/tasks_gen_integration_test.go @@ -140,7 +140,7 @@ func TestInt_Tasks(t *testing.T) { t.Run("create task: with initial warehouse", func(t *testing.T) { request := createTaskBasicRequest(t). - WithWarehouse(sdk.NewCreateTaskWarehouseRequest().WithUserTaskManagedInitialWarehouseSize(&sdk.WarehouseSizeXSmall)) + WithWarehouse(sdk.NewCreateTaskWarehouseRequest().WithUserTaskManagedInitialWarehouseSize(sdk.Pointer(sdk.WarehouseSizeXSmall))) task := createTaskWithRequest(t, request) diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index 08f84091d5..2a492a696c 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -5,6 +5,8 @@ import ( "testing" "time" + poc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -84,11 +86,11 @@ func TestInt_Warehouses(t *testing.T) { id := testClientHelper().Ids.RandomAccountObjectIdentifier() err := client.Warehouses.Create(ctx, id, &sdk.CreateWarehouseOptions{ OrReplace: sdk.Bool(true), - WarehouseType: &sdk.WarehouseTypeStandard, - WarehouseSize: &sdk.WarehouseSizeSmall, + WarehouseType: sdk.Pointer(sdk.WarehouseTypeStandard), + WarehouseSize: sdk.Pointer(sdk.WarehouseSizeSmall), MaxClusterCount: sdk.Int(8), MinClusterCount: sdk.Int(2), - ScalingPolicy: &sdk.ScalingPolicyEconomy, + ScalingPolicy: sdk.Pointer(sdk.ScalingPolicyEconomy), AutoSuspend: sdk.Int(1000), AutoResume: sdk.Bool(true), InitiallySuspended: sdk.Bool(false), @@ -113,6 +115,46 @@ func TestInt_Warehouses(t *testing.T) { require.NoError(t, err) t.Cleanup(testClientHelper().Warehouse.DropWarehouseFunc(t, id)) + // we can use the same assertion builder in the SDK tests + warehouseAssertions := poc.Warehouse(t, id). + HasName(id.Name()). + HasType(sdk.WarehouseTypeStandard). + HasSize(sdk.WarehouseSizeSmall). + HasMaxClusterCount(8). + HasMinClusterCount(2). + HasScalingPolicy(sdk.ScalingPolicyEconomy). + HasAutoSuspend(1000). + HasAutoResume(true). + HasStateOneOf(sdk.WarehouseStateResuming, sdk.WarehouseStateStarted). + HasResourceMonitor(resourceMonitor.ID()). + HasComment("comment"). + HasEnableQueryAcceleration(true). + HasQueryAccelerationMaxScaleFactor(90) + // and run it like this + poc.AssertThatObject(t, warehouseAssertions.SnowflakeObjectAssert) + // or alternatively + warehouseAssertions.CheckAll(t) + + //// to show errors + //warehouseAssertionsBad := poc.Warehouse(t, id). + // HasName("bad name"). + // HasState(sdk.WarehouseStateSuspended). + // HasType(sdk.WarehouseTypeSnowparkOptimized). + // HasSize(sdk.WarehouseSizeMedium). + // HasMaxClusterCount(12). + // HasMinClusterCount(13). + // HasScalingPolicy(sdk.ScalingPolicyStandard). + // HasAutoSuspend(123). + // HasAutoResume(false). + // HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + // HasComment("bad comment"). + // HasEnableQueryAcceleration(false). + // HasQueryAccelerationMaxScaleFactor(12) + ////and run it like this + //poc.AssertThatObject(t, warehouseAssertionsBad.SnowflakeObjectAssert) + ////or alternatively + //warehouseAssertionsBad.CheckAll(t) + warehouse, err := client.Warehouses.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, id.Name(), warehouse.Name) @@ -189,7 +231,7 @@ func TestInt_Warehouses(t *testing.T) { alterOptions := &sdk.AlterWarehouseOptions{ // WarehouseType omitted on purpose - it requires suspending the warehouse (separate test cases) Set: &sdk.WarehouseSet{ - WarehouseSize: &sdk.WarehouseSizeMedium, + WarehouseSize: sdk.Pointer(sdk.WarehouseSizeMedium), WaitForCompletion: sdk.Bool(true), MaxClusterCount: sdk.Int(3), MinClusterCount: sdk.Int(2), From d7083f67403eaa4e329715cdc77fe64bc93c22f7 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 15:33:34 +0200 Subject: [PATCH 04/17] Allow creating assertion from object --- .../bettertestspoc/snowflake_assertions.go | 37 ++++++++++++++----- .../bettertestspoc/warehouse_snowflake.go | 11 +++++- .../testint/warehouses_integration_test.go | 17 +++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go index 1845ca9fcb..9b733998ea 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -12,22 +12,32 @@ import ( ) type assertSdk[T any] func(*testing.T, T) error -type objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (T, error) +type objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (*T, error) type SnowflakeObjectAssert[T any, I sdk.ObjectIdentifier] struct { - assertions []assertSdk[T] + assertions []assertSdk[*T] id I - provider objectProvider[T, I] objectType sdk.ObjectType + object *T + provider objectProvider[T, I] TestCheckFuncProvider } -func NewSnowflakeObjectAssert[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, provider objectProvider[T, I]) *SnowflakeObjectAssert[T, I] { +func NewSnowflakeObjectAssertWithProvider[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, provider objectProvider[T, I]) *SnowflakeObjectAssert[T, I] { return &SnowflakeObjectAssert[T, I]{ - assertions: make([]assertSdk[T], 0), + assertions: make([]assertSdk[*T], 0), id: id, + objectType: objectType, provider: provider, + } +} + +func NewSnowflakeObjectAssertWithObject[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, object *T) *SnowflakeObjectAssert[T, I] { + return &SnowflakeObjectAssert[T, I]{ + assertions: make([]assertSdk[*T], 0), + id: id, objectType: objectType, + object: object, } } @@ -45,10 +55,19 @@ func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing } } -func (s *SnowflakeObjectAssert[_, _]) runSnowflakeObjectsAssertions(t *testing.T) error { - sdkObject, err := s.provider(t, s.id) - if err != nil { - return err +func (s *SnowflakeObjectAssert[T, _]) runSnowflakeObjectsAssertions(t *testing.T) error { + var sdkObject *T + var err error + switch { + case s.object != nil: + sdkObject = s.object + case s.provider != nil: + sdkObject, err = s.provider(t, s.id) + if err != nil { + return err + } + default: + return fmt.Errorf("cannot proceed with object %s[%s] assertion: object or provider must be specified", s.objectType, s.id.FullyQualifiedName()) } var result []error diff --git a/pkg/acceptance/bettertestspoc/warehouse_snowflake.go b/pkg/acceptance/bettertestspoc/warehouse_snowflake.go index fecff6ef70..014f4fc5d9 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_snowflake.go +++ b/pkg/acceptance/bettertestspoc/warehouse_snowflake.go @@ -10,13 +10,20 @@ import ( ) type WarehouseAssert struct { - *SnowflakeObjectAssert[*sdk.Warehouse, sdk.AccountObjectIdentifier] + *SnowflakeObjectAssert[sdk.Warehouse, sdk.AccountObjectIdentifier] } func Warehouse(t *testing.T, id sdk.AccountObjectIdentifier) *WarehouseAssert { t.Helper() return &WarehouseAssert{ - NewSnowflakeObjectAssert(sdk.ObjectTypeWarehouse, id, acc.TestClient().Warehouse.Show), + NewSnowflakeObjectAssertWithProvider(sdk.ObjectTypeWarehouse, id, acc.TestClient().Warehouse.Show), + } +} + +func WarehouseFromObject(t *testing.T, warehouse *sdk.Warehouse) *WarehouseAssert { + t.Helper() + return &WarehouseAssert{ + NewSnowflakeObjectAssertWithObject(sdk.ObjectTypeWarehouse, warehouse.ID(), warehouse), } } diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index 2a492a696c..83ac550534 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -171,6 +171,23 @@ func TestInt_Warehouses(t *testing.T) { assert.Equal(t, true, warehouse.EnableQueryAcceleration) assert.Equal(t, 90, warehouse.QueryAccelerationMaxScaleFactor) + // we can also use the read object to initialize: + poc.WarehouseFromObject(t, warehouse). + HasName(id.Name()). + HasType(sdk.WarehouseTypeStandard). + HasSize(sdk.WarehouseSizeSmall). + HasMaxClusterCount(8). + HasMinClusterCount(2). + HasScalingPolicy(sdk.ScalingPolicyEconomy). + HasAutoSuspend(1000). + HasAutoResume(true). + HasStateOneOf(sdk.WarehouseStateResuming, sdk.WarehouseStateStarted). + HasResourceMonitor(resourceMonitor.ID()). + HasComment("comment"). + HasEnableQueryAcceleration(true). + HasQueryAccelerationMaxScaleFactor(90). + CheckAll(t) + tag1Value, err := client.SystemFunctions.GetTag(ctx, tag.ID(), warehouse.ID(), sdk.ObjectTypeWarehouse) require.NoError(t, err) assert.Equal(t, "v1", tag1Value) From 937e66f69c1d374e3b403f3ae629ca6d2a7f3f38 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 16:19:28 +0200 Subject: [PATCH 05/17] Prepare warehouse model (WIP) --- pkg/acceptance/bettertestspoc/config.go | 1 + .../bettertestspoc/warehouse_model.go | 206 ++++++++++++++++++ .../bettertestspoc/warehouse_model_ext.go | 8 + 3 files changed, 215 insertions(+) create mode 100644 pkg/acceptance/bettertestspoc/config.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_model.go create mode 100644 pkg/acceptance/bettertestspoc/warehouse_model_ext.go diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go new file mode 100644 index 0000000000..129424c9a6 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/config.go @@ -0,0 +1 @@ +package bettertestspoc diff --git a/pkg/acceptance/bettertestspoc/warehouse_model.go b/pkg/acceptance/bettertestspoc/warehouse_model.go new file mode 100644 index 0000000000..d6419de0b0 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_model.go @@ -0,0 +1,206 @@ +package bettertestspoc + +import ( + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + "github.com/hashicorp/terraform-plugin-testing/config" +) + +type WarehouseModel struct { + Name config.Variable + WarehouseType config.Variable + WarehouseSize config.Variable + MaxClusterCount config.Variable + MinClusterCount config.Variable + ScalingPolicy config.Variable + AutoSuspend config.Variable + AutoResume config.Variable + InitiallySuspended config.Variable + ResourceMonitor config.Variable + Comment config.Variable + EnableQueryAcceleration config.Variable + QueryAccelerationMaxScaleFactor config.Variable + + MaxConcurrencyLevel config.Variable + StatementQueuedTimeoutInSeconds config.Variable + StatementTimeoutInSeconds config.Variable +} + +/////////////////////////////////// +// Basic builder (only required) // +/////////////////////////////////// + +func NewWarehouseModel( + name string, +) *WarehouseModel { + m := &WarehouseModel{} + m.WithName(name) + return m +} + +///////////////////////////////// +// below all the proper values // +///////////////////////////////// + +func (m *WarehouseModel) WithName(name string) *WarehouseModel { + m.Name = config.StringVariable(name) + return m +} + +func (m *WarehouseModel) WithWarehouseType(warehouseType sdk.WarehouseType) *WarehouseModel { + m.WarehouseType = config.StringVariable(string(warehouseType)) + return m +} + +func (m *WarehouseModel) WithWarehouseSize(warehouseSize sdk.WarehouseSize) *WarehouseModel { + m.WarehouseSize = config.StringVariable(string(warehouseSize)) + return m +} + +func (m *WarehouseModel) WithMaxClusterCount(maxClusterCount int) *WarehouseModel { + m.MaxClusterCount = config.IntegerVariable(maxClusterCount) + return m +} + +func (m *WarehouseModel) WithMinClusterCount(minClusterCount int) *WarehouseModel { + m.MinClusterCount = config.IntegerVariable(minClusterCount) + return m +} + +func (m *WarehouseModel) WithScalingPolicy(scalingPolicy sdk.ScalingPolicy) *WarehouseModel { + m.ScalingPolicy = config.StringVariable(string(scalingPolicy)) + return m +} + +func (m *WarehouseModel) WithAutoSuspend(autoSuspend int) *WarehouseModel { + m.AutoSuspend = config.IntegerVariable(autoSuspend) + return m +} + +func (m *WarehouseModel) WithAutoResume(autoResume bool) *WarehouseModel { + m.AutoResume = config.BoolVariable(autoResume) + return m +} + +func (m *WarehouseModel) WithInitiallySuspended(initiallySuspended bool) *WarehouseModel { + m.InitiallySuspended = config.BoolVariable(initiallySuspended) + return m +} + +func (m *WarehouseModel) WithResourceMonitor(resourceMonitor sdk.AccountObjectIdentifier) *WarehouseModel { + m.ResourceMonitor = config.StringVariable(resourceMonitor.Name()) + return m +} + +func (m *WarehouseModel) WithComment(comment string) *WarehouseModel { + m.Comment = config.StringVariable(comment) + return m +} + +func (m *WarehouseModel) WithEnableQueryAcceleration(enableQueryAcceleration bool) *WarehouseModel { + m.EnableQueryAcceleration = config.BoolVariable(enableQueryAcceleration) + return m +} + +func (m *WarehouseModel) WithQueryAccelerationMaxScaleFactor(queryAccelerationMaxScaleFactor int) *WarehouseModel { + m.QueryAccelerationMaxScaleFactor = config.IntegerVariable(queryAccelerationMaxScaleFactor) + return m +} + +func (m *WarehouseModel) WithMaxConcurrencyLevel(maxConcurrencyLevel int) *WarehouseModel { + m.MaxConcurrencyLevel = config.IntegerVariable(maxConcurrencyLevel) + return m +} + +func (m *WarehouseModel) WithStatementQueuedTimeoutInSeconds(statementQueuedTimeoutInSeconds int) *WarehouseModel { + m.StatementQueuedTimeoutInSeconds = config.IntegerVariable(statementQueuedTimeoutInSeconds) + return m +} + +func (m *WarehouseModel) WithStatementTimeoutInSeconds(statementTimeoutInSeconds int) *WarehouseModel { + m.StatementTimeoutInSeconds = config.IntegerVariable(statementTimeoutInSeconds) + return m +} + +////////////////////////////////////////// +// below it's possible to set any value // +////////////////////////////////////////// + +func (m *WarehouseModel) WithNameValue(value config.Variable) *WarehouseModel { + m.Name = value + return m +} + +func (m *WarehouseModel) WithWarehouseTypeValue(value config.Variable) *WarehouseModel { + m.WarehouseType = value + return m +} + +func (m *WarehouseModel) WithWarehouseSizeValue(value config.Variable) *WarehouseModel { + m.WarehouseSize = value + return m +} + +func (m *WarehouseModel) WithMaxClusterCountValue(value config.Variable) *WarehouseModel { + m.MaxClusterCount = value + return m +} + +func (m *WarehouseModel) WithMinClusterCountValue(value config.Variable) *WarehouseModel { + m.MinClusterCount = value + return m +} + +func (m *WarehouseModel) WithScalingPolicyValue(value config.Variable) *WarehouseModel { + m.ScalingPolicy = value + return m +} + +func (m *WarehouseModel) WithAutoSuspendValue(value config.Variable) *WarehouseModel { + m.AutoSuspend = value + return m +} + +func (m *WarehouseModel) WithAutoResumeValue(value config.Variable) *WarehouseModel { + m.AutoResume = value + return m +} + +func (m *WarehouseModel) WithInitiallySuspendedValue(value config.Variable) *WarehouseModel { + m.InitiallySuspended = value + return m +} + +func (m *WarehouseModel) WithResourceMonitorValue(value config.Variable) *WarehouseModel { + m.ResourceMonitor = value + return m +} + +func (m *WarehouseModel) WithCommentValue(value config.Variable) *WarehouseModel { + m.Comment = value + return m +} + +func (m *WarehouseModel) WithEnableQueryAccelerationValue(value config.Variable) *WarehouseModel { + m.EnableQueryAcceleration = value + return m +} + +func (m *WarehouseModel) WithQueryAccelerationMaxScaleFactorValue(value config.Variable) *WarehouseModel { + m.QueryAccelerationMaxScaleFactor = value + return m +} + +func (m *WarehouseModel) WithMaxConcurrencyLevelValue(value config.Variable) *WarehouseModel { + m.MaxConcurrencyLevel = value + return m +} + +func (m *WarehouseModel) WithStatementQueuedTimeoutInSecondsValue(value config.Variable) *WarehouseModel { + m.StatementQueuedTimeoutInSeconds = value + return m +} + +func (m *WarehouseModel) WithStatementTimeoutInSecondsValue(value config.Variable) *WarehouseModel { + m.StatementTimeoutInSeconds = value + return m +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_model_ext.go b/pkg/acceptance/bettertestspoc/warehouse_model_ext.go new file mode 100644 index 0000000000..8b7fe59208 --- /dev/null +++ b/pkg/acceptance/bettertestspoc/warehouse_model_ext.go @@ -0,0 +1,8 @@ +package bettertestspoc + +func BasicWarehouseModel( + name string, + comment string, +) *WarehouseModel { + return NewWarehouseModel(name).WithComment(comment) +} From 209e40e20889d3d2899c72411ec831da8940bc08 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 17:56:16 +0200 Subject: [PATCH 06/17] Use warehouse model in test config (WIP) --- pkg/acceptance/bettertestspoc/config.go | 66 +++++++++++++++++++ .../bettertestspoc/warehouse_model.go | 39 ++++++----- pkg/resources/warehouse_acceptance_test.go | 6 +- 3 files changed, 92 insertions(+), 19 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go index 129424c9a6..9794b5f970 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config.go @@ -1 +1,67 @@ package bettertestspoc + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" + "github.com/hashicorp/terraform-plugin-testing/config" + "github.com/stretchr/testify/require" +) + +type ResourceModel interface { + Resource() resources.Resource + ResourceName() string + SetResourceName(name string) +} + +type resourceModelMeta struct { + name string + resource resources.Resource +} + +func (m *resourceModelMeta) Resource() resources.Resource { + return m.resource +} + +func (m *resourceModelMeta) ResourceName() string { + return m.name +} + +func (m *resourceModelMeta) SetResourceName(name string) { + m.name = name +} + +const DefaultResourceName = "test" + +func defaultMeta(resource resources.Resource) *resourceModelMeta { + return &resourceModelMeta{name: DefaultResourceName, resource: resource} +} + +func ConfigurationFromModel(t *testing.T, model ResourceModel) string { + t.Helper() + + b, err := json.MarshalIndent(model, "", " ") + require.NoError(t, err) + var sb strings.Builder + sb.WriteString(fmt.Sprintf(`resource "%s" "%s" {`, model.Resource(), model.ResourceName())) + sb.WriteString(strings.Trim(string(b), "{}")) + sb.WriteString(`}`) + sb.WriteRune('\n') + s := sb.String() + t.Logf("Generated config:\n%s", s) + return s +} + +// TODO: save to tmp file and return path to it +func ConfigurationFromModelProvider(t *testing.T, model ResourceModel) func(config.TestStepConfigRequest) string { + t.Helper() + return func(req config.TestStepConfigRequest) string { + t.Logf("Generating config for test %s, step %d for resource %s", req.TestName, req.StepNumber, model.Resource()) + content := ConfigurationFromModel(t, model) + _ = content + return "" + } +} diff --git a/pkg/acceptance/bettertestspoc/warehouse_model.go b/pkg/acceptance/bettertestspoc/warehouse_model.go index d6419de0b0..28b64b0e1d 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model.go +++ b/pkg/acceptance/bettertestspoc/warehouse_model.go @@ -1,28 +1,31 @@ package bettertestspoc import ( + "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" ) type WarehouseModel struct { - Name config.Variable - WarehouseType config.Variable - WarehouseSize config.Variable - MaxClusterCount config.Variable - MinClusterCount config.Variable - ScalingPolicy config.Variable - AutoSuspend config.Variable - AutoResume config.Variable - InitiallySuspended config.Variable - ResourceMonitor config.Variable - Comment config.Variable - EnableQueryAcceleration config.Variable - QueryAccelerationMaxScaleFactor config.Variable - - MaxConcurrencyLevel config.Variable - StatementQueuedTimeoutInSeconds config.Variable - StatementTimeoutInSeconds config.Variable + Name config.Variable `json:"name,omitempty"` + WarehouseType config.Variable `json:"warehouse_type,omitempty"` + WarehouseSize config.Variable `json:"warehouse_size,omitempty"` + MaxClusterCount config.Variable `json:"max_cluster_count,omitempty"` + MinClusterCount config.Variable `json:"min_cluster_count,omitempty"` + ScalingPolicy config.Variable `json:"scaling_policy,omitempty"` + AutoSuspend config.Variable `json:"auto_suspend,omitempty"` + AutoResume config.Variable `json:"auto_resume,omitempty"` + InitiallySuspended config.Variable `json:"initially_suspended,omitempty"` + ResourceMonitor config.Variable `json:"resource_monitor,omitempty"` + Comment config.Variable `json:"comment,omitempty"` + EnableQueryAcceleration config.Variable `json:"enable_query_acceleration,omitempty"` + QueryAccelerationMaxScaleFactor config.Variable `json:"query_acceleration_max_scale_factor,omitempty"` + + MaxConcurrencyLevel config.Variable `json:"max_concurrency_level,omitempty"` + StatementQueuedTimeoutInSeconds config.Variable `json:"statement_queued_timeout_in_seconds,omitempty"` + StatementTimeoutInSeconds config.Variable `json:"statement_timeout_in_seconds,omitempty"` + + *resourceModelMeta } /////////////////////////////////// @@ -32,7 +35,7 @@ type WarehouseModel struct { func NewWarehouseModel( name string, ) *WarehouseModel { - m := &WarehouseModel{} + m := &WarehouseModel{resourceModelMeta: defaultMeta(resources.Warehouse)} m.WithName(name) return m } diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 949118865a..339a007ed7 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -37,6 +37,10 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { t.Cleanup(resourceMonitorCleanup) resourceMonitorId := resourceMonitor.ID() + model := poc.NewWarehouseModel(name).WithComment(comment) + // TODO: handle resource name better + model.SetResourceName("w") + resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, PreCheck: func() { acc.TestAccPreCheck(t) }, @@ -46,7 +50,7 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { CheckDestroy: acc.CheckDestroy(t, resources.Warehouse), Steps: []resource.TestStep{ { - Config: warehouseBasicConfigWithComment(name, comment), + Config: poc.ConfigurationFromModel(t, model), Check: poc.AssertThat(t, poc.WarehouseResource(t, "snowflake_warehouse.w"). HasName(name). From 2b6ddf4846c83663dd26e04a3d981a3f904a4b12 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 18:34:44 +0200 Subject: [PATCH 07/17] Try to use github.com/hashicorp/hcl2/hcl/json#Parse (WIP) --- go.mod | 2 ++ go.sum | 34 +++++++++++++++++++ pkg/acceptance/bettertestspoc/config.go | 29 ++++++++++------ .../bettertestspoc/resource_assertions.go | 22 +++++++----- .../bettertestspoc/snowflake_assertions.go | 8 +++-- pkg/sdk/tasks_gen_test.go | 4 +-- .../testint/warehouses_integration_test.go | 6 ++-- pkg/sdk/warehouses_test.go | 8 ++--- 8 files changed, 84 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index 1ff88672d3..061de5f76d 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/gookit/color v1.5.4 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/go-uuid v1.0.3 + github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 github.com/hashicorp/terraform-json v0.21.0 github.com/hashicorp/terraform-plugin-framework v1.8.0 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 @@ -47,6 +48,7 @@ require ( github.com/ProtonMail/go-crypto v1.1.0-alpha.0 // indirect github.com/agext/levenshtein v1.2.3 // indirect github.com/apache/arrow/go/v15 v15.0.0 // indirect + github.com/apparentlymart/go-textseg v1.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/aws/aws-sdk-go-v2 v1.21.0 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 // indirect diff --git a/go.sum b/go.sum index 78062756a5..141ad2ff6d 100644 --- a/go.sum +++ b/go.sum @@ -24,10 +24,14 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.1.0-alpha.0 h1:nHGfwXmFvJrSR9xu8qL7BkO4DqTHXE9N5vPhgY2I+j0= github.com/ProtonMail/go-crypto v1.1.0-alpha.0/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= +github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/apache/arrow/go/v15 v15.0.0 h1:1zZACWf85oEZY5/kd9dsQS7i+2G5zVQcbKTHgslqHNA= github.com/apache/arrow/go/v15 v15.0.0/go.mod h1:DGXsR3ajT524njufqf95822i+KTh+yea1jass9YXgjA= +github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= +github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0= +github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= @@ -73,6 +77,7 @@ github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ= github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4= github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= +github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= @@ -95,6 +100,7 @@ github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FM github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= @@ -119,12 +125,14 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/flatbuffers v23.5.26+incompatible h1:M9dgRyhJemaM4Sw8+66GHBu8ioaQmyPLg1b8VwK5WJg= github.com/google/flatbuffers v23.5.26+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -136,6 +144,7 @@ github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= +github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -148,6 +157,7 @@ github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUK github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs= github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c= github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= +github.com/hashicorp/go-multierror v0.0.0-20180717150148-3d5d8f294aa0/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= @@ -161,6 +171,8 @@ github.com/hashicorp/hc-install v0.6.3 h1:yE/r1yJvWbtrJ0STwScgEnCanb0U9v7zp0Gbkm github.com/hashicorp/hc-install v0.6.3/go.mod h1:KamGdbodYzlufbWh4r9NRo8y6GLHWZP2GBtdnms1Ln0= github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI= github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE= +github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 h1:PFfGModn55JA0oBsvFghhj0v93me+Ctr3uHC/UmFAls= +github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80/go.mod h1:Cxv+IJLuBiEhQ7pBYGEuORa0nr4U994pE8mYLuFd7v0= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8JyYF3vpnuEo= @@ -187,8 +199,10 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= +github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= @@ -211,6 +225,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -235,6 +250,7 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8= +github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -246,6 +262,9 @@ github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ib github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= +github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= @@ -258,6 +277,7 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -266,10 +286,12 @@ github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2 github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/snowflakedb/gosnowflake v1.10.0 h1:5hBGKa/jJEhciokzgJcz5xmLNlJ8oUm8vhfu5tg82tM= github.com/snowflakedb/gosnowflake v1.10.0/go.mod h1:WC4eGUOH3K9w3pLsdwZsdawIwtWgse4kZPPqNG0Ky/k= +github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= @@ -290,6 +312,7 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1z github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.14.2 h1:kTG7lqmBou0Zkx35r6HJHUQTvaRPr5bIAf3AoHS0izI= github.com/zclconf/go-cty v1.14.2/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= @@ -297,6 +320,7 @@ github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= @@ -306,7 +330,10 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQz golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -317,8 +344,10 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -373,11 +402,16 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0= diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go index 9794b5f970..64be43be1d 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config.go @@ -2,10 +2,10 @@ package bettertestspoc import ( "encoding/json" - "fmt" - "strings" "testing" + hclJson "github.com/hashicorp/hcl2/hcl/json" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/stretchr/testify/require" @@ -43,15 +43,20 @@ func defaultMeta(resource resources.Resource) *resourceModelMeta { func ConfigurationFromModel(t *testing.T, model ResourceModel) string { t.Helper() - b, err := json.MarshalIndent(model, "", " ") + m1 := make(map[string]map[string]ResourceModel) + m2 := make(map[string]ResourceModel) + m2[model.ResourceName()] = model + m1[model.Resource().String()] = m2 + b, err := json.Marshal(ResourceWrapper{m1}) require.NoError(t, err) - var sb strings.Builder - sb.WriteString(fmt.Sprintf(`resource "%s" "%s" {`, model.Resource(), model.ResourceName())) - sb.WriteString(strings.Trim(string(b), "{}")) - sb.WriteString(`}`) - sb.WriteRune('\n') - s := sb.String() - t.Logf("Generated config:\n%s", s) + t.Logf("Generated json:\n%s", string(b)) + // TODO: https://pkg.go.dev/github.com/hashicorp/hcl2/hcl/json#Parse + f, diag := hclJson.Parse(b, "") + if diag.HasErrors() { + t.Fatal("Could not parse") + } + s := string(f.Bytes) + t.Logf("Generated hcl:\n%s", s) return s } @@ -65,3 +70,7 @@ func ConfigurationFromModelProvider(t *testing.T, model ResourceModel) func(conf return "" } } + +type ResourceWrapper struct { + Resource map[string]map[string]ResourceModel `json:"resource"` +} diff --git a/pkg/acceptance/bettertestspoc/resource_assertions.go b/pkg/acceptance/bettertestspoc/resource_assertions.go index 1584fd0c9b..6260470444 100644 --- a/pkg/acceptance/bettertestspoc/resource_assertions.go +++ b/pkg/acceptance/bettertestspoc/resource_assertions.go @@ -11,8 +11,10 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) -var _ TestCheckFuncProvider = (*ResourceAssert)(nil) -var _ ImportStateCheckFuncProvider = (*ResourceAssert)(nil) +var ( + _ TestCheckFuncProvider = (*ResourceAssert)(nil) + _ ImportStateCheckFuncProvider = (*ResourceAssert)(nil) +) type ResourceAssert struct { name string @@ -40,8 +42,10 @@ func NewImportedResourceAssert(id string, prefix string) *ResourceAssert { type resourceAssertionType string -const resourceAssertionTypeValueSet = "VALUE_SET" -const resourceAssertionTypeValueNotSet = "VALUE_NOT_SET" +const ( + resourceAssertionTypeValueSet = "VALUE_SET" + resourceAssertionTypeValueNotSet = "VALUE_NOT_SET" +) type resourceAssertion struct { fieldName string @@ -63,9 +67,11 @@ func showOutputValueSet(fieldName string, expected string) resourceAssertion { return resourceAssertion{fieldName: showOutputPrefix + fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} } -const parametersPrefix = "parameters.0." -const parametersValueSuffix = ".0.value" -const parametersLevelSuffix = ".0.level" +const ( + parametersPrefix = "parameters.0." + parametersValueSuffix = ".0.value" + parametersLevelSuffix = ".0.level" +) func parameterValueSet(fieldName string, expected string) resourceAssertion { return resourceAssertion{fieldName: parametersPrefix + fieldName + parametersValueSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} @@ -108,7 +114,7 @@ func (r *ResourceAssert) ToTerraformImportStateCheckFunc(t *testing.T) resource. switch a.resourceAssertionType { case resourceAssertionTypeValueSet: if err := importchecks.TestCheckResourceAttrInstanceState(r.id, a.fieldName, a.expectedValue)(s); err != nil { - result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %s", r.id, r.prefix, i+1, len(r.assertions), err)) + result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %w", r.id, r.prefix, i+1, len(r.assertions), err)) } case resourceAssertionTypeValueNotSet: panic("implement") diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go index 9b733998ea..923eac909e 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -11,8 +11,10 @@ import ( "github.com/stretchr/testify/require" ) -type assertSdk[T any] func(*testing.T, T) error -type objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (*T, error) +type ( + assertSdk[T any] func(*testing.T, T) error + objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (*T, error) +) type SnowflakeObjectAssert[T any, I sdk.ObjectIdentifier] struct { assertions []assertSdk[*T] @@ -56,6 +58,8 @@ func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing } func (s *SnowflakeObjectAssert[T, _]) runSnowflakeObjectsAssertions(t *testing.T) error { + t.Helper() + var sdkObject *T var err error switch { diff --git a/pkg/sdk/tasks_gen_test.go b/pkg/sdk/tasks_gen_test.go index ee312de048..0278e1803b 100644 --- a/pkg/sdk/tasks_gen_test.go +++ b/pkg/sdk/tasks_gen_test.go @@ -55,7 +55,7 @@ func TestTasks_Create(t *testing.T) { t.Run("with initial warehouse size", func(t *testing.T) { req := NewCreateTaskRequest(id, sql). - WithWarehouse(NewCreateTaskWarehouseRequest().WithUserTaskManagedInitialWarehouseSize(&WarehouseSizeXSmall)) + WithWarehouse(NewCreateTaskWarehouseRequest().WithUserTaskManagedInitialWarehouseSize(Pointer(WarehouseSizeXSmall))) assertOptsValidAndSQLEquals(t, req.toOpts(), "CREATE TASK %s USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE = 'XSMALL' AS %s", id.FullyQualifiedName(), sql) }) @@ -177,7 +177,7 @@ func TestTasks_Alter(t *testing.T) { opts := defaultOpts() opts.Set = &TaskSet{} opts.Set.Warehouse = &warehouseId - opts.Set.UserTaskManagedInitialWarehouseSize = &WarehouseSizeXSmall + opts.Set.UserTaskManagedInitialWarehouseSize = Pointer(WarehouseSizeXSmall) assertOptsInvalidJoinedErrors(t, opts, errOneOf("AlterTaskOptions.Set", "Warehouse", "UserTaskManagedInitialWarehouseSize")) }) diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index 83ac550534..a89b669280 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -136,7 +136,7 @@ func TestInt_Warehouses(t *testing.T) { warehouseAssertions.CheckAll(t) //// to show errors - //warehouseAssertionsBad := poc.Warehouse(t, id). + // warehouseAssertionsBad := poc.Warehouse(t, id). // HasName("bad name"). // HasState(sdk.WarehouseStateSuspended). // HasType(sdk.WarehouseTypeSnowparkOptimized). @@ -151,9 +151,9 @@ func TestInt_Warehouses(t *testing.T) { // HasEnableQueryAcceleration(false). // HasQueryAccelerationMaxScaleFactor(12) ////and run it like this - //poc.AssertThatObject(t, warehouseAssertionsBad.SnowflakeObjectAssert) + // poc.AssertThatObject(t, warehouseAssertionsBad.SnowflakeObjectAssert) ////or alternatively - //warehouseAssertionsBad.CheckAll(t) + // warehouseAssertionsBad.CheckAll(t) warehouse, err := client.Warehouses.ShowByID(ctx, id) require.NoError(t, err) diff --git a/pkg/sdk/warehouses_test.go b/pkg/sdk/warehouses_test.go index 699ccf8eef..685dd9965c 100644 --- a/pkg/sdk/warehouses_test.go +++ b/pkg/sdk/warehouses_test.go @@ -26,11 +26,11 @@ func TestWarehouseCreate(t *testing.T) { name: NewAccountObjectIdentifier("completewarehouse"), IfNotExists: Bool(true), - WarehouseType: &WarehouseTypeStandard, - WarehouseSize: &WarehouseSizeX4Large, + WarehouseType: Pointer(WarehouseTypeStandard), + WarehouseSize: Pointer(WarehouseSizeX4Large), MaxClusterCount: Int(8), MinClusterCount: Int(3), - ScalingPolicy: &ScalingPolicyEconomy, + ScalingPolicy: Pointer(ScalingPolicyEconomy), AutoSuspend: Int(1000), AutoResume: Bool(true), InitiallySuspended: Bool(false), @@ -109,7 +109,7 @@ func TestWarehouseAlter(t *testing.T) { opts := &AlterWarehouseOptions{ name: NewAccountObjectIdentifier("mywarehouse"), Set: &WarehouseSet{ - WarehouseType: &WarehouseTypeSnowparkOptimized, + WarehouseType: Pointer(WarehouseTypeSnowparkOptimized), WaitForCompletion: Bool(false), MinClusterCount: Int(4), MaxClusterCount: Int(5), From 529411a25f396203cbf06f3d52346ccd93963030 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 18:50:29 +0200 Subject: [PATCH 08/17] Generate config (fast-way) --- go.mod | 2 -- go.sum | 34 ----------------------- pkg/acceptance/bettertestspoc/config.go | 36 ++++++++++++------------- 3 files changed, 18 insertions(+), 54 deletions(-) diff --git a/go.mod b/go.mod index 061de5f76d..1ff88672d3 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,6 @@ require ( github.com/gookit/color v1.5.4 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/go-uuid v1.0.3 - github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 github.com/hashicorp/terraform-json v0.21.0 github.com/hashicorp/terraform-plugin-framework v1.8.0 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 @@ -48,7 +47,6 @@ require ( github.com/ProtonMail/go-crypto v1.1.0-alpha.0 // indirect github.com/agext/levenshtein v1.2.3 // indirect github.com/apache/arrow/go/v15 v15.0.0 // indirect - github.com/apparentlymart/go-textseg v1.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/aws/aws-sdk-go-v2 v1.21.0 // indirect github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.13 // indirect diff --git a/go.sum b/go.sum index 141ad2ff6d..78062756a5 100644 --- a/go.sum +++ b/go.sum @@ -24,14 +24,10 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/ProtonMail/go-crypto v1.1.0-alpha.0 h1:nHGfwXmFvJrSR9xu8qL7BkO4DqTHXE9N5vPhgY2I+j0= github.com/ProtonMail/go-crypto v1.1.0-alpha.0/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE= -github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/apache/arrow/go/v15 v15.0.0 h1:1zZACWf85oEZY5/kd9dsQS7i+2G5zVQcbKTHgslqHNA= github.com/apache/arrow/go/v15 v15.0.0/go.mod h1:DGXsR3ajT524njufqf95822i+KTh+yea1jass9YXgjA= -github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= -github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0= -github.com/apparentlymart/go-textseg v1.0.0/go.mod h1:z96Txxhf3xSFMPmb5X/1W05FF/Nj9VFpLOpjS5yuumk= github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= @@ -77,7 +73,6 @@ github.com/aws/smithy-go v1.14.2 h1:MJU9hqBGbvWZdApzpvoF2WAIJDbtjK2NDJSiJP7HblQ= github.com/aws/smithy-go v1.14.2/go.mod h1:Tg+OJXh4MB2R/uN61Ko2f6hTZwB/ZYGOtib8J3gBHzA= github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4= github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs= -github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2mtRGSCPsat1kze3CUtvJN3/jTXlp29k= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= @@ -100,7 +95,6 @@ github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FM github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= -github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= @@ -125,14 +119,12 @@ github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVI github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/flatbuffers v23.5.26+incompatible h1:M9dgRyhJemaM4Sw8+66GHBu8ioaQmyPLg1b8VwK5WJg= github.com/google/flatbuffers v23.5.26+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= -github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= @@ -144,7 +136,6 @@ github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU= github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0= -github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I= github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -157,7 +148,6 @@ github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUK github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs= github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c= github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= -github.com/hashicorp/go-multierror v0.0.0-20180717150148-3d5d8f294aa0/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDmTk8A= @@ -171,8 +161,6 @@ github.com/hashicorp/hc-install v0.6.3 h1:yE/r1yJvWbtrJ0STwScgEnCanb0U9v7zp0Gbkm github.com/hashicorp/hc-install v0.6.3/go.mod h1:KamGdbodYzlufbWh4r9NRo8y6GLHWZP2GBtdnms1Ln0= github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI= github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE= -github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 h1:PFfGModn55JA0oBsvFghhj0v93me+Ctr3uHC/UmFAls= -github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80/go.mod h1:Cxv+IJLuBiEhQ7pBYGEuORa0nr4U994pE8mYLuFd7v0= github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8JyYF3vpnuEo= @@ -199,10 +187,8 @@ github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc= github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE= github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ= -github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c= github.com/jhump/protoreflect v1.15.1/go.mod h1:jD/2GMKKE6OqX8qTjhADU1e6DShO+gavG9e0Q693nKo= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= @@ -225,7 +211,6 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= @@ -250,7 +235,6 @@ github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU= github.com/mitchellh/go-testing-interface v1.14.1/go.mod h1:gfgS7OtZj6MA4U1UrDRp04twqAjfvlZyCfX3sDjEym8= -github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo= github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0= github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= @@ -262,9 +246,6 @@ github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ib github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA= github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU= -github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= -github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ= @@ -277,7 +258,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ= github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -286,12 +266,10 @@ github.com/skeema/knownhosts v1.2.1 h1:SHWdIUa82uGZz+F+47k8SY4QhhI291cXCpopT1lK2 github.com/skeema/knownhosts v1.2.1/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/snowflakedb/gosnowflake v1.10.0 h1:5hBGKa/jJEhciokzgJcz5xmLNlJ8oUm8vhfu5tg82tM= github.com/snowflakedb/gosnowflake v1.10.0/go.mod h1:WC4eGUOH3K9w3pLsdwZsdawIwtWgse4kZPPqNG0Ky/k= -github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= @@ -312,7 +290,6 @@ github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1z github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -github.com/zclconf/go-cty v1.0.0/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s= github.com/zclconf/go-cty v1.14.2 h1:kTG7lqmBou0Zkx35r6HJHUQTvaRPr5bIAf3AoHS0izI= github.com/zclconf/go-cty v1.14.2/go.mod h1:VvMs5i0vgZdhYawQNq5kePSpLAoz8u1xvZgrPIxfnZE= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= @@ -320,7 +297,6 @@ github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= @@ -330,10 +306,7 @@ golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQz golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190502183928-7f726cade0ab/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= @@ -344,10 +317,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20190502175342-a43fa875dd82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -402,16 +373,11 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= -gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= -gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0= diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go index 64be43be1d..3449c93370 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config.go @@ -2,10 +2,10 @@ package bettertestspoc import ( "encoding/json" + "fmt" + "strings" "testing" - hclJson "github.com/hashicorp/hcl2/hcl/json" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/hashicorp/terraform-plugin-testing/config" "github.com/stretchr/testify/require" @@ -40,23 +40,27 @@ func defaultMeta(resource resources.Resource) *resourceModelMeta { return &resourceModelMeta{name: DefaultResourceName, resource: resource} } +// TODO: use reflection to build config directly from model struct func ConfigurationFromModel(t *testing.T, model ResourceModel) string { t.Helper() - m1 := make(map[string]map[string]ResourceModel) - m2 := make(map[string]ResourceModel) - m2[model.ResourceName()] = model - m1[model.Resource().String()] = m2 - b, err := json.Marshal(ResourceWrapper{m1}) + b, err := json.Marshal(model) + require.NoError(t, err) + + var objMap map[string]json.RawMessage + err = json.Unmarshal(b, &objMap) require.NoError(t, err) - t.Logf("Generated json:\n%s", string(b)) - // TODO: https://pkg.go.dev/github.com/hashicorp/hcl2/hcl/json#Parse - f, diag := hclJson.Parse(b, "") - if diag.HasErrors() { - t.Fatal("Could not parse") + + var sb strings.Builder + sb.WriteString(fmt.Sprintf(`resource "%s" "%s" {`, model.Resource(), model.ResourceName())) + sb.WriteRune('\n') + for k, v := range objMap { + sb.WriteString(fmt.Sprintf("\t%s = %s\n", k, v)) } - s := string(f.Bytes) - t.Logf("Generated hcl:\n%s", s) + sb.WriteString(`}`) + sb.WriteRune('\n') + s := sb.String() + t.Logf("Generated config:\n%s", s) return s } @@ -70,7 +74,3 @@ func ConfigurationFromModelProvider(t *testing.T, model ResourceModel) func(conf return "" } } - -type ResourceWrapper struct { - Resource map[string]map[string]ResourceModel `json:"resource"` -} From e225343e12de30f2cbb65fa2c45397e2b4d3ae9f Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Mon, 8 Jul 2024 18:53:16 +0200 Subject: [PATCH 09/17] Add TODOs --- pkg/acceptance/bettertestspoc/warehouse_model.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/acceptance/bettertestspoc/warehouse_model.go b/pkg/acceptance/bettertestspoc/warehouse_model.go index 28b64b0e1d..095ed63f3b 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model.go +++ b/pkg/acceptance/bettertestspoc/warehouse_model.go @@ -6,6 +6,9 @@ import ( "github.com/hashicorp/terraform-plugin-testing/config" ) +// TODO: add possibility to have reference to another object (e.g. WithResourceMonitorReference); new config.Variable impl? +// TODO: add possibility to have depends_on to other resources (in meta?) +// TODO: add a convenience method to use multiple configs from multiple models type WarehouseModel struct { Name config.Variable `json:"name,omitempty"` WarehouseType config.Variable `json:"warehouse_type,omitempty"` From a593f3bba5a54035d083cb0607ff4d8576d8cc3b Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 9 Jul 2024 10:54:38 +0200 Subject: [PATCH 10/17] Add resource name to basic builder --- pkg/acceptance/bettertestspoc/config.go | 4 ++++ pkg/acceptance/bettertestspoc/warehouse_model.go | 15 ++++++++++++--- .../bettertestspoc/warehouse_model_ext.go | 2 +- pkg/resources/warehouse_acceptance_test.go | 7 ++++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go index 3449c93370..55d0e15d00 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config.go @@ -40,6 +40,10 @@ func defaultMeta(resource resources.Resource) *resourceModelMeta { return &resourceModelMeta{name: DefaultResourceName, resource: resource} } +func meta(resourceName string, resource resources.Resource) *resourceModelMeta { + return &resourceModelMeta{name: resourceName, resource: resource} +} + // TODO: use reflection to build config directly from model struct func ConfigurationFromModel(t *testing.T, model ResourceModel) string { t.Helper() diff --git a/pkg/acceptance/bettertestspoc/warehouse_model.go b/pkg/acceptance/bettertestspoc/warehouse_model.go index 095ed63f3b..755e15bd4d 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model.go +++ b/pkg/acceptance/bettertestspoc/warehouse_model.go @@ -31,11 +31,20 @@ type WarehouseModel struct { *resourceModelMeta } -/////////////////////////////////// -// Basic builder (only required) // -/////////////////////////////////// +///////////////////////////////////////////////// +// Basic builders (resource name and required) // +///////////////////////////////////////////////// func NewWarehouseModel( + resourceName string, + name string, +) *WarehouseModel { + m := &WarehouseModel{resourceModelMeta: meta(resourceName, resources.Warehouse)} + m.WithName(name) + return m +} + +func NewDefaultWarehouseModel( name string, ) *WarehouseModel { m := &WarehouseModel{resourceModelMeta: defaultMeta(resources.Warehouse)} diff --git a/pkg/acceptance/bettertestspoc/warehouse_model_ext.go b/pkg/acceptance/bettertestspoc/warehouse_model_ext.go index 8b7fe59208..fa7d90e87b 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model_ext.go +++ b/pkg/acceptance/bettertestspoc/warehouse_model_ext.go @@ -4,5 +4,5 @@ func BasicWarehouseModel( name string, comment string, ) *WarehouseModel { - return NewWarehouseModel(name).WithComment(comment) + return NewDefaultWarehouseModel(name).WithComment(comment) } diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 339a007ed7..4334ed84b0 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -37,9 +37,10 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { t.Cleanup(resourceMonitorCleanup) resourceMonitorId := resourceMonitor.ID() - model := poc.NewWarehouseModel(name).WithComment(comment) - // TODO: handle resource name better - model.SetResourceName("w") + model := poc.NewWarehouseModel("w", name).WithComment(comment) + + // alternatively some extension func + // model := poc.BasicWarehouseModel(name, comment) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, From 0c731a2e039188785a2107a17d29216bbad15375 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 9 Jul 2024 14:11:39 +0200 Subject: [PATCH 11/17] Add descriptions to all the exported methods --- pkg/acceptance/bettertestspoc/commons.go | 14 +++++++++++++ pkg/acceptance/bettertestspoc/config.go | 20 +++++++------------ .../bettertestspoc/resource_assertions.go | 9 ++++++++- .../bettertestspoc/snowflake_assertions.go | 11 +++++++++- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/commons.go b/pkg/acceptance/bettertestspoc/commons.go index 30f5ef312a..6f7346e83c 100644 --- a/pkg/acceptance/bettertestspoc/commons.go +++ b/pkg/acceptance/bettertestspoc/commons.go @@ -9,10 +9,15 @@ import ( "github.com/hashicorp/terraform-plugin-testing/terraform" ) +// TestCheckFuncProvider is an interface with just one method providing resource.TestCheckFunc. +// It allows using it as input the "Check:" in resource.TestStep. +// It should be used with AssertThat. type TestCheckFuncProvider interface { ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc } +// AssertThat should be used for "Check:" input in resource.TestStep instead of e.g. resource.ComposeTestCheckFunc. +// It allows performing all the checks implementing the TestCheckFuncProvider interface. func AssertThat(t *testing.T, fs ...TestCheckFuncProvider) resource.TestCheckFunc { t.Helper() return func(s *terraform.State) error { @@ -38,14 +43,21 @@ func (w *testCheckFuncWrapper) ToTerraformTestCheckFunc(_ *testing.T) resource.T return w.f } +// Check allows using the basic terraform checks while using AssertThat. +// To use, just simply wrap the check in Check. func Check(f resource.TestCheckFunc) TestCheckFuncProvider { return &testCheckFuncWrapper{f} } +// ImportStateCheckFuncProvider is an interface with just one method providing resource.ImportStateCheckFunc. +// It allows using it as input the "ImportStateCheck:" in resource.TestStep for import tests. +// It should be used with AssertThatImport. type ImportStateCheckFuncProvider interface { ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc } +// AssertThatImport should be used for "ImportStateCheck:" input in resource.TestStep instead of e.g. importchecks.ComposeImportStateCheck. +// It allows performing all the checks implementing the ImportStateCheckFuncProvider interface. func AssertThatImport(t *testing.T, fs ...ImportStateCheckFuncProvider) resource.ImportStateCheckFunc { t.Helper() return func(s []*terraform.InstanceState) error { @@ -71,6 +83,8 @@ func (w *importStateCheckFuncWrapper) ToTerraformImportStateCheckFunc(_ *testing return w.f } +// CheckImport allows using the basic terraform import checks while using AssertThatImport. +// To use, just simply wrap the check in CheckImport. func CheckImport(f resource.ImportStateCheckFunc) ImportStateCheckFuncProvider { return &importStateCheckFuncWrapper{f} } diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config.go index 55d0e15d00..5d16ff3c0a 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config.go @@ -7,10 +7,11 @@ import ( "testing" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" - "github.com/hashicorp/terraform-plugin-testing/config" "github.com/stretchr/testify/require" ) +// ResourceModel is the base interface all of our config models will implement. +// To allow easy implementation, resourceModelMeta can be embedded inside the struct (and the struct will automatically implement it). type ResourceModel interface { Resource() resources.Resource ResourceName() string @@ -34,6 +35,7 @@ func (m *resourceModelMeta) SetResourceName(name string) { m.name = name } +// DefaultResourceName is exported to allow assertions against the resources using the default name. const DefaultResourceName = "test" func defaultMeta(resource resources.Resource) *resourceModelMeta { @@ -44,7 +46,10 @@ func meta(resourceName string, resource resources.Resource) *resourceModelMeta { return &resourceModelMeta{name: resourceName, resource: resource} } -// TODO: use reflection to build config directly from model struct +// ConfigurationFromModel should be used in terraform acceptance tests for Config attribute to get string config from ResourceModel. +// Current implementation is really straightforward but it could be improved and tested. It may not handle all cases (like objects, lists, sets) correctly. +// TODO: use reflection to build config directly from model struct (or some other different way) +// TODO: add support for config.TestStepConfigFunc (to use as ConfigFile); the naive implementation would be to just create a tmp directory and save file there func ConfigurationFromModel(t *testing.T, model ResourceModel) string { t.Helper() @@ -67,14 +72,3 @@ func ConfigurationFromModel(t *testing.T, model ResourceModel) string { t.Logf("Generated config:\n%s", s) return s } - -// TODO: save to tmp file and return path to it -func ConfigurationFromModelProvider(t *testing.T, model ResourceModel) func(config.TestStepConfigRequest) string { - t.Helper() - return func(req config.TestStepConfigRequest) string { - t.Logf("Generating config for test %s, step %d for resource %s", req.TestName, req.StepNumber, model.Resource()) - content := ConfigurationFromModel(t, model) - _ = content - return "" - } -} diff --git a/pkg/acceptance/bettertestspoc/resource_assertions.go b/pkg/acceptance/bettertestspoc/resource_assertions.go index 6260470444..474574256c 100644 --- a/pkg/acceptance/bettertestspoc/resource_assertions.go +++ b/pkg/acceptance/bettertestspoc/resource_assertions.go @@ -16,14 +16,16 @@ var ( _ ImportStateCheckFuncProvider = (*ResourceAssert)(nil) ) +// ResourceAssert is an embeddable struct that should be used to construct new resource assertions (for resource, show output, parameters, etc.). +// It implements both TestCheckFuncProvider and ImportStateCheckFuncProvider which makes it easy to create new resource assertions. type ResourceAssert struct { name string id string prefix string assertions []resourceAssertion - TestCheckFuncProvider } +// NewResourceAssert creates a ResourceAssert where the resource name should be used as a key for assertions. func NewResourceAssert(name string, prefix string) *ResourceAssert { return &ResourceAssert{ name: name, @@ -32,6 +34,7 @@ func NewResourceAssert(name string, prefix string) *ResourceAssert { } } +// NewImportedResourceAssert creates a ResourceAssert where the resource id should be used as a key for assertions. func NewImportedResourceAssert(id string, prefix string) *ResourceAssert { return &ResourceAssert{ id: id, @@ -81,6 +84,8 @@ func parameterLevelSet(fieldName string, expected string) resourceAssertion { return resourceAssertion{fieldName: parametersPrefix + fieldName + parametersLevelSuffix, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet} } +// ToTerraformTestCheckFunc implements TestCheckFuncProvider to allow easier creation of new resource assertions. +// It goes through all the assertion accumulated earlier and gathers the results of the checks. func (r *ResourceAssert) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { t.Helper() return func(s *terraform.State) error { @@ -105,6 +110,8 @@ func (r *ResourceAssert) ToTerraformTestCheckFunc(t *testing.T) resource.TestChe } } +// ToTerraformImportStateCheckFunc implements ImportStateCheckFuncProvider to allow easier creation of new resource assertions. +// It goes through all the assertion accumulated earlier and gathers the results of the checks. func (r *ResourceAssert) ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc { t.Helper() return func(s []*terraform.InstanceState) error { diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go index 923eac909e..30ffadae60 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -16,15 +16,18 @@ type ( objectProvider[T any, I sdk.ObjectIdentifier] func(*testing.T, I) (*T, error) ) +// SnowflakeObjectAssert is an embeddable struct that should be used to construct new Snowflake object assertions. +// It implements both TestCheckFuncProvider and ImportStateCheckFuncProvider which makes it easy to create new resource assertions. type SnowflakeObjectAssert[T any, I sdk.ObjectIdentifier] struct { assertions []assertSdk[*T] id I objectType sdk.ObjectType object *T provider objectProvider[T, I] - TestCheckFuncProvider } +// NewSnowflakeObjectAssertWithProvider creates a SnowflakeObjectAssert with id and the provider. +// Object to check is lazily fetched from Snowflake when the checks are being run. func NewSnowflakeObjectAssertWithProvider[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, provider objectProvider[T, I]) *SnowflakeObjectAssert[T, I] { return &SnowflakeObjectAssert[T, I]{ assertions: make([]assertSdk[*T], 0), @@ -34,6 +37,8 @@ func NewSnowflakeObjectAssertWithProvider[T any, I sdk.ObjectIdentifier](objectT } } +// NewSnowflakeObjectAssertWithObject creates a SnowflakeObjectAssert with object that was already fetched from Snowflake. +// All the checks are run against the given object. func NewSnowflakeObjectAssertWithObject[T any, I sdk.ObjectIdentifier](objectType sdk.ObjectType, id I, object *T) *SnowflakeObjectAssert[T, I] { return &SnowflakeObjectAssert[T, I]{ assertions: make([]assertSdk[*T], 0), @@ -43,6 +48,8 @@ func NewSnowflakeObjectAssertWithObject[T any, I sdk.ObjectIdentifier](objectTyp } } +// ToTerraformTestCheckFunc implements TestCheckFuncProvider to allow easier creation of new Snowflake object assertions. +// It goes through all the assertion accumulated earlier and gathers the results of the checks. func (s *SnowflakeObjectAssert[_, _]) ToTerraformTestCheckFunc(t *testing.T) resource.TestCheckFunc { t.Helper() return func(_ *terraform.State) error { @@ -50,6 +57,8 @@ func (s *SnowflakeObjectAssert[_, _]) ToTerraformTestCheckFunc(t *testing.T) res } } +// ToTerraformImportStateCheckFunc implements ImportStateCheckFuncProvider to allow easier creation of new Snowflake object assertions. +// It goes through all the assertion accumulated earlier and gathers the results of the checks. func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing.T) resource.ImportStateCheckFunc { t.Helper() return func(_ []*terraform.InstanceState) error { From 3de5ea9e3f579a231b8efc986df8a4e85a0057c5 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 9 Jul 2024 14:24:08 +0200 Subject: [PATCH 12/17] Add in place assertion verifier --- pkg/acceptance/bettertestspoc/commons.go | 12 +++++++++++ .../bettertestspoc/snowflake_assertions.go | 20 ++++++++----------- .../testint/warehouses_integration_test.go | 18 +++++------------ 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pkg/acceptance/bettertestspoc/commons.go b/pkg/acceptance/bettertestspoc/commons.go index 6f7346e83c..1540276d3e 100644 --- a/pkg/acceptance/bettertestspoc/commons.go +++ b/pkg/acceptance/bettertestspoc/commons.go @@ -88,3 +88,15 @@ func (w *importStateCheckFuncWrapper) ToTerraformImportStateCheckFunc(_ *testing func CheckImport(f resource.ImportStateCheckFunc) ImportStateCheckFuncProvider { return &importStateCheckFuncWrapper{f} } + +// InPlaceAssertionVerifier is an interface providing a method allowing verifying all the prepared assertions in place. +// It does not return function like TestCheckFuncProvider or ImportStateCheckFuncProvider; it runs all the assertions in place instead. +type InPlaceAssertionVerifier interface { + VerifyAll(t *testing.T) +} + +// AssertThatObject should be used in the SDK tests for created object validation. +// It verifies all the prepared assertions in place. +func AssertThatObject(t *testing.T, objectAssert InPlaceAssertionVerifier) { + objectAssert.VerifyAll(t) +} diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/snowflake_assertions.go index 30ffadae60..4e861ce22e 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/snowflake_assertions.go @@ -66,6 +66,14 @@ func (s *SnowflakeObjectAssert[_, _]) ToTerraformImportStateCheckFunc(t *testing } } +// VerifyAll implements InPlaceAssertionVerifier to allow easier creation of new Snowflake object assertions. +// It verifies all the assertions accumulated earlier and gathers the results of the checks. +func (s *SnowflakeObjectAssert[_, _]) VerifyAll(t *testing.T) { + t.Helper() + err := s.runSnowflakeObjectsAssertions(t) + require.NoError(t, err) +} + func (s *SnowflakeObjectAssert[T, _]) runSnowflakeObjectsAssertions(t *testing.T) error { t.Helper() @@ -93,15 +101,3 @@ func (s *SnowflakeObjectAssert[T, _]) runSnowflakeObjectsAssertions(t *testing.T return errors.Join(result...) } - -func AssertThatObject[T any, I sdk.ObjectIdentifier](t *testing.T, objectAssert *SnowflakeObjectAssert[T, I]) { - t.Helper() - err := objectAssert.runSnowflakeObjectsAssertions(t) - require.NoError(t, err) -} - -func (s *SnowflakeObjectAssert[_, _]) CheckAll(t *testing.T) { - t.Helper() - err := s.runSnowflakeObjectsAssertions(t) - require.NoError(t, err) -} diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index a89b669280..c1d69adc12 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -116,7 +116,7 @@ func TestInt_Warehouses(t *testing.T) { t.Cleanup(testClientHelper().Warehouse.DropWarehouseFunc(t, id)) // we can use the same assertion builder in the SDK tests - warehouseAssertions := poc.Warehouse(t, id). + poc.AssertThatObject(t, poc.Warehouse(t, id). HasName(id.Name()). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeSmall). @@ -129,11 +129,7 @@ func TestInt_Warehouses(t *testing.T) { HasResourceMonitor(resourceMonitor.ID()). HasComment("comment"). HasEnableQueryAcceleration(true). - HasQueryAccelerationMaxScaleFactor(90) - // and run it like this - poc.AssertThatObject(t, warehouseAssertions.SnowflakeObjectAssert) - // or alternatively - warehouseAssertions.CheckAll(t) + HasQueryAccelerationMaxScaleFactor(90)) //// to show errors // warehouseAssertionsBad := poc.Warehouse(t, id). @@ -150,10 +146,7 @@ func TestInt_Warehouses(t *testing.T) { // HasComment("bad comment"). // HasEnableQueryAcceleration(false). // HasQueryAccelerationMaxScaleFactor(12) - ////and run it like this - // poc.AssertThatObject(t, warehouseAssertionsBad.SnowflakeObjectAssert) - ////or alternatively - // warehouseAssertionsBad.CheckAll(t) + // poc.AssertThatObject(t, warehouseAssertionsBad) warehouse, err := client.Warehouses.ShowByID(ctx, id) require.NoError(t, err) @@ -172,7 +165,7 @@ func TestInt_Warehouses(t *testing.T) { assert.Equal(t, 90, warehouse.QueryAccelerationMaxScaleFactor) // we can also use the read object to initialize: - poc.WarehouseFromObject(t, warehouse). + poc.AssertThatObject(t, poc.WarehouseFromObject(t, warehouse). HasName(id.Name()). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeSmall). @@ -185,8 +178,7 @@ func TestInt_Warehouses(t *testing.T) { HasResourceMonitor(resourceMonitor.ID()). HasComment("comment"). HasEnableQueryAcceleration(true). - HasQueryAccelerationMaxScaleFactor(90). - CheckAll(t) + HasQueryAccelerationMaxScaleFactor(90)) tag1Value, err := client.SystemFunctions.GetTag(ctx, tag.ID(), warehouse.ID(), sdk.ObjectTypeWarehouse) require.NoError(t, err) From 5c73b037b708d229f4da2d6752ee09995aa1becb Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 9 Jul 2024 15:53:11 +0200 Subject: [PATCH 13/17] Split into packages and add readme --- pkg/acceptance/bettertestspoc/README.md | 460 ++++++++++++++++++ .../bettertestspoc/{ => assert}/commons.go | 2 +- .../{ => assert}/resource_assertions.go | 2 +- .../{ => assert}/snowflake_assertions.go | 2 +- .../{ => assert}/warehouse_parameters.go | 2 +- .../{ => assert}/warehouse_parameters_ext.go | 2 +- .../{ => assert}/warehouse_resource.go | 2 +- .../{ => assert}/warehouse_resource_ext.go | 2 +- .../{ => assert}/warehouse_show_output.go | 2 +- .../{ => assert}/warehouse_snowflake.go | 2 +- .../{ => assert}/warehouse_snowflake_ext.go | 2 +- .../bettertestspoc/{ => config}/config.go | 6 +- .../{ => config}/warehouse_model.go | 2 +- .../{ => config}/warehouse_model_ext.go | 2 +- pkg/resources/warehouse_acceptance_test.go | 98 +--- .../testint/warehouses_integration_test.go | 23 +- 16 files changed, 496 insertions(+), 115 deletions(-) create mode 100644 pkg/acceptance/bettertestspoc/README.md rename pkg/acceptance/bettertestspoc/{ => assert}/commons.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/resource_assertions.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/snowflake_assertions.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_parameters.go (98%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_parameters_ext.go (96%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_resource.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_resource_ext.go (98%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_show_output.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_snowflake.go (99%) rename pkg/acceptance/bettertestspoc/{ => assert}/warehouse_snowflake_ext.go (95%) rename pkg/acceptance/bettertestspoc/{ => config}/config.go (90%) rename pkg/acceptance/bettertestspoc/{ => config}/warehouse_model.go (99%) rename pkg/acceptance/bettertestspoc/{ => config}/warehouse_model_ext.go (85%) diff --git a/pkg/acceptance/bettertestspoc/README.md b/pkg/acceptance/bettertestspoc/README.md new file mode 100644 index 0000000000..331e41631d --- /dev/null +++ b/pkg/acceptance/bettertestspoc/README.md @@ -0,0 +1,460 @@ +# Better tests poc +This package contains a quick implementation of helpers that should allow us a quicker, more pleasant, and more readable implementation of tests, mainly the acceptance ones. +It contains the following packages: +- `assert` - all the assertions reside here. The currently supported assertions are: resource assertions, show output assertions, parameters assertions, and Snowflake object assertions. +The package contains utilities to build assertions for new objects. +All the assertions will be ultimately generated; the ones presented for warehouse were manually created. +- `config` - the new ResourceModel abstraction resides here. It provides models for objects and the builder methods allowing better config preparation in the acceptance tests. +It aims to be more readable than using `Config:` with hardcoded string or `ConfigFile:` for file that is not directly reachable from the test body. Also, it should be easier to reuse the models and prepare convenience extension methods. +All the models will be ultimately generated; the ones presented for warehouse were manually created. + +## Usage +You can check the current example usage in `TestAcc_Warehouse_BasicFlows` and the `create: complete` inside `TestInt_Warehouses`. To see the output after invalid assertions: +- add the following to the first step of `TestAcc_Warehouse_BasicFlows` +```go + // bad checks below + assert.WarehouseResource(t, "snowflake_warehouse.w"). + HasType(string(sdk.WarehouseTypeSnowparkOptimized)). + HasSize(string(sdk.WarehouseSizeMedium)), + assert.WarehouseShowOutput(t, "snowflake_warehouse.w"). + HasType(sdk.WarehouseTypeSnowparkOptimized), + assert.WarehouseParameters(t, "snowflake_warehouse.w"). + HasMaxConcurrencyLevel(16). + HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse), + assert.Warehouse(t, warehouseId). + HasName("bad name"). + HasState(sdk.WarehouseStateSuspended). + HasType(sdk.WarehouseTypeSnowparkOptimized). + HasSize(sdk.WarehouseSizeMedium). + HasMaxClusterCount(12). + HasMinClusterCount(13). + HasScalingPolicy(sdk.ScalingPolicyEconomy). + HasAutoSuspend(123). + HasAutoResume(false). + HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + HasComment("bad comment"). + HasEnableQueryAcceleration(true). + HasQueryAccelerationMaxScaleFactor(12), + assert.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "warehouse_type", string(sdk.WarehouseTypeSnowparkOptimized))), +``` +it will result in: +``` + warehouse_acceptance_test.go:46: Step 1/8 error: Check failed: check 6/10 error: + snowflake_warehouse.w resource assertion [1/2]: failed with error: Attribute 'warehouse_type' not found + snowflake_warehouse.w resource assertion [2/2]: failed with error: Attribute 'warehouse_size' not found + check 7/10 error: + snowflake_warehouse.w show_output assertion [2/2]: failed with error: Attribute 'show_output.0.type' expected "SNOWPARK-OPTIMIZED", got "STANDARD" + check 8/10 error: + snowflake_warehouse.w parameters assertion [2/3]: failed with error: Attribute 'parameters.0.max_concurrency_level.0.value' expected "16", got "8" + snowflake_warehouse.w parameters assertion [3/3]: failed with error: Attribute 'parameters.0.max_concurrency_level.0.level' expected "WAREHOUSE", got "" + check 9/10 error: + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [1/13]: failed with error: expected name: bad name; got: URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7 + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [2/13]: failed with error: expected state: SUSPENDED; got: STARTED + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [3/13]: failed with error: expected type: SNOWPARK-OPTIMIZED; got: STANDARD + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [4/13]: failed with error: expected size: MEDIUM; got: XSMALL + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [5/13]: failed with error: expected max cluster count: 12; got: 1 + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [6/13]: failed with error: expected min cluster count: 13; got: 1 + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [7/13]: failed with error: expected type: ECONOMY; got: STANDARD + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [8/13]: failed with error: expected auto suspend: 123; got: 600 + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [9/13]: failed with error: expected auto resume: false; got: true + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [10/13]: failed with error: expected resource monitor: some-id; got: + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [11/13]: failed with error: expected comment: bad comment; got: From furthermore rarely cast anything those you could also whoever. + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [12/13]: failed with error: expected enable query acceleration: true; got: false + object WAREHOUSE["URVBDDAT_E7589B32_6534_1F93_DC1B_9E94FB8D27D7"] assertion [13/13]: failed with error: expected query acceleration max scale factor: 12; got: 8 + check 10/10 error: + snowflake_warehouse.w: Attribute 'warehouse_type' not found +``` + +- add the following to the second step of `TestAcc_Warehouse_BasicFlows` +```go + // bad checks below + assert.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "bad name", name)), + assert.ImportedWarehouseResource(t, warehouseId.Name()). + HasName("bad name"). + HasType(string(sdk.WarehouseTypeSnowparkOptimized)). + HasSize(string(sdk.WarehouseSizeMedium)). + HasMaxClusterCount("2"). + HasMinClusterCount("3"). + HasScalingPolicy(string(sdk.ScalingPolicyEconomy)). + HasAutoSuspend("123"). + HasAutoResume("false"). + HasResourceMonitor("abc"). + HasComment("bad comment"). + HasEnableQueryAcceleration("true"). + HasQueryAccelerationMaxScaleFactor("16"), + assert.ImportedWarehouseParameters(t, warehouseId.Name()). + HasMaxConcurrencyLevel(1). + HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse). + HasStatementQueuedTimeoutInSeconds(23). + HasStatementQueuedTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse). + HasStatementTimeoutInSeconds(1232). + HasStatementTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse), + assert.Warehouse(t, warehouseId). + HasName("bad name"). + HasState(sdk.WarehouseStateSuspended). + HasType(sdk.WarehouseTypeSnowparkOptimized). + HasSize(sdk.WarehouseSizeMedium). + HasMaxClusterCount(12). + HasMinClusterCount(13). + HasScalingPolicy(sdk.ScalingPolicyEconomy). + HasAutoSuspend(123). + HasAutoResume(false). + HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + HasComment("bad comment"). + HasEnableQueryAcceleration(true). + HasQueryAccelerationMaxScaleFactor(12), +``` +it will result in: +``` + warehouse_acceptance_test.go:46: check 6/9 error: + attribute bad name not found in instance state + check 7/9 error: + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [1/12]: failed with error: expected: bad name, got: RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [2/12]: failed with error: expected: SNOWPARK-OPTIMIZED, got: STANDARD + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [3/12]: failed with error: expected: MEDIUM, got: XSMALL + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [4/12]: failed with error: expected: 2, got: 1 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [5/12]: failed with error: expected: 3, got: 1 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [6/12]: failed with error: expected: ECONOMY, got: STANDARD + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [7/12]: failed with error: expected: 123, got: 600 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [8/12]: failed with error: expected: false, got: true + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [9/12]: failed with error: expected: abc, got: + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [10/12]: failed with error: expected: bad comment, got: School huh one here entirely mustering where crew though wealth. + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [11/12]: failed with error: expected: true, got: false + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported resource assertion [12/12]: failed with error: expected: 16, got: 8 + check 8/9 error: + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [2/7]: failed with error: expected: 1, got: 8 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [3/7]: failed with error: expected: WAREHOUSE, got: + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [4/7]: failed with error: expected: 23, got: 0 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [5/7]: failed with error: expected: WAREHOUSE, got: + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [6/7]: failed with error: expected: 1232, got: 172800 + RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 imported parameters assertion [7/7]: failed with error: expected: WAREHOUSE, got: + check 9/9 error: + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [1/13]: failed with error: expected name: bad name; got: RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844 + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [2/13]: failed with error: expected state: SUSPENDED; got: STARTED + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [3/13]: failed with error: expected type: SNOWPARK-OPTIMIZED; got: STANDARD + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [4/13]: failed with error: expected size: MEDIUM; got: XSMALL + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [5/13]: failed with error: expected max cluster count: 12; got: 1 + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [6/13]: failed with error: expected min cluster count: 13; got: 1 + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [7/13]: failed with error: expected type: ECONOMY; got: STANDARD + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [8/13]: failed with error: expected auto suspend: 123; got: 600 + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [9/13]: failed with error: expected auto resume: false; got: true + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [10/13]: failed with error: expected resource monitor: some-id; got: + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [11/13]: failed with error: expected comment: bad comment; got: School huh one here entirely mustering where crew though wealth. + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [12/13]: failed with error: expected enable query acceleration: true; got: false + object WAREHOUSE["RQYLJJAT_04646516_1F33_50E9_CC19_D6B14E374844"] assertion [13/13]: failed with error: expected query acceleration max scale factor: 12; got: 8 +``` + +- add the following to the `create: complete` in `TestInt_Warehouses`: +```go + // to show errors + warehouseAssertionsBad := objectAssert.Warehouse(t, id). + HasName("bad name"). + HasState(sdk.WarehouseStateSuspended). + HasType(sdk.WarehouseTypeSnowparkOptimized). + HasSize(sdk.WarehouseSizeMedium). + HasMaxClusterCount(12). + HasMinClusterCount(13). + HasScalingPolicy(sdk.ScalingPolicyStandard). + HasAutoSuspend(123). + HasAutoResume(false). + HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). + HasComment("bad comment"). + HasEnableQueryAcceleration(false). + HasQueryAccelerationMaxScaleFactor(12) + objectAssert.AssertThatObject(t, warehouseAssertionsBad) +``` +it will result in: +``` + commons.go:101: + Error Trace: /Users/asawicki/Projects/terraform-provider-snowflake/pkg/sdk/testint/warehouses_integration_test.go:149 + Error: Received unexpected error: + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [1/13]: failed with error: expected name: bad name; got: VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [2/13]: failed with error: expected state: SUSPENDED; got: STARTED + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [3/13]: failed with error: expected type: SNOWPARK-OPTIMIZED; got: STANDARD + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [4/13]: failed with error: expected size: MEDIUM; got: SMALL + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [5/13]: failed with error: expected max cluster count: 12; got: 8 + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [6/13]: failed with error: expected min cluster count: 13; got: 2 + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [7/13]: failed with error: expected type: STANDARD; got: ECONOMY + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [8/13]: failed with error: expected auto suspend: 123; got: 1000 + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [9/13]: failed with error: expected auto resume: false; got: true + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [10/13]: failed with error: expected resource monitor: some-id; got: OOUJMDIT_535F314F_6549_348F_370E_AB430EE4BC7B + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [11/13]: failed with error: expected comment: bad comment; got: comment + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [12/13]: failed with error: expected enable query acceleration: false; got: true + object WAREHOUSE["VKSENEIT_535F314F_6549_348F_370E_AB430EE4BC7B"] assertion [13/13]: failed with error: expected query acceleration max scale factor: 12; got: 90 + Test: TestInt_Warehouses/create:_complete +``` + +## Adding new resource assertions +For object `abc` create the following files with the described content in the `assert` package: +- `abc_resource.go` +```go +type AbcResourceAssert struct { + *ResourceAssert +} + +func AbcResource(t *testing.T, name string) *AbcResourceAssert { + t.Helper() + + return &AbcResourceAssert{ + ResourceAssert: NewResourceAssert(name, "resource"), + } +} + +func ImportedAbcResource(t *testing.T, id string) *AbcResourceAssert { + t.Helper() + + return &AbcResourceAssert{ + ResourceAssert: NewImportedResourceAssert(id, "imported resource"), + } +} +``` +Two methods for each parameter (let's say parameter name is xyz): +```go +func (w *AbcResourceAssert) HasXyz(expected string) *AbcResourceAssert { + w.assertions = append(w.assertions, valueSet("xyz", expected)) + return w +} + +func (w *AbcResourceAssert) HasNoXyz() *AbcResourceAssert { + w.assertions = append(w.assertions, valueNotSet("xyz")) + return w +} +``` + +- `abc_show_output.go` +```go +type AbcShowOutputAssert struct { + *ResourceAssert +} + +func AbcShowOutput(t *testing.T, name string) *AbcShowOutputAssert { + t.Helper() + w := AbcShowOutputAssert{ + NewResourceAssert(name, "show_output"), + } + w.assertions = append(w.assertions, valueSet("show_output.#", "1")) + return &w +} + +func ImportedAbcShowOutput(t *testing.T, id string) *AbcShowOutputAssert { + t.Helper() + w := AbcShowOutputAssert{ + NewImportedResourceAssert(id, "show_output"), + } + w.assertions = append(w.assertions, valueSet("show_output.#", "1")) + return &w +} +``` + +A method for each parameter (let's say parameter name is xyz): +```go +func (w *AbcShowOutputAssert) HasXyz(expected string) *AbcShowOutputAssert { + w.assertions = append(w.assertions, showOutputValueSet("xyz", string(expected))) + return w +} +``` + +- `abc_parameters.go` +```go +type AbcParametersAssert struct { + *ResourceAssert +} + +func AbcParameters(t *testing.T, name string) *AbcParametersAssert { + t.Helper() + w := AbcParametersAssert{ + NewResourceAssert(name, "parameters"), + } + w.assertions = append(w.assertions, valueSet("parameters.#", "1")) + return &w +} + +func ImportedAbcParameters(t *testing.T, id string) *AbcParametersAssert { + t.Helper() + w := AbcParametersAssert{ + NewImportedResourceAssert(id, "imported parameters"), + } + w.assertions = append(w.assertions, valueSet("parameters.#", "1")) + return &w +} +``` +Two methods for each parameter (let's say parameter name is xyz): +```go +func (w *AbcParametersAssert) HasXyz(expected int) *AbcParametersAssert { + w.assertions = append(w.assertions, parameterValueSet("xyz", strconv.Itoa(expected))) + return w +} + +func (w *AbcParametersAssert) HasXyzLevel(expected sdk.ParameterType) *AbcParametersAssert { + w.assertions = append(w.assertions, parameterLevelSet("xyz", string(expected))) + return w +} +``` +- extensions should be put in `abc_resource_ext.go`, `abc_show_output_ext.go`, or `abc_parameters_ext.go`. We can put here the named aggregations of other assertions. It allows us extendability. Later, we may choose to generate some of these methods too. Currently, the split will help when we start the generation of aforementioned methods. Current examples for extension could be: +```go +func (w *WarehouseResourceAssert) HasDefaultMaxConcurrencyLevel() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("max_concurrency_level", "8")) + return w +} + +func (w *WarehouseResourceAssert) HasDefaultStatementQueuedTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_queued_timeout_in_seconds", "0")) + return w +} + +func (w *WarehouseResourceAssert) HasDefaultStatementTimeoutInSeconds() *WarehouseResourceAssert { + w.assertions = append(w.assertions, valueSet("statement_timeout_in_seconds", "172800")) + return w +} + +func (w *WarehouseResourceAssert) HasAllDefault() *WarehouseResourceAssert { + return w.HasDefaultMaxConcurrencyLevel(). + HasNoType(). + HasNoSize(). + HasNoMaxClusterCount(). + HasNoMinClusterCount(). + HasNoScalingPolicy(). + HasAutoSuspend(r.IntDefaultString). + HasAutoResume(r.BooleanDefault). + HasNoInitiallySuspended(). + HasNoResourceMonitor(). + HasEnableQueryAcceleration(r.BooleanDefault). + HasQueryAccelerationMaxScaleFactor(r.IntDefaultString). + HasDefaultMaxConcurrencyLevel(). + HasDefaultStatementQueuedTimeoutInSeconds(). + HasDefaultStatementTimeoutInSeconds() +} + +func (w *WarehouseParametersAssert) HasDefaultMaxConcurrencyLevel() *WarehouseParametersAssert { + return w. + HasMaxConcurrencyLevel(8). + HasMaxConcurrencyLevelLevel("") +} + +func (w *WarehouseParametersAssert) HasDefaultStatementQueuedTimeoutInSeconds() *WarehouseParametersAssert { + return w. + HasStatementQueuedTimeoutInSeconds(0). + HasStatementQueuedTimeoutInSecondsLevel("") +} + +func (w *WarehouseParametersAssert) HasDefaultStatementTimeoutInSeconds() *WarehouseParametersAssert { + return w. + HasStatementTimeoutInSeconds(172800). + HasStatementTimeoutInSecondsLevel("") +} +``` + +## Adding new Snowflake object assertions +For object `abc` create the following files with the described content in the `assert` package: +- `abc_snowflake.go` +```go +type AbcAssert struct { + *SnowflakeObjectAssert[sdk.Abc, sdk.AccountObjectIdentifier] +} + +func Abc(t *testing.T, id sdk.AccountObjectIdentifier) *AbcAssert { + t.Helper() + return &AbcAssert{ + NewSnowflakeObjectAssertWithProvider(sdk.ObjectTypeAbc, id, acc.TestClient().Abc.Show), + } +} + +func AbcFromObject(t *testing.T, abc *sdk.Abc) *AbcAssert { + t.Helper() + return &AbcAssert{ + NewSnowflakeObjectAssertWithObject(sdk.ObjectTypeAbc, abc.ID(), abc), + } +} +``` + +A method for each object parameter (let's say parameter name is xyz): +```go +func (w *AbcAssert) HasXyz(expected string) *AbcAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Abc) error { + t.Helper() + if o.Xyz != expected { + return fmt.Errorf("expected xyz: %v; got: %v", expected, o.Xyz) + } + return nil + }) + return w +} +``` + +- `abc_snowflake_ext.go` - for the easier separation later (when we start generating the common checks for each object). Example would be: +```go +func (w *WarehouseAssert) HasStateOneOf(expected ...sdk.WarehouseState) *WarehouseAssert { + w.assertions = append(w.assertions, func(t *testing.T, o *sdk.Warehouse) error { + t.Helper() + if !slices.Contains(expected, o.State) { + return fmt.Errorf("expected state one of: %v; got: %v", expected, string(o.State)) + } + return nil + }) + return w +} +``` + +## Adding new models +For object `abc` create the following files with the described content in the `config` package: +- `abc_model.go` +```go +type AbcModel struct { + Xyz config.Variable `json:"xyz,omitempty"` + *resourceModelMeta +} +``` +two builders with required params only: +```go +func NewAbcModel( + resourceName string, + xyz string, +) *AbcModel { + m := &AbcModel{resourceModelMeta: meta(resourceName, resources.Abc)} + m.WithXyz(xyz) + return m +} + +func NewDefaultAbcModel( + xyz string, +) *AbcModel { + m := &AbcModel{resourceModelMeta: defaultMeta(resources.Abc)} + m.WithXyz(xyz) + return m +} +``` +Two methods for each param (with good value type and with any value type): +```go +func (m *AbcModel) WithXyz(xyz string) *AbcModel { + m.Xyz = config.StringVariable(xyz) + return m +} + +func (m *AbcModel) WithXyzValue(value config.Variable) *AbcModel { + m.Xyz = value + return m +} +``` + +- `abc_model_ext.go` - for the easier separation later (when we start generating the models for each object). Example would be: +```go +func BasicWarehouseModel( + name string, + comment string, +) *WarehouseModel { + return NewDefaultWarehouseModel(name).WithComment(comment) +} +``` + +## Known limitations/planned improvements +- Generate all assertions and models. +- Test all the utilities for assertion/model construction (public interfaces, methods, functions). +- Verify if all the config types are supported. +- Consider a better implementation for the model conversion to config (TODO left). +- Support additional methods for references in models (TODO left). +- Support depends_on in models (TODO left). +- Add a convenience function to concatenate multiple models (TODO left). +- Add function to support using `ConfigFile:` in the acceptance tests. +- Replace `acceptance/snowflakechecks` with the new proposed Snowflake objects assertions. +- Support `showOutputValueUnset` and add a second function for each `show_output` attribute. +- Support `resourceAssertionTypeValueNotSet` for import checks (`panic` left currently). diff --git a/pkg/acceptance/bettertestspoc/commons.go b/pkg/acceptance/bettertestspoc/assert/commons.go similarity index 99% rename from pkg/acceptance/bettertestspoc/commons.go rename to pkg/acceptance/bettertestspoc/assert/commons.go index 1540276d3e..65d943a197 100644 --- a/pkg/acceptance/bettertestspoc/commons.go +++ b/pkg/acceptance/bettertestspoc/assert/commons.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "errors" diff --git a/pkg/acceptance/bettertestspoc/resource_assertions.go b/pkg/acceptance/bettertestspoc/assert/resource_assertions.go similarity index 99% rename from pkg/acceptance/bettertestspoc/resource_assertions.go rename to pkg/acceptance/bettertestspoc/assert/resource_assertions.go index 474574256c..99c4b203bb 100644 --- a/pkg/acceptance/bettertestspoc/resource_assertions.go +++ b/pkg/acceptance/bettertestspoc/assert/resource_assertions.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "errors" diff --git a/pkg/acceptance/bettertestspoc/snowflake_assertions.go b/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go similarity index 99% rename from pkg/acceptance/bettertestspoc/snowflake_assertions.go rename to pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go index 4e861ce22e..ec6d9b234a 100644 --- a/pkg/acceptance/bettertestspoc/snowflake_assertions.go +++ b/pkg/acceptance/bettertestspoc/assert/snowflake_assertions.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "errors" diff --git a/pkg/acceptance/bettertestspoc/warehouse_parameters.go b/pkg/acceptance/bettertestspoc/assert/warehouse_parameters.go similarity index 98% rename from pkg/acceptance/bettertestspoc/warehouse_parameters.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_parameters.go index 5d7394990c..169ea17955 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_parameters.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_parameters.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "strconv" diff --git a/pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go b/pkg/acceptance/bettertestspoc/assert/warehouse_parameters_ext.go similarity index 96% rename from pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_parameters_ext.go index 8798b9711b..4e3a3ad231 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_parameters_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_parameters_ext.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert func (w *WarehouseParametersAssert) HasDefaultMaxConcurrencyLevel() *WarehouseParametersAssert { return w. diff --git a/pkg/acceptance/bettertestspoc/warehouse_resource.go b/pkg/acceptance/bettertestspoc/assert/warehouse_resource.go similarity index 99% rename from pkg/acceptance/bettertestspoc/warehouse_resource.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_resource.go index 7b49060a84..474f59a387 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_resource.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_resource.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "testing" diff --git a/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go b/pkg/acceptance/bettertestspoc/assert/warehouse_resource_ext.go similarity index 98% rename from pkg/acceptance/bettertestspoc/warehouse_resource_ext.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_resource_ext.go index b825249584..f246ae8f43 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_resource_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_resource_ext.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import r "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" diff --git a/pkg/acceptance/bettertestspoc/warehouse_show_output.go b/pkg/acceptance/bettertestspoc/assert/warehouse_show_output.go similarity index 99% rename from pkg/acceptance/bettertestspoc/warehouse_show_output.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_show_output.go index 073850497d..068d97f76e 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_show_output.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_show_output.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "strconv" diff --git a/pkg/acceptance/bettertestspoc/warehouse_snowflake.go b/pkg/acceptance/bettertestspoc/assert/warehouse_snowflake.go similarity index 99% rename from pkg/acceptance/bettertestspoc/warehouse_snowflake.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_snowflake.go index 014f4fc5d9..c6be645f25 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_snowflake.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_snowflake.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "fmt" diff --git a/pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go b/pkg/acceptance/bettertestspoc/assert/warehouse_snowflake_ext.go similarity index 95% rename from pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go rename to pkg/acceptance/bettertestspoc/assert/warehouse_snowflake_ext.go index 95c0e72d38..a9b835dd95 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_snowflake_ext.go +++ b/pkg/acceptance/bettertestspoc/assert/warehouse_snowflake_ext.go @@ -1,4 +1,4 @@ -package bettertestspoc +package assert import ( "fmt" diff --git a/pkg/acceptance/bettertestspoc/config.go b/pkg/acceptance/bettertestspoc/config/config.go similarity index 90% rename from pkg/acceptance/bettertestspoc/config.go rename to pkg/acceptance/bettertestspoc/config/config.go index 5d16ff3c0a..b38c9c6bc0 100644 --- a/pkg/acceptance/bettertestspoc/config.go +++ b/pkg/acceptance/bettertestspoc/config/config.go @@ -1,4 +1,4 @@ -package bettertestspoc +package config import ( "encoding/json" @@ -46,11 +46,11 @@ func meta(resourceName string, resource resources.Resource) *resourceModelMeta { return &resourceModelMeta{name: resourceName, resource: resource} } -// ConfigurationFromModel should be used in terraform acceptance tests for Config attribute to get string config from ResourceModel. +// FromModel should be used in terraform acceptance tests for Config attribute to get string config from ResourceModel. // Current implementation is really straightforward but it could be improved and tested. It may not handle all cases (like objects, lists, sets) correctly. // TODO: use reflection to build config directly from model struct (or some other different way) // TODO: add support for config.TestStepConfigFunc (to use as ConfigFile); the naive implementation would be to just create a tmp directory and save file there -func ConfigurationFromModel(t *testing.T, model ResourceModel) string { +func FromModel(t *testing.T, model ResourceModel) string { t.Helper() b, err := json.Marshal(model) diff --git a/pkg/acceptance/bettertestspoc/warehouse_model.go b/pkg/acceptance/bettertestspoc/config/warehouse_model.go similarity index 99% rename from pkg/acceptance/bettertestspoc/warehouse_model.go rename to pkg/acceptance/bettertestspoc/config/warehouse_model.go index 755e15bd4d..0a9a55cb4e 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model.go +++ b/pkg/acceptance/bettertestspoc/config/warehouse_model.go @@ -1,4 +1,4 @@ -package bettertestspoc +package config import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" diff --git a/pkg/acceptance/bettertestspoc/warehouse_model_ext.go b/pkg/acceptance/bettertestspoc/config/warehouse_model_ext.go similarity index 85% rename from pkg/acceptance/bettertestspoc/warehouse_model_ext.go rename to pkg/acceptance/bettertestspoc/config/warehouse_model_ext.go index fa7d90e87b..11eba4f7ab 100644 --- a/pkg/acceptance/bettertestspoc/warehouse_model_ext.go +++ b/pkg/acceptance/bettertestspoc/config/warehouse_model_ext.go @@ -1,4 +1,4 @@ -package bettertestspoc +package config func BasicWarehouseModel( name string, diff --git a/pkg/resources/warehouse_acceptance_test.go b/pkg/resources/warehouse_acceptance_test.go index 4334ed84b0..5536a39c5e 100644 --- a/pkg/resources/warehouse_acceptance_test.go +++ b/pkg/resources/warehouse_acceptance_test.go @@ -7,10 +7,11 @@ import ( "testing" acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" - poc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc" r "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" tfjson "github.com/hashicorp/terraform-json" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/importchecks" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/planchecks" @@ -37,10 +38,9 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { t.Cleanup(resourceMonitorCleanup) resourceMonitorId := resourceMonitor.ID() - model := poc.NewWarehouseModel("w", name).WithComment(comment) - - // alternatively some extension func - // model := poc.BasicWarehouseModel(name, comment) + model := config.NewWarehouseModel("w", name).WithComment(comment) + // alternatively we can add an extension func + _ = config.BasicWarehouseModel(name, comment) resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, @@ -51,9 +51,9 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { CheckDestroy: acc.CheckDestroy(t, resources.Warehouse), Steps: []resource.TestStep{ { - Config: poc.ConfigurationFromModel(t, model), - Check: poc.AssertThat(t, - poc.WarehouseResource(t, "snowflake_warehouse.w"). + Config: config.FromModel(t, model), + Check: assert.AssertThat(t, + assert.WarehouseResource(t, "snowflake_warehouse.w"). HasName(name). HasNoType(). HasNoSize(). @@ -76,7 +76,7 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasDefaultStatementTimeoutInSeconds(). // alternatively extension possible HasAllDefault(), - poc.WarehouseShowOutput(t, "snowflake_warehouse.w"). + assert.WarehouseShowOutput(t, "snowflake_warehouse.w"). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeXSmall). HasMaxClusterCount(1). @@ -88,7 +88,7 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasComment(comment). HasEnableQueryAcceleration(false). HasQueryAccelerationMaxScaleFactor(8), - poc.WarehouseParameters(t, "snowflake_warehouse.w"). + assert.WarehouseParameters(t, "snowflake_warehouse.w"). HasMaxConcurrencyLevel(8). HasStatementQueuedTimeoutInSeconds(0). HasStatementTimeoutInSeconds(172800). @@ -96,7 +96,7 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasDefaultMaxConcurrencyLevel(). HasDefaultStatementQueuedTimeoutInSeconds(). HasDefaultStatementTimeoutInSeconds(), - poc.Warehouse(t, warehouseId). + assert.Warehouse(t, warehouseId). HasName(warehouseId.Name()). HasState(sdk.WarehouseStateStarted). HasType(sdk.WarehouseTypeStandard). @@ -111,41 +111,16 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasEnableQueryAcceleration(false). HasQueryAccelerationMaxScaleFactor(8), // we can still use normal checks - poc.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "name", warehouseId.Name())), - - // bad checks below - // poc.WarehouseResource(t, "snowflake_warehouse.w"). - // HasType(string(sdk.WarehouseTypeSnowparkOptimized)). - // HasSize(string(sdk.WarehouseSizeMedium)), - // poc.WarehouseShowOutput(t, "snowflake_warehouse.w"). - // HasType(sdk.WarehouseTypeSnowparkOptimized), - // poc.WarehouseParameters(t, "snowflake_warehouse.w"). - // HasMaxConcurrencyLevel(16). - // HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse), - // poc.Warehouse(t, warehouseId). - // HasName("bad name"). - // HasState(sdk.WarehouseStateSuspended). - // HasType(sdk.WarehouseTypeSnowparkOptimized). - // HasSize(sdk.WarehouseSizeMedium). - // HasMaxClusterCount(12). - // HasMinClusterCount(13). - // HasScalingPolicy(sdk.ScalingPolicyEconomy). - // HasAutoSuspend(123). - // HasAutoResume(false). - // HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). - // HasComment("bad comment"). - // HasEnableQueryAcceleration(true). - // HasQueryAccelerationMaxScaleFactor(12), - // poc.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "warehouse_type", string(sdk.WarehouseTypeSnowparkOptimized))), + assert.Check(resource.TestCheckResourceAttr("snowflake_warehouse.w", "name", warehouseId.Name())), ), }, // IMPORT after empty config (in this method, most of the attributes will be filled with the defaults acquired from Snowflake) { ResourceName: "snowflake_warehouse.w", ImportState: true, - ImportStateCheck: poc.AssertThatImport(t, - poc.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "name", name)), - poc.ImportedWarehouseResource(t, warehouseId.Name()). + ImportStateCheck: assert.AssertThatImport(t, + assert.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "name", name)), + assert.ImportedWarehouseResource(t, warehouseId.Name()). HasName(name). HasType(string(sdk.WarehouseTypeStandard)). HasSize(string(sdk.WarehouseSizeXSmall)). @@ -161,15 +136,15 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasDefaultMaxConcurrencyLevel(). HasDefaultStatementQueuedTimeoutInSeconds(). HasDefaultStatementTimeoutInSeconds(), - poc.ImportedWarehouseShowOutput(t, warehouseId.Name()), - poc.ImportedWarehouseParameters(t, warehouseId.Name()). + assert.ImportedWarehouseShowOutput(t, warehouseId.Name()), + assert.ImportedWarehouseParameters(t, warehouseId.Name()). HasMaxConcurrencyLevel(8). HasMaxConcurrencyLevelLevel(""). HasStatementQueuedTimeoutInSeconds(0). HasStatementQueuedTimeoutInSecondsLevel(""). HasStatementTimeoutInSeconds(172800). HasStatementTimeoutInSecondsLevel(""), - poc.Warehouse(t, warehouseId). + assert.Warehouse(t, warehouseId). HasName(warehouseId.Name()). HasState(sdk.WarehouseStateStarted). HasType(sdk.WarehouseTypeStandard). @@ -183,43 +158,6 @@ func TestAcc_Warehouse_BasicFlows(t *testing.T) { HasComment(comment). HasEnableQueryAcceleration(false). HasQueryAccelerationMaxScaleFactor(8), - - // bad checks below - poc.CheckImport(importchecks.TestCheckResourceAttrInstanceState(warehouseId.Name(), "bad name", name)), - poc.ImportedWarehouseResource(t, warehouseId.Name()). - HasName("bad name"). - HasType(string(sdk.WarehouseTypeSnowparkOptimized)). - HasSize(string(sdk.WarehouseSizeMedium)). - HasMaxClusterCount("2"). - HasMinClusterCount("3"). - HasScalingPolicy(string(sdk.ScalingPolicyEconomy)). - HasAutoSuspend("123"). - HasAutoResume("false"). - HasResourceMonitor("abc"). - HasComment("bad comment"). - HasEnableQueryAcceleration("true"). - HasQueryAccelerationMaxScaleFactor("16"), - poc.ImportedWarehouseParameters(t, warehouseId.Name()). - HasMaxConcurrencyLevel(1). - HasMaxConcurrencyLevelLevel(sdk.ParameterTypeWarehouse). - HasStatementQueuedTimeoutInSeconds(23). - HasStatementQueuedTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse). - HasStatementTimeoutInSeconds(1232). - HasStatementTimeoutInSecondsLevel(sdk.ParameterTypeWarehouse), - poc.Warehouse(t, warehouseId). - HasName("bad name"). - HasState(sdk.WarehouseStateSuspended). - HasType(sdk.WarehouseTypeSnowparkOptimized). - HasSize(sdk.WarehouseSizeMedium). - HasMaxClusterCount(12). - HasMinClusterCount(13). - HasScalingPolicy(sdk.ScalingPolicyEconomy). - HasAutoSuspend(123). - HasAutoResume(false). - HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). - HasComment("bad comment"). - HasEnableQueryAcceleration(true). - HasQueryAccelerationMaxScaleFactor(12), ), }, // RENAME diff --git a/pkg/sdk/testint/warehouses_integration_test.go b/pkg/sdk/testint/warehouses_integration_test.go index c1d69adc12..c2c7a5c422 100644 --- a/pkg/sdk/testint/warehouses_integration_test.go +++ b/pkg/sdk/testint/warehouses_integration_test.go @@ -5,7 +5,7 @@ import ( "testing" "time" - poc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc" + objectAssert "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random" @@ -116,7 +116,7 @@ func TestInt_Warehouses(t *testing.T) { t.Cleanup(testClientHelper().Warehouse.DropWarehouseFunc(t, id)) // we can use the same assertion builder in the SDK tests - poc.AssertThatObject(t, poc.Warehouse(t, id). + objectAssert.AssertThatObject(t, objectAssert.Warehouse(t, id). HasName(id.Name()). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeSmall). @@ -131,23 +131,6 @@ func TestInt_Warehouses(t *testing.T) { HasEnableQueryAcceleration(true). HasQueryAccelerationMaxScaleFactor(90)) - //// to show errors - // warehouseAssertionsBad := poc.Warehouse(t, id). - // HasName("bad name"). - // HasState(sdk.WarehouseStateSuspended). - // HasType(sdk.WarehouseTypeSnowparkOptimized). - // HasSize(sdk.WarehouseSizeMedium). - // HasMaxClusterCount(12). - // HasMinClusterCount(13). - // HasScalingPolicy(sdk.ScalingPolicyStandard). - // HasAutoSuspend(123). - // HasAutoResume(false). - // HasResourceMonitor(sdk.NewAccountObjectIdentifier("some-id")). - // HasComment("bad comment"). - // HasEnableQueryAcceleration(false). - // HasQueryAccelerationMaxScaleFactor(12) - // poc.AssertThatObject(t, warehouseAssertionsBad) - warehouse, err := client.Warehouses.ShowByID(ctx, id) require.NoError(t, err) assert.Equal(t, id.Name(), warehouse.Name) @@ -165,7 +148,7 @@ func TestInt_Warehouses(t *testing.T) { assert.Equal(t, 90, warehouse.QueryAccelerationMaxScaleFactor) // we can also use the read object to initialize: - poc.AssertThatObject(t, poc.WarehouseFromObject(t, warehouse). + objectAssert.AssertThatObject(t, objectAssert.WarehouseFromObject(t, warehouse). HasName(id.Name()). HasType(sdk.WarehouseTypeStandard). HasSize(sdk.WarehouseSizeSmall). From 5ce3bea66516f8a9f6ed137b9e65016c78530af6 Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Tue, 9 Jul 2024 15:54:44 +0200 Subject: [PATCH 14/17] Run pre-push --- pkg/acceptance/bettertestspoc/assert/commons.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/acceptance/bettertestspoc/assert/commons.go b/pkg/acceptance/bettertestspoc/assert/commons.go index 65d943a197..59e1c86ffa 100644 --- a/pkg/acceptance/bettertestspoc/assert/commons.go +++ b/pkg/acceptance/bettertestspoc/assert/commons.go @@ -98,5 +98,6 @@ type InPlaceAssertionVerifier interface { // AssertThatObject should be used in the SDK tests for created object validation. // It verifies all the prepared assertions in place. func AssertThatObject(t *testing.T, objectAssert InPlaceAssertionVerifier) { + t.Helper() objectAssert.VerifyAll(t) } From af87a1468840ff10ee826645c002c07ebe87e21e Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 11 Jul 2024 14:15:29 +0200 Subject: [PATCH 15/17] Fix after review --- pkg/acceptance/bettertestspoc/README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/acceptance/bettertestspoc/README.md b/pkg/acceptance/bettertestspoc/README.md index 331e41631d..68b698ea0c 100644 --- a/pkg/acceptance/bettertestspoc/README.md +++ b/pkg/acceptance/bettertestspoc/README.md @@ -458,3 +458,13 @@ func BasicWarehouseModel( - Replace `acceptance/snowflakechecks` with the new proposed Snowflake objects assertions. - Support `showOutputValueUnset` and add a second function for each `show_output` attribute. - Support `resourceAssertionTypeValueNotSet` for import checks (`panic` left currently). +- Add assertions for the `describe_output`. +- Add support for datasource tests (assertions and config builders). +- Consider overriding the assertions when invoking same check multiple times with different params (e.g. `Warehouse(...).HasType(X).HasType(Y)`; it could use the last-check-wins approach, to more easily reuse complex checks between the test steps). +- Consider not adding the check for `show_output` presence on creation (same with `parameters`). The majority of the use cases need it to be present but there are a few others (like conditional presence in the datasources). Currently, it seems that they should be always present in the resources, so no change is made. Later, with adding the support for the datasource tests, consider simple destructive implementation like: +```go +func (w *WarehouseDatasourceShowOutputAssert) IsEmpty() { + w.assertions = make([]resourceAssertion, 0) + w.assertions = append(w.assertions, valueSet("show_output.#", "0")) +} +``` From 4408b19cb15ddced2b712d053ff3b19bfbdeb73c Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 11 Jul 2024 14:16:59 +0200 Subject: [PATCH 16/17] Run make pre-push --- docs/resources/table_constraint.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/resources/table_constraint.md b/docs/resources/table_constraint.md index 879c1eb1ce..a7238ffd4b 100644 --- a/docs/resources/table_constraint.md +++ b/docs/resources/table_constraint.md @@ -148,6 +148,5 @@ Required: Import is supported using the following syntax: ```shell -# format is constraint name ❄ constraint type ❄ database name | schema name | table name terraform import snowflake_table_constraint.example 'myconstraintfk❄️FOREIGN KEY❄️databaseName|schemaName|tableName' ``` From e641aa0379800364480324c2b6d146a0ebd34d0d Mon Sep 17 00:00:00 2001 From: Artur Sawicki Date: Thu, 11 Jul 2024 15:59:40 +0200 Subject: [PATCH 17/17] Fix cortex search service describe --- pkg/sdk/cortex_search_services_def.go | 54 +++++++++++-------- pkg/sdk/cortex_search_services_gen.go | 54 +++++++++++-------- pkg/sdk/cortex_search_services_impl_gen.go | 37 ++++++++----- ...cortex_search_services_integration_test.go | 24 ++++++--- 4 files changed, 103 insertions(+), 66 deletions(-) diff --git a/pkg/sdk/cortex_search_services_def.go b/pkg/sdk/cortex_search_services_def.go index f97a2a6ffc..e59dbebee0 100644 --- a/pkg/sdk/cortex_search_services_def.go +++ b/pkg/sdk/cortex_search_services_def.go @@ -81,29 +81,39 @@ var CortexSearchServiceDef = g.NewInterface( g.DescriptionMappingKindSingleValue, "https://docs.snowflake.com/LIMITEDACCESS/cortex-search/sql/desc-cortex-search", g.DbStruct("cortexSearchServiceDetailsRow"). - Field("name", "string"). - Field("schema", "string"). - Field("database", "string"). - Field("warehouse", "string"). - Field("target_lag", "string"). - Field("search_column", "string"). - OptionalText("included_columns"). - Field("service_url", "string"). - OptionalText("refreshed_on"). - OptionalNumber("num_rows_indexed"). - OptionalText("comment"), + Text("created_on"). + Text("name"). + Text("database_name"). + Text("schema_name"). + Text("target_lag"). + Text("warehouse"). + OptionalText("search_column"). + OptionalText("attribute_columns"). + OptionalText("columns"). + OptionalText("definition"). + OptionalText("comment"). + Text("service_query_url"). + Text("data_timestamp"). + Number("source_data_num_rows"). + Text("indexing_state"). + OptionalText("indexing_error"), g.PlainStruct("CortexSearchServiceDetails"). - Field("Name", "string"). - Field("Schema", "string"). - Field("Database", "string"). - Field("Warehouse", "string"). - Field("TargetLag", "string"). - Field("On", "string"). - Field("Attributes", "[]string"). - Field("ServiceUrl", "string"). - Field("RefreshedOn", "string"). - Field("NumRowsIndexed", "int"). - Field("Comment", "string"), + Text("CreatedOn"). + Text("Name"). + Text("DatabaseName"). + Text("SchemaName"). + Text("TargetLag"). + Text("Warehouse"). + OptionalText("SearchColumn"). + Field("AttributeColumns", "[]string"). + Field("Columns", "[]string"). + OptionalText("Definition"). + OptionalText("Comment"). + Text("ServiceQueryUrl"). + Text("DataTimestamp"). + Number("SourceDataNumRows"). + Text("IndexingState"). + OptionalText("IndexingError"), g.NewQueryStruct("DescribeCortexSearchService"). Describe(). SQL("CORTEX SEARCH SERVICE"). diff --git a/pkg/sdk/cortex_search_services_gen.go b/pkg/sdk/cortex_search_services_gen.go index 2c62f7cd8e..9e76154603 100644 --- a/pkg/sdk/cortex_search_services_gen.go +++ b/pkg/sdk/cortex_search_services_gen.go @@ -83,30 +83,40 @@ type DescribeCortexSearchServiceOptions struct { name SchemaObjectIdentifier `ddl:"identifier"` } type cortexSearchServiceDetailsRow struct { - Name string `db:"name"` - Schema string `db:"schema"` - Database string `db:"database"` - Warehouse string `db:"warehouse"` - TargetLag string `db:"target_lag"` - SearchColumn string `db:"search_column"` - IncludedColumns sql.NullString `db:"included_columns"` - ServiceUrl string `db:"service_url"` - RefreshedOn sql.NullString `db:"refreshed_on"` - NumRowsIndexed sql.NullInt64 `db:"num_rows_indexed"` - Comment sql.NullString `db:"comment"` + CreatedOn string `db:"created_on"` + Name string `db:"name"` + DatabaseName string `db:"database_name"` + SchemaName string `db:"schema_name"` + TargetLag string `db:"target_lag"` + Warehouse string `db:"warehouse"` + SearchColumn sql.NullString `db:"search_column"` + AttributeColumns sql.NullString `db:"attribute_columns"` + Columns sql.NullString `db:"columns"` + Definition sql.NullString `db:"definition"` + Comment sql.NullString `db:"comment"` + ServiceQueryUrl string `db:"service_query_url"` + DataTimestamp string `db:"data_timestamp"` + SourceDataNumRows int `db:"source_data_num_rows"` + IndexingState string `db:"indexing_state"` + IndexingError sql.NullString `db:"indexing_error"` } type CortexSearchServiceDetails struct { - Name string - Schema string - Database string - Warehouse string - TargetLag string - On string - Attributes []string - ServiceUrl string - RefreshedOn string - NumRowsIndexed int - Comment string + CreatedOn string + Name string + DatabaseName string + SchemaName string + TargetLag string + Warehouse string + SearchColumn *string + AttributeColumns []string + Columns []string + Definition *string + Comment *string + ServiceQueryUrl string + DataTimestamp string + SourceDataNumRows int + IndexingState string + IndexingError *string } // DropCortexSearchServiceOptions is based on https://docs.snowflake.com/LIMITEDACCESS/cortex-search/sql/drop-cortex-search. diff --git a/pkg/sdk/cortex_search_services_impl_gen.go b/pkg/sdk/cortex_search_services_impl_gen.go index f88edefe28..e9968da053 100644 --- a/pkg/sdk/cortex_search_services_impl_gen.go +++ b/pkg/sdk/cortex_search_services_impl_gen.go @@ -131,25 +131,34 @@ func (r *DescribeCortexSearchServiceRequest) toOpts() *DescribeCortexSearchServi func (r cortexSearchServiceDetailsRow) convert() *CortexSearchServiceDetails { row := &CortexSearchServiceDetails{ - Name: r.Name, - Schema: r.Schema, - Database: r.Database, - Warehouse: r.Warehouse, - TargetLag: r.TargetLag, - On: r.SearchColumn, - ServiceUrl: r.ServiceUrl, + CreatedOn: r.CreatedOn, + Name: r.Name, + DatabaseName: r.DatabaseName, + SchemaName: r.SchemaName, + TargetLag: r.TargetLag, + Warehouse: r.Warehouse, + ServiceQueryUrl: r.ServiceQueryUrl, + DataTimestamp: r.DataTimestamp, + SourceDataNumRows: r.SourceDataNumRows, + IndexingState: r.IndexingState, } - if r.IncludedColumns.Valid { - row.Attributes = strings.Split(r.IncludedColumns.String, ",") + if r.SearchColumn.Valid { + row.SearchColumn = String(r.SearchColumn.String) } - if r.NumRowsIndexed.Valid { - row.NumRowsIndexed = int(r.NumRowsIndexed.Int64) + if r.AttributeColumns.Valid { + row.AttributeColumns = strings.Split(r.AttributeColumns.String, ",") } - if r.RefreshedOn.Valid { - row.RefreshedOn = r.RefreshedOn.String + if r.Columns.Valid { + row.Columns = strings.Split(r.Columns.String, ",") + } + if r.Definition.Valid { + row.Definition = String(r.Definition.String) } if r.Comment.Valid { - row.Comment = r.Comment.String + row.Comment = String(r.Comment.String) + } + if r.IndexingError.Valid { + row.IndexingError = String(r.IndexingError.String) } return row diff --git a/pkg/sdk/testint/cortex_search_services_integration_test.go b/pkg/sdk/testint/cortex_search_services_integration_test.go index 1200afae2b..6397a86865 100644 --- a/pkg/sdk/testint/cortex_search_services_integration_test.go +++ b/pkg/sdk/testint/cortex_search_services_integration_test.go @@ -76,15 +76,23 @@ func TestInt_CortexSearchServices(t *testing.T) { cortexSearchServiceDetails, err := client.CortexSearchServices.Describe(ctx, cortexSearchService.ID()) require.NoError(t, err) + assert.NotEmpty(t, cortexSearchServiceDetails.CreatedOn) assert.Equal(t, cortexSearchService.Name, cortexSearchServiceDetails.Name) - assert.Equal(t, cortexSearchService.SchemaName, cortexSearchServiceDetails.Schema) - assert.Equal(t, cortexSearchService.DatabaseName, cortexSearchServiceDetails.Database) - assert.NotEmpty(t, cortexSearchServiceDetails.Warehouse) + // Yes, the names are exchanged on purpose, because now it works like this + assert.Equal(t, cortexSearchService.DatabaseName, cortexSearchServiceDetails.SchemaName) + assert.Equal(t, cortexSearchService.SchemaName, cortexSearchServiceDetails.DatabaseName) assert.Equal(t, targetLag, cortexSearchServiceDetails.TargetLag) - assert.Equal(t, strings.ToUpper(on), cortexSearchServiceDetails.On) - assert.NotEmpty(t, cortexSearchServiceDetails.ServiceUrl) - assert.GreaterOrEqual(t, cortexSearchServiceDetails.NumRowsIndexed, 0) - assert.Empty(t, cortexSearchServiceDetails.Comment) + assert.NotEmpty(t, cortexSearchServiceDetails.Warehouse) + assert.Equal(t, strings.ToUpper(on), *cortexSearchServiceDetails.SearchColumn) + assert.NotEmpty(t, cortexSearchServiceDetails.AttributeColumns) + assert.NotEmpty(t, cortexSearchServiceDetails.Columns) + assert.NotEmpty(t, cortexSearchServiceDetails.Definition) + assert.Nil(t, cortexSearchServiceDetails.Comment) + assert.NotEmpty(t, cortexSearchServiceDetails.ServiceQueryUrl) + assert.NotEmpty(t, cortexSearchServiceDetails.DataTimestamp) + assert.GreaterOrEqual(t, cortexSearchServiceDetails.SourceDataNumRows, 0) + assert.NotEmpty(t, cortexSearchServiceDetails.IndexingState) + assert.Empty(t, cortexSearchServiceDetails.IndexingError) }) t.Run("describe: when cortex search service does not exist", func(t *testing.T) { @@ -113,7 +121,7 @@ func TestInt_CortexSearchServices(t *testing.T) { cortexSearchServiceDetails, err := client.CortexSearchServices.Describe(ctx, id) require.NoError(t, err) - require.Equal(t, newComment, cortexSearchServiceDetails.Comment) + require.Equal(t, newComment, *cortexSearchServiceDetails.Comment) require.Equal(t, newTargetLag, cortexSearchServiceDetails.TargetLag) })