Skip to content

Commit 569be2d

Browse files
Integration tests for asset and archive handling (#3112)
This PR adds integration tests for asset and archive handling. Part of #3102
1 parent bcad849 commit 569be2d

File tree

2 files changed

+338
-0
lines changed

2 files changed

+338
-0
lines changed

pkg/tests/schema_pulumi_test.go

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

pkg/tfbridge/detailed_diff_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,3 +3399,106 @@ func TestMakeSetDiffElementResult(t *testing.T) {
33993399
})
34003400
}
34013401
}
3402+
3403+
func TestDetailedDiffAssets(t *testing.T) {
3404+
t.Parallel()
3405+
3406+
schMap := map[string]*schema.Schema{
3407+
"path": {
3408+
Type: schema.TypeString,
3409+
Optional: true,
3410+
},
3411+
}
3412+
tfs := shimv2.NewSchemaMap(schMap)
3413+
3414+
t.Run("file asset", func(t *testing.T) {
3415+
ps := map[string]*info.Schema{
3416+
"path": {
3417+
Fields: map[string]*info.Schema{
3418+
"path": {
3419+
Asset: &info.AssetTranslation{
3420+
Kind: info.FileAsset,
3421+
},
3422+
},
3423+
},
3424+
},
3425+
}
3426+
3427+
oldAsset, err := resource.NewTextAsset("old")
3428+
require.NoError(t, err)
3429+
3430+
old := resource.PropertyMap{
3431+
"path": resource.NewAssetProperty(oldAsset),
3432+
}
3433+
3434+
newAsset, err := resource.NewTextAsset("new")
3435+
require.NoError(t, err)
3436+
3437+
new := resource.PropertyMap{
3438+
"path": resource.NewAssetProperty(newAsset),
3439+
}
3440+
3441+
runDetailedDiffTest(t, old, new, tfs, ps,
3442+
map[string]*pulumirpc.PropertyDiff{
3443+
"path": {Kind: pulumirpc.PropertyDiff_UPDATE},
3444+
},
3445+
)
3446+
})
3447+
3448+
t.Run("archive asset", func(t *testing.T) {
3449+
ps := map[string]*info.Schema{
3450+
"path": {
3451+
Fields: map[string]*info.Schema{
3452+
"path": {
3453+
Asset: &info.AssetTranslation{
3454+
Kind: info.FileArchive,
3455+
Format: resource.ZIPArchive,
3456+
},
3457+
},
3458+
},
3459+
},
3460+
}
3461+
3462+
file1Asset, err := resource.NewTextAsset("file1")
3463+
require.NoError(t, err)
3464+
3465+
file2Asset, err := resource.NewTextAsset("file2")
3466+
require.NoError(t, err)
3467+
3468+
archive1 := map[string]interface{}{
3469+
"file1": file1Asset,
3470+
"file2": file2Asset,
3471+
}
3472+
3473+
oldAsset, err := resource.NewAssetArchive(archive1)
3474+
require.NoError(t, err)
3475+
3476+
old := resource.PropertyMap{
3477+
"path": resource.NewArchiveProperty(oldAsset),
3478+
}
3479+
3480+
file1AssetNew, err := resource.NewTextAsset("file1New")
3481+
require.NoError(t, err)
3482+
3483+
file2AssetNew, err := resource.NewTextAsset("file2New")
3484+
require.NoError(t, err)
3485+
3486+
archive2 := map[string]interface{}{
3487+
"file1": file1AssetNew,
3488+
"file2": file2AssetNew,
3489+
}
3490+
3491+
newAsset, err := resource.NewAssetArchive(archive2)
3492+
require.NoError(t, err)
3493+
3494+
new := resource.PropertyMap{
3495+
"path": resource.NewArchiveProperty(newAsset),
3496+
}
3497+
3498+
runDetailedDiffTest(t, old, new, tfs, ps,
3499+
map[string]*pulumirpc.PropertyDiff{
3500+
"path": {Kind: pulumirpc.PropertyDiff_UPDATE},
3501+
},
3502+
)
3503+
})
3504+
}

0 commit comments

Comments
 (0)