Skip to content

Commit 0c77fff

Browse files
more tests around secrets and outputs
1 parent d1fbc89 commit 0c77fff

File tree

2 files changed

+119
-15
lines changed

2 files changed

+119
-15
lines changed

pkg/tfbridge/detailed_diff_test.go

Lines changed: 97 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -488,43 +488,49 @@ func TestBasicDetailedDiff(t *testing.T) {
488488
"foo": tt.value1,
489489
},
490490
)
491-
propertyMapSecretValue1["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue1["foo"]})
491+
propertyMapSecretValue1["foo"] = resource.NewSecretProperty(
492+
&resource.Secret{Element: propertyMapSecretValue1["foo"]})
492493

493494
propertyMapSecretValue2 := resource.NewPropertyMapFromMap(
494495
map[string]interface{}{
495496
"foo": tt.value2,
496497
},
497498
)
498-
propertyMapSecretValue2["foo"] = resource.NewSecretProperty(&resource.Secret{Element: propertyMapValue2["foo"]})
499+
propertyMapSecretValue2["foo"] = resource.NewSecretProperty(
500+
&resource.Secret{Element: propertyMapSecretValue2["foo"]})
499501

500502
propertyMapOutputValue1 := resource.NewPropertyMapFromMap(
501503
map[string]interface{}{
502504
"foo": tt.value1,
503505
},
504506
)
505-
propertyMapOutputValue1["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue1["foo"]})
507+
propertyMapOutputValue1["foo"] = resource.NewOutputProperty(
508+
resource.Output{Element: propertyMapOutputValue1["foo"], Known: true})
506509

507510
propertyMapOutputValue2 := resource.NewPropertyMapFromMap(
508511
map[string]interface{}{
509512
"foo": tt.value2,
510513
},
511514
)
512-
propertyMapOutputValue2["foo"] = resource.NewOutputProperty(resource.Output{Element: propertyMapValue2["foo"]})
515+
propertyMapOutputValue2["foo"] = resource.NewOutputProperty(
516+
resource.Output{Element: propertyMapOutputValue2["foo"], Known: true})
517+
518+
defaultChangePath := "foo"
519+
if tt.listLike && tt.objectLike {
520+
defaultChangePath = "foo[0].foo"
521+
} else if tt.listLike {
522+
defaultChangePath = "foo[0]"
523+
} else if tt.objectLike {
524+
defaultChangePath = "foo.foo"
525+
}
513526

514527
t.Run("unchanged", func(t *testing.T) {
515528
runDetailedDiffTest(t, propertyMapValue1, propertyMapValue1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
516529
})
517530

518531
t.Run("changed non-empty", func(t *testing.T) {
519-
expected := make(map[string]*pulumirpc.PropertyDiff)
520-
if tt.listLike && tt.objectLike {
521-
expected["foo[0].foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE}
522-
} else if tt.listLike {
523-
expected["foo[0]"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE}
524-
} else if tt.objectLike {
525-
expected["foo.foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE}
526-
} else {
527-
expected["foo"] = &pulumirpc.PropertyDiff{Kind: pulumirpc.PropertyDiff_UPDATE}
532+
expected := map[string]*pulumirpc.PropertyDiff{
533+
defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE},
528534
}
529535
runDetailedDiffTest(t, propertyMapValue1, propertyMapValue2, tfs, ps, expected)
530536
})
@@ -612,7 +618,10 @@ func TestBasicDetailedDiff(t *testing.T) {
612618
})
613619

614620
t.Run("secret changed", func(t *testing.T) {
615-
runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapSecretValue2, tfs, ps, updated())
621+
expected := map[string]*pulumirpc.PropertyDiff{
622+
defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE},
623+
}
624+
runDetailedDiffTest(t, propertyMapSecretValue1, propertyMapSecretValue2, tfs, ps, expected)
616625
})
617626

618627
t.Run("output unchanged", func(t *testing.T) {
@@ -634,7 +643,10 @@ func TestBasicDetailedDiff(t *testing.T) {
634643
})
635644

636645
t.Run("output changed", func(t *testing.T) {
637-
runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapOutputValue2, tfs, ps, updated())
646+
expected := map[string]*pulumirpc.PropertyDiff{
647+
defaultChangePath: {Kind: pulumirpc.PropertyDiff_UPDATE},
648+
}
649+
runDetailedDiffTest(t, propertyMapOutputValue1, propertyMapOutputValue2, tfs, ps, expected)
638650
})
639651
})
640652
}
@@ -682,6 +694,16 @@ func TestDetailedDiffObject(t *testing.T) {
682694
},
683695
)
684696

697+
propertyMapWithSecrets := resource.PropertyMap{
698+
resource.PropertyKey("foo"): resource.NewPropertyValue(
699+
resource.PropertyMap{
700+
resource.PropertyKey("prop1"): resource.NewSecretProperty(
701+
&resource.Secret{Element: resource.NewStringProperty("val1")}),
702+
resource.PropertyKey("prop2"): resource.NewStringProperty("qux"),
703+
},
704+
),
705+
}
706+
685707
t.Run("unchanged", func(t *testing.T) {
686708
runDetailedDiffTest(t, propertyMapProp1Val1, propertyMapProp1Val1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
687709
})
@@ -723,6 +745,18 @@ func TestDetailedDiffObject(t *testing.T) {
723745
"foo.prop2": {Kind: pulumirpc.PropertyDiff_ADD},
724746
})
725747
})
748+
749+
t.Run("secret added", func(t *testing.T) {
750+
runDetailedDiffTest(t, propertyMapProp2, propertyMapWithSecrets, tfs, ps, map[string]*pulumirpc.PropertyDiff{
751+
"foo.prop1": {Kind: pulumirpc.PropertyDiff_ADD},
752+
})
753+
})
754+
755+
t.Run("secret deleted", func(t *testing.T) {
756+
runDetailedDiffTest(t, propertyMapWithSecrets, propertyMapProp2, tfs, ps, map[string]*pulumirpc.PropertyDiff{
757+
"foo.prop1": {Kind: pulumirpc.PropertyDiff_DELETE},
758+
})
759+
})
726760
}
727761

