Skip to content

Commit

Permalink
WIP on requested tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Jul 26, 2024
1 parent d3c9b59 commit fdef94a
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 1 deletion.
159 changes: 159 additions & 0 deletions infer/apply_secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright 2022, 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 infer

import (
"reflect"
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestApplySecrets(t *testing.T) {

type nested struct {
F1 string `pulumi:"f1"`
}

tests := []struct {
name string
input, expected resource.PropertyMap
typ reflect.Type
}{
{
name: "no-secrets",
typ: typeFor[struct {
F1 string `pulumi:"f1"`
F2 map[string]string `pulumi:"f2"`
F3 map[string]nested `pulumi:"F3"`
F4 []string `pulumi:"F4"`
F5 []nested `pulumi:"F5"`
}](),
input: resource.NewPropertyMapFromMap(map[string]any{
"f1": "v1",
"f2": map[string]any{
"n1": "v2",
},
"f3": map[string]any{
"k1": map[string]any{"f1": "v3"},
},
"f4": []any{
"v4",
"v5",
},
"f5": []any{
map[string]any{
"k1": map[string]any{
"f1": "v3",
},
},
},
}),
expected: resource.PropertyMap{
"f1": resource.NewProperty("v1"),
"f2": resource.NewProperty(resource.PropertyMap{
"n1": resource.NewProperty("v2"),
}),
"f3": resource.NewProperty(resource.PropertyMap{
"k1": resource.NewProperty(resource.PropertyMap{
"f1": resource.NewProperty("v3"),
}),
}),
"f4": resource.NewProperty([]resource.PropertyValue{
resource.NewProperty("v4"),
resource.NewProperty("v5"),
}),
"f5": resource.NewProperty([]resource.PropertyValue{
resource.NewProperty(resource.PropertyMap{
"k1": resource.NewProperty(resource.PropertyMap{
"f1": resource.NewProperty("v3"),
}),
}),
}),
},
},
{
name: "nested-secrets",
typ: typeFor[struct {
F1 struct {
F1 string `pulumi:"f1" provider:"secret"`
} `pulumi:"f1"`
F2 []struct {
F1 string `pulumi:"f1" provider:"secret"`
} `pulumi:"f2"`
F3 map[string]struct {
F1 string `pulumi:"f1" provider:"secret"`
} `pulumi:"f3"`
F4 struct {
F1 struct {
F1 string `pulumi:"f1" provider:"secret"`
} `pulumi:"f1"`
} `pulumi:"f4"`
}](),
input: resource.NewPropertyMapFromMap(map[string]any{
"f1": map[string]any{
"f1": "secret1",
},
"f2": []any{
map[string]any{
"f1": "secret2",
},
},
"f3": map[string]any{
"key1": map[string]any{
"f1": "secret3",
},
},
"f4": map[string]any{
"f1": map[string]any{
"f1": "secret4",
},
},
}),
expected: resource.PropertyMap{
"f1": resource.NewProperty(resource.PropertyMap{
"f1": resource.MakeSecret(resource.NewProperty("secret1")),
}),
"f2": resource.NewProperty([]resource.PropertyValue{
resource.NewProperty(resource.PropertyMap{
"f1": resource.MakeSecret(resource.NewProperty("secret2")),
}),
}),
"f3": resource.NewProperty(resource.PropertyMap{
"key1": resource.NewProperty(resource.PropertyMap{
"f1": resource.MakeSecret(resource.NewProperty("secret3")),
}),
}),
"f4": resource.NewProperty(resource.PropertyMap{
"f1": resource.NewProperty(resource.PropertyMap{
"f1": resource.MakeSecret(resource.NewProperty("secret4")),
}),
}),
},
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
var walker secretsWalker
result := walker.walk(tt.typ, resource.NewProperty(tt.input))
require.Empty(t, walker.errs)
assert.Equal(t, tt.expected, result.ObjectValue())
})
}
}
29 changes: 28 additions & 1 deletion tests/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ func TestInferCheckConfigSecrets(t *testing.T) {
Int int `pulumi:"int" provider:"secret"`
NotSecret string `pulumi:"not-nested"`
} `pulumi:"nested"`
NotSecret string `pulumi:"not"`
NotSecret string `pulumi:"not"`
ArrayNested []struct {
Field string `pulumi:"field" provider:"secret"`
} `pulumi:"arrayNested"`
MapNested map[string]struct {
Field string `pulumi:"field" provider:"secret"`
} `pulumi:"mapNested"`
}

resp, err := integration.NewServer("test", semver.MustParse("0.0.0"), infer.Provider(infer.Options{
Expand All @@ -107,6 +113,16 @@ func TestInferCheckConfigSecrets(t *testing.T) {
"int": resource.NewProperty(1.0),
"not-nested": resource.NewProperty("not-secret"),
}),
"arrayNested": resource.NewProperty([]resource.PropertyValue{
resource.NewProperty(resource.PropertyMap{
"field": resource.NewProperty("123"),
}),
}),
"mapNested": resource.NewProperty(resource.PropertyMap{
"key": resource.NewProperty(resource.PropertyMap{
"field": resource.NewProperty("123"),
}),
}),
"not": resource.NewProperty("not-secret"),
},
})
Expand All @@ -118,6 +134,16 @@ func TestInferCheckConfigSecrets(t *testing.T) {
"int": resource.MakeSecret(resource.NewProperty(1.0)),
"not-nested": resource.NewProperty("not-secret"),
}),
"arrayNested": resource.NewProperty([]resource.PropertyValue{
resource.NewProperty(resource.PropertyMap{
"field": resource.MakeSecret(resource.NewProperty("123")),
}),
}),
"mapNested": resource.NewProperty(resource.PropertyMap{
"key": resource.NewProperty(resource.PropertyMap{
"field": resource.MakeSecret(resource.NewProperty("123")),
}),
}),
"not": resource.NewProperty("not-secret"),
}, resp.Inputs)
}
Expand Down Expand Up @@ -150,6 +176,7 @@ func TestInferCustomCheckConfig(t *testing.T) {
resp, err := integration.NewServer("test", semver.MustParse("0.0.0"), infer.Provider(infer.Options{
Config: infer.Config[*config](),
})).CheckConfig(p.CheckRequest{
Urn: resource.CreateURN("p", "pulumi:providers:test", "", "test", "dev"),
News: resource.PropertyMap{
"field": resource.NewProperty("value"),
"nested": resource.NewProperty(resource.PropertyMap{
Expand Down

0 comments on commit fdef94a

Please sign in to comment.