@@ -11,6 +11,7 @@ import (
1111 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1212 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313 "github.com/hexops/autogold/v2"
14+ "github.com/pulumi/pulumi/sdk/v3/go/auto/optpreview"
1415 "github.com/pulumi/pulumi/sdk/v3/go/common/resource"
1516 "github.com/stretchr/testify/assert"
1617 "github.com/stretchr/testify/require"
@@ -777,3 +778,237 @@ func TestDefaultSchemaChanged(t *testing.T) {
777778 }
778779 }
779780}
781+
782+ func TestAssetDiff (t * testing.T ) {
783+ t .Parallel ()
784+
785+ res := & schema.Resource {
786+ Schema : map [string ]* schema.Schema {
787+ "test_path" : {
788+ Type : schema .TypeString ,
789+ Optional : true ,
790+ },
791+ },
792+ }
793+ tfp := & schema.Provider {
794+ ResourcesMap : map [string ]* schema.Resource {
795+ "prov_test" : res ,
796+ },
797+ }
798+ resInfo := & info.Resource {
799+ Fields : map [string ]* info.Schema {
800+ "test_path" : {
801+ Asset : & info.AssetTranslation {
802+ Kind : info .FileAsset ,
803+ },
804+ },
805+ },
806+ }
807+ infoMap := map [string ]* info.Resource {
808+ "prov_test" : resInfo ,
809+ }
810+
811+ bridgedProvider := pulcheck .BridgedProvider (t , "prov" , tfp , pulcheck .WithResourceInfo (infoMap ))
812+
813+ t .Run ("file asset" , func (t * testing.T ) {
814+ tempDir := t .TempDir ()
815+ assetPath := filepath .Join (tempDir , "asset.txt" )
816+ err := os .WriteFile (assetPath , []byte ("hello" ), 0o600 )
817+ require .NoError (t , err )
818+
819+ pt := pulcheck .PulCheck (t , bridgedProvider , fmt .Sprintf (`
820+ name: test
821+ runtime: yaml
822+ variables:
823+ fileAsset:
824+ fn::fileAsset: %s
825+ resources:
826+ mainRes:
827+ type: prov:index:Test
828+ properties:
829+ testPath: ${fileAsset}
830+ ` , assetPath ))
831+
832+ pt .Up (t )
833+
834+ err = os .WriteFile (assetPath , []byte ("world" ), 0o600 )
835+ require .NoError (t , err )
836+
837+ prev := pt .Preview (t , optpreview .Diff ())
838+
839+ require .Contains (t , prev .StdOut , "~ 1 to update" )
840+
841+ pt .Up (t )
842+ })
843+
844+ t .Run ("string asset" , func (t * testing.T ) {
845+ pt := pulcheck .PulCheck (t , bridgedProvider , `
846+ name: test
847+ runtime: yaml
848+ variables:
849+ stringAsset:
850+ fn::stringAsset: hello
851+ resources:
852+ mainRes:
853+ type: prov:index:Test
854+ properties:
855+ testPath: ${stringAsset}` )
856+ pt .Up (t )
857+
858+ pt .WritePulumiYaml (t , `
859+ name: test
860+ runtime: yaml
861+ variables:
862+ stringAsset:
863+ fn::stringAsset: world
864+ resources:
865+ mainRes:
866+ type: prov:index:Test
867+ properties:
868+ testPath: ${stringAsset}` )
869+
870+ prev := pt .Preview (t , optpreview .Diff ())
871+
872+ autogold .Expect (`Previewing update (test):
873+ pulumi:pulumi:Stack: (same)
874+ [urn=urn:pulumi:test::test::pulumi:pulumi:Stack::test-test]
875+ ~ prov:index/test:Test: (update)
876+ [id=newid]
877+ [urn=urn:pulumi:test::test::prov:index/test:Test::mainRes]
878+ testPath : asset(text:2cf24db) {
879+ <contents elided>
880+ }
881+ Resources:
882+ ~ 1 to update
883+ 1 unchanged
884+ ` ).Equal (t , prev .StdOut )
885+ pt .Up (t )
886+ })
887+ }
888+
889+ func TestArchiveDiff (t * testing.T ) {
890+ t .Parallel ()
891+
892+ res := & schema.Resource {
893+ Schema : map [string ]* schema.Schema {
894+ "test_path" : {
895+ Type : schema .TypeString ,
896+ Optional : true ,
897+ },
898+ },
899+ }
900+ tfp := & schema.Provider {
901+ ResourcesMap : map [string ]* schema.Resource {
902+ "prov_test" : res ,
903+ },
904+ }
905+ resInfo := & info.Resource {
906+ Fields : map [string ]* info.Schema {
907+ "test_path" : {
908+ Asset : & info.AssetTranslation {
909+ Kind : info .FileArchive ,
910+ Format : resource .ZIPArchive ,
911+ },
912+ },
913+ },
914+ }
915+ infoMap := map [string ]* info.Resource {
916+ "prov_test" : resInfo ,
917+ }
918+
919+ bridgedProvider := pulcheck .BridgedProvider (t , "prov" , tfp , pulcheck .WithResourceInfo (infoMap ))
920+
921+ t .Run ("file archive" , func (t * testing.T ) {
922+ tempDir := t .TempDir ()
923+ assetPath := filepath .Join (tempDir , "asset" )
924+ err := os .Mkdir (assetPath , 0o700 )
925+ require .NoError (t , err )
926+ file1Path := filepath .Join (assetPath , "file1.txt" )
927+ file2Path := filepath .Join (assetPath , "file2.txt" )
928+ err = os .WriteFile (file1Path , []byte ("hello" ), 0o600 )
929+ require .NoError (t , err )
930+ err = os .WriteFile (file2Path , []byte ("world" ), 0o600 )
931+ require .NoError (t , err )
932+
933+ pt := pulcheck .PulCheck (t , bridgedProvider , fmt .Sprintf (`
934+ name: test
935+ runtime: yaml
936+ variables:
937+ archiveAsset:
938+ fn::fileArchive: %s
939+ resources:
940+ mainRes:
941+ type: prov:index:Test
942+ properties:
943+ testPath: ${archiveAsset}` , assetPath ))
944+ pt .Up (t )
945+
946+ err = os .WriteFile (file1Path , []byte ("hello1" ), 0o600 )
947+ require .NoError (t , err )
948+ err = os .WriteFile (file2Path , []byte ("world1" ), 0o600 )
949+ require .NoError (t , err )
950+
951+ prev := pt .Preview (t , optpreview .Diff ())
952+
953+ require .Contains (t , prev .StdOut , "~ 1 to update" )
954+
955+ pt .Up (t )
956+ })
957+
958+ t .Run ("asset archive" , func (t * testing.T ) {
959+ pt := pulcheck .PulCheck (t , bridgedProvider , `
960+ name: test
961+ runtime: yaml
962+ variables:
963+ assetArchive:
964+ fn::assetArchive:
965+ file1:
966+ fn::stringAsset: hello
967+ file2:
968+ fn::stringAsset: world
969+ resources:
970+ mainRes:
971+ type: prov:index:Test
972+ properties:
973+ testPath: ${assetArchive}` )
974+ pt .Up (t )
975+
976+ pt .WritePulumiYaml (t , `
977+ name: test
978+ runtime: yaml
979+ variables:
980+ assetArchive:
981+ fn::assetArchive:
982+ file1:
983+ fn::stringAsset: hello1
984+ file2:
985+ fn::stringAsset: world1
986+ resources:
987+ mainRes:
988+ type: prov:index:Test
989+ properties:
990+ testPath: ${assetArchive}` )
991+
992+ prev := pt .Preview (t , optpreview .Diff ())
993+
994+ autogold .Expect (`Previewing update (test):
995+ pulumi:pulumi:Stack: (same)
996+ [urn=urn:pulumi:test::test::pulumi:pulumi:Stack::test-test]
997+ ~ prov:index/test:Test: (update)
998+ [id=newid]
999+ [urn=urn:pulumi:test::test::prov:index/test:Test::mainRes]
1000+ testPath : archive(assets:2a03253) {
1001+ "file1": asset(text:2cf24db) {
1002+ <contents elided>
1003+ }
1004+ "file2": asset(text:486ea46) {
1005+ <contents elided>
1006+ }
1007+ }
1008+ Resources:
1009+ ~ 1 to update
1010+ 1 unchanged
1011+ ` ).Equal (t , prev .StdOut )
1012+ pt .Up (t )
1013+ })
1014+ }
0 commit comments