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

Refactor SDKv2 non-set detailed diff tests #2725

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion pkg/internal/tests/pulcheck/pulcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func propNeedsUpdate(prop *schema.Schema) bool {
return true
}

// resourceNeedsUpdate returns true if the TF SDK schema validation would consider the
// resource to need an update method.
func resourceNeedsUpdate(res *schema.Resource) bool {
// If any of the properties need an update, then the resource needs an update.
for _, s := range res.Schema {
Expand All @@ -53,6 +55,26 @@ func resourceNeedsUpdate(res *schema.Resource) bool {
// This is an experimental API.
func EnsureProviderValid(t T, tfp *schema.Provider) {
for _, r := range tfp.ResourcesMap {
if r.Schema["id"] == nil {
r.Schema["id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
}

// If the resource will be flagged as not needing an Update method by the TF SDK schema
// validation then add a no-op update_prop property that will instead force the resource
// to need an Update method.
// This is necessary to work around a bug in the TF SDK schema validation where some resources which
// do need an Update method will instead be flagged as not needing an Update method.
// See https://github.com/pulumi/pulumi-terraform-bridge/pull/2723#issuecomment-2541518646
if !resourceNeedsUpdate(r) {
r.Schema["update_prop"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
}
}

if r.ReadContext == nil {
r.ReadContext = func(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
Expand All @@ -75,7 +97,9 @@ func EnsureProviderValid(t T, tfp *schema.Provider) {
}
}

if resourceNeedsUpdate(r) && r.UpdateContext == nil {
// Because of the no-op update_prop property added above, all resources will now
// need an Update method.
if r.UpdateContext == nil {
r.UpdateContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
Expand Down
221 changes: 221 additions & 0 deletions pkg/tests/detailed_diff_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package tests

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hexops/autogold/v2"
"github.com/zclconf/go-cty/cty"

crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
)

func TestDetailedDiffList(t *testing.T) {
t.Parallel()

listAttrSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}

maxItemsOneAttrSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}

listBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prop": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}

maxItemsOneBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nested_prop": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}

attrList := func(arr *[]string) map[string]cty.Value {
if arr == nil {
return map[string]cty.Value{}
}

if len(*arr) == 0 {
return map[string]cty.Value{
"list_attr": cty.ListValEmpty(cty.String),
}
}

slice := make([]cty.Value, len(*arr))
for i, v := range *arr {
slice[i] = cty.StringVal(v)
}
return map[string]cty.Value{
"list_attr": cty.ListVal(slice),
}
}

blockList := func(arr *[]string) map[string]cty.Value {
if arr == nil {
return map[string]cty.Value{}
}

if len(*arr) == 0 {
return map[string]cty.Value{
"list_block": cty.ListValEmpty(cty.DynamicPseudoType),
}
}

slice := make([]cty.Value, len(*arr))
for i, v := range *arr {
slice[i] = cty.ObjectVal(map[string]cty.Value{"prop": cty.StringVal(v)})
}
return map[string]cty.Value{
"list_block": cty.ListVal(slice),
}
}

nestedBlockList := func(arr *[]string) map[string]cty.Value {
if arr == nil {
return map[string]cty.Value{}
}

if len(*arr) == 0 {
return map[string]cty.Value{
"list_block": cty.ListValEmpty(cty.DynamicPseudoType),
}
}

slice := make([]cty.Value, len(*arr))
for i, v := range *arr {
slice[i] = cty.ObjectVal(map[string]cty.Value{"nested_prop": cty.StringVal(v)})
}
return map[string]cty.Value{
"list_block": cty.ListVal(slice),
}
}

listPairs := []struct {
name string
schema schema.Resource
valueMaker func(*[]string) map[string]cty.Value
}{
{"list attribute", listAttrSchema, attrList},
{"list block", listBlockSchema, blockList},
}

maxItemsOnePairs := []struct {
name string
schema schema.Resource
valueMaker func(*[]string) map[string]cty.Value
}{
{"max items one attribute", maxItemsOneAttrSchema, attrList},
{"max items one block", maxItemsOneBlockSchema, nestedBlockList},
}

oneElementScenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
{"unchanged empty", nil, nil},
{"unchanged non-empty", ref([]string{"val1"}), ref([]string{"val1"})},
{"added non-empty", nil, ref([]string{"val1"})},
{"added empty", nil, ref([]string{})},
{"removed non-empty", ref([]string{"val1"}), nil},
{"removed empty", ref([]string{}), nil},
{"changed", ref([]string{"val1"}), ref([]string{"val2"})},
}

multiElementScenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
{"list element added front", ref([]string{"val2", "val3"}), ref([]string{"val1", "val2", "val3"})},
{"list element added back", ref([]string{"val1", "val2"}), ref([]string{"val1", "val2", "val3"})},
{"list element added middle", ref([]string{"val1", "val3"}), ref([]string{"val1", "val2", "val3"})},
{"list element removed front", ref([]string{"val1", "val2", "val3"}), ref([]string{"val3", "val2"})},
{"list element removed middle", ref([]string{"val1", "val2", "val3"}), ref([]string{"val3", "val1"})},
{"list element removed end", ref([]string{"val1", "val2", "val3"}), ref([]string{"val2", "val1"})},
}

scenarios := append(oneElementScenarios, multiElementScenarios...)

type testOutput struct {
initialValue *[]string
changeValue *[]string
tfOut string
pulumiOut string
detailedDiff map[string]any
}

runTest := func(t *testing.T, schema schema.Resource, valueMaker func(*[]string) map[string]cty.Value, initialValue *[]string, changeValue *[]string) {
diff := crosstests.Diff(t, &schema, valueMaker(initialValue), valueMaker(changeValue))
autogold.ExpectFile(t, testOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
}

for _, schemaValueMakerPair := range listPairs {
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
t.Parallel()
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
runTest(t, schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker, scenario.initialValue, scenario.changeValue)
})
}
})
}

for _, schemaValueMakerPair := range maxItemsOnePairs {
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
t.Parallel()
for _, scenario := range oneElementScenarios {
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
runTest(t, schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker, scenario.initialValue, scenario.changeValue)
})
}
})
}
}
Loading
Loading