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

SDKv2 Diff tests for primitive types #2829

Merged
merged 7 commits into from
Jan 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,6 @@ import (
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
)

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

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

valueOne := ref("val1")
valueTwo := ref("val2")
var noValue *string

ctyVal := func(v *string) map[string]cty.Value {
if v == nil {
return map[string]cty.Value{}
}
return map[string]cty.Value{
"string_prop": cty.StringVal(*v),
}
}

scenarios := []struct {
name string
initialValue *string
changeValue *string
}{
{"unchanged empty", noValue, noValue},
{"unchanged non-empty", valueOne, valueOne},
{"added", noValue, valueOne},
{"removed", valueOne, noValue},
{"changed", valueOne, valueTwo},
}

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,
})
})
}
}

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

Expand Down
48 changes: 48 additions & 0 deletions pkg/tests/diff_test/detailed_diff_primitive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package tests

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zclconf/go-cty/cty"
)

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

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

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}

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

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

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}

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

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

runSDKv2TestMatrix(t, schemaValueMakerPairs, scenarios)
}

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

var nilVal float64
schemaValueMakerPairs, scenarios := generateBaseTests(
schema.TypeFloat, 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
@@ -0,0 +1,31 @@
tests.testOutput{
initialValue: valast.Ptr(false), changeValue: valast.Ptr(true),
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 = "newid"
~ prop = false -> true
}

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=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ prop: false => true
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests.testOutput{
initialValue: valast.Ptr(true), changeValue: valast.Ptr(false),
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 = "newid"
~ prop = true -> false
}

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=newid]
[urn=urn:pulumi:test::project::crossprovider:index/testRes:TestRes::example]
~ prop: true => false
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests.testOutput{
initialValue: valast.Ptr("val1"), changeValue: nil,
initialValue: valast.Ptr(true), changeValue: valast.Ptr(false),
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
Expand All @@ -9,8 +9,8 @@ Terraform will perform the following actions:

# crossprovider_test_res.example will be updated in-place
~ resource "crossprovider_test_res" "example" {
id = "newid"
- string_prop = "val1" -> null
id = "newid"
~ prop = true -> false
}

Plan: 0 to add, 1 to change, 0 to destroy.
Expand All @@ -22,10 +22,10 @@ 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]
- stringProp: "val1"
~ prop: true => false
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"stringProp": map[string]interface{}{"kind": "DELETE"}},
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests.testOutput{
initialValue: valast.Ptr(false), changeValue: valast.Ptr(false),
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,15 @@
tests.testOutput{
initialValue: valast.Ptr(true), changeValue: valast.Ptr(true),
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,15 @@
tests.testOutput{
initialValue: valast.Ptr(false), changeValue: valast.Ptr(true),
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,31 @@
tests.testOutput{
initialValue: valast.Ptr(true), changeValue: valast.Ptr(false),
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 = true -> false
}

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: true => false
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests.testOutput{
initialValue: valast.Ptr(true), changeValue: valast.Ptr(false),
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 = true -> false
}

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: true => false
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
tests.testOutput{
initialValue: valast.Ptr(false), changeValue: valast.Ptr(false),
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 = true -> false
}

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: true => false
Resources:
~ 1 to update
1 unchanged
`,
detailedDiff: map[string]interface{}{"prop": map[string]interface{}{"kind": "UPDATE"}},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests.testOutput{
initialValue: valast.Ptr(true), changeValue: valast.Ptr(true),
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,15 @@
tests.testOutput{
initialValue: valast.Ptr(false), changeValue: valast.Ptr(true),
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
Loading