Skip to content

Commit

Permalink
SDKv2 detailed diff set tests for replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Dec 12, 2024
1 parent e8278ba commit b2b8cf9
Show file tree
Hide file tree
Showing 201 changed files with 1,820 additions and 18 deletions.
89 changes: 71 additions & 18 deletions pkg/tests/detailed_diff_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ Plan: 0 to add, 1 to change, 0 to destroy.
})
}

func TestDetailedDiffSetCrossTest(t *testing.T) {
func TestDetailedDiffSet(t *testing.T) {
t.Parallel()

attributeSchema := schema.Resource{
Expand Down Expand Up @@ -823,6 +823,24 @@ func TestDetailedDiffSetCrossTest(t *testing.T) {
}

blockSchemaForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"test": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"nested": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
}

blockSchemaNestedForceNew := schema.Resource{
Schema: map[string]*schema.Schema{
"test": {
Type: schema.TypeSet,
Expand Down Expand Up @@ -872,15 +890,23 @@ func TestDetailedDiffSetCrossTest(t *testing.T) {
return cty.ListVal(slice)
}

schemaValueMakerPairs := []struct {
noForceNewSchemaValueMakerPairs := []struct {
name string
res schema.Resource
valueMaker func(*[]string) cty.Value
}{
{"attribute no force new", attributeSchema, attrList},
{"attribute force new", attributeSchemaForceNew, attrList},
{"block no force new", blockSchema, nestedAttrList},
{"block force new", blockSchemaForceNew, nestedAttrList},
}

forceNewSchemaValueMakerPairs := []struct {
name string
res schema.Resource
valueMaker func(*[]string) cty.Value
}{
{"attribute force new", attributeSchemaForceNew, attrList},
{"block top level force new", blockSchemaForceNew, nestedAttrList},
{"block nested force new", blockSchemaNestedForceNew, nestedAttrList},
}

scenarios := []struct {
Expand Down Expand Up @@ -916,7 +942,6 @@ func TestDetailedDiffSetCrossTest(t *testing.T) {
{"added end unordered", &[]string{"val2", "val3"}, &[]string{"val2", "val3", "val1"}},

{"same element updated", &[]string{"val1", "val2", "val3"}, &[]string{"val1", "val4", "val3"}},
{"same element updated unordered", &[]string{"val2", "val3", "val1"}, &[]string{"val2", "val4", "val1"}},

{"shuffled", &[]string{"val1", "val2", "val3"}, &[]string{"val3", "val1", "val2"}},
{"shuffled unordered", &[]string{"val2", "val3", "val1"}, &[]string{"val3", "val1", "val2"}},
Expand All @@ -933,12 +958,24 @@ func TestDetailedDiffSetCrossTest(t *testing.T) {

{"two added", &[]string{"val1", "val2"}, &[]string{"val1", "val2", "val3", "val4"}},
{"two removed", &[]string{"val1", "val2", "val3", "val4"}, &[]string{"val1", "val2"}},
}

// TODO[pulumi/pulumi-terraform-bridge#2726]: These tests fail to produce the correct replacement plan
// for the force new shcemas
extraScenarios := []struct {
name string
initialValue *[]string
changeValue *[]string
}{
{"same element updated unordered", &[]string{"val2", "val3", "val1"}, &[]string{"val2", "val4", "val1"}},
{"two added and two removed", &[]string{"val1", "val2", "val3", "val4"}, &[]string{"val1", "val2", "val5", "val6"}},
{"two added and two removed shuffled, one overlaps", &[]string{"val1", "val2", "val3", "val4"}, &[]string{"val1", "val5", "val6", "val2"}},
{"two added and two removed shuffled, no overlaps", &[]string{"val1", "val2", "val3", "val4"}, &[]string{"val5", "val6", "val1", "val2"}},
{"two added and two removed shuffled, with duplicates", &[]string{"val1", "val2", "val3", "val4"}, &[]string{"val1", "val5", "val6", "val2", "val1", "val2"}},
}

allScenarios := append(scenarios, extraScenarios...)

type testOutput struct {
initialValue *[]string
changeValue *[]string
Expand All @@ -947,24 +984,40 @@ func TestDetailedDiffSetCrossTest(t *testing.T) {
detailedDiff map[string]any
}

for _, schemaValueMakerPair := range schemaValueMakerPairs {
runTest := func(t *testing.T, schema schema.Resource, valueMaker func(*[]string) cty.Value, val1 *[]string, val2 *[]string) {
initialValue := valueMaker(val1)
changeValue := valueMaker(val2)

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

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

for _, schemaValueMakerPair := range forceNewSchemaValueMakerPairs {
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.res, map[string]cty.Value{"test": initialValue}, map[string]cty.Value{"test": changeValue})

autogold.ExpectFile(t, testOutput{
initialValue: scenario.initialValue,
changeValue: scenario.changeValue,
tfOut: diff.TFOut,
pulumiOut: diff.PulumiOut,
detailedDiff: diff.PulumiDiff.DetailedDiff,
})
runTest(t, schemaValueMakerPair.res, schemaValueMakerPair.valueMaker, scenario.initialValue, scenario.changeValue)
})
}
})
}

for _, schemaValueMakerPair := range noForceNewSchemaValueMakerPairs {
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
t.Parallel()
for _, scenario := range allScenarios {
t.Run(scenario.name, func(t *testing.T) {
t.Parallel()
runTest(t, schemaValueMakerPair.res, schemaValueMakerPair.valueMaker, scenario.initialValue, scenario.changeValue)
})
}
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
tests.testOutput{
initialValue: &[]string{},
changeValue: &[]string{"value"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+/- create replacement and then destroy

Terraform will perform the following actions:

# crossprovider_test_res.example must be replaced
+/- resource "crossprovider_test_res" "example" {
~ id = "newid" -> (known after apply)

+ test { # forces replacement
+ nested = "value"
}
}

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

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-crossprovider:index/testRes:TestRes: (replace)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
+ tests: [
+ [0]: {
+ nested : "value"
}
]
Resources:
+-1 to replace
1 unchanged
`,
detailedDiff: map[string]interface{}{"tests": map[string]interface{}{"kind": "ADD_REPLACE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tests.testOutput{
initialValue: &[]string{
"val1",
"val2",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+/- create replacement and then destroy

Terraform will perform the following actions:

# crossprovider_test_res.example must be replaced
+/- resource "crossprovider_test_res" "example" {
~ id = "newid" -> (known after apply)

+ test { # forces replacement
+ nested = "val3"
}

# (2 unchanged blocks hidden)
}

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

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-crossprovider:index/testRes:TestRes: (replace)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ tests: [
+ [2]: {
+ nested : "val3"
}
]
Resources:
+-1 to replace
1 unchanged
`,
detailedDiff: map[string]interface{}{"tests[2]": map[string]interface{}{"kind": "ADD_REPLACE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val2",
"val3",
"val1",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+/- create replacement and then destroy

Terraform will perform the following actions:

# crossprovider_test_res.example must be replaced
+/- resource "crossprovider_test_res" "example" {
~ id = "newid" -> (known after apply)

+ test { # forces replacement
+ nested = "val1"
}

# (2 unchanged blocks hidden)
}

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

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-crossprovider:index/testRes:TestRes: (replace)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ tests: [
+ [2]: {
+ nested : "val1"
}
]
Resources:
+-1 to replace
1 unchanged
`,
detailedDiff: map[string]interface{}{"tests[2]": map[string]interface{}{"kind": "ADD_REPLACE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+/- create replacement and then destroy

Terraform will perform the following actions:

# crossprovider_test_res.example must be replaced
+/- resource "crossprovider_test_res" "example" {
~ id = "newid" -> (known after apply)

+ test { # forces replacement
+ nested = "val1"
}

# (2 unchanged blocks hidden)
}

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

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-crossprovider:index/testRes:TestRes: (replace)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ tests: [
+ [0]: {
+ nested : "val1"
}
]
Resources:
+-1 to replace
1 unchanged
`,
detailedDiff: map[string]interface{}{"tests[0]": map[string]interface{}{"kind": "ADD_REPLACE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
tests.testOutput{
initialValue: &[]string{
"val3",
"val1",
},
changeValue: &[]string{
"val2",
"val3",
"val1",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+/- create replacement and then destroy

Terraform will perform the following actions:

# crossprovider_test_res.example must be replaced
+/- resource "crossprovider_test_res" "example" {
~ id = "newid" -> (known after apply)

+ test { # forces replacement
+ nested = "val2"
}

# (2 unchanged blocks hidden)
}

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

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
+-crossprovider:index/testRes:TestRes: (replace)
[id=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ tests: [
+ [0]: {
+ nested : "val2"
}
]
Resources:
+-1 to replace
1 unchanged
`,
detailedDiff: map[string]interface{}{"tests[0]": map[string]interface{}{"kind": "ADD_REPLACE"}},
}
Loading

0 comments on commit b2b8cf9

Please sign in to comment.