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

Add SDKv2 detailed diff set tests for top-level ForceNew #2724

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion pkg/internal/tests/pulcheck/pulcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func propNeedsUpdate(prop *schema.Schema) bool {
return true
}

// resourceNeedsUpdate returns true if the TF SDK schema validation would consider the
// resource to need an update method.
func resourceNeedsUpdate(res *schema.Resource) bool {
// If any of the properties need an update, then the resource needs an update.
for _, s := range res.Schema {
Expand All @@ -53,6 +55,26 @@ func resourceNeedsUpdate(res *schema.Resource) bool {
// This is an experimental API.
func EnsureProviderValid(t T, tfp *schema.Provider) {
for _, r := range tfp.ResourcesMap {
if r.Schema["id"] == nil {
r.Schema["id"] = &schema.Schema{
Type: schema.TypeString,
Computed: true,
}
}

// If the resource will be flagged as not needing an Update method by the TF SDK schema
// validation then add a no-op update_prop property that will instead force the resource
// to need an Update method.
// This is necessary to work around a bug in the TF SDK schema validation where some resources which
// do need an Update method will instead be flagged as not needing an Update method.
// See https://github.com/pulumi/pulumi-terraform-bridge/pull/2723#issuecomment-2541518646
if !resourceNeedsUpdate(r) {
r.Schema["update_prop"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
}
}

if r.ReadContext == nil {
r.ReadContext = func(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
Expand All @@ -75,7 +97,9 @@ func EnsureProviderValid(t T, tfp *schema.Provider) {
}
}

if resourceNeedsUpdate(r) && r.UpdateContext == nil {
// Because of the no-op update_prop property added above, all resources will now
// need an Update method.
if r.UpdateContext == nil {
r.UpdateContext = func(
ctx context.Context, rd *schema.ResourceData, i interface{},
) diag.Diagnostics {
Expand Down
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"}},
}
Loading
Loading