github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs-tfbridge20/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 "strings" 20 "testing" 21 "time" 22 23 "github.com/stretchr/testify/assert" 24 25 "github.com/pulumi/pulumi/sdk/v3/go/common/resource" 26 "github.com/pulumi/pulumi/sdk/v3/go/pulumi" 27 28 "output-funcs-tfbridge20/mypkg" 29 ) 30 31 type mocks int 32 33 // Create the mock. 34 func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) { 35 panic("NewResource not supported") 36 } 37 38 func (mocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) { 39 if args.Token == "mypkg::listStorageAccountKeys" { 40 41 targs := mypkg.ListStorageAccountKeysArgs{} 42 for k, v := range args.Args { 43 switch k { 44 case "accountName": 45 targs.AccountName = v.V.(string) 46 case "expand": 47 expand := v.V.(string) 48 targs.Expand = &expand 49 case "resourceGroupName": 50 targs.ResourceGroupName = v.V.(string) 51 } 52 } 53 54 var expand string 55 if targs.Expand != nil { 56 expand = *targs.Expand 57 } 58 59 inputs := []mypkg.StorageAccountKeyResponse{ 60 { 61 KeyName: "key", 62 Permissions: "permissions", 63 Value: fmt.Sprintf("accountName=%v, resourceGroupName=%v, expand=%v", 64 targs.AccountName, 65 targs.ResourceGroupName, 66 expand), 67 }, 68 } 69 result := mypkg.ListStorageAccountKeysResult{ 70 Keys: inputs, 71 } 72 outputs := map[string]interface{}{ 73 "keys": result.Keys, 74 } 75 return resource.NewPropertyMapFromMap(outputs), nil 76 } 77 78 if args.Token == "mypkg::getAmiIds" { 79 // NOTE: only subset of possible fields are tested here in the smoke-test. 80 81 targs := mypkg.GetAmiIdsArgs{} 82 for k, v := range args.Args { 83 switch k { 84 case "owners": 85 x := v.V.([]resource.PropertyValue) 86 for _, owner := range x { 87 targs.Owners = append(targs.Owners, owner.V.(string)) 88 } 89 case "nameRegex": 90 x := v.V.(string) 91 targs.NameRegex = &x 92 case "sortAscending": 93 x := v.V.(bool) 94 targs.SortAscending = &x 95 case "filters": 96 filters := v.V.([]resource.PropertyValue) 97 for _, filter := range filters { 98 propMap := filter.V.(resource.PropertyMap) 99 name := propMap["name"].V.(string) 100 values := propMap["values"].V.([]resource.PropertyValue) 101 var theValues []string 102 for _, v := range values { 103 theValues = append(theValues, v.V.(string)) 104 } 105 targs.Filters = append(targs.Filters, mypkg.GetAmiIdsFilter{ 106 Name: name, 107 Values: theValues, 108 }) 109 } 110 } 111 } 112 113 var filterStrings []string 114 for _, f := range targs.Filters { 115 fs := fmt.Sprintf("name=%s values=[%s]", f.Name, strings.Join(f.Values, ", ")) 116 filterStrings = append(filterStrings, fs) 117 } 118 119 var id string = fmt.Sprintf("my-id [owners: %s] [filters: %s]", 120 strings.Join(targs.Owners, ", "), 121 strings.Join(filterStrings, ", ")) 122 123 result := mypkg.GetAmiIdsResult{ 124 Id: id, 125 NameRegex: targs.NameRegex, 126 SortAscending: targs.SortAscending, 127 } 128 129 outputs := map[string]interface{}{ 130 "id": result.Id, 131 "nameRegex": result.NameRegex, 132 "sortAscending": result.SortAscending, 133 } 134 135 return resource.NewPropertyMapFromMap(outputs), nil 136 137 } 138 139 panic(fmt.Errorf("Unknown token: %s", args.Token)) 140 } 141 142 func TestListStorageAccountKeysOutput(t *testing.T) { 143 pulumiTest(t, func(ctx *pulumi.Context) error { 144 output := mypkg.ListStorageAccountKeysOutput(ctx, mypkg.ListStorageAccountKeysOutputArgs{ 145 AccountName: pulumi.String("my-account-name"), 146 ResourceGroupName: pulumi.String("my-resource-group-name"), 147 }) 148 149 keys := waitOut(t, output.Keys()).([]mypkg.StorageAccountKeyResponse) 150 151 assert.Equal(t, 1, len(keys)) 152 assert.Equal(t, "key", keys[0].KeyName) 153 assert.Equal(t, "permissions", keys[0].Permissions) 154 assert.Equal(t, "accountName=my-account-name, resourceGroupName=my-resource-group-name, expand=", 155 keys[0].Value) 156 157 output = mypkg.ListStorageAccountKeysOutput(ctx, mypkg.ListStorageAccountKeysOutputArgs{ 158 AccountName: pulumi.String("my-account-name"), 159 ResourceGroupName: pulumi.String("my-resource-group-name"), 160 Expand: pulumi.String("my-expand"), 161 }) 162 163 keys = waitOut(t, output.Keys()).([]mypkg.StorageAccountKeyResponse) 164 165 assert.Equal(t, 1, len(keys)) 166 assert.Equal(t, "key", keys[0].KeyName) 167 assert.Equal(t, "permissions", keys[0].Permissions) 168 assert.Equal(t, "accountName=my-account-name, resourceGroupName=my-resource-group-name, expand=my-expand", 169 keys[0].Value) 170 171 return nil 172 }) 173 } 174 175 func TestGetAmiIdsWorks(t *testing.T) { 176 177 makeFilter := func(n int) mypkg.GetAmiIdsFilterInput { 178 return &mypkg.GetAmiIdsFilterArgs{ 179 Name: pulumi.String(fmt.Sprintf("filter-%d-name", n)), 180 Values: pulumi.StringArray{ 181 pulumi.String(fmt.Sprintf("value-%d-1", n)), 182 pulumi.String(fmt.Sprintf("value-%d-2", n)), 183 }, 184 } 185 } 186 187 pulumiTest(t, func(ctx *pulumi.Context) error { 188 output := mypkg.GetAmiIdsOutput(ctx, mypkg.GetAmiIdsOutputArgs{ 189 NameRegex: pulumi.String("[a-z]").ToStringPtrOutput(), 190 SortAscending: pulumi.Bool(true).ToBoolPtrOutput(), 191 Owners: pulumi.StringArray{ 192 pulumi.String("owner-1"), 193 pulumi.String("owner-2"), 194 }.ToStringArrayOutput(), 195 Filters: mypkg.GetAmiIdsFilterArray{ 196 makeFilter(1), 197 makeFilter(2), 198 }.ToGetAmiIdsFilterArrayOutput(), 199 }) 200 201 result := waitOut(t, output).(mypkg.GetAmiIdsResult) 202 203 assert.Equal(t, *result.NameRegex, "[a-z]") 204 205 expectId := strings.Join([]string{ 206 "my-id ", 207 "[owners: owner-1, owner-2] ", 208 "[filters: ", 209 "name=filter-1-name values=[value-1-1, value-1-2], ", 210 "name=filter-2-name values=[value-2-1, value-2-2]", 211 "]", 212 }, "") 213 214 assert.Equal(t, result.Id, expectId) 215 216 return nil 217 }) 218 } 219 220 func pulumiTest(t *testing.T, testBody func(ctx *pulumi.Context) error) { 221 err := pulumi.RunErr(testBody, pulumi.WithMocks("project", "stack", mocks(0))) 222 assert.NoError(t, err) 223 } 224 225 func waitOut(t *testing.T, output pulumi.Output) interface{} { 226 result, err := waitOutput(output, 1*time.Second) 227 if err != nil { 228 t.Error(err) 229 return nil 230 } 231 return result 232 } 233 234 func waitOutput(output pulumi.Output, timeout time.Duration) (interface{}, error) { 235 c := make(chan interface{}, 2) 236 output.ApplyT(func(v interface{}) interface{} { 237 c <- v 238 return v 239 }) 240 var timeoutMarker *int = new(int) 241 go func() { 242 time.Sleep(timeout) 243 c <- timeoutMarker 244 }() 245 246 result := <-c 247 if result == timeoutMarker { 248 return nil, fmt.Errorf("Timed out waiting for pulumi.Output after %v", timeout) 249 } else { 250 return result, nil 251 } 252 }