github.com/myhau/pulumi/pkg/v3@v3.70.2-0.20221116134521-f2775972e587/codegen/testing/test/testdata/output-funcs/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  	"output-funcs/mypkg"
    28  )
    29  
    30  type mocks int
    31  
    32  // Create the mock.
    33  func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) {
    34  	panic("NewResource not supported")
    35  }
    36  
    37  func (mocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) {
    38  	switch args.Token {
    39  	case "mypkg::listStorageAccountKeys":
    40  		targs := mypkg.ListStorageAccountKeysArgs{}
    41  		for k, v := range args.Args {
    42  			switch k {
    43  			case "accountName":
    44  				targs.AccountName = v.V.(string)
    45  			case "expand":
    46  				expand := v.V.(string)
    47  				targs.Expand = &expand
    48  			case "resourceGroupName":
    49  				targs.ResourceGroupName = v.V.(string)
    50  			}
    51  		}
    52  
    53  		var expand string
    54  		if targs.Expand != nil {
    55  			expand = *targs.Expand
    56  		}
    57  
    58  		inputs := []mypkg.StorageAccountKeyResponse{
    59  			{
    60  				KeyName:     "key",
    61  				Permissions: "permissions",
    62  				Value: fmt.Sprintf("accountName=%v, resourceGroupName=%v, expand=%v",
    63  					targs.AccountName,
    64  					targs.ResourceGroupName,
    65  					expand),
    66  			},
    67  		}
    68  		result := mypkg.ListStorageAccountKeysResult{
    69  			Keys: inputs,
    70  		}
    71  		outputs := map[string]interface{}{
    72  			"keys": result.Keys,
    73  		}
    74  		return resource.NewPropertyMapFromMap(outputs), nil
    75  
    76  	case "mypkg::funcWithDefaultValue",
    77  		"mypkg::funcWithAllOptionalInputs",
    78  		"mypkg::funcWithListParam",
    79  		"mypkg::funcWithDictParam":
    80  		result := mypkg.FuncWithDefaultValueResult{
    81  			R: fmt.Sprintf("%v", args.Args),
    82  		}
    83  		outputs := map[string]interface{}{
    84  			"r": result.R,
    85  		}
    86  		return resource.NewPropertyMapFromMap(outputs), nil
    87  
    88  	case "mypkg::getIntegrationRuntimeObjectMetadatum":
    89  		targs := mypkg.GetIntegrationRuntimeObjectMetadatumArgs{}
    90  		for k, v := range args.Args {
    91  			switch k {
    92  			case "factoryName":
    93  				targs.FactoryName = v.V.(string)
    94  			case "integrationRuntimeName":
    95  				targs.IntegrationRuntimeName = v.V.(string)
    96  			case "metadataPath":
    97  				metadataPath := v.V.(string)
    98  				targs.MetadataPath = &metadataPath
    99  			case "resourceGroupName":
   100  				targs.ResourceGroupName = v.V.(string)
   101  			}
   102  		}
   103  		nextLink := "my-next-link"
   104  		result := mypkg.GetIntegrationRuntimeObjectMetadatumResult{
   105  			NextLink: &nextLink,
   106  			Value:    []interface{}{targs},
   107  		}
   108  		outputs := map[string]interface{}{
   109  			"nextLink": result.NextLink,
   110  			"value":    []interface{}{fmt.Sprintf("factoryName=%s", targs.FactoryName)},
   111  		}
   112  
   113  		return resource.NewPropertyMapFromMap(outputs), nil
   114  	}
   115  
   116  	panic(fmt.Errorf("Unknown token: %s", args.Token))
   117  }
   118  
   119  func TestListStorageAccountKeysOutput(t *testing.T) {
   120  	pulumiTest(t, func(ctx *pulumi.Context) error {
   121  		output := mypkg.ListStorageAccountKeysOutput(ctx, mypkg.ListStorageAccountKeysOutputArgs{
   122  			AccountName:       pulumi.String("my-account-name"),
   123  			ResourceGroupName: pulumi.String("my-resource-group-name"),
   124  		})
   125  
   126  		keys := waitOut(t, output.Keys()).([]mypkg.StorageAccountKeyResponse)
   127  
   128  		assert.Equal(t, 1, len(keys))
   129  		assert.Equal(t, "key", keys[0].KeyName)
   130  		assert.Equal(t, "permissions", keys[0].Permissions)
   131  		assert.Equal(t, "accountName=my-account-name, resourceGroupName=my-resource-group-name, expand=",
   132  			keys[0].Value)
   133  
   134  		output = mypkg.ListStorageAccountKeysOutput(ctx, mypkg.ListStorageAccountKeysOutputArgs{
   135  			AccountName:       pulumi.String("my-account-name"),
   136  			ResourceGroupName: pulumi.String("my-resource-group-name"),
   137  			Expand:            pulumi.String("my-expand"),
   138  		})
   139  
   140  		keys = waitOut(t, output.Keys()).([]mypkg.StorageAccountKeyResponse)
   141  
   142  		assert.Equal(t, 1, len(keys))
   143  		assert.Equal(t, "key", keys[0].KeyName)
   144  		assert.Equal(t, "permissions", keys[0].Permissions)
   145  		assert.Equal(t, "accountName=my-account-name, resourceGroupName=my-resource-group-name, expand=my-expand",
   146  			keys[0].Value)
   147  
   148  		return nil
   149  	})
   150  }
   151  
   152  func TestFuncWithDefaultValueOutput(t *testing.T) {
   153  	pulumiTest(t, func(ctx *pulumi.Context) error {
   154  		output := mypkg.FuncWithDefaultValueOutput(ctx, mypkg.FuncWithDefaultValueOutputArgs{
   155  			A: pulumi.String("my-a"),
   156  		})
   157  		r := waitOut(t, output.R())
   158  		assert.Equal(t, "map[a:{my-a} b:{b-default}]", r)
   159  		return nil
   160  	})
   161  }
   162  
   163  func TestFuncWithAllOptionalInputsOutput(t *testing.T) {
   164  	pulumiTest(t, func(ctx *pulumi.Context) error {
   165  		output := mypkg.FuncWithAllOptionalInputsOutput(ctx, mypkg.FuncWithAllOptionalInputsOutputArgs{
   166  			A: pulumi.String("my-a"),
   167  		})
   168  		r := waitOut(t, output.R())
   169  		assert.Equal(t, "map[a:{my-a}]", r)
   170  		return nil
   171  	})
   172  }
   173  
   174  func TestFuncWithListParamOutput(t *testing.T) {
   175  	pulumiTest(t, func(ctx *pulumi.Context) error {
   176  		output := mypkg.FuncWithListParamOutput(ctx, mypkg.FuncWithListParamOutputArgs{
   177  			A: pulumi.StringArray{
   178  				pulumi.String("my-a1"),
   179  				pulumi.String("my-a2"),
   180  				pulumi.String("my-a3"),
   181  			},
   182  		})
   183  		r := waitOut(t, output.R())
   184  		assert.Equal(t, "map[a:{[{my-a1} {my-a2} {my-a3}]}]", r)
   185  		return nil
   186  	})
   187  }
   188  
   189  func TestFuncWithDictParamOutput(t *testing.T) {
   190  	pulumiTest(t, func(ctx *pulumi.Context) error {
   191  		output := mypkg.FuncWithDictParamOutput(ctx, mypkg.FuncWithDictParamOutputArgs{
   192  			A: pulumi.StringMap{
   193  				"one": pulumi.String("1"),
   194  				"two": pulumi.String("2"),
   195  			},
   196  		})
   197  		r := waitOut(t, output.R())
   198  		assert.Equal(t, "map[a:{map[one:{1} two:{2}]}]", r)
   199  		return nil
   200  	})
   201  }
   202  
   203  func TestGetIntegrationRuntimeObjectMetadatumOutput(t *testing.T) {
   204  	pulumiTest(t, func(ctx *pulumi.Context) error {
   205  		output := mypkg.GetIntegrationRuntimeObjectMetadatumOutput(ctx, mypkg.GetIntegrationRuntimeObjectMetadatumOutputArgs{
   206  			FactoryName:            pulumi.String("my-factory-name"),
   207  			IntegrationRuntimeName: pulumi.String("my-integration-runtime-name"),
   208  			MetadataPath:           pulumi.String("my-metadata-path"),
   209  			ResourceGroupName:      pulumi.String("my-resource-group-name"),
   210  		})
   211  		nextLink := waitOut(t, output.NextLink())
   212  		assert.Equal(t, "my-next-link", *(nextLink.(*string)))
   213  
   214  		value := waitOut(t, output.Value())
   215  		assert.Equal(t, []interface{}{"factoryName=my-factory-name"}, value)
   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  }