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

Add SDKv2 Detailed Diff tests for Computed properties in sets #2740

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
123 changes: 123 additions & 0 deletions pkg/tests/detailed_diff_list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -23,6 +24,17 @@ func TestDetailedDiffList(t *testing.T) {
},
}

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

maxItemsOneAttrSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_attr": {
Expand All @@ -34,6 +46,18 @@ func TestDetailedDiffList(t *testing.T) {
},
}

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

listBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
Expand All @@ -51,6 +75,43 @@ func TestDetailedDiffList(t *testing.T) {
},
}

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

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

maxItemsOneBlockSchema := schema.Resource{
Schema: map[string]*schema.Schema{
"list_block": {
Expand All @@ -69,6 +130,46 @@ func TestDetailedDiffList(t *testing.T) {
},
}

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

maxItemsOneBlockSchemaNestedForceNew := 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,
ForceNew: true,
},
},
},
},
},
}
_ = maxItemsOneBlockSchemaNestedForceNew

attrList := func(arr *[]string) map[string]cty.Value {
if arr == nil {
return map[string]cty.Value{}
Expand Down Expand Up @@ -135,7 +236,11 @@ func TestDetailedDiffList(t *testing.T) {
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},
// TODO[pulumi/pulumi-terraform-bridge#2726]: These tests fail to produce the correct replacement plan
// {"list block nested force new", listBlockSchemaNestedForceNew, blockList},
}

maxItemsOnePairs := []struct {
Expand All @@ -144,7 +249,11 @@ func TestDetailedDiffList(t *testing.T) {
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},
// TODO[pulumi/pulumi-terraform-bridge#2726]: These tests fail to produce the correct replacement plan
// {"max items one block force new", maxItemsOneBlockSchemaForceNew, nestedBlockList},
// {"max items one block nested force new", maxItemsOneBlockSchemaNestedForceNew, nestedBlockList},
}

oneElementScenarios := []struct {
Expand All @@ -161,6 +270,14 @@ func TestDetailedDiffList(t *testing.T) {
{"changed", ref([]string{"val1"}), ref([]string{"val2"})},
}

longList := &[]string{}
for i := 0; i < 20; i++ {
*longList = append(*longList, fmt.Sprintf("value%d", i))
}
longListAddedBack := append([]string{}, *longList...)
longListAddedBack = append(longListAddedBack, "value20")
longListAddedFront := append([]string{"value20"}, *longList...)

multiElementScenarios := []struct {
name string
initialValue *[]string
Expand All @@ -172,6 +289,12 @@ func TestDetailedDiffList(t *testing.T) {
{"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"})},
{"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
{"long list added front", longList, &longListAddedFront},
{"long list removed front", &longListAddedFront, longList},
{"long list removed back", &longListAddedBack, longList},
}

scenarios := append(oneElementScenarios, multiElementScenarios...)
Expand Down
Loading
Loading