From fde56714fdeac25e9353d1fd2e0d86ea6fe9751e Mon Sep 17 00:00:00 2001 From: VenelinMartinov Date: Fri, 15 Nov 2024 16:36:57 +0000 Subject: [PATCH] Add opts to SDKv2 Pulcheck and use Parallel in tests which require env (#2573) This PR adds a variadic opts parameter to the SDKv2 PulCheck, to match what PF does. This allows us to pass env vars to pulumitest. ~Use pulumitests env vars to pass bridge feature flags.~ PulumTest env vars do not work for bridge features because the provider is started in-process instead of by the engine. Instead we specify bridge features as arguments to `pulcheck`. This in turn allows us to use t.Parallel for some of our slowest tests which require env vars. --- pkg/internal/tests/cross-tests/diff_check.go | 2 +- .../tests/cross-tests/diff_cross_test.go | 3 +- pkg/tests/detailed_diff_set_test.go | 31 +++++++------------ pkg/tests/detailed_diff_test.go | 17 +++++----- pkg/tests/pulcheck/pulcheck.go | 29 +++++++++++++---- pkg/tests/type_checker_test.go | 10 ++++-- 6 files changed, 51 insertions(+), 41 deletions(-) diff --git a/pkg/internal/tests/cross-tests/diff_check.go b/pkg/internal/tests/cross-tests/diff_check.go index e2ec0d088..ae17b763f 100644 --- a/pkg/internal/tests/cross-tests/diff_check.go +++ b/pkg/internal/tests/cross-tests/diff_check.go @@ -58,7 +58,7 @@ func runDiffCheck(t T, tc diffTestCase) crosstestsimpl.DiffResult { resMap := map[string]*schema.Resource{defRtype: tc.Resource} tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, tfp) + bridgedProvider := pulcheck.BridgedProvider(t, defProviderShortName, tfp, pulcheck.EnableAccurateBridgePreviews()) if tc.DeleteBeforeReplace { bridgedProvider.Resources[defRtype].DeleteBeforeReplace = true } diff --git a/pkg/internal/tests/cross-tests/diff_cross_test.go b/pkg/internal/tests/cross-tests/diff_cross_test.go index a394b0134..105a9911b 100644 --- a/pkg/internal/tests/cross-tests/diff_cross_test.go +++ b/pkg/internal/tests/cross-tests/diff_cross_test.go @@ -1054,8 +1054,7 @@ func findKeyInPulumiDetailedDiff(detailedDiff map[string]interface{}, key string } func TestNilVsEmptyNestedCollections(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: remove once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() for _, MaxItems := range []int{0, 1} { t.Run(fmt.Sprintf("MaxItems=%d", MaxItems), func(t *testing.T) { res := &schema.Resource{ diff --git a/pkg/tests/detailed_diff_set_test.go b/pkg/tests/detailed_diff_set_test.go index cdae45ee9..3566b7c7d 100644 --- a/pkg/tests/detailed_diff_set_test.go +++ b/pkg/tests/detailed_diff_set_test.go @@ -22,7 +22,7 @@ func runDetailedDiffTest( t *testing.T, resMap map[string]*schema.Resource, program1, program2 string, ) (string, map[string]interface{}) { tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) pt := pulcheck.PulCheck(t, bridgedProvider, program1) pt.Up(t) pulumiYamlPath := filepath.Join(pt.CurrentStack().Workspace().WorkDir(), "Pulumi.yaml") @@ -49,8 +49,7 @@ func runDetailedDiffTest( } func TestDetailedDiffSet(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() runTest := func(t *testing.T, resMap map[string]*schema.Resource, props1, props2 interface{}, expected, expectedDetailedDiff autogold.Value, ) { @@ -1716,6 +1715,7 @@ resources: t.Run(tc.name, func(t *testing.T) { for _, forceNew := range []bool{false, true} { t.Run(fmt.Sprintf("ForceNew=%v", forceNew), func(t *testing.T) { + t.Parallel() expected := tc.expectedAttr if forceNew { expected = tc.expectedAttrForceNew @@ -1828,8 +1828,7 @@ outputs: } func TestDetailedDiffUnknownSetAttributeElement(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() resMap := map[string]*schema.Resource{ "prov_test": { Schema: map[string]*schema.Schema{ @@ -2037,8 +2036,7 @@ func TestDetailedDiffUnknownSetAttributeElement(t *testing.T) { } func TestUnknownSetAttributeDiff(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() resMap := map[string]*schema.Resource{ "prov_test": { Schema: map[string]*schema.Schema{ @@ -2110,8 +2108,7 @@ func TestUnknownSetAttributeDiff(t *testing.T) { } func TestDetailedDiffSetDuplicates(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() resMap := map[string]*schema.Resource{ "prov_test": { Schema: map[string]*schema.Schema{ @@ -2126,7 +2123,7 @@ func TestDetailedDiffSetDuplicates(t *testing.T) { }, } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) program := ` name: test @@ -2200,9 +2197,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. } func TestDetailedDiffSetNestedAttributeUpdated(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") - + t.Parallel() resMap := map[string]*schema.Resource{ "prov_test": { Schema: map[string]*schema.Schema{ @@ -2232,7 +2227,7 @@ func TestDetailedDiffSetNestedAttributeUpdated(t *testing.T) { tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) program := ` name: test @@ -2367,9 +2362,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. } func TestDetailedDiffSetComputedNestedAttribute(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") - + t.Parallel() resCount := 0 setComputedProp := func(t *testing.T, d *schema.ResourceData, _ interface{}) diag.Diagnostics { testSet := d.Get("test").(*schema.Set) @@ -2421,7 +2414,7 @@ func TestDetailedDiffSetComputedNestedAttribute(t *testing.T) { } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) program := ` name: test @@ -2518,4 +2511,4 @@ Plan: 0 to add, 1 to change, 0 to destroy. `).Equal(t, plan.StdOut) }) -} \ No newline at end of file +} diff --git a/pkg/tests/detailed_diff_test.go b/pkg/tests/detailed_diff_test.go index 0916230b2..85217dfdb 100644 --- a/pkg/tests/detailed_diff_test.go +++ b/pkg/tests/detailed_diff_test.go @@ -703,8 +703,7 @@ resources: } func TestDetailedDiffPlainTypes(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") + t.Parallel() resMap := map[string]*schema.Resource{ "prov_test": { Schema: map[string]*schema.Schema{ @@ -768,7 +767,7 @@ func TestDetailedDiffPlainTypes(t *testing.T) { }, } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) program := ` name: test @@ -1779,9 +1778,7 @@ resources: } func TestUnknownCollectionForceNewDetailedDiff(t *testing.T) { - // TODO[pulumi/pulumi-terraform-bridge#2517]: Remove this once accurate bridge previews are rolled out - t.Setenv("PULUMI_TF_BRIDGE_ACCURATE_BRIDGE_PREVIEW", "true") - + t.Parallel() collectionForceNewResource := func(typ schema.ValueType) *schema.Resource { return &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -1886,7 +1883,7 @@ func TestUnknownCollectionForceNewDetailedDiff(t *testing.T) { } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) runTest := func(t *testing.T, program2 string, expectedOutput autogold.Value) { runTest(t, program2, bridgedProvider, expectedOutput) } @@ -1949,7 +1946,7 @@ func TestUnknownCollectionForceNewDetailedDiff(t *testing.T) { } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) runTest := func(t *testing.T, program2 string, expectedOutput autogold.Value) { runTest(t, program2, bridgedProvider, expectedOutput) } @@ -2012,7 +2009,7 @@ func TestUnknownCollectionForceNewDetailedDiff(t *testing.T) { } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) runTest := func(t *testing.T, program2 string, expectedOutput autogold.Value) { runTest(t, program2, bridgedProvider, expectedOutput) } @@ -2075,7 +2072,7 @@ func TestUnknownCollectionForceNewDetailedDiff(t *testing.T) { } tfp := &schema.Provider{ResourcesMap: resMap} - bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp) + bridgedProvider := pulcheck.BridgedProvider(t, "prov", tfp, pulcheck.EnableAccurateBridgePreviews()) runTest := func(t *testing.T, program2 string, expectedOutput autogold.Value) { runTest(t, program2, bridgedProvider, expectedOutput) } diff --git a/pkg/tests/pulcheck/pulcheck.go b/pkg/tests/pulcheck/pulcheck.go index 38b90ad9f..188769cf9 100644 --- a/pkg/tests/pulcheck/pulcheck.go +++ b/pkg/tests/pulcheck/pulcheck.go @@ -127,14 +127,21 @@ type T interface { } type bridgedProviderOpts struct { - StateEdit shimv2.PlanStateEditFunc - resourceInfo map[string]*info.Resource - configInfo map[string]*info.Schema + StateEdit shimv2.PlanStateEditFunc + resourceInfo map[string]*info.Resource + configInfo map[string]*info.Schema + EnableAccurateBridgePreviews bool } // BridgedProviderOpts type BridgedProviderOpt func(*bridgedProviderOpts) +func EnableAccurateBridgePreviews() BridgedProviderOpt { + return func(o *bridgedProviderOpts) { + o.EnableAccurateBridgePreviews = true + } +} + func WithStateEdit(f shimv2.PlanStateEditFunc) BridgedProviderOpt { return func(o *bridgedProviderOpts) { o.StateEdit = f @@ -166,6 +173,14 @@ func BridgedProvider(t T, providerName string, tfp *schema.Provider, opts ...Bri EnsureProviderValid(t, tfp) + // If the PULUMI_ACCURATE_BRIDGE_PREVIEWS environment variable is set, use it to enable + // accurate bridge previews. + accurateBridgePreviews := os.Getenv("PULUMI_ACCURATE_BRIDGE_PREVIEWS") == "true" + // Otherwise, use the value of the EnableAccurateBridgePreviews option. + if !accurateBridgePreviews { + accurateBridgePreviews = options.EnableAccurateBridgePreviews + } + shimProvider := shimv2.NewProvider(tfp, shimv2.WithPlanStateEdit(options.StateEdit), ) @@ -177,6 +192,7 @@ func BridgedProvider(t T, providerName string, tfp *schema.Provider, opts ...Bri MetadataInfo: &tfbridge.MetadataInfo{}, EnableZeroDefaultSchemaVersion: true, Resources: options.resourceInfo, + EnableAccurateBridgePreview: accurateBridgePreviews, Config: options.configInfo, } makeToken := func(module, name string) (string, error) { @@ -194,7 +210,7 @@ func skipUnlessLinux(t T) { } // This is an experimental API. -func PulCheck(t T, bridgedProvider info.Provider, program string) *pulumitest.PulumiTest { +func PulCheck(t T, bridgedProvider info.Provider, program string, opts ...opttest.Option) *pulumitest.PulumiTest { skipUnlessLinux(t) puwd := t.TempDir() p := filepath.Join(puwd, "Pulumi.yaml") @@ -203,7 +219,7 @@ func PulCheck(t T, bridgedProvider info.Provider, program string) *pulumitest.Pu err := os.WriteFile(p, []byte(program), 0o600) require.NoError(t, err) - opts := []opttest.Option{ + defaultOpts := []opttest.Option{ opttest.Env("PULUMI_DISABLE_AUTOMATIC_PLUGIN_ACQUISITION", "true"), opttest.TestInPlace(), opttest.SkipInstall(), @@ -217,5 +233,6 @@ func PulCheck(t T, bridgedProvider info.Provider, program string) *pulumitest.Pu ), } - return pulumitest.NewPulumiTest(t, puwd, opts...) + defaultOpts = append(defaultOpts, opts...) + return pulumitest.NewPulumiTest(t, puwd, defaultOpts...) } diff --git a/pkg/tests/type_checker_test.go b/pkg/tests/type_checker_test.go index a7b4bbd71..792d3d5b5 100644 --- a/pkg/tests/type_checker_test.go +++ b/pkg/tests/type_checker_test.go @@ -6,12 +6,13 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/pulumi/providertest/pulumitest/opttest" "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck" "github.com/stretchr/testify/require" ) func TestTypeChecker(t *testing.T) { - t.Setenv("PULUMI_DEBUG_YAML_DISABLE_TYPE_CHECKING", "true") + t.Parallel() makeResMap := func(sch map[string]*schema.Schema) map[string]*schema.Resource { return map[string]*schema.Resource{ "prov_test": {Schema: sch}, @@ -28,10 +29,13 @@ runtime: yaml resources: mainRes: type: prov:index:Test - properties: %s`, propsJSON) + properties: %s`, propsJSON) bridgedProvider := pulcheck.BridgedProvider(t, "prov", &schema.Provider{ResourcesMap: resMap}) - pt := pulcheck.PulCheck(t, bridgedProvider, program) + pt := pulcheck.PulCheck(t, bridgedProvider, program, + opttest.Env("PULUMI_DEBUG_YAML_DISABLE_TYPE_CHECKING", "true"), + opttest.Env("PULUMI_ERROR_TYPE_CHECKER", "true"), + ) _, err = pt.CurrentStack().Up(pt.Context()) require.ErrorContains(t, err, "Unexpected type at field")