2018-05-22 21:43:36 +02: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-07-17 19:38:57 +02:00
|
|
|
|
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
2017-10-22 22:39:21 +02:00
|
|
|
"fmt"
|
2018-10-31 03:59:36 +01:00
|
|
|
"runtime"
|
2017-07-17 19:38:57 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-08-07 01:32:05 +02:00
|
|
|
structpb "github.com/golang/protobuf/ptypes/struct"
|
2017-07-17 19:38:57 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-03-18 21:36:19 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/resource"
|
2020-03-18 22:40:07 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/contract"
|
2017-07-17 19:38:57 +02:00
|
|
|
)
|
|
|
|
|
2019-08-07 01:32:05 +02:00
|
|
|
func setProperty(s *structpb.Value, k string, v interface{}) {
|
|
|
|
marshaled, err := MarshalPropertyValue(resource.NewPropertyValue(v), MarshalOptions{})
|
|
|
|
contract.Assert(err == nil)
|
|
|
|
s.GetStructValue().Fields[k] = marshaled
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:38:57 +02:00
|
|
|
func TestAssetSerialize(t *testing.T) {
|
|
|
|
// Ensure that asset and archive serialization round trips.
|
|
|
|
text := "a test asset"
|
2017-10-22 22:39:21 +02:00
|
|
|
asset, err := resource.NewTextAsset(text)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, text, asset.Text)
|
|
|
|
assert.Equal(t, "e34c74529110661faae4e121e57165ff4cb4dbdde1ef9770098aa3695e6b6704", asset.Hash)
|
2017-09-15 01:40:44 +02:00
|
|
|
assetProps, err := MarshalPropertyValue(resource.NewAssetProperty(asset), MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
2017-10-22 22:39:21 +02:00
|
|
|
fmt.Printf("%v\n", assetProps)
|
2017-09-15 01:40:44 +02:00
|
|
|
assetValue, err := UnmarshalPropertyValue(assetProps, MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
2017-07-17 19:38:57 +02:00
|
|
|
assert.True(t, assetValue.IsAsset())
|
|
|
|
assetDes := assetValue.AssetValue()
|
|
|
|
assert.True(t, assetDes.IsText())
|
|
|
|
assert.Equal(t, text, assetDes.Text)
|
2017-10-22 22:39:21 +02:00
|
|
|
assert.Equal(t, "e34c74529110661faae4e121e57165ff4cb4dbdde1ef9770098aa3695e6b6704", assetDes.Hash)
|
2017-07-17 19:38:57 +02:00
|
|
|
|
2019-08-07 01:32:05 +02:00
|
|
|
// Ensure that an invalid asset produces an error.
|
|
|
|
setProperty(assetProps, resource.AssetHashProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(assetProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(assetProps, resource.AssetHashProperty, asset.Hash)
|
|
|
|
|
|
|
|
setProperty(assetProps, resource.AssetTextProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(assetProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(assetProps, resource.AssetTextProperty, "")
|
|
|
|
|
|
|
|
setProperty(assetProps, resource.AssetPathProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(assetProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(assetProps, resource.AssetPathProperty, "")
|
|
|
|
|
|
|
|
setProperty(assetProps, resource.AssetURIProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(assetProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(assetProps, resource.AssetURIProperty, "")
|
|
|
|
|
2017-10-22 22:39:21 +02:00
|
|
|
arch, err := resource.NewAssetArchive(map[string]interface{}{"foo": asset})
|
|
|
|
assert.Nil(t, err)
|
2018-10-31 03:59:36 +01:00
|
|
|
switch runtime.Version() {
|
|
|
|
case "go1.9":
|
|
|
|
assert.Equal(t, "d8ce0142b3b10300c7c76487fad770f794c1e84e1b0c73a4b2e1503d4fbac093", arch.Hash)
|
|
|
|
default:
|
|
|
|
// Go 1.10 introduced breaking changes to archive/zip and archive/tar headers
|
|
|
|
assert.Equal(t, "27ab4a14a617df10cff3e1cf4e30cf510302afe56bf4cc91f84041c9f7b62fd8", arch.Hash)
|
|
|
|
}
|
2017-09-15 01:40:44 +02:00
|
|
|
archProps, err := MarshalPropertyValue(resource.NewArchiveProperty(arch), MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
archValue, err := UnmarshalPropertyValue(archProps, MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
2017-07-17 19:38:57 +02:00
|
|
|
assert.True(t, archValue.IsArchive())
|
|
|
|
archDes := archValue.ArchiveValue()
|
|
|
|
assert.True(t, archDes.IsAssets())
|
|
|
|
assert.Equal(t, 1, len(archDes.Assets))
|
2017-10-22 22:39:21 +02:00
|
|
|
assert.True(t, archDes.Assets["foo"].(*resource.Asset).IsText())
|
|
|
|
assert.Equal(t, text, archDes.Assets["foo"].(*resource.Asset).Text)
|
2018-10-31 03:59:36 +01:00
|
|
|
switch runtime.Version() {
|
|
|
|
case "go1.9":
|
|
|
|
assert.Equal(t, "d8ce0142b3b10300c7c76487fad770f794c1e84e1b0c73a4b2e1503d4fbac093", archDes.Hash)
|
|
|
|
default:
|
|
|
|
// Go 1.10 introduced breaking changes to archive/zip and archive/tar headers
|
|
|
|
assert.Equal(t, "27ab4a14a617df10cff3e1cf4e30cf510302afe56bf4cc91f84041c9f7b62fd8", archDes.Hash)
|
|
|
|
}
|
2019-08-07 01:32:05 +02:00
|
|
|
|
|
|
|
// Ensure that an invalid archive produces an error.
|
|
|
|
setProperty(archProps, resource.ArchiveHashProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(archProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(archProps, resource.ArchiveHashProperty, arch.Hash)
|
|
|
|
|
|
|
|
setProperty(archProps, resource.ArchiveAssetsProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(archProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(archProps, resource.ArchiveAssetsProperty, nil)
|
|
|
|
|
|
|
|
setProperty(archProps, resource.ArchivePathProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(archProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(archProps, resource.ArchivePathProperty, "")
|
|
|
|
|
|
|
|
setProperty(archProps, resource.ArchiveURIProperty, 0)
|
|
|
|
_, err = UnmarshalPropertyValue(archProps, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
setProperty(archProps, resource.ArchiveURIProperty, "")
|
2017-07-17 19:38:57 +02:00
|
|
|
}
|
2017-07-31 20:38:31 +02:00
|
|
|
|
|
|
|
func TestComputedSerialize(t *testing.T) {
|
|
|
|
// Ensure that computed properties survive round trips.
|
2017-11-03 21:40:15 +01:00
|
|
|
opts := MarshalOptions{KeepUnknowns: true}
|
2017-07-31 20:38:31 +02:00
|
|
|
{
|
2017-09-15 01:40:44 +02:00
|
|
|
cprop, err := MarshalPropertyValue(
|
2017-07-31 20:38:31 +02:00
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewStringProperty("")}), opts)
|
2017-09-15 01:40:44 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
cpropU, err := UnmarshalPropertyValue(cprop, opts)
|
|
|
|
assert.Nil(t, err)
|
2017-07-31 20:38:31 +02:00
|
|
|
assert.True(t, cpropU.IsComputed())
|
2018-02-05 23:44:23 +01:00
|
|
|
assert.True(t, cpropU.Input().Element.IsString())
|
2017-07-31 20:38:31 +02:00
|
|
|
}
|
|
|
|
{
|
2017-09-15 01:40:44 +02:00
|
|
|
cprop, err := MarshalPropertyValue(
|
2017-07-31 20:38:31 +02:00
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewNumberProperty(0)}), opts)
|
2017-09-15 01:40:44 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
cpropU, err := UnmarshalPropertyValue(cprop, opts)
|
|
|
|
assert.Nil(t, err)
|
2017-07-31 20:38:31 +02:00
|
|
|
assert.True(t, cpropU.IsComputed())
|
2018-02-05 23:44:23 +01:00
|
|
|
assert.True(t, cpropU.Input().Element.IsNumber())
|
2017-07-31 20:38:31 +02:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 21:40:15 +01:00
|
|
|
|
|
|
|
func TestComputedSkip(t *testing.T) {
|
|
|
|
// Ensure that computed properties are skipped when KeepUnknowns == false.
|
|
|
|
opts := MarshalOptions{KeepUnknowns: false}
|
|
|
|
{
|
|
|
|
cprop, err := MarshalPropertyValue(
|
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewStringProperty("")}), opts)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, cprop)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cprop, err := MarshalPropertyValue(
|
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewNumberProperty(0)}), opts)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, cprop)
|
|
|
|
}
|
|
|
|
}
|
2018-01-09 21:21:47 +01:00
|
|
|
|
|
|
|
func TestComputedReject(t *testing.T) {
|
|
|
|
// Ensure that computed properties produce errors when RejectUnknowns == true.
|
|
|
|
opts := MarshalOptions{RejectUnknowns: true}
|
|
|
|
{
|
|
|
|
cprop, err := MarshalPropertyValue(
|
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewStringProperty("")}), opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, cprop)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
cprop, err := MarshalPropertyValue(
|
|
|
|
resource.NewComputedProperty(
|
|
|
|
resource.Computed{Element: resource.NewStringProperty("")}), MarshalOptions{KeepUnknowns: true})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
cpropU, err := UnmarshalPropertyValue(cprop, opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, cpropU)
|
|
|
|
}
|
|
|
|
}
|
2019-01-29 02:38:16 +01:00
|
|
|
|
2019-08-24 21:27:39 +02:00
|
|
|
func TestAssetReject(t *testing.T) {
|
|
|
|
// Ensure that asset and archive properties produce errors when RejectAssets == true.
|
|
|
|
|
|
|
|
opts := MarshalOptions{RejectAssets: true}
|
|
|
|
|
|
|
|
text := "a test asset"
|
|
|
|
asset, err := resource.NewTextAsset(text)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
{
|
|
|
|
assetProps, err := MarshalPropertyValue(resource.NewAssetProperty(asset), opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, assetProps)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assetProps, err := MarshalPropertyValue(resource.NewAssetProperty(asset), MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assetPropU, err := UnmarshalPropertyValue(assetProps, opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, assetPropU)
|
|
|
|
}
|
|
|
|
|
|
|
|
arch, err := resource.NewAssetArchive(map[string]interface{}{"foo": asset})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
{
|
|
|
|
archProps, err := MarshalPropertyValue(resource.NewArchiveProperty(arch), opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, archProps)
|
|
|
|
}
|
|
|
|
{
|
|
|
|
archProps, err := MarshalPropertyValue(resource.NewArchiveProperty(arch), MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
archValue, err := UnmarshalPropertyValue(archProps, opts)
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
assert.Nil(t, archValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-29 02:38:16 +01:00
|
|
|
func TestUnsupportedSecret(t *testing.T) {
|
|
|
|
rawProp := resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]interface{}{
|
|
|
|
resource.SigKey: resource.SecretSig,
|
2020-02-26 02:45:36 +01:00
|
|
|
"value": "foo",
|
2019-01-29 02:38:16 +01:00
|
|
|
}))
|
|
|
|
prop, err := MarshalPropertyValue(rawProp, MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
2020-02-26 02:45:36 +01:00
|
|
|
val, err := UnmarshalPropertyValue(prop, MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.True(t, val.IsString())
|
|
|
|
assert.False(t, val.IsSecret())
|
|
|
|
assert.Equal(t, "foo", val.StringValue())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSupportedSecret(t *testing.T) {
|
|
|
|
rawProp := resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]interface{}{
|
|
|
|
resource.SigKey: resource.SecretSig,
|
|
|
|
"value": "foo",
|
|
|
|
}))
|
|
|
|
prop, err := MarshalPropertyValue(rawProp, MarshalOptions{KeepSecrets: true})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
val, err := UnmarshalPropertyValue(prop, MarshalOptions{KeepSecrets: true})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, val.IsString())
|
|
|
|
assert.True(t, val.IsSecret())
|
|
|
|
assert.Equal(t, "foo", val.SecretValue().Element.StringValue())
|
2019-01-29 02:38:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnknownSig(t *testing.T) {
|
|
|
|
rawProp := resource.NewObjectProperty(resource.NewPropertyMapFromMap(map[string]interface{}{
|
|
|
|
resource.SigKey: "foobar",
|
|
|
|
}))
|
|
|
|
prop, err := MarshalPropertyValue(rawProp, MarshalOptions{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
_, err = UnmarshalPropertyValue(prop, MarshalOptions{})
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
}
|
2020-03-18 19:39:13 +01:00
|
|
|
|
|
|
|
func TestSkipInternalKeys(t *testing.T) {
|
|
|
|
opts := MarshalOptions{SkipInternalKeys: true}
|
|
|
|
expected := &structpb.Struct{
|
|
|
|
Fields: map[string]*structpb.Value{
|
|
|
|
"keepers": {
|
|
|
|
Kind: &structpb.Value_StructValue{
|
|
|
|
StructValue: &structpb.Struct{
|
|
|
|
Fields: map[string]*structpb.Value{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
props := resource.NewPropertyMapFromMap(map[string]interface{}{
|
|
|
|
"__defaults": []string{},
|
|
|
|
"keepers": map[string]interface{}{
|
|
|
|
"__defaults": []string{},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
actual, err := MarshalProperties(props, opts)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|