github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/plain-object-defaults/go-extras/tests/codegen_test.go (about) 1 // Copyright 2016-2021, Pulumi Corporation. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package codegentest 16 17 import ( 18 "fmt" 19 "testing" 20 "time" 21 22 "github.com/stretchr/testify/assert" 23 24 "github.com/pulumi/pulumi/sdk/v3/go/common/resource" 25 "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 26 27 "plain-object-defaults/example" 28 ) 29 30 type mocks int 31 32 // We assert that default values were passed to our constuctor 33 func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) { 34 checkFloat64 := func(v resource.PropertyValue, k string, expected float64) { 35 m := v.V.(resource.PropertyMap) 36 if m[resource.PropertyKey(k)].V.(float64) != expected { 37 panic(fmt.Sprintf("Expected %s to have value %.2f", k, expected)) 38 } 39 } 40 for k, v := range args.Inputs { 41 switch k { 42 case "kubeClientSettings": 43 checkFloat64(v, "burst", 42) 44 case "backupKubeClientSettings": 45 checkFloat64(v, "qps", 7) 46 } 47 } 48 return args.Name, args.Inputs.Copy(), nil 49 } 50 51 func (mocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) { 52 panic("Call not supported") 53 } 54 55 func TestObjectDefaults(t *testing.T) { 56 path := "thePath" 57 defaultDriver := "secret" 58 kcs := example.HelmReleaseSettings{ 59 PluginsPath: &path, 60 RequiredArg: "This is required", 61 } 62 withDefaults := kcs.Defaults() 63 assert.Equal(t, kcs.RequiredArg, withDefaults.RequiredArg) 64 assert.Equal(t, kcs.PluginsPath, withDefaults.PluginsPath) 65 assert.Nil(t, kcs.Driver) 66 assert.Equal(t, withDefaults.Driver, &defaultDriver) 67 } 68 69 func TestTransitiveObjectDefaults(t *testing.T) { 70 layered := example.LayeredType{ 71 Other: example.HelmReleaseSettings{}, 72 } 73 withDefaults := layered.Defaults() 74 assert.Equal(t, "secret", *withDefaults.Other.Driver) 75 } 76 77 // We already have that defaults for resources. We test that they translate through objects. 78 func TestDefaultResource(t *testing.T) { 79 t.Setenv("PULUMI_K8S_CLIENT_BURST", "42") 80 pulumi.Run(func(ctx *pulumi.Context) error { 81 _, err := example.NewFoo(ctx, "foo", &example.FooArgs{ 82 KubeClientSettings: example.KubeClientSettingsPtr(&example.KubeClientSettingsArgs{}), 83 BackupKubeClientSettings: &example.KubeClientSettingsArgs{Qps: pulumi.Float64(7)}, 84 }) 85 assert.NoError(t, err) 86 return nil 87 }, pulumi.WithMocks("example", "stack", mocks(0))) 88 } 89 90 func waitOut(t *testing.T, output pulumi.Output) interface{} { 91 result, err := waitOutput(output, 1*time.Second) 92 if err != nil { 93 t.Error(err) 94 return nil 95 } 96 return result 97 } 98 99 func waitOutput(output pulumi.Output, timeout time.Duration) (interface{}, error) { 100 c := make(chan interface{}, 2) 101 output.ApplyT(func(v interface{}) interface{} { 102 c <- v 103 return v 104 }) 105 var timeoutMarker *int = new(int) 106 go func() { 107 time.Sleep(timeout) 108 c <- timeoutMarker 109 }() 110 111 result := <-c 112 if result == timeoutMarker { 113 return nil, fmt.Errorf("Timed out waiting for pulumi.Output after %v", timeout) 114 } else { 115 return result, nil 116 } 117 }