2020-04-03 04:59:08 +02:00
|
|
|
package gen
|
|
|
|
|
|
|
|
import (
|
2020-11-09 19:55:53 +01:00
|
|
|
"path/filepath"
|
2020-11-05 01:56:39 +01:00
|
|
|
"sync"
|
2020-04-03 04:59:08 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-03-17 14:20:05 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/internal/test"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/internal/test/testdata/simple-enum-schema/go/plant"
|
|
|
|
tree "github.com/pulumi/pulumi/pkg/v3/codegen/internal/test/testdata/simple-enum-schema/go/plant/tree/v1"
|
|
|
|
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
|
|
|
|
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
|
2020-04-03 04:59:08 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-11 19:49:08 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-03 04:59:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestInputUsage(t *testing.T) {
|
2021-03-31 07:23:04 +02:00
|
|
|
pkg := &pkgContext{}
|
|
|
|
arrayUsage := pkg.getInputUsage("FooArray")
|
2020-04-03 04:59:08 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooArrayInput is an input type that accepts FooArray and FooArrayOutput values.\nYou can construct a "+
|
|
|
|
"concrete instance of `FooArrayInput` via:\n\n\t\t FooArray{ FooArgs{...} }\n ",
|
|
|
|
arrayUsage)
|
|
|
|
|
2021-03-31 07:23:04 +02:00
|
|
|
mapUsage := pkg.getInputUsage("FooMap")
|
2020-04-03 04:59:08 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooMapInput is an input type that accepts FooMap and FooMapOutput values.\nYou can construct a concrete"+
|
|
|
|
" instance of `FooMapInput` via:\n\n\t\t FooMap{ \"key\": FooArgs{...} }\n ",
|
|
|
|
mapUsage)
|
|
|
|
|
2021-03-31 07:23:04 +02:00
|
|
|
ptrUsage := pkg.getInputUsage("FooPtr")
|
2020-04-03 04:59:08 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooPtrInput is an input type that accepts FooArgs, FooPtr and FooPtrOutput values.\nYou can construct a "+
|
|
|
|
"concrete instance of `FooPtrInput` via:\n\n\t\t FooArgs{...}\n\n or:\n\n\t\t nil\n ",
|
|
|
|
ptrUsage)
|
|
|
|
|
2021-03-31 07:23:04 +02:00
|
|
|
usage := pkg.getInputUsage("Foo")
|
2020-04-03 04:59:08 +02:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
"FooInput is an input type that accepts FooArgs and FooOutput values.\nYou can construct a concrete instance"+
|
|
|
|
" of `FooInput` via:\n\n\t\t FooArgs{...}\n ",
|
|
|
|
usage)
|
|
|
|
}
|
2020-09-15 13:56:58 +02:00
|
|
|
|
|
|
|
func TestGoPackageName(t *testing.T) {
|
|
|
|
assert.Equal(t, "aws", goPackage("aws"))
|
|
|
|
assert.Equal(t, "azure", goPackage("azure-nextgen"))
|
2020-11-03 08:02:56 +01:00
|
|
|
assert.Equal(t, "plant", goPackage("plant-provider"))
|
2020-09-15 13:56:58 +02:00
|
|
|
assert.Equal(t, "", goPackage(""))
|
|
|
|
}
|
2020-11-09 19:55:53 +01:00
|
|
|
|
|
|
|
func TestGeneratePackage(t *testing.T) {
|
|
|
|
tests := []struct {
|
2021-01-20 01:47:42 +01:00
|
|
|
name string
|
|
|
|
schemaDir string
|
|
|
|
expectedFiles []string
|
|
|
|
genResourceContainerTypes bool
|
2020-11-09 19:55:53 +01:00
|
|
|
}{
|
2020-11-18 09:05:13 +01:00
|
|
|
{
|
|
|
|
"Simple schema with local resource properties",
|
|
|
|
"simple-resource-schema",
|
|
|
|
[]string{
|
2021-01-15 19:09:09 +01:00
|
|
|
filepath.Join("example", "argFunction.go"),
|
|
|
|
filepath.Join("example", "doc.go"),
|
|
|
|
filepath.Join("example", "init.go"),
|
|
|
|
filepath.Join("example", "otherResource.go"),
|
|
|
|
filepath.Join("example", "provider.go"),
|
|
|
|
filepath.Join("example", "pulumiTypes.go"),
|
|
|
|
filepath.Join("example", "pulumiUtilities.go"),
|
|
|
|
filepath.Join("example", "resource.go"),
|
2020-11-18 09:05:13 +01:00
|
|
|
},
|
2021-01-20 01:47:42 +01:00
|
|
|
false,
|
2020-11-18 09:05:13 +01:00
|
|
|
},
|
2020-11-09 19:55:53 +01:00
|
|
|
{
|
2020-11-03 08:02:56 +01:00
|
|
|
"Simple schema with enum types",
|
|
|
|
"simple-enum-schema",
|
2020-11-09 19:55:53 +01:00
|
|
|
[]string{
|
2021-01-15 19:09:09 +01:00
|
|
|
filepath.Join("plant", "doc.go"),
|
2021-01-20 03:02:33 +01:00
|
|
|
filepath.Join("plant", "init.go"),
|
2020-12-11 19:49:08 +01:00
|
|
|
filepath.Join("plant", "provider.go"),
|
2021-01-20 00:59:51 +01:00
|
|
|
filepath.Join("plant", "pulumiTypes.go"),
|
|
|
|
filepath.Join("plant", "pulumiUtilities.go"),
|
2021-01-15 19:09:09 +01:00
|
|
|
filepath.Join("plant", "pulumiEnums.go"),
|
|
|
|
filepath.Join("plant", "provider.go"),
|
2021-01-20 00:59:51 +01:00
|
|
|
filepath.Join("plant", "tree", "v1", "init.go"),
|
2020-11-03 08:02:56 +01:00
|
|
|
filepath.Join("plant", "tree", "v1", "rubberTree.go"),
|
|
|
|
filepath.Join("plant", "tree", "v1", "pulumiEnums.go"),
|
2021-02-25 19:08:05 +01:00
|
|
|
filepath.Join("plant", "tree", "v1", "nursery.go"),
|
2020-11-09 19:55:53 +01:00
|
|
|
},
|
2021-01-20 01:47:42 +01:00
|
|
|
false,
|
2020-11-09 19:55:53 +01:00
|
|
|
},
|
2021-01-15 19:09:09 +01:00
|
|
|
{
|
|
|
|
"External resource schema",
|
|
|
|
"external-resource-schema",
|
|
|
|
[]string{
|
|
|
|
filepath.Join("example", "init.go"),
|
|
|
|
filepath.Join("example", "argFunction.go"),
|
|
|
|
filepath.Join("example", "cat.go"),
|
|
|
|
filepath.Join("example", "component.go"),
|
|
|
|
filepath.Join("example", "doc.go"),
|
|
|
|
filepath.Join("example", "provider.go"),
|
|
|
|
filepath.Join("example", "pulumiTypes.go"),
|
|
|
|
filepath.Join("example", "pulumiUtilities.go"),
|
|
|
|
filepath.Join("example", "workload.go"),
|
|
|
|
},
|
2021-01-20 01:47:42 +01:00
|
|
|
true,
|
2021-01-15 19:09:09 +01:00
|
|
|
},
|
2021-03-10 16:08:08 +01:00
|
|
|
{
|
|
|
|
"Simple schema with plain properties",
|
|
|
|
"simple-plain-schema",
|
|
|
|
[]string{
|
|
|
|
filepath.Join("example", "doc.go"),
|
|
|
|
filepath.Join("example", "init.go"),
|
|
|
|
filepath.Join("example", "component.go"),
|
|
|
|
filepath.Join("example", "provider.go"),
|
|
|
|
filepath.Join("example", "pulumiTypes.go"),
|
|
|
|
filepath.Join("example", "pulumiUtilities.go"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2021-04-29 21:30:01 +02:00
|
|
|
{
|
|
|
|
"Simple schema with root package set",
|
|
|
|
"simple-plain-schema-with-root-package",
|
|
|
|
[]string{
|
|
|
|
filepath.Join("doc.go"),
|
|
|
|
filepath.Join("init.go"),
|
|
|
|
filepath.Join("component.go"),
|
|
|
|
filepath.Join("provider.go"),
|
|
|
|
filepath.Join("pulumiTypes.go"),
|
|
|
|
filepath.Join("pulumiUtilities.go"),
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2020-11-09 19:55:53 +01:00
|
|
|
}
|
|
|
|
testDir := filepath.Join("..", "internal", "test", "testdata")
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
files, err := test.GeneratePackageFilesFromSchema(
|
2020-11-05 01:56:39 +01:00
|
|
|
filepath.Join(testDir, tt.schemaDir, "schema.json"),
|
|
|
|
func(tool string, pkg *schema.Package, files map[string][]byte) (map[string][]byte, error) {
|
2021-01-20 20:28:50 +01:00
|
|
|
return GeneratePackage(tool, pkg)
|
2020-11-03 08:02:56 +01:00
|
|
|
})
|
2020-11-09 19:55:53 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
expectedFiles, err := test.LoadFiles(filepath.Join(testDir, tt.schemaDir), "go", tt.expectedFiles)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
test.ValidateFileEquality(t, files, expectedFiles)
|
2021-01-15 19:09:09 +01:00
|
|
|
test.CheckAllFilesGenerated(t, files, expectedFiles)
|
2020-11-09 19:55:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 01:56:39 +01:00
|
|
|
|
|
|
|
type mocks int
|
|
|
|
|
2021-04-08 13:47:08 +02:00
|
|
|
func (mocks) NewResource(args pulumi.MockResourceArgs) (string, resource.PropertyMap, error) {
|
|
|
|
return args.Name + "_id", args.Inputs, nil
|
2020-11-05 01:56:39 +01:00
|
|
|
}
|
|
|
|
|
2021-04-08 13:47:08 +02:00
|
|
|
func (mocks) Call(args pulumi.MockCallArgs) (resource.PropertyMap, error) {
|
|
|
|
return args.Args, nil
|
2020-11-05 01:56:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEnumUsage(t *testing.T) {
|
|
|
|
t.Run("Success", func(t *testing.T) {
|
|
|
|
require.NoError(t, pulumi.RunErr(func(ctx *pulumi.Context) error {
|
|
|
|
tree, err := tree.NewRubberTree(ctx, "blah", &tree.RubberTreeArgs{
|
2021-01-15 19:09:09 +01:00
|
|
|
Container: &plant.ContainerArgs{
|
2020-11-09 22:05:59 +01:00
|
|
|
Color: plant.ContainerColorRed,
|
2020-11-05 01:56:39 +01:00
|
|
|
Material: pulumi.String("ceramic"),
|
2020-11-09 22:05:59 +01:00
|
|
|
Size: plant.ContainerSizeFourInch,
|
2020-11-05 01:56:39 +01:00
|
|
|
},
|
2020-11-09 23:18:41 +01:00
|
|
|
Farm: tree.Farm_Plants_R_Us,
|
2020-11-09 22:05:59 +01:00
|
|
|
Type: tree.RubberTreeVarietyRuby,
|
2020-11-05 01:56:39 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, tree)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
pulumi.All(
|
2021-01-08 02:09:55 +01:00
|
|
|
tree.URN(),
|
|
|
|
tree.Container.Material(),
|
|
|
|
tree.Container.Color(),
|
|
|
|
tree.Container.Size(),
|
|
|
|
tree.Container.Brightness(),
|
|
|
|
tree.Type,
|
2020-11-05 01:56:39 +01:00
|
|
|
).ApplyT(func(all []interface{}) error {
|
|
|
|
urn := all[0].(pulumi.URN)
|
|
|
|
material := all[1].(*string)
|
|
|
|
color := all[2].(*string)
|
|
|
|
size := all[3].(*int)
|
2021-01-08 01:28:19 +01:00
|
|
|
brightness := all[4].(*float64)
|
|
|
|
typ := all[5].(string)
|
2020-11-05 01:56:39 +01:00
|
|
|
assert.Equal(t, *material, "ceramic", "unexpected material on resource: %v", urn)
|
|
|
|
assert.Equal(t, *color, "red", "unexpected color on resource: %v", urn)
|
|
|
|
assert.Equal(t, *size, 4, "unexpected size on resource: %v", urn)
|
2021-01-08 01:28:19 +01:00
|
|
|
assert.Nil(t, brightness)
|
2020-11-05 01:56:39 +01:00
|
|
|
assert.Equal(t, typ, "Ruby", "unexpected type on resource: %v", urn)
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}, pulumi.WithMocks("project", "stack", mocks(0))))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("StringsForRelaxedEnum", func(t *testing.T) {
|
|
|
|
require.NoError(t, pulumi.RunErr(func(ctx *pulumi.Context) error {
|
|
|
|
tree, err := tree.NewRubberTree(ctx, "blah", &tree.RubberTreeArgs{
|
|
|
|
Container: plant.ContainerArgs{
|
|
|
|
Color: pulumi.String("Magenta"),
|
|
|
|
Material: pulumi.String("ceramic"),
|
|
|
|
Size: plant.ContainerSize(22),
|
|
|
|
},
|
2020-11-09 23:18:41 +01:00
|
|
|
Farm: tree.Farm_Plants_R_Us,
|
2020-11-09 22:05:59 +01:00
|
|
|
Type: tree.RubberTreeVarietyRuby,
|
2020-11-05 01:56:39 +01:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, tree)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
pulumi.All(
|
|
|
|
tree.URN(), tree.Container.Material(), tree.Container.Color(), tree.Container.Size(), tree.Type,
|
|
|
|
).ApplyT(func(all []interface{}) error {
|
|
|
|
urn := all[0].(pulumi.URN)
|
|
|
|
material := all[1].(*string)
|
|
|
|
color := all[2].(*string)
|
|
|
|
size := all[3].(*int)
|
|
|
|
typ := all[4].(string)
|
|
|
|
assert.Equal(t, *material, "ceramic", "unexpected material on resource: %v", urn)
|
|
|
|
assert.Equal(t, *color, "Magenta", "unexpected color on resource: %v", urn)
|
|
|
|
assert.Equal(t, *size, 22, "unexpected size on resource: %v", urn)
|
|
|
|
assert.Equal(t, typ, "Ruby", "unexpected type on resource: %v", urn)
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}, pulumi.WithMocks("project", "stack", mocks(1))))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("StringsForStrictEnum", func(t *testing.T) {
|
|
|
|
require.NoError(t, pulumi.RunErr(func(ctx *pulumi.Context) error {
|
|
|
|
tree, err := tree.NewRubberTree(ctx, "blah", &tree.RubberTreeArgs{
|
|
|
|
Container: plant.ContainerArgs{
|
|
|
|
Color: pulumi.String("Magenta"),
|
|
|
|
Material: pulumi.String("ceramic"),
|
|
|
|
Size: plant.ContainerSize(22),
|
|
|
|
},
|
2020-11-09 23:18:41 +01:00
|
|
|
Farm: tree.Farm_Plants_R_Us,
|
2020-11-05 01:56:39 +01:00
|
|
|
Type: "Burgundy",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, tree)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
pulumi.All(
|
|
|
|
tree.URN(), tree.Container.Material(), tree.Container.Color(), tree.Container.Size(), tree.Type,
|
|
|
|
).ApplyT(func(all []interface{}) error {
|
|
|
|
urn := all[0].(pulumi.URN)
|
|
|
|
material := all[1].(*string)
|
|
|
|
color := all[2].(*string)
|
|
|
|
size := all[3].(*int)
|
|
|
|
typ := all[4].(string)
|
|
|
|
assert.Equal(t, *material, "ceramic", "unexpected material on resource: %v", urn)
|
|
|
|
assert.Equal(t, *color, "Magenta", "unexpected color on resource: %v", urn)
|
|
|
|
assert.Equal(t, *size, 22, "unexpected size on resource: %v", urn)
|
|
|
|
assert.Equal(t, typ, "Burgundy", "unexpected type on resource: %v", urn)
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}, pulumi.WithMocks("project", "stack", mocks(1))))
|
|
|
|
})
|
|
|
|
}
|