Skip to content

Commit

Permalink
Additional tests for failures in provider.go Create (#2710)
Browse files Browse the repository at this point in the history
Additional tests for failure modes in `provider.go`'s `Create` method.
This would have caught
#2706
  • Loading branch information
VenelinMartinov authored Dec 10, 2024
1 parent 57aedeb commit 1506028
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions pkg/tests/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import (
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
testutils "github.com/pulumi/providertest/replay"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"

crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck"
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
)

Expand Down Expand Up @@ -410,3 +414,69 @@ func TestInputsEmptyCollections(t *testing.T) {
))
}
}

func TestCreateFails(t *testing.T) {
t.Parallel()

resMap := map[string]*schema.Resource{
"prov_test": {
Schema: map[string]*schema.Schema{
"test": {
Type: schema.TypeString,
Required: true,
},
},
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
return diag.Errorf("CREATE FAILURE")
},
},
}
prov := &schema.Provider{ResourcesMap: resMap}
bridgedProvider := pulcheck.BridgedProvider(t, "prov", prov)

pt := pulcheck.PulCheck(t, bridgedProvider, `
name: test
runtime: yaml
resources:
mainRes:
type: prov:index:Test
properties:
test: "hello"
`)

_, err := pt.CurrentStack().Up(pt.Context())
require.Error(t, err)
require.ErrorContains(t, err, "CREATE FAILURE")
}

func TestCreateUnrecognizedType(t *testing.T) {
t.Parallel()

resMap := map[string]*schema.Resource{
"prov_test": {
Schema: map[string]*schema.Schema{
"test": {
Type: schema.TypeString,
Required: true,
},
},
},
}
prov := &schema.Provider{ResourcesMap: resMap}
bridgedProvider := pulcheck.BridgedProvider(t, "prov", prov)
providerServer, err := pulcheck.ProviderServerFromInfo(context.Background(), bridgedProvider)
require.NoError(t, err)

testutils.Replay(t, providerServer, `
{
"method": "/pulumirpc.ResourceProvider/Create",
"request": {
"urn": "urn:pulumi:dev::teststack::prov:index/unknownResource:UnknownResource::exres",
"properties": {
"test": "hello"
}
},
"errors": ["unrecognized resource type (Create): prov:index/unknownResource:UnknownResource"]
}
`)
}

0 comments on commit 1506028

Please sign in to comment.