Skip to content

Commit

Permalink
SDKv2 Diff tests for Map (#2830)
Browse files Browse the repository at this point in the history
This PR finishes the coverage of the SDKv2 bridge for Diff for Map
types.


[f2a0c1c](f2a0c1c)
contains the test recordings

fixes #2787
  • Loading branch information
VenelinMartinov authored Jan 15, 2025
1 parent 9660285 commit 8bb538e
Show file tree
Hide file tree
Showing 51 changed files with 1,435 additions and 96 deletions.
64 changes: 16 additions & 48 deletions pkg/tests/diff_test/detailed_diff_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,34 @@ 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 TestSDKv2DetailedDiffMap(t *testing.T) {
t.Parallel()

res := schema.Resource{
Schema: map[string]*schema.Schema{
"map_prop": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}

ctyVal := func(v map[string]string) map[string]cty.Value {
ctyMap := make(map[string]cty.Value)

ctyMaker := func(v map[string]string) cty.Value {
if len(v) == 0 {
return map[string]cty.Value{
"map_prop": cty.MapValEmpty(cty.String),
}
return cty.MapValEmpty(cty.String)
}

ctyMap := make(map[string]cty.Value)
for k, v := range v {
ctyMap[k] = cty.StringVal(v)
}
return map[string]cty.Value{
"map_prop": cty.MapVal(ctyMap),
}
return cty.MapVal(ctyMap)
}

scenarios := []struct {
name string
initialValue map[string]string
changeValue map[string]string
}{
{"unchanged empty", map[string]string{}, map[string]string{}},
{"unchanged non-empty", map[string]string{"key": "val"}, map[string]string{"key": "val"}},
{"added", map[string]string{}, map[string]string{"key": "val"}},
{"removed", map[string]string{"key": "val"}, map[string]string{}},
{"value changed", map[string]string{"key": "val"}, map[string]string{"key": "val2"}},
{"key changed", map[string]string{"key": "val"}, map[string]string{"key2": "val"}},
}
var nilVal map[string]string
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeMap, &schema.Schema{Type: schema.TypeString}, ctyMaker,
map[string]string{"key": "val1"}, map[string]string{"key": "val2"},
map[string]string{"key": "computedVal"}, map[string]string{"key": "defaultVal"}, nilVal)

for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
diff := crosstests.Diff(t, &res, ctyVal(scenario.initialValue), ctyVal(scenario.changeValue))
autogold.ExpectFile(t, testOutput{
initialValue: scenario.initialValue,
changeValue: scenario.changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
})
}
scenarios = append(scenarios, diffScenario[map[string]string]{
name: "key changed",
initialValue: ref(map[string]string{"key": "val1"}),
changeValue: ref(map[string]string{"key2": "val1"}),
})

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
8 changes: 4 additions & 4 deletions pkg/tests/diff_test/detailed_diff_primitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {

var nilVal string
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
schema.TypeString, nil, cty.StringVal, "val1", "val2", "computed", "default", nilVal)

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
Expand All @@ -22,7 +22,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) {

var nilVal bool
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
schema.TypeBool, nil, cty.BoolVal, true, false, true, false, nilVal)

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
Expand All @@ -32,7 +32,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) {

var nilVal int64
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
schema.TypeInt, nil, cty.NumberIntVal, 1, 2, 3, 4, nilVal)

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
Expand All @@ -42,7 +42,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) {

var nilVal float64
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
schema.TypeFloat, nil, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests.testOutput{
initialValue: map[string]string{},
changeValue: map[string]string{"key": "val"},
initialValue: &map[string]string{},
changeValue: &map[string]string{"key": "val1"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand All @@ -10,9 +10,9 @@ Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"
+ map_prop = {
+ "key" = "val"
id = "newid"
+ prop = {
+ "key" = "val1"
}
}

Expand All @@ -25,12 +25,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
+ mapProp: {
+ key: "val"
+ prop: {
+ key: "val1"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"mapProp": map[string]interface{}{}},
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{}},
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests.testOutput{
initialValue: map[string]string{
"key": "val",
initialValue: &map[string]string{
"key": "val1",
},
changeValue: map[string]string{"key": "val2"},
changeValue: &map[string]string{"key": "val2"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand All @@ -12,9 +12,9 @@ Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"
~ map_prop = {
~ "key" = "val" -> "val2"
id = "newid"
~ prop = {
~ "key" = "val1" -> "val2"
}
}

Expand All @@ -27,12 +27,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ mapProp: {
~ key: "val" => "val2"
~ prop: {
~ key: "val1" => "val2"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"mapProp.key": map[string]interface{}{"kind": "UPDATE"}},
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests.testOutput{
initialValue: map[string]string{
"key": "val",
initialValue: &map[string]string{
"key": "val1",
},
changeValue: map[string]string{"key2": "val"},
changeValue: &map[string]string{"key2": "val1"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand All @@ -12,10 +12,10 @@ Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"
~ map_prop = {
- "key" = "val" -> null
+ "key2" = "val"
id = "newid"
~ prop = {
- "key" = "val1" -> null
+ "key2" = "val1"
}
}

Expand All @@ -28,16 +28,16 @@ Plan: 0 to add, 1 to change, 0 to destroy.
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ mapProp: {
- key : "val"
+ key2: "val"
~ prop: {
- key : "val1"
+ key2: "val1"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{
"mapProp.key": map[string]interface{}{"kind": "DELETE"},
"mapProp.key2": map[string]interface{}{},
"prop.key": map[string]interface{}{"kind": "DELETE"},
"prop.key2": map[string]interface{}{},
},
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests.testOutput{
initialValue: map[string]string{
"key": "val",
initialValue: &map[string]string{
"key": "val1",
},
changeValue: map[string]string{},
changeValue: &map[string]string{},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand All @@ -12,9 +12,9 @@ Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"
~ map_prop = {
- "key" = "val" -> null
id = "newid"
~ prop = {
- "key" = "val1" -> null
}
}

Expand All @@ -27,12 +27,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
~ crossprovider:index/testRes:TestRes: (update)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ mapProp: {
- key: "val"
~ prop: {
- key: "val1"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"mapProp.key": map[string]interface{}{"kind": "DELETE"}},
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "DELETE"}},
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests.testOutput{
initialValue: map[string]string{},
changeValue: map[string]string{},
initialValue: &map[string]string{},
changeValue: &map[string]string{},
tfOut: `
No changes. Your infrastructure matches the configuration.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests.testOutput{
initialValue: map[string]string{
"key": "val",
initialValue: &map[string]string{
"key": "val1",
},
changeValue: map[string]string{"key": "val"},
changeValue: &map[string]string{"key": "val1"},
tfOut: `
No changes. Your infrastructure matches the configuration.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
tests.testOutput{
initialValue: &map[string]string{},
changeValue: &map[string]string{"key": "val1"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "id"
~ prop = {
~ "key" = "computedVal" -> "val1"
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=id]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ prop: {
~ key: "computedVal" => "val1"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
tests.testOutput{
initialValue: &map[string]string{
"key": "val1",
},
changeValue: &map[string]string{"key": "val2"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "id"
~ prop = {
~ "key" = "val1" -> "val2"
}
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ crossprovider:index/testRes:TestRes: (update)
[id=id]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ prop: {
~ key: "val1" => "val2"
}
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
}
Loading

0 comments on commit 8bb538e

Please sign in to comment.