Skip to content

Commit 8bb538e

Browse files
SDKv2 Diff tests for Map (#2830)
This PR finishes the coverage of the SDKv2 bridge for Diff for Map types. [f2a0c1c](f2a0c1c) contains the test recordings fixes #2787
1 parent 9660285 commit 8bb538e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1435
-96
lines changed

pkg/tests/diff_test/detailed_diff_map_test.go

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,66 +4,34 @@ import (
44
"testing"
55

66
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7-
"github.com/hexops/autogold/v2"
87
"github.com/zclconf/go-cty/cty"
9-
10-
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
118
)
129

1310
func TestSDKv2DetailedDiffMap(t *testing.T) {
1411
t.Parallel()
1512

16-
res := schema.Resource{
17-
Schema: map[string]*schema.Schema{
18-
"map_prop": {
19-
Type: schema.TypeMap,
20-
Optional: true,
21-
Elem: &schema.Schema{Type: schema.TypeString},
22-
},
23-
},
24-
}
25-
26-
ctyVal := func(v map[string]string) map[string]cty.Value {
27-
ctyMap := make(map[string]cty.Value)
28-
13+
ctyMaker := func(v map[string]string) cty.Value {
2914
if len(v) == 0 {
30-
return map[string]cty.Value{
31-
"map_prop": cty.MapValEmpty(cty.String),
32-
}
15+
return cty.MapValEmpty(cty.String)
3316
}
34-
17+
ctyMap := make(map[string]cty.Value)
3518
for k, v := range v {
3619
ctyMap[k] = cty.StringVal(v)
3720
}
38-
return map[string]cty.Value{
39-
"map_prop": cty.MapVal(ctyMap),
40-
}
21+
return cty.MapVal(ctyMap)
4122
}
4223

43-
scenarios := []struct {
44-
name string
45-
initialValue map[string]string
46-
changeValue map[string]string
47-
}{
48-
{"unchanged empty", map[string]string{}, map[string]string{}},
49-
{"unchanged non-empty", map[string]string{"key": "val"}, map[string]string{"key": "val"}},
50-
{"added", map[string]string{}, map[string]string{"key": "val"}},
51-
{"removed", map[string]string{"key": "val"}, map[string]string{}},
52-
{"value changed", map[string]string{"key": "val"}, map[string]string{"key": "val2"}},
53-
{"key changed", map[string]string{"key": "val"}, map[string]string{"key2": "val"}},
54-
}
24+
var nilVal map[string]string
25+
schemaValueMakerPairs, scenarios := generateBaseTests(
26+
schema.TypeMap, &schema.Schema{Type: schema.TypeString}, ctyMaker,
27+
map[string]string{"key": "val1"}, map[string]string{"key": "val2"},
28+
map[string]string{"key": "computedVal"}, map[string]string{"key": "defaultVal"}, nilVal)
5529

56-
for _, scenario := range scenarios {
57-
t.Run(scenario.name, func(t *testing.T) {
58-
t.Parallel()
59-
diff := crosstests.Diff(t, &res, ctyVal(scenario.initialValue), ctyVal(scenario.changeValue))
60-
autogold.ExpectFile(t, testOutput{
61-
initialValue: scenario.initialValue,
62-
changeValue: scenario.changeValue,
63-
tfOut: diff.TFOut,
64-
pulumiOut: diff.PulumiOut,
65-
detailedDiff: diff.PulumiDiff.DetailedDiff,
66-
})
67-
})
68-
}
30+
scenarios = append(scenarios, diffScenario[map[string]string]{
31+
name: "key changed",
32+
initialValue: ref(map[string]string{"key": "val1"}),
33+
changeValue: ref(map[string]string{"key2": "val1"}),
34+
})
35+
36+
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
6937
}

pkg/tests/diff_test/detailed_diff_primitive_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func TestSDKv2DetailedDiffString(t *testing.T) {
1212

1313
var nilVal string
1414
schemaValueMakerPairs, scenarios := generateBaseTests(
15-
schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
15+
schema.TypeString, nil, cty.StringVal, "val1", "val2", "computed", "default", nilVal)
1616

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

2323
var nilVal bool
2424
schemaValueMakerPairs, scenarios := generateBaseTests(
25-
schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal)
25+
schema.TypeBool, nil, cty.BoolVal, true, false, true, false, nilVal)
2626

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

3333
var nilVal int64
3434
schemaValueMakerPairs, scenarios := generateBaseTests(
35-
schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
35+
schema.TypeInt, nil, cty.NumberIntVal, 1, 2, 3, 4, nilVal)
3636

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

4343
var nilVal float64
4444
schemaValueMakerPairs, scenarios := generateBaseTests(
45-
schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
45+
schema.TypeFloat, nil, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal)
4646

4747
runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
4848
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests.testOutput{
2-
initialValue: map[string]string{},
3-
changeValue: map[string]string{"key": "val"},
2+
initialValue: &map[string]string{},
3+
changeValue: &map[string]string{"key": "val1"},
44
tfOut: `
55
Terraform used the selected providers to generate the following execution
66
plan. Resource actions are indicated with the following symbols:
@@ -10,9 +10,9 @@ Terraform will perform the following actions:
1010

1111
# crossprovider_test_res.example will be updated in-place
1212
~ resource "crossprovider_test_res" "example" {
13-
id = "newid"
14-
+ map_prop = {
15-
+ "key" = "val"
13+
id = "newid"
14+
+ prop = {
15+
+ "key" = "val1"
1616
}
1717
}
1818

@@ -25,12 +25,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
2525
~ crossprovider:index/testRes:TestRes: (update)
2626
[id=newid]
2727
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
28-
+ mapProp: {
29-
+ key: "val"
28+
+ prop: {
29+
+ key: "val1"
3030
}
3131
Resources:
3232
~ 1 to update
3333
1 unchanged
3434
`,
35-
detailedDiff: map[string]interface{}{"mapProp": map[string]interface{}{}},
35+
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{}},
3636
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests.testOutput{
2-
initialValue: map[string]string{
3-
"key": "val",
2+
initialValue: &map[string]string{
3+
"key": "val1",
44
},
5-
changeValue: map[string]string{"key": "val2"},
5+
changeValue: &map[string]string{"key": "val2"},
66
tfOut: `
77
Terraform used the selected providers to generate the following execution
88
plan. Resource actions are indicated with the following symbols:
@@ -12,9 +12,9 @@ Terraform will perform the following actions:
1212

1313
# crossprovider_test_res.example will be updated in-place
1414
~ resource "crossprovider_test_res" "example" {
15-
id = "newid"
16-
~ map_prop = {
17-
~ "key" = "val" -> "val2"
15+
id = "newid"
16+
~ prop = {
17+
~ "key" = "val1" -> "val2"
1818
}
1919
}
2020

@@ -27,12 +27,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
2727
~ crossprovider:index/testRes:TestRes: (update)
2828
[id=newid]
2929
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
30-
~ mapProp: {
31-
~ key: "val" => "val2"
30+
~ prop: {
31+
~ key: "val1" => "val2"
3232
}
3333
Resources:
3434
~ 1 to update
3535
1 unchanged
3636
`,
37-
detailedDiff: map[string]interface{}{"mapProp.key": map[string]interface{}{"kind": "UPDATE"}},
37+
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
3838
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests.testOutput{
2-
initialValue: map[string]string{
3-
"key": "val",
2+
initialValue: &map[string]string{
3+
"key": "val1",
44
},
5-
changeValue: map[string]string{"key2": "val"},
5+
changeValue: &map[string]string{"key2": "val1"},
66
tfOut: `
77
Terraform used the selected providers to generate the following execution
88
plan. Resource actions are indicated with the following symbols:
@@ -12,10 +12,10 @@ Terraform will perform the following actions:
1212

1313
# crossprovider_test_res.example will be updated in-place
1414
~ resource "crossprovider_test_res" "example" {
15-
id = "newid"
16-
~ map_prop = {
17-
- "key" = "val" -> null
18-
+ "key2" = "val"
15+
id = "newid"
16+
~ prop = {
17+
- "key" = "val1" -> null
18+
+ "key2" = "val1"
1919
}
2020
}
2121

@@ -28,16 +28,16 @@ Plan: 0 to add, 1 to change, 0 to destroy.
2828
~ crossprovider:index/testRes:TestRes: (update)
2929
[id=newid]
3030
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
31-
~ mapProp: {
32-
- key : "val"
33-
+ key2: "val"
31+
~ prop: {
32+
- key : "val1"
33+
+ key2: "val1"
3434
}
3535
Resources:
3636
~ 1 to update
3737
1 unchanged
3838
`,
3939
detailedDiff: map[string]interface{}{
40-
"mapProp.key": map[string]interface{}{"kind": "DELETE"},
41-
"mapProp.key2": map[string]interface{}{},
40+
"prop.key": map[string]interface{}{"kind": "DELETE"},
41+
"prop.key2": map[string]interface{}{},
4242
},
4343
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests.testOutput{
2-
initialValue: map[string]string{
3-
"key": "val",
2+
initialValue: &map[string]string{
3+
"key": "val1",
44
},
5-
changeValue: map[string]string{},
5+
changeValue: &map[string]string{},
66
tfOut: `
77
Terraform used the selected providers to generate the following execution
88
plan. Resource actions are indicated with the following symbols:
@@ -12,9 +12,9 @@ Terraform will perform the following actions:
1212

1313
# crossprovider_test_res.example will be updated in-place
1414
~ resource "crossprovider_test_res" "example" {
15-
id = "newid"
16-
~ map_prop = {
17-
- "key" = "val" -> null
15+
id = "newid"
16+
~ prop = {
17+
- "key" = "val1" -> null
1818
}
1919
}
2020

@@ -27,12 +27,12 @@ Plan: 0 to add, 1 to change, 0 to destroy.
2727
~ crossprovider:index/testRes:TestRes: (update)
2828
[id=newid]
2929
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
30-
~ mapProp: {
31-
- key: "val"
30+
~ prop: {
31+
- key: "val1"
3232
}
3333
Resources:
3434
~ 1 to update
3535
1 unchanged
3636
`,
37-
detailedDiff: map[string]interface{}{"mapProp.key": map[string]interface{}{"kind": "DELETE"}},
37+
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "DELETE"}},
3838
}

pkg/tests/diff_test/testdata/TestSDKv2DetailedDiffMap/unchanged_empty.golden renamed to pkg/tests/diff_test/testdata/TestSDKv2DetailedDiffMap/optional/unchanged_empty.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests.testOutput{
2-
initialValue: map[string]string{},
3-
changeValue: map[string]string{},
2+
initialValue: &map[string]string{},
3+
changeValue: &map[string]string{},
44
tfOut: `
55
No changes. Your infrastructure matches the configuration.
66

pkg/tests/diff_test/testdata/TestSDKv2DetailedDiffMap/unchanged_non-empty.golden renamed to pkg/tests/diff_test/testdata/TestSDKv2DetailedDiffMap/optional/unchanged_non-empty.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
tests.testOutput{
2-
initialValue: map[string]string{
3-
"key": "val",
2+
initialValue: &map[string]string{
3+
"key": "val1",
44
},
5-
changeValue: map[string]string{"key": "val"},
5+
changeValue: &map[string]string{"key": "val1"},
66
tfOut: `
77
No changes. Your infrastructure matches the configuration.
88

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
tests.testOutput{
2+
initialValue: &map[string]string{},
3+
changeValue: &map[string]string{"key": "val1"},
4+
tfOut: `
5+
Terraform used the selected providers to generate the following execution
6+
plan. Resource actions are indicated with the following symbols:
7+
~ update in-place
8+
9+
Terraform will perform the following actions:
10+
11+
# crossprovider_test_res.example will be updated in-place
12+
~ resource "crossprovider_test_res" "example" {
13+
id = "id"
14+
~ prop = {
15+
~ "key" = "computedVal" -> "val1"
16+
}
17+
}
18+
19+
Plan: 0 to add, 1 to change, 0 to destroy.
20+
21+
`,
22+
pulumiOut: `Previewing update (test):
23+
pulumi:pulumi:Stack: (same)
24+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
25+
~ crossprovider:index/testRes:TestRes: (update)
26+
[id=id]
27+
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
28+
~ prop: {
29+
~ key: "computedVal" => "val1"
30+
}
31+
Resources:
32+
~ 1 to update
33+
1 unchanged
34+
`,
35+
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
tests.testOutput{
2+
initialValue: &map[string]string{
3+
"key": "val1",
4+
},
5+
changeValue: &map[string]string{"key": "val2"},
6+
tfOut: `
7+
Terraform used the selected providers to generate the following execution
8+
plan. Resource actions are indicated with the following symbols:
9+
~ update in-place
10+
11+
Terraform will perform the following actions:
12+
13+
# crossprovider_test_res.example will be updated in-place
14+
~ resource "crossprovider_test_res" "example" {
15+
id = "id"
16+
~ prop = {
17+
~ "key" = "val1" -> "val2"
18+
}
19+
}
20+
21+
Plan: 0 to add, 1 to change, 0 to destroy.
22+
23+
`,
24+
pulumiOut: `Previewing update (test):
25+
pulumi:pulumi:Stack: (same)
26+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
27+
~ crossprovider:index/testRes:TestRes: (update)
28+
[id=id]
29+
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
30+
~ prop: {
31+
~ key: "val1" => "val2"
32+
}
33+
Resources:
34+
~ 1 to update
35+
1 unchanged
36+
`,
37+
detailedDiff: map[string]interface{}{"prop.key": map[string]interface{}{"kind": "UPDATE"}},
38+
}

0 commit comments

Comments
 (0)