pulumi/sdk/go/common/resource/properties_diff_test.go

373 lines
13 KiB
Go
Raw Permalink Normal View History

2018-05-22 19:43:36 +00:00
// Copyright 2016-2018, 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.
2017-06-01 22:36:22 +00:00
package resource
import (
"os"
2017-06-01 22:36:22 +00:00
"testing"
"github.com/stretchr/testify/assert"
Move assets and archives to their own package (#15157) <!--- 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 This PR is motivated by https://github.com/pulumi/pulumi/pull/15145. `resource.*` should be built on top of `property.Value`,[^1] which means that `resource` needs to be able to import `property.Value`, and so `property` cannot import `resource`. Since Assets and Archives are both types of properties, they must be moved out of `resource`. [^1]: For example: https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36 ## Open Question This PR moves them to their own sub-folders in `resource`. Should `asset` and `archive` live somewhere more high level, like `sdk/go/property/{asset,archive}`? <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] 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. -->
2024-01-25 20:39:31 +00:00
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/archive"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
2017-06-01 22:36:22 +00:00
)
func assertDeepEqualsIffEmptyDiff(t *testing.T, val1, val2 PropertyValue) {
diff := val1.Diff(val2)
equals := val1.DeepEquals(val2)
assert.Equal(t, diff == nil, equals, "DeepEquals <--> empty diff")
}
2017-06-01 22:36:22 +00:00
func TestNullPropertyValueDiffs(t *testing.T) {
t.Parallel()
d1 := NewNullProperty().Diff(NewNullProperty())
assert.Nil(t, d1)
d2 := NewNullProperty().Diff(NewProperty(true))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d2)
assert.Nil(t, d2.Array)
assert.Nil(t, d2.Object)
assert.True(t, d2.Old.IsNull())
assert.True(t, d2.New.IsBool())
assert.Equal(t, true, d2.New.BoolValue())
}
func TestBoolPropertyValueDiffs(t *testing.T) {
t.Parallel()
d1 := NewProperty(true).Diff(NewProperty(true))
2017-06-01 22:36:22 +00:00
assert.Nil(t, d1)
d2 := NewProperty(true).Diff(NewProperty(false))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d2)
assert.Nil(t, d2.Array)
assert.Nil(t, d2.Object)
assert.True(t, d2.Old.IsBool())
assert.Equal(t, true, d2.Old.BoolValue())
assert.True(t, d2.New.IsBool())
assert.Equal(t, false, d2.New.BoolValue())
d3 := NewProperty(true).Diff(NewNullProperty())
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d3)
assert.Nil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.True(t, d3.Old.IsBool())
assert.Equal(t, true, d3.Old.BoolValue())
assert.True(t, d3.New.IsNull())
}
func TestNumberPropertyValueDiffs(t *testing.T) {
t.Parallel()
d1 := NewProperty(42.0).Diff(NewProperty(42.0))
2017-06-01 22:36:22 +00:00
assert.Nil(t, d1)
d2 := NewProperty(42.0).Diff(NewProperty(66.0))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d2)
assert.Nil(t, d2.Array)
assert.Nil(t, d2.Object)
assert.True(t, d2.Old.IsNumber())
assert.Equal(t, float64(42), d2.Old.NumberValue())
assert.True(t, d2.New.IsNumber())
assert.Equal(t, float64(66), d2.New.NumberValue())
d3 := NewProperty(88.0).Diff(NewProperty(true))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d3)
assert.Nil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.True(t, d3.Old.IsNumber())
assert.Equal(t, float64(88), d3.Old.NumberValue())
assert.True(t, d3.New.IsBool())
assert.Equal(t, true, d3.New.BoolValue())
}
func TestStringPropertyValueDiffs(t *testing.T) {
t.Parallel()
d1 := NewProperty("a string").Diff(NewProperty("a string"))
2017-06-01 22:36:22 +00:00
assert.Nil(t, d1)
d2 := NewProperty("a string").Diff(NewProperty("some other string"))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d2)
assert.True(t, d2.Old.IsString())
assert.Equal(t, "a string", d2.Old.StringValue())
assert.True(t, d2.New.IsString())
assert.Equal(t, "some other string", d2.New.StringValue())
d3 := NewProperty("what a string").Diff(NewProperty(973.0))
2017-06-01 22:36:22 +00:00
assert.NotNil(t, d3)
assert.Nil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.True(t, d3.Old.IsString())
assert.Equal(t, "what a string", d3.Old.StringValue(), "what a string")
assert.True(t, d3.New.IsNumber())
assert.Equal(t, float64(973), d3.New.NumberValue())
}
func TestArrayPropertyValueDiffs(t *testing.T) {
t.Parallel()
// no diffs:
d1 := NewProperty([]PropertyValue{}).Diff(NewProperty([]PropertyValue{}))
2017-06-01 22:36:22 +00:00
assert.Nil(t, d1)
d2 := NewProperty([]PropertyValue{
NewProperty("element one"), NewProperty(2.0), NewNullProperty(),
}).Diff(NewProperty([]PropertyValue{
NewProperty("element one"), NewProperty(2.0), NewNullProperty(),
2017-06-01 22:36:22 +00:00
}))
assert.Nil(t, d2)
// all updates:
d3a1 := NewProperty([]PropertyValue{
NewProperty("element one"), NewProperty(2.0), NewNullProperty(),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
d3a2 := NewProperty([]PropertyValue{
NewProperty(1.0), NewNullProperty(), NewProperty("element three"),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
assertDeepEqualsIffEmptyDiff(t, NewPropertyValue(d3a1), NewPropertyValue(d3a2))
2017-06-01 22:36:22 +00:00
d3 := d3a1.Diff(d3a2)
assert.NotNil(t, d3)
assert.NotNil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.Equal(t, 0, len(d3.Array.Adds))
assert.Equal(t, 0, len(d3.Array.Deletes))
assert.Equal(t, 0, len(d3.Array.Sames))
assert.Equal(t, 3, len(d3.Array.Updates))
for i, update := range d3.Array.Updates {
assert.Equal(t, d3a1.ArrayValue()[i], update.Old)
assert.Equal(t, d3a2.ArrayValue()[i], update.New)
}
// update one, keep one, delete one:
d4a1 := NewProperty([]PropertyValue{
NewProperty("element one"), NewProperty(2.0), NewProperty(true),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
d4a2 := NewProperty([]PropertyValue{
NewProperty("element 1"), NewProperty(2.0),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
assertDeepEqualsIffEmptyDiff(t, NewPropertyValue(d4a1), NewPropertyValue(d4a2))
2017-06-01 22:36:22 +00:00
d4 := d4a1.Diff(d4a2)
assert.NotNil(t, d4)
assert.NotNil(t, d4.Array)
assert.Nil(t, d4.Object)
assert.Equal(t, 0, len(d4.Array.Adds))
assert.Equal(t, 1, len(d4.Array.Deletes))
for i, delete := range d4.Array.Deletes {
assert.Equal(t, 2, i)
assert.Equal(t, d4a1.ArrayValue()[i], delete)
}
assert.Equal(t, 1, len(d4.Array.Sames))
for i, same := range d4.Array.Sames {
assert.Equal(t, 1, i)
assert.Equal(t, d4a1.ArrayValue()[i], same)
assert.Equal(t, d4a2.ArrayValue()[i], same)
}
assert.Equal(t, 1, len(d4.Array.Updates))
for i, update := range d4.Array.Updates {
assert.Equal(t, 0, i)
assert.Equal(t, d4a1.ArrayValue()[i], update.Old)
assert.Equal(t, d4a2.ArrayValue()[i], update.New)
}
// keep one, update one, add one:
d5a1 := NewProperty([]PropertyValue{
NewProperty("element one"), NewProperty(2.0),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
d5a2 := NewProperty([]PropertyValue{
NewProperty("element 1"), NewProperty(2.0), NewProperty(true),
all: Reformat with gofumpt Per team discussion, switching to gofumpt. [gofumpt][1] is an alternative, stricter alternative to gofmt. It addresses other stylistic concerns that gofmt doesn't yet cover. [1]: https://github.com/mvdan/gofumpt See the full list of [Added rules][2], but it includes: - Dropping empty lines around function bodies - Dropping unnecessary variable grouping when there's only one variable - Ensuring an empty line between multi-line functions - simplification (`-s` in gofmt) is always enabled - Ensuring multi-line function signatures end with `) {` on a separate line. [2]: https://github.com/mvdan/gofumpt#Added-rules gofumpt is stricter, but there's no lock-in. All gofumpt output is valid gofmt output, so if we decide we don't like it, it's easy to switch back without any code changes. gofumpt support is built into the tooling we use for development so this won't change development workflows. - golangci-lint includes a gofumpt check (enabled in this PR) - gopls, the LSP for Go, includes a gofumpt option (see [installation instrutions][3]) [3]: https://github.com/mvdan/gofumpt#installation This change was generated by running: ```bash gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error) ``` The following files were manually tweaked afterwards: - pkg/cmd/pulumi/stack_change_secrets_provider.go: one of the lines overflowed and had comments in an inconvenient place - pkg/cmd/pulumi/destroy.go: `var x T = y` where `T` wasn't necessary - pkg/cmd/pulumi/policy_new.go: long line because of error message - pkg/backend/snapshot_test.go: long line trying to assign three variables in the same assignment I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 16:36:39 +00:00
})
assertDeepEqualsIffEmptyDiff(t, NewPropertyValue(d5a1), NewPropertyValue(d5a2))
2017-06-01 22:36:22 +00:00
d5 := d5a1.Diff(d5a2)
assert.NotNil(t, d5)
assert.NotNil(t, d5.Array)
assert.Nil(t, d5.Object)
assert.Equal(t, 1, len(d5.Array.Adds))
for i, add := range d5.Array.Adds {
assert.Equal(t, 2, i)
assert.Equal(t, d5a2.ArrayValue()[i], add)
}
assert.Equal(t, 0, len(d5.Array.Deletes))
assert.Equal(t, 1, len(d5.Array.Sames))
for i, same := range d5.Array.Sames {
assert.Equal(t, 1, i)
assert.Equal(t, d5a1.ArrayValue()[i], same)
assert.Equal(t, d5a2.ArrayValue()[i], same)
}
assert.Equal(t, 1, len(d5.Array.Updates))
for i, update := range d5.Array.Updates {
assert.Equal(t, 0, i)
assert.Equal(t, d5a1.ArrayValue()[i], update.Old)
assert.Equal(t, d5a2.ArrayValue()[i], update.New)
}
// from nil to empty array:
d6 := NewNullProperty().Diff(NewProperty([]PropertyValue{}))
assert.NotNil(t, d6)
2017-06-01 22:36:22 +00:00
}
func TestObjectPropertyValueDiffs(t *testing.T) {
t.Parallel()
// no diffs:
d1 := PropertyMap{}.Diff(PropertyMap{})
assert.Nil(t, d1)
d2 := PropertyMap{
PropertyKey("a"): NewProperty(true),
2017-06-01 22:36:22 +00:00
}.Diff(PropertyMap{
PropertyKey("a"): NewProperty(true),
2017-06-01 22:36:22 +00:00
})
assert.Nil(t, d2)
// all updates:
{
obj1 := PropertyMap{
PropertyKey("prop-a"): NewProperty(true),
PropertyKey("prop-b"): NewProperty("bbb"),
PropertyKey("prop-c"): NewProperty(PropertyMap{
PropertyKey("inner-prop-a"): NewProperty(673.0),
2017-06-01 22:36:22 +00:00
}),
}
obj2 := PropertyMap{
PropertyKey("prop-a"): NewProperty(false),
PropertyKey("prop-b"): NewProperty(89.0),
PropertyKey("prop-c"): NewProperty(PropertyMap{
PropertyKey("inner-prop-a"): NewProperty(672.0),
2017-06-01 22:36:22 +00:00
}),
}
assertDeepEqualsIffEmptyDiff(t, NewPropertyValue(obj1), NewPropertyValue(obj2))
2017-06-01 22:36:22 +00:00
d3 := obj1.Diff(obj2)
assert.NotNil(t, d3)
assert.Equal(t, 0, len(d3.Adds))
assert.Equal(t, 0, len(d3.Deletes))
assert.Equal(t, 0, len(d3.Sames))
assert.Equal(t, 3, len(d3.Updates))
d3pa := d3.Updates[PropertyKey("prop-a")]
assert.Nil(t, d3pa.Array)
assert.Nil(t, d3pa.Object)
assert.True(t, d3pa.Old.IsBool())
assert.Equal(t, true, d3pa.Old.BoolValue())
assert.True(t, d3pa.Old.IsBool())
assert.Equal(t, false, d3pa.New.BoolValue())
d3pb := d3.Updates[PropertyKey("prop-b")]
assert.Nil(t, d3pb.Array)
assert.Nil(t, d3pb.Object)
assert.True(t, d3pb.Old.IsString())
assert.Equal(t, "bbb", d3pb.Old.StringValue())
assert.True(t, d3pb.New.IsNumber())
assert.Equal(t, float64(89), d3pb.New.NumberValue())
d3pc := d3.Updates[PropertyKey("prop-c")]
assert.Nil(t, d3pc.Array)
assert.NotNil(t, d3pc.Object)
assert.Equal(t, 0, len(d3pc.Object.Adds))
assert.Equal(t, 0, len(d3pc.Object.Deletes))
assert.Equal(t, 0, len(d3pc.Object.Sames))
assert.Equal(t, 1, len(d3pc.Object.Updates))
d3pcu := d3pc.Object.Updates[PropertyKey("inner-prop-a")]
assert.True(t, d3pcu.Old.IsNumber())
assert.Equal(t, float64(673), d3pcu.Old.NumberValue())
assert.True(t, d3pcu.New.IsNumber())
assert.Equal(t, float64(672), d3pcu.New.NumberValue())
}
// add two (1 missing key, 1 null), update one, keep two, delete two (1 missing key, 1 null).
{
obj1 := PropertyMap{
PropertyKey("prop-a-2"): NewNullProperty(),
PropertyKey("prop-b"): NewProperty("bbb"),
PropertyKey("prop-c-1"): NewProperty(6767.0),
2017-06-01 22:36:22 +00:00
PropertyKey("prop-c-2"): NewNullProperty(),
PropertyKey("prop-d-1"): NewProperty(true),
PropertyKey("prop-d-2"): NewProperty(false),
2017-06-01 22:36:22 +00:00
}
obj2 := PropertyMap{
PropertyKey("prop-a-1"): NewProperty("a fresh value"),
PropertyKey("prop-a-2"): NewProperty("a non-nil value"),
PropertyKey("prop-b"): NewProperty(89.0),
PropertyKey("prop-c-1"): NewProperty(6767.0),
2017-06-01 22:36:22 +00:00
PropertyKey("prop-c-2"): NewNullProperty(),
PropertyKey("prop-d-2"): NewNullProperty(),
}
assertDeepEqualsIffEmptyDiff(t, NewPropertyValue(obj1), NewPropertyValue(obj2))
2017-06-01 22:36:22 +00:00
d4 := obj1.Diff(obj2)
assert.NotNil(t, d4)
assert.Equal(t, 2, len(d4.Adds))
assert.Equal(t, obj2[PropertyKey("prop-a-1")], d4.Adds[PropertyKey("prop-a-1")])
assert.Equal(t, obj2[PropertyKey("prop-a-2")], d4.Adds[PropertyKey("prop-a-2")])
assert.Equal(t, 2, len(d4.Deletes))
assert.Equal(t, obj1[PropertyKey("prop-d-1")], d4.Deletes[PropertyKey("prop-d-1")])
assert.Equal(t, obj1[PropertyKey("prop-d-2")], d4.Deletes[PropertyKey("prop-d-2")])
assert.Equal(t, 2, len(d4.Sames))
assert.Equal(t, obj1[PropertyKey("prop-c-1")], d4.Sames[PropertyKey("prop-c-1")])
assert.Equal(t, obj1[PropertyKey("prop-c-2")], d4.Sames[PropertyKey("prop-c-2")])
assert.Equal(t, obj2[PropertyKey("prop-c-1")], d4.Sames[PropertyKey("prop-c-1")])
assert.Equal(t, obj2[PropertyKey("prop-c-2")], d4.Sames[PropertyKey("prop-c-2")])
assert.Equal(t, 1, len(d4.Updates))
assert.Equal(t, obj1[PropertyKey("prop-b")], d4.Updates[PropertyKey("prop-b")].Old)
assert.Equal(t, obj2[PropertyKey("prop-b")], d4.Updates[PropertyKey("prop-b")].New)
}
}
2017-07-17 19:11:15 +00:00
func TestAssetPropertyValueDiffs(t *testing.T) {
t.Parallel()
Move assets and archives to their own package (#15157) <!--- 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 This PR is motivated by https://github.com/pulumi/pulumi/pull/15145. `resource.*` should be built on top of `property.Value`,[^1] which means that `resource` needs to be able to import `property.Value`, and so `property` cannot import `resource`. Since Assets and Archives are both types of properties, they must be moved out of `resource`. [^1]: For example: https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36 ## Open Question This PR moves them to their own sub-folders in `resource`. Should `asset` and `archive` live somewhere more high level, like `sdk/go/property/{asset,archive}`? <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] 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. -->
2024-01-25 20:39:31 +00:00
a1, err := asset.FromText("test")
assert.NoError(t, err)
d1 := NewProperty(a1).Diff(NewProperty(a1))
2017-07-17 19:11:15 +00:00
assert.Nil(t, d1)
Move assets and archives to their own package (#15157) <!--- 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 This PR is motivated by https://github.com/pulumi/pulumi/pull/15145. `resource.*` should be built on top of `property.Value`,[^1] which means that `resource` needs to be able to import `property.Value`, and so `property` cannot import `resource`. Since Assets and Archives are both types of properties, they must be moved out of `resource`. [^1]: For example: https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36 ## Open Question This PR moves them to their own sub-folders in `resource`. Should `asset` and `archive` live somewhere more high level, like `sdk/go/property/{asset,archive}`? <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] 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. -->
2024-01-25 20:39:31 +00:00
a2, err := asset.FromText("test2")
assert.NoError(t, err)
d2 := NewProperty(a1).Diff(NewProperty(a2))
2017-07-17 19:11:15 +00:00
assert.NotNil(t, d2)
assert.Nil(t, d2.Array)
assert.Nil(t, d2.Object)
assert.True(t, d2.Old.IsAsset())
assert.Equal(t, "test", d2.Old.AssetValue().Text)
assert.True(t, d2.New.IsAsset())
assert.Equal(t, "test2", d2.New.AssetValue().Text)
d3 := NewProperty(a1).Diff(NewNullProperty())
2017-07-17 19:11:15 +00:00
assert.NotNil(t, d3)
assert.Nil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.True(t, d3.Old.IsAsset())
assert.Equal(t, "test", d3.Old.AssetValue().Text)
assert.True(t, d3.New.IsNull())
}
func TestArchivePropertyValueDiffs(t *testing.T) {
t.Parallel()
Rework asset identity and exposure of old assets. (#548) Note: for the purposes of this discussion, archives will be treated as assets, as their differences are not particularly meaningful. Currently, the identity of an asset is derived from the hash and the location of its contents (i.e. two assets are equal iff their contents have the same hash and the same path/URI/inline value). This means that changing the source of an asset will cause the engine to detect a difference in the asset even if the source's contents are identical. At best, this leads to inefficiencies such as unnecessary updates. This commit changes asset identity so that it is derived solely from an asset's hash. The source of an asset's contents is no longer part of the asset's identity, and need only be provided if the contents themselves may need to be available (e.g. if a hash does not yet exist for the asset or if the asset's contents might be needed for an update). This commit also changes the way old assets are exposed to providers. Currently, an old asset is exposed as both its hash and its contents. This allows providers to take a dependency on the contents of an old asset being available, even though this is not an invariant of the system. These changes remove the contents of old assets from their serialized form when they are passed to providers, eliminating the ability of a provider to take such a dependency. In combination with the changes to asset identity, this allows a provider to detect changes to an asset simply by comparing its old and new hashes. This is half of the fix for [pulumi/pulumi-cloud#158]. The other half involves changes in [pulumi/pulumi-terraform].
2017-11-12 19:45:13 +00:00
path, err := tempArchive("test", false)
assert.NoError(t, err)
defer func() { contract.IgnoreError(os.Remove(path)) }()
Move assets and archives to their own package (#15157) <!--- 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 This PR is motivated by https://github.com/pulumi/pulumi/pull/15145. `resource.*` should be built on top of `property.Value`,[^1] which means that `resource` needs to be able to import `property.Value`, and so `property` cannot import `resource`. Since Assets and Archives are both types of properties, they must be moved out of `resource`. [^1]: For example: https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36 ## Open Question This PR moves them to their own sub-folders in `resource`. Should `asset` and `archive` live somewhere more high level, like `sdk/go/property/{asset,archive}`? <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] 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. -->
2024-01-25 20:39:31 +00:00
a1, err := archive.FromPath(path)
assert.NoError(t, err)
d1 := NewProperty(a1).Diff(NewProperty(a1))
2017-07-17 19:11:15 +00:00
assert.Nil(t, d1)
Rework asset identity and exposure of old assets. (#548) Note: for the purposes of this discussion, archives will be treated as assets, as their differences are not particularly meaningful. Currently, the identity of an asset is derived from the hash and the location of its contents (i.e. two assets are equal iff their contents have the same hash and the same path/URI/inline value). This means that changing the source of an asset will cause the engine to detect a difference in the asset even if the source's contents are identical. At best, this leads to inefficiencies such as unnecessary updates. This commit changes asset identity so that it is derived solely from an asset's hash. The source of an asset's contents is no longer part of the asset's identity, and need only be provided if the contents themselves may need to be available (e.g. if a hash does not yet exist for the asset or if the asset's contents might be needed for an update). This commit also changes the way old assets are exposed to providers. Currently, an old asset is exposed as both its hash and its contents. This allows providers to take a dependency on the contents of an old asset being available, even though this is not an invariant of the system. These changes remove the contents of old assets from their serialized form when they are passed to providers, eliminating the ability of a provider to take such a dependency. In combination with the changes to asset identity, this allows a provider to detect changes to an asset simply by comparing its old and new hashes. This is half of the fix for [pulumi/pulumi-cloud#158]. The other half involves changes in [pulumi/pulumi-terraform].
2017-11-12 19:45:13 +00:00
path2, err := tempArchive("test2", true)
assert.NoError(t, err)
defer func() { contract.IgnoreError(os.Remove(path)) }()
Move assets and archives to their own package (#15157) <!--- 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 This PR is motivated by https://github.com/pulumi/pulumi/pull/15145. `resource.*` should be built on top of `property.Value`,[^1] which means that `resource` needs to be able to import `property.Value`, and so `property` cannot import `resource`. Since Assets and Archives are both types of properties, they must be moved out of `resource`. [^1]: For example: https://github.com/pulumi/pulumi/blob/a1d686227cd7e3c70c51bd772450cb0cd57c1479/sdk/go/common/resource/resource_state.go#L35-L36 ## Open Question This PR moves them to their own sub-folders in `resource`. Should `asset` and `archive` live somewhere more high level, like `sdk/go/property/{asset,archive}`? <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [ ] 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. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] 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. -->
2024-01-25 20:39:31 +00:00
a2, err := archive.FromPath(path2)
assert.NoError(t, err)
d2 := NewProperty(a1).Diff(NewProperty(a2))
2017-07-17 19:11:15 +00:00
assert.NotNil(t, d2)
assert.Nil(t, d2.Array)
assert.Nil(t, d2.Object)
assert.True(t, d2.Old.IsArchive())
assert.Equal(t, path, d2.Old.ArchiveValue().Path)
2017-07-17 19:11:15 +00:00
assert.True(t, d2.New.IsArchive())
assert.Equal(t, path2, d2.New.ArchiveValue().Path)
d3 := NewProperty(a1).Diff(NewNullProperty())
2017-07-17 19:11:15 +00:00
assert.NotNil(t, d3)
assert.Nil(t, d3.Array)
assert.Nil(t, d3.Object)
assert.True(t, d3.Old.IsArchive())
assert.Equal(t, path, d3.Old.ArchiveValue().Path)
2017-07-17 19:11:15 +00:00
assert.True(t, d3.New.IsNull())
}
func TestMismatchedPropertyValueDiff(t *testing.T) {
t.Parallel()
a1 := NewPropertyValue([]string{"a", "b", "c"})
a2 := NewPropertyValue([]string{"a", "b", "c"})
s1 := MakeSecret(a1)
s2 := MakeSecret(a2)
assert.True(t, s2.DeepEquals(s1))
assert.True(t, s1.DeepEquals(s2))
}