From ede0755d7ebc727c04e62da93e1220000b0989fd Mon Sep 17 00:00:00 2001 From: Venelin Date: Thu, 21 Nov 2024 12:41:50 +0000 Subject: [PATCH] more tests around secrets and outputs --- pkg/tfbridge/detailed_diff_test.go | 112 +++++++++++++++++++++++++---- 1 file changed, 97 insertions(+), 15 deletions(-) diff --git a/pkg/tfbridge/detailed_diff_test.go b/pkg/tfbridge/detailed_diff_test.go index 46817a1a8d..2130d0ce8c 100644 --- a/pkg/tfbridge/detailed_diff_test.go +++ b/pkg/tfbridge/detailed_diff_test.go @@ -488,43 +488,49 @@ func TestBasicDetailedDiff(t *testing.T) { "foo": tt.value1, }, ) - propertyMapSecretValue1["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue1["foo"]}) + propertyMapSecretValue1["foo"] = resource.NewSecretProperty( + &resource.Secret{Element: propertyMapSecretValue1["foo"]}) propertyMapSecretValue2 := resource.NewPropertyMapFromMap( map[string]interface{}{ "foo": tt.value2, }, ) - propertyMapSecretValue2["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue2["foo"]}) + propertyMapSecretValue2["foo"] = resource.NewSecretProperty( + &resource.Secret{Element: propertyMapSecretValue2["foo"]}) propertyMapOutputValue1 := resource.NewPropertyMapFromMap( map[string]interface{}{ "foo": tt.value1, }, ) - propertyMapOutputValue1["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue1["foo"]}) + propertyMapOutputValue1["foo"] = resource.NewOutputProperty( + resource.Output{Element: propertyMapOutputValue1["foo"], Known: true}) propertyMapOutputValue2 := resource.NewPropertyMapFromMap( map[string]interface{}{ "foo": tt.value2, }, ) - propertyMapOutputValue2["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue2["foo"]}) + propertyMapOutputValue2["foo"] = resource.NewOutputProperty( + resource.Output{Element: propertyMapOutputValue2["foo"], Known: true}) + + defaultChangePath := "foo" + if tt.listLike && tt.objectLike { + defaultChangePath = "foo[0].foo" + } else if tt.listLike { + defaultChangePath = "foo[0]" + } else if tt.objectLike { + defaultChangePath = "foo.foo" + } t.Run("unchanged", func(t *testing.T) { runDetailedDiffTest(t, propertyMapValue1, propertyMapValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{}) }) t.Run("changed non-empty", func(t *testing.T) { - expected := make(map[string]*pulumirpc.PropertyDiff) - if tt.listLike && tt.objectLike { - expected["foo[0].foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE} - } else if tt.listLike { - expected["foo[0]"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE} - } else if tt.objectLike { - expected["foo.foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE} - } else { - expected["foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE} + expected := map[string]*pulumirpc.PropertyDiff{ + defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE}, } runDetailedDiffTest(t, propertyMapValue1, propertyMapValue2, tfs, ps, expected) }) @@ -612,7 +618,10 @@ func TestBasicDetailedDiff(t *testing.T) { }) t.Run("secret changed", func(t *testing.T) { - runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapSecretValue2, tfs, ps, updated()) + expected := map[string]*pulumirpc.PropertyDiff{ + defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE}, + } + runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapSecretValue2, tfs, ps, expected) }) t.Run("output unchanged", func(t *testing.T) { @@ -634,7 +643,10 @@ func TestBasicDetailedDiff(t *testing.T) { }) t.Run("output changed", func(t *testing.T) { - runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapOutputValue2, tfs, ps, updated()) + expected := map[string]*pulumirpc.PropertyDiff{ + defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE}, + } + runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapOutputValue2, tfs, ps, expected) }) }) } @@ -682,6 +694,16 @@ func TestDetailedDiffObject(t *testing.T) { }, ) + propertyMapWithSecrets := resource.PropertyMap{ + resource.PropertyKey("foo"): resource.NewPropertyValue( + resource.PropertyMap{ + resource.PropertyKey("prop1"): resource.NewSecretProperty( + &resource.Secret{Element: resource.NewStringProperty("val1")}), + resource.PropertyKey("prop2"): resource.NewStringProperty("qux"), + }, + ), + } + t.Run("unchanged", func(t *testing.T) { runDetailedDiffTest(t, propertyMapProp1Val1, propertyMapProp1Val1, tfs, ps, map[string]*pulumirpc.PropertyDiff{}) }) @@ -723,6 +745,18 @@ func TestDetailedDiffObject(t *testing.T) { "foo.prop2": {Kind: pulumirpc.PropertyDiff_ADD}, }) }) + + t.Run("secret added", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapProp2, propertyMapWithSecrets, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo.prop1": {Kind: pulumirpc.PropertyDiff_ADD}, + }) + }) + + t.Run("secret deleted", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapWithSecrets, propertyMapProp2, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo.prop1": {Kind: pulumirpc.PropertyDiff_DELETE}, + }) + }) } func TestDetailedDiffList(t *testing.T) { @@ -892,6 +926,22 @@ func TestDetailedDiffSet(t *testing.T) { }, ) + propertyMapWithSecrets := resource.NewPropertyMapFromMap( + map[string]interface{}{ + "foo": []interface{}{resource.NewSecretProperty( + &resource.Secret{Element: resource.NewStringProperty("val1")}), "val2"}, + }, + ) + + propertyMapWithSecretsAndOutputs := resource.NewPropertyMapFromMap( + map[string]interface{}{ + "foo": []interface{}{ + resource.NewSecretProperty(&resource.Secret{Element: resource.NewStringProperty("val1")}), + resource.NewOutputProperty(resource.Output{Element: resource.NewStringProperty("val2")}), + }, + }, + ) + t.Run("unchanged", func(t *testing.T) { runDetailedDiffTest(t, propertyMapVal1, propertyMapVal1, tfs, ps, map[string]*pulumirpc.PropertyDiff{}) }) @@ -940,6 +990,38 @@ func TestDetailedDiffSet(t *testing.T) { "foo[1]": {Kind: pulumirpc.PropertyDiff_ADD}, }) }) + + t.Run("secret added", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapVal2, propertyMapWithSecrets, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo[0]": {Kind: pulumirpc.PropertyDiff_ADD}, + }) + }) + + t.Run("secret and output added", func(t *testing.T) { + runDetailedDiffTest( + t, propertyMapEmpty, propertyMapWithSecretsAndOutputs, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo[0]": {Kind: pulumirpc.PropertyDiff_ADD}, + "foo[1]": {Kind: pulumirpc.PropertyDiff_ADD}, + }) + }) + + t.Run("secret removed", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapWithSecrets, propertyMapVal2, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo[0]": {Kind: pulumirpc.PropertyDiff_DELETE}, + }) + }) + + t.Run("output removed", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapWithSecretsAndOutputs, propertyMapVal1, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo[1]": {Kind: pulumirpc.PropertyDiff_DELETE}, + }) + }) + + t.Run("secretness and outputness changed", func(t *testing.T) { + runDetailedDiffTest(t, propertyMapWithSecretsAndOutputs, propertyMapBoth, tfs, ps, map[string]*pulumirpc.PropertyDiff{ + "foo[1]": {Kind: pulumirpc.PropertyDiff_UPDATE}, + }) + }) } func TestDetailedDiffTFForceNewPlain(t *testing.T) {