Skip to content

Commit

Permalink
Detailed diff tests for properties with dot in the name
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Nov 26, 2024
1 parent 4eeafda commit 4c8c818
Show file tree
Hide file tree
Showing 9 changed files with 415 additions and 13 deletions.
6 changes: 5 additions & 1 deletion pkg/internal/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ func runDiffCheck(t T, tc diffTestCase) crosstestsimpl.DiffResult {

resMap := map[string]*schema.Resource{defRtype: tc.Resource}
tfp := &schema.Provider{ResourcesMap: resMap}
bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, tfp, pulcheck.EnableAccurateBridgePreviews())
opts := []pulcheck.BridgedProviderOpt{}
if os.Getenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW") != "false" {
opts = append(opts, pulcheck.EnableAccurateBridgePreviews())
}
bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, tfp, opts...)
if tc.DeleteBeforeReplace {
bridgedProvider.Resources[defRtype].DeleteBeforeReplace = true
}
Expand Down
134 changes: 122 additions & 12 deletions pkg/internal/tests/cross-tests/diff_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"hash/crc32"
"slices"
"strconv"
"strings"
"testing"

Expand Down Expand Up @@ -1625,6 +1626,14 @@ func TestBlockCollectionElementForceNew(t *testing.T) {
})
}

type diffTestOutput struct {
initialValue cty.Value
changeValue cty.Value
tfOut string
pulumiOut string
detailedDiff map[string]any
}

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

Expand All @@ -1649,14 +1658,6 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
},
}

type testOutput struct {
initialValue cty.Value
changeValue cty.Value
tfOut string
pulumiOut string
detailedDiff map[string]any
}

t.Run("no change", func(t *testing.T) {
t.Parallel()
initialValue := cty.ObjectVal(map[string]cty.Value{})
Expand All @@ -1667,7 +1668,7 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
Config2: changeValue,
})

autogold.ExpectFile(t, testOutput{
autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
Expand All @@ -1686,7 +1687,7 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
Config2: changeValue,
})

autogold.ExpectFile(t, testOutput{
autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
Expand All @@ -1705,7 +1706,7 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
Config2: changeValue,
})

autogold.ExpectFile(t, testOutput{
autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
Expand All @@ -1724,7 +1725,7 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
Config2: changeValue,
})

autogold.ExpectFile(t, testOutput{
autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
Expand All @@ -1733,3 +1734,112 @@ func TestDetailedDiffReplacementComputedProperty(t *testing.T) {
})
})
}

func TestPropertyWithDot(t *testing.T) {
res := &schema.Resource{
Schema: map[string]*schema.Schema{
"prop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"foo": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}

for _, accuratePreivewsEnabled := range []bool{true, false} {
t.Run(fmt.Sprintf("accuratePreivewsEnabled=%v", accuratePreivewsEnabled), func(t *testing.T) {
t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", strconv.FormatBool(accuratePreivewsEnabled))
t.Run("unchanged", func(t *testing.T) {
initialValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"foo.bar": cty.StringVal("baz")}),
}),
})
changeValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"foo.bar": cty.StringVal("baz")}),
}),
})
diff := runDiffCheck(t, diffTestCase{
Resource: res,
Config1: initialValue,
Config2: changeValue,
})

autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
})

t.Run("added", func(t *testing.T) {
initialValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("bar")}),
}),
})
changeValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
"foo.bar": cty.StringVal("baz"),
}),
}),
})
diff := runDiffCheck(t, diffTestCase{
Resource: res,
Config1: initialValue,
Config2: changeValue,
})

autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
})

t.Run("deleted", func(t *testing.T) {
initialValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"foo": cty.StringVal("bar"),
"foo.bar": cty.StringVal("baz"),
}),
}),
})
changeValue := cty.ObjectVal(map[string]cty.Value{
"prop": cty.ListVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{"foo": cty.StringVal("bar")}),
}),
})
diff := runDiffCheck(t, diffTestCase{
Resource: res,
Config1: initialValue,
Config2: changeValue,
})

autogold.ExpectFile(t, diffTestOutput{
initialValue: initialValue,
changeValue: changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
})
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
crosstests.diffTestOutput{
initialValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"prop": {
typeImpl: cty.typeList{ElementTypeT: cty.Type{
typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{
"foo": {typeImpl: cty.primitiveType{
Kind: cty.primitiveTypeKind(83),
}},
}},
}},
}},
}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{"foo": "bar"}}},
},
changeValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{"prop": {typeImpl: cty.typeList{
ElementTypeT: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{
"foo": {
typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)},
},
"foo.bar": {typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)}},
},
}},
}}}}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{
"foo": "bar",
"foo.bar": "baz",
}}},
},
tfOut: `
No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
Resources:
2 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
crosstests.diffTestOutput{
initialValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"prop": {
typeImpl: cty.typeList{ElementTypeT: cty.Type{
typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{
"foo": {typeImpl: cty.primitiveType{
Kind: cty.primitiveTypeKind(83),
}},
"foo.bar": {typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)}},
}},
}},
}},
}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{
"foo": "bar",
"foo.bar": "baz",
}}},
},
changeValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{"prop": {typeImpl: cty.typeList{
ElementTypeT: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"foo": {
typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)},
}},
}},
}}}}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{"foo": "bar"}}},
},
tfOut: `
No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
Resources:
2 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
crosstests.diffTestOutput{
initialValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"prop": {
typeImpl: cty.typeList{ElementTypeT: cty.Type{
typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{
"foo.bar": {typeImpl: cty.primitiveType{
Kind: cty.primitiveTypeKind(83),
}},
}},
}},
}},
}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{"foo.bar": "baz"}}},
},
changeValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{"prop": {typeImpl: cty.typeList{
ElementTypeT: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"foo.bar": {
typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)},
}},
}},
}}}}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{"foo.bar": "baz"}}},
},
tfOut: `
No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
Resources:
2 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
crosstests.diffTestOutput{
initialValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{"prop": {
typeImpl: cty.typeList{ElementTypeT: cty.Type{
typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{
"foo": {typeImpl: cty.primitiveType{
Kind: cty.primitiveTypeKind(83),
}},
}},
}},
}},
}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{"foo": "bar"}}},
},
changeValue: cty.Value{
ty: cty.Type{typeImpl: cty.typeObject{AttrTypes: map[string]cty.Type{"prop": {typeImpl: cty.typeList{
ElementTypeT: cty.Type{typeImpl: cty.typeObject{
AttrTypes: map[string]cty.Type{
"foo": {
typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)},
},
"foo.bar": {typeImpl: cty.primitiveType{Kind: cty.primitiveTypeKind(83)}},
},
}},
}}}}},
v: map[string]interface{}{"prop": []interface{}{map[string]interface{}{
"foo": "bar",
"foo.bar": "baz",
}}},
},
tfOut: `
No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
Resources:
2 unchanged
`,
}
Loading

0 comments on commit 4c8c818

Please sign in to comment.