728762
func TestDetailedDiffList(t *testing.T) {
@@ -892,6 +926,22 @@ func TestDetailedDiffSet(t *testing.T) {
892926
},
893927
)
894928

929+
propertyMapWithSecrets := resource.NewPropertyMapFromMap(
930+
map[string]interface{}{
931+
"foo": []interface{}{resource.NewSecretProperty(
932+
&resource.Secret{Element: resource.NewStringProperty("val1")}), "val2"},
933+
},
934+
)
935+
936+
propertyMapWithSecretsAndOutputs := resource.NewPropertyMapFromMap(
937+
map[string]interface{}{
938+
"foo": []interface{}{
939+
resource.NewSecretProperty(&resource.Secret{Element: resource.NewStringProperty("val1")}),
940+
resource.NewOutputProperty(resource.Output{Element: resource.NewStringProperty("val2")}),
941+
},
942+
},
943+
)
944+
895945
t.Run("unchanged", func(t *testing.T) {
896946
runDetailedDiffTest(t, propertyMapVal1, propertyMapVal1, tfs, ps, map[string]*pulumirpc.PropertyDiff{})
897947
})
@@ -940,6 +990,38 @@ func TestDetailedDiffSet(t *testing.T) {
940990
"foo[1]": {Kind: pulumirpc.PropertyDiff_ADD},
941991
})
942992
})
993+
994+
t.Run("secret added", func(t *testing.T) {
995+
runDetailedDiffTest(t, propertyMapVal2, propertyMapWithSecrets, tfs, ps, map[string]*pulumirpc.PropertyDiff{
996+
"foo[0]": {Kind: pulumirpc.PropertyDiff_ADD},
997+
})
998+
})
999+
1000+
t.Run("secret and output added", func(t *testing.T) {
1001+
runDetailedDiffTest(
1002+
t, propertyMapEmpty, propertyMapWithSecretsAndOutputs, tfs, ps, map[string]*pulumirpc.PropertyDiff{
1003+
"foo[0]": {Kind: pulumirpc.PropertyDiff_ADD},
1004+
"foo[1]": {Kind: pulumirpc.PropertyDiff_ADD},
1005+
})
1006+
})
1007+
1008+
t.Run("secret removed", func(t *testing.T) {
1009+
runDetailedDiffTest(t, propertyMapWithSecrets, propertyMapVal2, tfs, ps, map[string]*pulumirpc.PropertyDiff{
1010+
"foo[0]": {Kind: pulumirpc.PropertyDiff_DELETE},
1011+
})
1012+
})
1013+
1014+
t.Run("output removed", func(t *testing.T) {
1015+
runDetailedDiffTest(t, propertyMapWithSecretsAndOutputs, propertyMapVal1, tfs, ps, map[string]*pulumirpc.PropertyDiff{
1016+
"foo[1]": {Kind: pulumirpc.PropertyDiff_DELETE},
1017+
})
1018+
})
1019+
1020+
t.Run("secretness and outputness changed", func(t *testing.T) {
1021+
runDetailedDiffTest(t, propertyMapWithSecretsAndOutputs, propertyMapBoth, tfs, ps, map[string]*pulumirpc.PropertyDiff{
1022+
"foo[1]": {Kind: pulumirpc.PropertyDiff_UPDATE},
1023+
})
1024+
})
9431025
}
9441026

9451027
func TestDetailedDiffTFForceNewPlain(t *testing.T) {

unstable/propertyvalue/propertyvalue_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ package propertyvalue
1717
import (
1818
"testing"
1919

20+
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
2021
rtesting "github.com/pulumi/pulumi/sdk/v3/go/common/resource/testing"
22+
"github.com/stretchr/testify/require"
2123
"pgregory.net/rapid"
2224
)
2325

@@ -30,3 +32,23 @@ func TestRemoveSecrets(t *testing.T) {
3032
}
3133
})
3234
}
35+
36+
func TestRemoveSecretsAndOutputs(t *testing.T) {
37+
t.Parallel()
38+
rapid.Check(t, func(t *rapid.T) {
39+
randomPV := rtesting.PropertyValueGenerator(5 /* maxDepth */).Draw(t, "pv")
40+
result := RemoveSecretsAndOutputs(randomPV)
41+
if result.ContainsSecrets() {
42+
t.Fatalf("RemoveSecretsAndOutputs(randomPV).ContainsSecrets()")
43+
}
44+
45+
visitor := func(path resource.PropertyPath, val resource.PropertyValue) (resource.PropertyValue, error) {
46+
require.False(t, val.IsSecret())
47+
require.False(t, val.IsOutput())
48+
return val, nil
49+
}
50+
51+
_, err := TransformPropertyValue(resource.PropertyPath{}, visitor, result)
52+
require.NoError(t, err)
53+
})
54+
}

0 commit comments

Comments
 (0)