Skip to content

Commit

Permalink
Refactor Set and List Diff tests to use generic utilities (#2837)
Browse files Browse the repository at this point in the history
This PR refactors the Set and List Diff tests to use the generic diff
utility functions and types introduced in
#2829. This should
make the tests more maintainable and more in-line with what other tests
do.


[861ec1f](861ec1f)
contains the changes:
- Refactors the tests to use the generic `diffSchemaValueMakerPair` and
`diffScenario` types for their tests.
- Refactors the tests to use `prop` for top-level properties and
`nested_prop` for nested ones, like the rest of the tests.
- Refactors the tests to use the generic `runSDKv2TestMatrix` test
function.
- Refactors the `valueMaker` functions from `value_makers.go` to return
a `map[string]cty.Value ` instead of a `cty.Value` which needs to be
transformed further after.
- Corrects three tests in `list element removed`, where the order of the
elements was wrong in the changed values.



[f800d2d](f800d2d)
contains the test recordings.

Related to #2788
Related to #2789
  • Loading branch information
VenelinMartinov authored Jan 17, 2025
1 parent ec61116 commit 413419b
Show file tree
Hide file tree
Showing 755 changed files with 8,897 additions and 8,306 deletions.
182 changes: 35 additions & 147 deletions pkg/tests/diff_test/detailed_diff_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ 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 TestSDKv2DetailedDiffList(t *testing.T) {
t.Parallel()

listAttrSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
"prop": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -26,7 +22,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listAttrSchemaForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
"prop": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Expand All @@ -37,7 +33,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

maxItemsOneAttrSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand All @@ -48,7 +44,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

maxItemsOneAttrSchemaForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand All @@ -60,12 +56,12 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prop": {
"nested_prop": {
Type: schema.TypeString,
Optional: true,
},
Expand All @@ -77,13 +73,13 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listBlockSchemaForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"prop": {
"nested_prop": {
Type: schema.TypeString,
Optional: true,
},
Expand All @@ -95,7 +91,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listBlockSchemaNestedForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Expand All @@ -113,7 +109,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

maxItemsOneBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand All @@ -131,7 +127,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

maxItemsOneBlockSchemaForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand All @@ -150,7 +146,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

maxItemsOneBlockSchemaNestedForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Expand All @@ -169,7 +165,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listBlockSchemaSensitive := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
Sensitive: true,
Expand All @@ -187,7 +183,7 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

listBlockSchemaNestedSensitive := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
"prop": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Expand All @@ -203,97 +199,25 @@ func TestSDKv2DetailedDiffList(t *testing.T) {
},
}

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 attribute force new", listAttrSchemaForceNew, attrList},
{"list block", listBlockSchema, blockList},
{"list block force new", listBlockSchemaForceNew, blockList},
{"list block nested force new", listBlockSchemaNestedForceNew, blockList},
{"list block sensitive", listBlockSchemaSensitive, blockList},
{"list block nested sensitive", listBlockSchemaNestedSensitive, nestedBlockList},
listPairs := []diffSchemaValueMakerPair[[]string]{
{"list attribute", listAttrSchema, listValueMaker},
{"list attribute force new", listAttrSchemaForceNew, listValueMaker},
{"list block", listBlockSchema, nestedListValueMaker},
{"list block force new", listBlockSchemaForceNew, nestedListValueMaker},
{"list block nested force new", listBlockSchemaNestedForceNew, nestedListValueMaker},
{"list block sensitive", listBlockSchemaSensitive, nestedListValueMaker},
{"list block nested sensitive", listBlockSchemaNestedSensitive, nestedListValueMaker},
}

maxItemsOnePairs := []struct {
name string
schema schema.Resource
valueMaker func(*[]string) map[string]cty.Value
}{
{"max items one attribute", maxItemsOneAttrSchema, attrList},
{"max items one attribute force new", maxItemsOneAttrSchemaForceNew, attrList},
{"max items one block", maxItemsOneBlockSchema, nestedBlockList},
{"max items one block force new", maxItemsOneBlockSchemaForceNew, nestedBlockList},
{"max items one block nested force new", maxItemsOneBlockSchemaNestedForceNew, nestedBlockList},
maxItemsOnePairs := []diffSchemaValueMakerPair[[]string]{
{"max items one attribute", maxItemsOneAttrSchema, listValueMaker},
{"max items one attribute force new", maxItemsOneAttrSchemaForceNew, listValueMaker},
{"max items one block", maxItemsOneBlockSchema, nestedListValueMaker},
{"max items one block force new", maxItemsOneBlockSchemaForceNew, nestedListValueMaker},
{"max items one block nested force new", maxItemsOneBlockSchemaNestedForceNew, nestedListValueMaker},
}

oneElementScenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
oneElementScenarios := []diffScenario[[]string]{
{"unchanged empty", nil, nil},
{"unchanged non-empty", ref([]string{"val1"}), ref([]string{"val1"})},
{"added non-empty", nil, ref([]string{"val1"})},
Expand All @@ -311,17 +235,13 @@ func TestSDKv2DetailedDiffList(t *testing.T) {
longListAddedBack = append(longListAddedBack, "value20")
longListAddedFront := append([]string{"value20"}, *longList...)

multiElementScenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
multiElementScenarios := []diffScenario[[]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"})},
{"list element removed front", ref([]string{"val1", "val2", "val3"}), ref([]string{"val2", "val3"})},
{"list element removed middle", ref([]string{"val1", "val2", "val3"}), ref([]string{"val1", "val3"})},
{"list element removed end", ref([]string{"val1", "val2", "val3"}), ref([]string{"val1", "val2"})},
{"one added, one removed", ref([]string{"val1", "val2", "val3"}), ref([]string{"val2", "val3", "val4"})},
{"long list added back", longList, &longListAddedBack},
// TODO[pulumi/pulumi-terraform-bridge#2239]: These cases present as multiple changes instead of just one
Expand All @@ -332,38 +252,6 @@ func TestSDKv2DetailedDiffList(t *testing.T) {

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

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)
})
}
})
}
runSDKv2TestMatrix(t, listPairs, scenarios)
runSDKv2TestMatrix(t, maxItemsOnePairs, oneElementScenarios)
}
Loading

0 comments on commit 413419b

Please sign in to comment.