Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproduce aws-3880 in a test #1917

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/tests/cross-tests/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,23 @@ func (ta *typeAdapter) NewValue(value any) tftypes.Value {
switch v := value.(type) {
case map[string]any:
values := map[string]tftypes.Value{}
for k, el := range v {
values[k] = fromType(aT[k]).NewValue(el)
for key, expectedType := range aT {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted that tftypes.Value representation insists (via panics) that every attribute has an entry, even if it's a nil entry. It also insisted on no optional attributes. This is now the case in this adapter, it frees the test writer from writing explicit nulls for attributes that do not matter.

if vk, ok := v[key]; ok {
values[key] = fromType(expectedType).NewValue(vk)
} else {
values[key] = tftypes.NewValue(expectedType, nil)
}
}
// Could detect and warn on extraneous keys here but do not do that for now.
return tftypes.NewValue(t, values)
}
}
return tftypes.NewValue(t, value)
}

func fromType(t tftypes.Type) *typeAdapter {
return &typeAdapter{t}
contract.Assertf(t != nil, "t cannot be nil here")
return &typeAdapter{typ: t}
}

type valueAdapter struct {
Expand Down
57 changes: 57 additions & 0 deletions pkg/tests/cross-tests/cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
webaclschema "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/internal/webaclschema"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -403,3 +404,59 @@ func TestAws2442(t *testing.T) {
Config2: cfg2,
})
}

