pulumi/pkg/resource/properties_path_test.go
Pat Gavlin 67ec74bdc5
Pass ignoreChanges to providers. (#3005)
These changes add support for passing `ignoreChanges` paths to resource
providers. This is intended to accommodate providers that perform diffs
between resource inputs and resource state (e.g. all Terraform-based
providers, the k8s provider when using API server dry-runs). These paths
are specified using the same syntax as the paths used in detailed diffs.

In addition to passing these paths to providers, the existing support
for `ignoreChanges` in inputs has been extended to accept paths rather
than top-level keys. It is an error to specify a path that is missing
one or more component in the old or new inputs.

Fixes #2936, #2663.
2019-07-31 11:39:07 -05:00

165 lines
3 KiB
Go

package resource
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPropertyPath(t *testing.T) {
value := NewObjectProperty(NewPropertyMapFromMap(map[string]interface{}{
"root": map[string]interface{}{
"nested": map[string]interface{}{
"array": []interface{}{
map[string]interface{}{
"double": []interface{}{
nil,
true,
},
},
},
},
"double": map[string]interface{}{
"nest": true,
},
"array": []interface{}{
map[string]interface{}{
"nested": true,
},
true,
},
"array2": []interface{}{
[]interface{}{
nil,
map[string]interface{}{
"nested": true,
},
},
},
`key with "escaped" quotes`: true,
"key with a .": true,
},
`root key with "escaped" quotes`: map[string]interface{}{
"nested": true,
},
"root key with a .": []interface{}{
nil,
true,
},
}))
cases := []struct {
path string
parsed PropertyPath
}{
{
"root",
PropertyPath{"root"},
},
{
"root.nested",
PropertyPath{"root", "nested"},
},
{
`root["nested"]`,
PropertyPath{"root", "nested"},
},
{
"root.double.nest",
PropertyPath{"root", "double", "nest"},
},
{
`root["double"].nest`,
PropertyPath{"root", "double", "nest"},
},
{
`root["double"]["nest"]`,
PropertyPath{"root", "double", "nest"},
},
{
"root.array[0]",
PropertyPath{"root", "array", 0},
},
{
"root.array[1]",
PropertyPath{"root", "array", 1},
},
{
"root.array[0].nested",
PropertyPath{"root", "array", 0, "nested"},
},
{
"root.array2[0][1].nested",
PropertyPath{"root", "array2", 0, 1, "nested"},
},
{
"root.nested.array[0].double[1]",
PropertyPath{"root", "nested", "array", 0, "double", 1},
},
{
`root["key with \"escaped\" quotes"]`,
PropertyPath{"root", `key with "escaped" quotes`},
},
{
`root["key with a ."]`,
PropertyPath{"root", "key with a ."},
},
{
`["root key with \"escaped\" quotes"].nested`,
PropertyPath{`root key with "escaped" quotes`, "nested"},
},
{
`["root key with a ."][1]`,
PropertyPath{"root key with a .", 1},
},
}
for _, c := range cases {
t.Run(c.path, func(t *testing.T) {
parsed, err := ParsePropertyPath(c.path)
assert.NoError(t, err)
assert.Equal(t, c.parsed, parsed)
v, ok := parsed.Get(value)
assert.True(t, ok)
assert.False(t, v.IsNull())
ok = parsed.Delete(value)
assert.True(t, ok)
ok = parsed.Set(value, v)
assert.True(t, ok)
u, ok := parsed.Get(value)
assert.True(t, ok)
assert.Equal(t, v, u)
})
}
negativeCases := []string{
// Syntax errors
"root[",
`root["nested]`,
`root."double".nest`,
"root.array[abc]",
"root.[1]",
// Missing values
"root[1]",
"root.nested.array[100]",
"root.nested.array.bar",
"foo",
}
for _, c := range negativeCases {
t.Run(c, func(t *testing.T) {
parsed, err := ParsePropertyPath(c)
if err == nil {
v, ok := parsed.Get(value)
assert.False(t, ok)
assert.True(t, v.IsNull())
}
})
}
}