Skip to content

Commit

Permalink
Add opts to SDKv2 Pulcheck and use Parallel in tests which require env (
Browse files Browse the repository at this point in the history
#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.
  • Loading branch information
VenelinMartinov authored Nov 15, 2024
1 parent b965b42 commit fde5671
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pkg/internal/tests/cross-tests/diff_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/internal/tests/cross-tests/diff_cross_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
31 changes: 12 additions & 19 deletions pkg/tests/detailed_diff_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2518,4 +2511,4 @@ Plan: 0 to add, 1 to change, 0 to destroy.
`).Equal(t, plan.StdOut)
})
}
}
17 changes: 7 additions & 10 deletions pkg/tests/detailed_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
29 changes: 23 additions & 6 deletions pkg/tests/pulcheck/pulcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
)
Expand All @@ -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) {
Expand All @@ -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")
Expand All @@ -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(),
Expand All @@ -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...)
}
10 changes: 7 additions & 3 deletions pkg/tests/type_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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")
Expand Down

0 comments on commit fde5671

Please sign in to comment.