func TestAws3880(t *testing.T) {
cfg := map[string]any{
"scope": "REGIONAL",
"name": "autogenerated-name",
"default_action": []any{
map[string]any{
"allow": []any{map[string]any{}},
},
},
"visibility_config": []any{
map[string]any{
"cloudwatch_metrics_enabled": true,
"metric_name": "myWebAclMetrics",
"sampled_requests_enabled": false,
},
},
"rule": []any{
map[string]any{
"action": []any{
map[string]any{
"block": []any{map[string]any{}},
},
},
"name": "IPAllowRule",
"priority": 0,
"statement": []any{
map[string]any{
"ip_set_reference_statement": []any{map[string]any{
"arn": "some-arn",
}},
},
},
"visibility_config": []any{
map[string]any{
"cloudwatch_metrics_enabled": true,
"metric_name": "IPAllowRule",
"sampled_requests_enabled": true,
},
},
},
},
}

resource := webaclschema.ResourceWebACL()
resource.Schema = resource.SchemaFunc()
resource.SchemaFunc = nil
delete(resource.Schema, "tags")
delete(resource.Schema, "tags_all")

runDiffCheck(t, diffTestCase{
Resource: resource,
Config1: cfg,
Config2: cfg,
})
}
5 changes: 3 additions & 2 deletions pkg/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func runDiffCheck(t T, tc diffTestCase) {
tfwd := t.TempDir()

tfd := newTfDriver(t, tfwd, providerShortName, rtype, tc.Resource)
_ = tfd.writePlanApply(t, tc.Resource.Schema, rtype, "example", tc.Config1)
tfDiffPlan := tfd.writePlanApply(t, tc.Resource.Schema, rtype, "example", tc.Config2)
_ = tfd.writePlanApply(t, tc.Resource.SchemaMap(), rtype, "example", tc.Config1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resource may specify Schema or SchemaFunc, and SchemaMap() normalizes to access either, this is the way.

tfDiffPlan := tfd.writePlanApply(t, tc.Resource.SchemaMap(), rtype, "example", tc.Config2)

tfp := &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -102,6 +102,7 @@ func runDiffCheck(t T, tc diffTestCase) {
x := pt.Up()

tfAction := tfd.parseChangesFromTFPlan(*tfDiffPlan)
t.Logf("Terraform decided to take the %q action", tfAction)

tc.verifyBasicDiffAgreement(t, tfAction, x.Summary)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/tests/cross-tests/tf_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (d *tfDriver) coalesce(t T, x any) *tftypes.Value {
if x == nil {
return nil
}
objectType := convert.InferObjectType(sdkv2.NewSchemaMap(d.res.Schema), nil)
t.Logf("infer object type: %v", objectType)
objectType := convert.InferObjectType(sdkv2.NewSchemaMap(d.res.SchemaMap()), nil)
v := fromType(objectType).NewValue(x)
return &v
}
Expand Down
16 changes: 10 additions & 6 deletions pkg/tests/cross-tests/tfwrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ func writeBlock(body *hclwrite.Body, schemas map[string]*schema.Schema, values m
if sch.Type == schema.TypeMap {
body.SetAttributeValue(key, value)
} else if sch.Type == schema.TypeSet {
for _, v := range value.AsValueSet().Values() {
newBlock := body.AppendNewBlock(key, nil)
writeBlock(newBlock.Body(), elem.Schema, v.AsValueMap())
if !value.IsNull() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tolerate missing values encoded by nulls - as required by tftypes.Value (and translated to cty.Value).

for _, v := range value.AsValueSet().Values() {
newBlock := body.AppendNewBlock(key, nil)
writeBlock(newBlock.Body(), elem.Schema, v.AsValueMap())
}
}
} else if sch.Type == schema.TypeList {
for _, v := range value.AsValueSlice() {
newBlock := body.AppendNewBlock(key, nil)
writeBlock(newBlock.Body(), elem.Schema, v.AsValueMap())
if !value.IsNull() {
for _, v := range value.AsValueSlice() {
newBlock := body.AppendNewBlock(key, nil)
writeBlock(newBlock.Body(), elem.Schema, v.AsValueMap())
}
}
} else {
contract.Failf("unexpected schema type %v", sch.Type)
Expand Down
54 changes: 25 additions & 29 deletions pkg/tests/internal/webaclschema/webacl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
package wafv2

import (
"bytes"
"context"
"fmt"
"hash/crc32"
"regexp"
"strings"

Expand All @@ -18,17 +16,17 @@ import (
)

func ResourceWebACL() *schema.Resource {
hashcodeString := func(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
if -v >= 0 {
return -v
}
// v == MinInt
return 0
}
// hashcodeString := func(s string) int {
// v := int(crc32.ChecksumIEEE([]byte(s)))
// if v >= 0 {
// return v
// }
// if -v >= 0 {
// return -v
// }
// // v == MinInt
// return 0
// }

ruleElement := &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -138,20 +136,20 @@ func ResourceWebACL() *schema.Resource {
},
"rule": {
Type: schema.TypeSet,
Set: func(v interface{}) int {
var buf bytes.Buffer
schema.SerializeResourceForHash(&buf, v, ruleElement)
// before := "action:(<allow:(<custom_request_handling:();>;);"
// after := "action:(<allow:(<>;);"
s := buf.String()
//s = strings.ReplaceAll(s, before, after)
n := hashcodeString(s)
if 1+2 == 18 {
fmt.Printf("PRE-HASH:\n%s\n\n", s)
fmt.Printf("HASHED: %d\n", n)
}
return n
},
// Set: func(v interface{}) int {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was really here for debugging only. The original in AWS does not specify a custom Set.

// var buf bytes.Buffer
// schema.SerializeResourceForHash(&buf, v, ruleElement)
// // before := "action:(<allow:(<custom_request_handling:();>;);"
// // after := "action:(<allow:(<>;);"
// s := buf.String()
// //s = strings.ReplaceAll(s, before, after)
// n := hashcodeString(s)
// if 1+2 == 18 {
// fmt.Printf("PRE-HASH:\n%s\n\n", s)
// fmt.Printf("HASHED: %d\n", n)
// }
// return n
// },
Optional: true,
Elem: ruleElement,
},
Expand All @@ -161,8 +159,6 @@ func ResourceWebACL() *schema.Resource {
ForceNew: true,
//ValidateFunc: validation.StringInSlice(wafv2.Scope_Values(), false),
},
// names.AttrTags: tftags.TagsSchema(),
// names.AttrTagsAll: tftags.TagsSchemaTrulyComputed(),
"token_domains": {
Type: schema.TypeSet,
Optional: true,
Expand Down
Loading