@@ -37,6 +37,7 @@ import (
3737
3838 "github.com/pulumi/pulumi-terraform-bridge/v3/internal/testprovider"
3939 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/reservedkeys"
40+ "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
4041 shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
4142 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/schema"
4243 shimv1 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v1"
@@ -4222,3 +4223,167 @@ func Test_makeTerraformStateWithOptsMaxItemsOneAdded(t *testing.T) {
42224223 autogold .Expect (map [string ]interface {}{"props" : []interface {}{"X" }}).Equal (t , inputs )
42234224 })
42244225}
4226+
4227+ func TestGetAssetTable (t * testing.T ) {
4228+ t .Parallel ()
4229+
4230+ t .Run ("simple asset" , func (t * testing.T ) {
4231+ asset , err := resource .NewTextAsset ("hello world" )
4232+ require .NoError (t , err )
4233+ assetProp := resource .NewAssetProperty (asset )
4234+ props := resource.PropertyMap {"foo" : assetProp }
4235+ ps := map [string ]* SchemaInfo {"foo" : {Asset : & AssetTranslation {Kind : FileAsset }}}
4236+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4237+ "foo" : {
4238+ Type : schemav2 .TypeString ,
4239+ Optional : true ,
4240+ },
4241+ })
4242+ assets := getAssetTable (props , ps , tfs )
4243+ require .Len (t , assets , 1 )
4244+ for info , v := range assets {
4245+ assert .Same (t , ps ["foo" ], info )
4246+ assert .True (t , v .DeepEquals (assetProp ))
4247+ }
4248+ })
4249+
4250+ t .Run ("no assets present" , func (t * testing.T ) {
4251+ props := resource.PropertyMap {"bar" : resource .NewStringProperty ("baz" )}
4252+ ps := map [string ]* SchemaInfo {"bar" : {}}
4253+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4254+ "bar" : {
4255+ Type : schemav2 .TypeString ,
4256+ Optional : true ,
4257+ },
4258+ })
4259+ assets := getAssetTable (props , ps , tfs )
4260+ assert .Empty (t , assets )
4261+ })
4262+
4263+ t .Run ("archive present" , func (t * testing.T ) {
4264+ asset , err := resource .NewTextAsset ("hello world" )
4265+ require .NoError (t , err )
4266+ archive , err := resource .NewAssetArchive (map [string ]interface {}{"file.txt" : asset })
4267+ require .NoError (t , err )
4268+ archiveProp := resource .NewArchiveProperty (archive )
4269+ props := resource.PropertyMap {"arch" : archiveProp }
4270+ ps := map [string ]* SchemaInfo {"arch" : {Asset : & AssetTranslation {Kind : FileArchive }}}
4271+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4272+ "arch" : {
4273+ Type : schemav2 .TypeString ,
4274+ Optional : true ,
4275+ },
4276+ })
4277+ assets := getAssetTable (props , ps , tfs )
4278+ require .Len (t , assets , 1 )
4279+ for info , v := range assets {
4280+ assert .Same (t , ps ["arch" ], info )
4281+ assert .True (t , v .DeepEquals (archiveProp ))
4282+ }
4283+ })
4284+
4285+ t .Run ("asset with no matching SchemaInfo" , func (t * testing.T ) {
4286+ asset , err := resource .NewTextAsset ("hello world" )
4287+ require .NoError (t , err )
4288+ assetProp := resource .NewAssetProperty (asset )
4289+ props := resource.PropertyMap {"missing" : assetProp }
4290+ ps := map [string ]* SchemaInfo {}
4291+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4292+ "bar" : {
4293+ Type : schemav2 .TypeString ,
4294+ Optional : true ,
4295+ },
4296+ })
4297+ defer func () {
4298+ if r := recover (); r == nil {
4299+ assert .Fail (t , "panic did not occur" , r )
4300+ }
4301+ }()
4302+ getAssetTable (props , ps , tfs )
4303+ })
4304+
4305+ t .Run ("nested asset" , func (t * testing.T ) {
4306+ asset , err := resource .NewTextAsset ("hello world" )
4307+ require .NoError (t , err )
4308+ nestedAsset := resource .NewAssetProperty (asset )
4309+ nestedProps := resource.PropertyMap {"outer" : resource .NewObjectProperty (resource.PropertyMap {"inner" : nestedAsset })}
4310+ nestedPS := map [string ]* SchemaInfo {
4311+ "outer" : {
4312+ Elem : & info.Schema {Fields : map [string ]* SchemaInfo {
4313+ "inner" : {Asset : & AssetTranslation {Kind : FileAsset }},
4314+ }},
4315+ },
4316+ }
4317+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4318+ "outer" : {
4319+ Type : schemav2 .TypeList ,
4320+ Optional : true ,
4321+ MaxItems : 1 ,
4322+ Elem : & schemav2.Resource {
4323+ Schema : map [string ]* schemav2.Schema {
4324+ "inner" : {Type : schemav2 .TypeString , Optional : true },
4325+ },
4326+ },
4327+ },
4328+ })
4329+ assets := getAssetTable (nestedProps , nestedPS , tfs )
4330+ found := false
4331+ for info , v := range assets {
4332+ if info == nestedPS ["outer" ].Elem .Fields ["inner" ] && v .DeepEquals (nestedAsset ) {
4333+ found = true
4334+ }
4335+ }
4336+ assert .True (t , found )
4337+ })
4338+
4339+ t .Run ("multiple assets" , func (t * testing.T ) {
4340+ asset1 , err := resource .NewTextAsset ("hello world" )
4341+ require .NoError (t , err )
4342+ asset2 , err := resource .NewTextAsset ("another asset" )
4343+ require .NoError (t , err )
4344+ assetProp1 := resource .NewAssetProperty (asset1 )
4345+ assetProp2 := resource .NewAssetProperty (asset2 )
4346+ props := resource.PropertyMap {"foo" : assetProp1 , "bar" : assetProp2 }
4347+ ps := map [string ]* SchemaInfo {
4348+ "foo" : {Asset : & AssetTranslation {Kind : FileAsset }},
4349+ "bar" : {Asset : & AssetTranslation {Kind : FileAsset }},
4350+ }
4351+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4352+ "foo" : {Type : schemav2 .TypeString , Optional : true },
4353+ "bar" : {Type : schemav2 .TypeString , Optional : true },
4354+ })
4355+ assets := getAssetTable (props , ps , tfs )
4356+ assert .Len (t , assets , 2 )
4357+ assert .Contains (t , assets , ps ["foo" ])
4358+ assert .Contains (t , assets , ps ["bar" ])
4359+ assert .True (t , assets [ps ["foo" ]].DeepEquals (assetProp1 ))
4360+ assert .True (t , assets [ps ["bar" ]].DeepEquals (assetProp2 ))
4361+ })
4362+
4363+ t .Run ("asset with nil SchemaInfo" , func (t * testing.T ) {
4364+ asset , err := resource .NewTextAsset ("hello world" )
4365+ require .NoError (t , err )
4366+ assetProp := resource .NewAssetProperty (asset )
4367+ props := resource.PropertyMap {"foo" : assetProp }
4368+ ps := map [string ]* SchemaInfo {"foo" : nil }
4369+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4370+ "foo" : {Type : schemav2 .TypeString , Optional : true },
4371+ })
4372+ defer func () {
4373+ if r := recover (); r == nil {
4374+ assert .Fail (t , "panic did not occur" , r )
4375+ }
4376+ }()
4377+ getAssetTable (props , ps , tfs )
4378+ })
4379+
4380+ t .Run ("non-asset value with asset SchemaInfo" , func (t * testing.T ) {
4381+ props := resource.PropertyMap {"foo" : resource .NewStringProperty ("not an asset" )}
4382+ ps := map [string ]* SchemaInfo {"foo" : {Asset : & AssetTranslation {Kind : FileAsset }}}
4383+ tfs := shimv2 .NewSchemaMap (map [string ]* schemav2.Schema {
4384+ "foo" : {Type : schemav2 .TypeString , Optional : true },
4385+ })
4386+ assets := getAssetTable (props , ps , tfs )
4387+ assert .Empty (t , assets )
4388+ })
4389+ }
0 commit comments