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

Plugin Framework bridge cross tests #2592

Merged
merged 30 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
dcbe8ba
string attributes
VenelinMartinov Nov 5, 2024
72263b6
set and list tests
VenelinMartinov Nov 5, 2024
a605479
more list tests
VenelinMartinov Nov 5, 2024
44eaede
handle empty slices
VenelinMartinov Nov 5, 2024
0b3e07f
test recording s
VenelinMartinov Nov 5, 2024
c10c0db
rename list test file
VenelinMartinov Nov 6, 2024
5cbe949
rename tests
VenelinMartinov Nov 6, 2024
daa63b2
map and obj schemas
VenelinMartinov Nov 6, 2024
bb1dfe3
list test stubs
VenelinMartinov Nov 6, 2024
de8468b
primitive replace schema
VenelinMartinov Nov 6, 2024
c084144
set test stubs
VenelinMartinov Nov 6, 2024
eae6b87
primitive test stubs
VenelinMartinov Nov 6, 2024
ba5ab37
test plan
VenelinMartinov Nov 6, 2024
075ff48
fix plan
VenelinMartinov Nov 6, 2024
2ea89df
tests without recordings
VenelinMartinov Nov 6, 2024
61e671c
add TODOs
VenelinMartinov Nov 6, 2024
e336ecd
record tests
VenelinMartinov Nov 6, 2024
f9a52df
remove test plan
VenelinMartinov Nov 6, 2024
9922a1c
empty object block tests
VenelinMartinov Nov 7, 2024
7b0743d
address todos
VenelinMartinov Nov 7, 2024
1f98e20
re-record tests with --diff
VenelinMartinov Nov 7, 2024
de0274e
lint
VenelinMartinov Nov 7, 2024
b8fe6a5
add default and planmodifier tests
VenelinMartinov Nov 7, 2024
7735393
object tests with default and plan modifiers
VenelinMartinov Nov 7, 2024
b89219d
update test
VenelinMartinov Nov 7, 2024
6bbe9c7
break test
VenelinMartinov Nov 7, 2024
b6bc19b
Revert "break test"
VenelinMartinov Nov 7, 2024
6d57a22
three element list tests
VenelinMartinov Nov 13, 2024
7d14ed2
update tests
VenelinMartinov Nov 14, 2024
6fda624
add todo for nondeterministic test
VenelinMartinov Nov 14, 2024
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
  •  
  •  
  •  
233 changes: 233 additions & 0 deletions pkg/pf/tests/diff_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package tfbridgetests

import (
"testing"

rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hexops/autogold/v2"
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/cross-tests"
"github.com/zclconf/go-cty/cty"
)

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

attributeSchema := rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.ListAttribute{
Optional: true,
ElementType: types.StringType,
},
},
}

attributeReplaceSchema := rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.ListAttribute{
Optional: true,
ElementType: types.StringType,
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
},
},
}

nestedAttributeSchema := rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.ListNestedAttribute{
Optional: true,
NestedObject: rschema.NestedAttributeObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{Optional: true},
},
},
},
},
}

nestedAttributeReplaceSchema := rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.ListNestedAttribute{
Optional: true,
NestedObject: rschema.NestedAttributeObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{
Optional: true,
},
},
},
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
},
},
}

nestedAttributeNestedReplaceSchema := rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.ListNestedAttribute{
Optional: true,
NestedObject: rschema.NestedAttributeObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
},
},
},
}

blockSchema := rschema.Schema{
Blocks: map[string]rschema.Block{
"key": rschema.ListNestedBlock{
NestedObject: rschema.NestedBlockObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{Optional: true},
},
},
},
},
}

blockReplaceSchema := rschema.Schema{
Blocks: map[string]rschema.Block{
"key": rschema.ListNestedBlock{
NestedObject: rschema.NestedBlockObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{Optional: true},
},
},
PlanModifiers: []planmodifier.List{
listplanmodifier.RequiresReplace(),
},
},
},
}

blockNestedReplaceSchema := rschema.Schema{
Blocks: map[string]rschema.Block{
"key": rschema.ListNestedBlock{
NestedObject: rschema.NestedBlockObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
},
},
},
},
}

attrList := func(arr *[]string) cty.Value {
if arr == nil {
return cty.NullVal(cty.DynamicPseudoType)
}
slice := make([]cty.Value, len(*arr))
for i, v := range *arr {
slice[i] = cty.StringVal(v)
}
if len(slice) == 0 {
return cty.ListValEmpty(cty.String)
}
return cty.ListVal(slice)
}

nestedAttrList := func(arr *[]string) cty.Value {
if arr == nil {
return cty.NullVal(cty.DynamicPseudoType)
}
slice := make([]cty.Value, len(*arr))
for i, v := range *arr {
slice[i] = cty.ObjectVal(
map[string]cty.Value{
"nested": cty.StringVal(v),
},
)
}
if len(slice) == 0 {
return cty.ListValEmpty(cty.Object(map[string]cty.Type{"nested": cty.String}))
}
return cty.ListVal(slice)
}

schemaValueMakerPairs := []struct {
name string
schema rschema.Schema
valueMaker func(*[]string) cty.Value
}{
{"attribute no replace", attributeSchema, attrList},
{"attribute requires replace", attributeReplaceSchema, attrList},
{"nested attribute no replace", nestedAttributeSchema, nestedAttrList},
{"nested attribute requires replace", nestedAttributeReplaceSchema, nestedAttrList},
{"nested attribute nested requires replace", nestedAttributeNestedReplaceSchema, nestedAttrList},
{"block no replace", blockSchema, nestedAttrList},
{"block requires replace", blockReplaceSchema, nestedAttrList},
{"block nested requires replace", blockNestedReplaceSchema, nestedAttrList},
}

scenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
{"unchanged non-empty", &[]string{"value"}, &[]string{"value"}},
VenelinMartinov marked this conversation as resolved.
Show resolved Hide resolved
{"changed non-empty", &[]string{"value"}, &[]string{"value1"}},
{"added", &[]string{}, &[]string{"value"}},
{"removed", &[]string{"value"}, &[]string{}},
{"null unchanged", nil, nil},
{"null to non-null", nil, &[]string{"value"}},
{"non-null to null", &[]string{"value"}, nil},
{"changed null to empty", nil, &[]string{}},
{"changed empty to null", &[]string{}, nil},
{"element added", &[]string{"value"}, &[]string{"value", "value1"}},
{"element removed", &[]string{"value", "value1"}, &[]string{"value"}},
{"removed front", &[]string{"val1", "val2", "val3"}, &[]string{"val2", "val3"}},
{"removed middle", &[]string{"val1", "val2", "val3"}, &[]string{"val1", "val3"}},
{"removed end", &[]string{"val1", "val2", "val3"}, &[]string{"val1", "val2"}},
{"added front", &[]string{"val2", "val3"}, &[]string{"val1", "val2", "val3"}},
{"added middle", &[]string{"val1", "val3"}, &[]string{"val1", "val2", "val3"}},
{"added end", &[]string{"val1", "val2"}, &[]string{"val1", "val2", "val3"}},
}

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

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

diff := crosstests.Diff(
t, schemaValueMakerPair.schema, map[string]cty.Value{"key": initialValue}, map[string]cty.Value{"key": changeValue})

autogold.ExpectFile(t, testOutput{
initialValue: scenario.initialValue,
changeValue: scenario.changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
})
})
}
})
}
}
Loading