Skip to content

Commit

Permalink
Implement runCreateInputCheck in terms of Create (#2503)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe authored Oct 18, 2024
1 parent 0e6ed77 commit 1dd0380
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 57 deletions.
41 changes: 38 additions & 3 deletions pkg/internal/tests/cross-tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func Create(

makeResource := func(writeTo *result) *schema.Resource {
return &schema.Resource{
Schema: resource,
Schema: resource,
SchemaVersion: opts.stateUpgrader.schemaVersion,
StateUpgraders: opts.stateUpgrader.stateUpgraders,
Timeouts: opts.timeouts,
CreateContext: func(_ context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics {
*writeTo = result{rd, meta, true}
rd.SetId("someid") // CreateContext must pick an ID
Expand Down Expand Up @@ -103,17 +106,49 @@ func Create(
assertCtyValEqual(t, "RawConfig", tfResult.data.GetRawConfig(), puResult.data.GetRawConfig())
assertCtyValEqual(t, "RawPlan", tfResult.data.GetRawPlan(), puResult.data.GetRawPlan())
assertCtyValEqual(t, "RawState", tfResult.data.GetRawState(), puResult.data.GetRawState())

for k := range resource {
// TODO: make this recursive
tfVal := tfResult.data.Get(k)
pulVal := puResult.data.Get(k)

tfChangeValOld, tfChangeValNew := tfResult.data.GetChange(k)
pulChangeValOld, pulChangeValNew := puResult.data.GetChange(k)

assertValEqual(t, k, tfVal, pulVal)
assertValEqual(t, k+" Change Old", tfChangeValOld, pulChangeValOld)
assertValEqual(t, k+" Change New", tfChangeValNew, pulChangeValNew)
}
}

type createOpts struct {
resourceInfo *info.Resource
resourceInfo *info.Resource
stateUpgrader createOptsUpgraders
timeouts *schema.ResourceTimeout
}

type createOptsUpgraders struct {
schemaVersion int
stateUpgraders []schema.StateUpgrader
}

// An option that can be used to customize [Create].
type CreateOption func(*createOpts)

// Specify an [info.Resource] to apply to the resource under test.
// CreateResourceInfo specifies an [info.Resource] to apply to the resource under test.
func CreateResourceInfo(info info.Resource) CreateOption {
contract.Assertf(info.Tok == "", "cannot set info.Tok, it will not be respected")
return func(o *createOpts) { o.resourceInfo = &info }
}

// CreateStateUpgrader specifies a schema version and list of state upgrader for [Create].
func CreateStateUpgrader(schemaVersion int, upgraders []schema.StateUpgrader) CreateOption {
return func(o *createOpts) {
o.stateUpgrader = createOptsUpgraders{schemaVersion, upgraders}
}
}

// CreateTimeout specifies a timeout option for [Create].
func CreateTimeout(timeouts *schema.ResourceTimeout) CreateOption {
return func(o *createOpts) { o.timeouts = timeouts }
}
64 changes: 10 additions & 54 deletions pkg/internal/tests/cross-tests/input_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@
package crosstests

import (
"context"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck"
shimv2 "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/sdk-v2"
)

// Adapted from diff_check.go
Expand All @@ -34,59 +31,18 @@ type inputTestCase struct {
}

// Adapted from diff_check.go
//
// Deprecated: [Create] should be used for new tests.
func runCreateInputCheck(t T, tc inputTestCase) {
if tc.Resource.CreateContext != nil {
t.Errorf("Create methods should not be set for these tests!")
}

var tfResData, pulResData *schema.ResourceData
tc.Resource.CreateContext = func(_ context.Context, rd *schema.ResourceData, meta interface{}) diag.Diagnostics {
if tfResData == nil {
tfResData = rd
} else {
pulResData = rd
}

rd.SetId("someid") // CreateContext must pick an ID
return make(diag.Diagnostics, 0)
}

tfwd := t.TempDir()

tfd := newTFResDriver(t, tfwd, defProviderShortName, defRtype, tc.Resource)
tfd.writePlanApply(t, tc.Resource.Schema, defRtype, "example",
coalesceInputs(t, tc.Resource.Schema, tc.Config), lifecycleArgs{})

resMap := map[string]*schema.Resource{defRtype: tc.Resource}
tfp := &schema.Provider{ResourcesMap: resMap}
bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, tfp)
pd := &pulumiDriver{
name: defProviderShortName,
pulumiResourceToken: defRtoken,
tfResourceName: defRtype,
}

yamlProgram := pd.generateYAML(t, convertConfigValueForYamlProperties(t,
bridgedProvider.P.ResourcesMap().Get(pd.tfResourceName).Schema(), tc.ObjectType, tc.Config))

pt := pulcheck.PulCheck(t, bridgedProvider, string(yamlProgram))

pt.Up(t)

for k := range tc.Resource.Schema {
// TODO: make this recursive
tfVal := tfResData.Get(k)
pulVal := pulResData.Get(k)

tfChangeValOld, tfChangeValNew := tfResData.GetChange(k)
pulChangeValOld, pulChangeValNew := pulResData.GetChange(k)

assertValEqual(t, k, tfVal, pulVal)
assertValEqual(t, k+" Change Old", tfChangeValOld, pulChangeValOld)
assertValEqual(t, k+" Change New", tfChangeValNew, pulChangeValNew)
}

assertCtyValEqual(t, "RawConfig", tfResData.GetRawConfig(), pulResData.GetRawConfig())
assertCtyValEqual(t, "RawPlan", tfResData.GetRawPlan(), pulResData.GetRawPlan())
assertCtyValEqual(t, "RawState", tfResData.GetRawState(), pulResData.GetRawState())
Create(t,
tc.Resource.Schema,
coalesceInputs(t, tc.Resource.Schema, tc.Config),
convertConfigValueForYamlProperties(t, shimv2.NewResource(tc.Resource).Schema(), tc.ObjectType, tc.Config),
CreateStateUpgrader(tc.Resource.SchemaVersion, tc.Resource.StateUpgraders),
CreateTimeout(tc.Resource.Timeouts),
)
}
2 changes: 2 additions & 0 deletions pkg/internal/tests/cross-tests/input_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// TestInputsEqualStringBasic validates that [runCreateInputCheck] works across both input
// types.
func TestInputsEqualStringBasic(t *testing.T) {
// Test both config representations.
for _, tc := range []struct {
Expand Down

0 comments on commit 1dd0380

Please sign in to comment.