diff --git a/pkg/pf/tests/autonaming_test.go b/pkg/pf/tests/autonaming_test.go new file mode 100644 index 000000000..963192a32 --- /dev/null +++ b/pkg/pf/tests/autonaming_test.go @@ -0,0 +1,57 @@ +package tfbridgetests + +import ( + "testing" + + rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/pulumi/providertest/pulumitest/opttest" + "github.com/stretchr/testify/require" + + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/providerbuilder" + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/pulcheck" + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" +) + +func TestAutonaming(t *testing.T) { + t.Parallel() + provBuilder := providerbuilder.NewProvider( + providerbuilder.NewProviderArgs{ + AllResources: []providerbuilder.Resource{ + providerbuilder.NewResource(providerbuilder.NewResourceArgs{ + ResourceSchema: rschema.Schema{ + Attributes: map[string]rschema.Attribute{ + "name": rschema.StringAttribute{Optional: true}, + }, + }, + }), + }, + }) + + prov := bridgedProvider(provBuilder) + prov.Resources["testprovider_test"] = &tfbridge.ResourceInfo{ + Tok: "testprovider:index:Test", + Fields: map[string]*tfbridge.SchemaInfo{ + "name": tfbridge.AutoName("name", 50, "-"), + }, + } + program := ` +name: test +runtime: yaml +config: + pulumi:autonaming: + value: + pattern: ${project}-${name} +resources: + hello: + type: testprovider:index:Test +outputs: + testOut: ${hello.name} +` + opts := []opttest.Option{ + opttest.Env("PULUMI_EXPERIMENTAL", "true"), + } + pt, err := pulcheck.PulCheck(t, prov, program, opts...) + require.NoError(t, err) + res := pt.Up(t) + require.Equal(t, "test-hello", res.Outputs["testOut"].Value) +} diff --git a/pkg/tests/autonaming_test.go b/pkg/tests/autonaming_test.go new file mode 100644 index 000000000..7e8d8f38a --- /dev/null +++ b/pkg/tests/autonaming_test.go @@ -0,0 +1,53 @@ +package tests + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/pulumi/providertest/pulumitest/opttest" + "github.com/stretchr/testify/require" + + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/pulcheck" + "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge" +) + +func TestAutonaming(t *testing.T) { + t.Parallel() + resMap := map[string]*schema.Resource{ + "prov_test": { + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + } + tfp := &schema.Provider{ResourcesMap: resMap} + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider.Resources["prov_test"] = &tfbridge.ResourceInfo{ + Tok: "prov:index:Test", + Fields: map[string]*tfbridge.SchemaInfo{ + "name": tfbridge.AutoName("name", 50, "-"), + }, + } + program := ` +name: test +runtime: yaml +config: + pulumi:autonaming: + value: + pattern: ${name}-world +resources: + hello: + type: prov:index:Test +outputs: + testOut: ${hello.name} +` + opts := []opttest.Option{ + opttest.Env("PULUMI_EXPERIMENTAL", "true"), + } + pt := pulcheck.PulCheck(t, bridgedProvider, program, opts...) + res := pt.Up(t) + require.Equal(t, "hello-world", res.Outputs["testOut"].Value) +}