Skip to content

Commit

Permalink
more tests around secrets and outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
VenelinMartinov committed Nov 21, 2024
1 parent 6a84135 commit fb364c0
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 15 deletions.
112 changes: 97 additions & 15 deletions pkg/tfbridge/detailed_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
})
})
}
Expand Down Expand Up @@ -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{})
})
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{})
})
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 22 additions & 0 deletions unstable/propertyvalue/propertyvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package propertyvalue
import (
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
rtesting "github.com/pulumi/pulumi/sdk/v3/go/common/resource/testing"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"
)

Expand All @@ -30,3 +32,23 @@ func TestRemoveSecrets(t *testing.T) {
}
})
}

func TestRemoveSecretsAndOutputs(t *testing.T) {
t.Parallel()
rapid.Check(t, func(t *rapid.T) {
randomPV := rtesting.PropertyValueGenerator(5 /* maxDepth */).Draw(t, "pv")
result := RemoveSecretsAndOutputs(randomPV)
if result.ContainsSecrets() {
t.Fatalf("RemoveSecretsAndOutputs(randomPV).ContainsSecrets()")
}

visitor := func(path resource.PropertyPath, val resource.PropertyValue) (resource.PropertyValue, error) {
require.False(t, val.IsSecret())
require.False(t, val.IsOutput())
return val, nil
}

_, err := TransformPropertyValue(resource.PropertyPath{}, visitor, result)
require.NoError(t, err)
})
}

0 comments on commit fb364c0

Please sign in to comment.