Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Sep 9, 2024
1 parent 9d69de8 commit 048b510
Show file tree
Hide file tree
Showing 19 changed files with 869 additions and 356 deletions.
4 changes: 2 additions & 2 deletions pkg/acceptance/bettertestspoc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func (w *WarehouseModel) WithWarehouseSizeEnum(warehouseSize sdk.WarehouseSize)
Each of the above assertion types/config models has its own generator and cleanup entry in our Makefile.
You can generate config models with:
```shell
make clean-resource-model-builder generate-resource-model-builder
make clean-resource-model-builders generate-resource-model-builders
```

You can use cli flags:
```shell
make clean-resource-model-builder generate-resource-model-builder SF_TF_GENERATOR_ARGS='--dry-run --verbose'
make clean-resource-model-builders generate-resource-model-builders SF_TF_GENERATOR_ARGS='--dry-run --verbose'
```

To clean/generate all from this package run
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions pkg/acceptance/bettertestspoc/assert/resource_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func NewDatasourceAssert(name string, prefix string, additionalPrefix string) *R
type resourceAssertionType string

const (
resourceAssertionTypeValuePresent = "VALUE_PRESENT"
resourceAssertionTypeValueSet = "VALUE_SET"
resourceAssertionTypeValueNotSet = "VALUE_NOT_SET"
resourceAssertionTypeValuePresent = "VALUE_PRESENT"
)

type ResourceAssertion struct {
Expand All @@ -75,6 +75,10 @@ func (r *ResourceAssert) AddAssertion(assertion ResourceAssertion) {
r.assertions = append(r.assertions, assertion)
}

func ValuePresent(fieldName string) ResourceAssertion {
return ResourceAssertion{fieldName: fieldName, resourceAssertionType: resourceAssertionTypeValuePresent}
}

func ValueSet(fieldName string, expected string) ResourceAssertion {
return ResourceAssertion{fieldName: fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet}
}
Expand Down Expand Up @@ -105,7 +109,10 @@ func ResourceShowOutputValueSet(fieldName string, expected string) ResourceAsser
return ResourceAssertion{fieldName: showOutputPrefix + fieldName, expectedValue: expected, resourceAssertionType: resourceAssertionTypeValueSet}
}

// TODO [SNOW-1501905]: generate assertions with resourceAssertionTypeValuePresent
func ResourceShowOutputValueNotSet(fieldName string) ResourceAssertion {
return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValueNotSet}
}

func ResourceShowOutputValuePresent(fieldName string) ResourceAssertion {
return ResourceAssertion{fieldName: showOutputPrefix + fieldName, resourceAssertionType: resourceAssertionTypeValuePresent}
}
Expand Down Expand Up @@ -181,9 +188,13 @@ func (r *ResourceAssert) ToTerraformImportStateCheckFunc(t *testing.T) resource.
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")
if err := importchecks.TestCheckResourceAttrNotInInstanceState(r.id, a.fieldName)(s); err != nil {
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 resourceAssertionTypeValuePresent:
panic("implement")
if err := importchecks.TestCheckResourceAttrInstanceStateSet(r.id, a.fieldName)(s); err != nil {
result = append(result, fmt.Errorf("%s %s assertion [%d/%d]: failed with error: %w", r.id, r.prefix, i+1, len(r.assertions), err))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
package resourceassert

import (
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"strconv"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"
)

func (r *ResourceMonitorResourceAssert) HasStartTimestampNotEmpty() *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValuePresent("start_timestamp"))
return r
}

func (r *ResourceMonitorResourceAssert) HasEndTimestampNotEmpty() *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValuePresent("end_timestamp"))
return r
}

func (r *ResourceMonitorResourceAssert) HasNotifyUsersLen(len int) *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValueSet("notify_users.#", strconv.FormatInt(int64(len), 10)))
return r
}

func (r *ResourceMonitorResourceAssert) HasNotifyUser(index int, userName string) *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValueSet(fmt.Sprintf("notify_users.%d", index), userName))
return r
}

func (r *ResourceMonitorResourceAssert) HasTriggerLen(len int) *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValueSet("trigger.#", strconv.FormatInt(int64(len), 10)))
return r
}

func (r *ResourceMonitorResourceAssert) HasTrigger(index int, threshold int, action sdk.TriggerAction) *ResourceMonitorResourceAssert {
r.AddAssertion(assert.ValueSet(fmt.Sprintf("trigger.%d.threshold", index), strconv.FormatInt(int64(threshold), 10)))
r.AddAssertion(assert.ValueSet(fmt.Sprintf("trigger.%d.on_threshold_reached", index), string(action)))
return r
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package resourceshowoutputassert

import "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert"

func (r *ResourceMonitorShowOutputAssert) HasStartTimeNotEmpty() *ResourceMonitorShowOutputAssert {
r.AddAssertion(assert.ResourceShowOutputValuePresent("start_time"))
return r
}

func (r *ResourceMonitorShowOutputAssert) HasEndTimeNotEmpty() *ResourceMonitorShowOutputAssert {
r.AddAssertion(assert.ResourceShowOutputValuePresent("end_time"))
return r
}

func (r *ResourceMonitorShowOutputAssert) HasCreatedOnNotEmpty() *ResourceMonitorShowOutputAssert {
r.AddAssertion(assert.ResourceShowOutputValuePresent("created_on"))
return r
}

func (r *ResourceMonitorShowOutputAssert) HasOwnerNotEmpty() *ResourceMonitorShowOutputAssert {
r.AddAssertion(assert.ResourceShowOutputValuePresent("owner"))
return r
}
3 changes: 2 additions & 1 deletion pkg/acceptance/bettertestspoc/config/model/gen/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func ModelFromResourceSchemaDetails(resourceSchemaDetails genhelpers.ResourceSch
Name: resourceSchemaDetails.ObjectName(),
Attributes: attributes,
PreambleModel: PreambleModel{
PackageName: packageWithGenerateDirective,
PackageName: packageWithGenerateDirective,
AdditionalStandardImports: []string{"reflect", "strings"},
},
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,19 @@ func {{ .Name }}WithDefaultMeta(
{{- end -}}
return {{ $modelVar }}
}

func (r *{{ $modelName }}) ToConfigVariables() tfconfig.Variables {
variables := make(tfconfig.Variables)
rType := reflect.TypeOf(r).Elem()
rValue := reflect.ValueOf(r).Elem()
for i := 0; i < rType.NumField(); i++ {
field := rType.Field(i)
if jsonTag, ok := field.Tag.Lookup("json"); ok {
name := strings.Split(jsonTag, ",")[0]
if fieldValue, ok := rValue.Field(i).Interface().(tfconfig.Variable); ok {
variables[name] = fieldValue
}
}
}
return variables
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/acceptance/bettertestspoc/config/model/user_model_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions pkg/acceptance/bettertestspoc/config/model/warehouse_model_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 048b510

Please sign in to comment.