pulumi/sdk/go/common/resource/plugin/provider_test.go

229 lines
5.4 KiB
Go
Raw Normal View History

// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package plugin
import (
"testing"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/stretchr/testify/assert"
Fix PropertyPaths generated by NewDetailedDiffFromObjectDiff (#14337) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/14335. This was incorrectly building property keys by just splicing "." inbetween every key. This was incorrect for keys which required quoting (e.g. those with "." in the key itself, or other special characters). ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-10-25 10:39:03 +00:00
"github.com/stretchr/testify/require"
)
func TestNewDetailedDiff(t *testing.T) {
t.Parallel()
cases := []struct {
name string
diff *resource.ObjectDiff
expected map[string]PropertyDiff
}{
{
name: "updates",
diff: resource.NewPropertyMapFromMap(map[string]interface{}{
"a": 1,
"b": map[string]interface{}{
"c": 2,
"d": 3,
},
}).Diff(resource.NewPropertyMapFromMap(map[string]interface{}{
"a": -1,
"b": map[string]interface{}{
"c": -2,
"d": 3,
},
})),
expected: map[string]PropertyDiff{
"a": {
Kind: DiffUpdate,
},
"b.c": {
Kind: DiffUpdate,
},
},
},
{
name: "adds and deletes",
diff: resource.NewPropertyMapFromMap(map[string]interface{}{
"b": map[string]interface{}{
"c": 2,
"d": 3,
},
}).Diff(resource.NewPropertyMapFromMap(map[string]interface{}{
"a": 1,
"b": map[string]interface{}{
"d": 3,
},
})),
expected: map[string]PropertyDiff{
"a": {
Kind: DiffAdd,
},
"b.c": {
Kind: DiffDelete,
},
},
},
{
name: "arrays",
diff: resource.NewPropertyMapFromMap(map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"a": 1,
"b": []interface{}{
2,
3,
},
},
},
}).Diff(resource.NewPropertyMapFromMap(
map[string]interface{}{
"a": []interface{}{
map[string]interface{}{
"a": -1,
"b": []interface{}{
2,
},
},
4,
},
})),
expected: map[string]PropertyDiff{
"a[0].a": {
Kind: DiffUpdate,
},
"a[0].b[1]": {
Kind: DiffDelete,
},
"a[1]": {
Kind: DiffAdd,
},
},
},
{
name: "nil diff",
diff: nil,
expected: map[string]PropertyDiff{},
},
}
for _, c := range cases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
Mark diff as an input diff when auto-diffing in the step generator (#14256) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/14040 When a provider returns `DiffUnknown` the step generator calculates a simple diff based on the old and new inputs. We were not correctly marking that this is an input diff, and so when reconstructing objects from the detailed diff later in `TranslateDetailedDiff` we we're looking at the old output properties rather than the old input properties. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. --> --------- Co-authored-by: Justin Van Patten <jvp@justinvp.com>
2023-10-18 10:33:04 +00:00
actual := NewDetailedDiffFromObjectDiff(c.diff, false)
assert.Equal(t, c.expected, actual)
})
}
}
// Assert that UnimplementedProvider implements Provider
var _ = Provider((*UnimplementedProvider)(nil))
Fix PropertyPaths generated by NewDetailedDiffFromObjectDiff (#14337) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Fixes https://github.com/pulumi/pulumi/issues/14335. This was incorrectly building property keys by just splicing "." inbetween every key. This was incorrect for keys which required quoting (e.g. those with "." in the key itself, or other special characters). ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-10-25 10:39:03 +00:00
// Regression test for https://github.com/pulumi/pulumi/issues/14335.
// Ensure that NewDetailedDiffFromObjectDiff builds correct keys.
func TestNewDetailedDiffFromObjectDiff(t *testing.T) {
t.Parallel()
cases := map[string]struct {
diff *resource.ObjectDiff
inputDiff bool
expected map[string]PropertyDiff
expectedPaths map[string]resource.PropertyPath
}{
"simple add": {
diff: &resource.ObjectDiff{
Adds: resource.PropertyMap{
"a": resource.NewPropertyValue(1),
},
},
expected: map[string]PropertyDiff{
"a": {Kind: DiffAdd},
},
expectedPaths: map[string]resource.PropertyPath{
"a": {"a"},
},
},
"simple update": {
diff: &resource.ObjectDiff{
Updates: map[resource.PropertyKey]resource.ValueDiff{
"a": *resource.NewPropertyValue(1).Diff(resource.NewPropertyValue(2)),
},
},
expected: map[string]PropertyDiff{
"a": {Kind: DiffUpdate},
},
expectedPaths: map[string]resource.PropertyPath{
"a": {"a"},
},
},
"nested update": {
diff: &resource.ObjectDiff{
Updates: map[resource.PropertyKey]resource.ValueDiff{
"a": *resource.NewObjectProperty(resource.PropertyMap{
"b": resource.NewPropertyValue(1),
}).Diff(resource.NewObjectProperty(resource.PropertyMap{
"b": resource.NewPropertyValue(2),
})),
},
},
expected: map[string]PropertyDiff{
"a.b": {Kind: DiffUpdate},
},
expectedPaths: map[string]resource.PropertyPath{
"a.b": {"a", "b"},
},
},
"nested update with quoted keys": {
diff: &resource.ObjectDiff{
Updates: map[resource.PropertyKey]resource.ValueDiff{
"a": *resource.NewObjectProperty(resource.PropertyMap{
"b.c": resource.NewPropertyValue(1),
`"quoted key"`: resource.NewPropertyValue(2),
}).Diff(resource.NewObjectProperty(resource.PropertyMap{
"b.c": resource.NewPropertyValue(2),
`"quoted key"`: resource.NewPropertyValue(3),
})),
},
},
expected: map[string]PropertyDiff{
`a["\"quoted key\""]`: {Kind: DiffUpdate},
`a["b.c"]`: {Kind: DiffUpdate},
},
expectedPaths: map[string]resource.PropertyPath{
`a["\"quoted key\""]`: {"a", `"quoted key"`},
`a["b.c"]`: {"a", "b.c"},
},
},
}
for name, tt := range cases {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
result := NewDetailedDiffFromObjectDiff(tt.diff, tt.inputDiff)
assert.Equal(t, tt.expected, result)
for k := range result {
path, err := resource.ParsePropertyPath(k)
require.NoError(t, err)
assert.Equal(t, tt.expectedPaths[k], path)
}
})
}
}