Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement runCreateInputCheck in terms of Create #2503

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this mean here? How can we get to 100%?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember right, we are comparing some eeky structures here coming from the TFSDK, these structures resist directly comparing for equality but this is something we need for the purposes of the tests. It is a little tedious to get right but sounds like it should be doable within an hour or two. Am I understanding right? Need a hand here perhaps?

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
Loading