From 1dd0380282966d20077c767b2cea7f0ffe0da763 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Fri, 18 Oct 2024 22:02:19 +0200 Subject: [PATCH] Implement `runCreateInputCheck` in terms of `Create` (#2503) --- pkg/internal/tests/cross-tests/create.go | 41 +++++++++++- pkg/internal/tests/cross-tests/input_check.go | 64 +++---------------- .../tests/cross-tests/input_cross_test.go | 2 + 3 files changed, 50 insertions(+), 57 deletions(-) diff --git a/pkg/internal/tests/cross-tests/create.go b/pkg/internal/tests/cross-tests/create.go index 445758ee0..32b76dd96 100644 --- a/pkg/internal/tests/cross-tests/create.go +++ b/pkg/internal/tests/cross-tests/create.go @@ -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 @@ -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 } +} diff --git a/pkg/internal/tests/cross-tests/input_check.go b/pkg/internal/tests/cross-tests/input_check.go index b9c94bb27..96864ebc1 100644 --- a/pkg/internal/tests/cross-tests/input_check.go +++ b/pkg/internal/tests/cross-tests/input_check.go @@ -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 @@ -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), + ) } diff --git a/pkg/internal/tests/cross-tests/input_cross_test.go b/pkg/internal/tests/cross-tests/input_cross_test.go index 5549b87f7..fda999bf3 100644 --- a/pkg/internal/tests/cross-tests/input_cross_test.go +++ b/pkg/internal/tests/cross-tests/input_cross_test.go @@ -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 {