Skip to content

Commit f865647

Browse files
authored
Default to inferring pulumi values (#2512)
This API change has been recommended by Venelin and Anton.
1 parent 32c8c4d commit f865647

File tree

7 files changed

+43
-58
lines changed

7 files changed

+43
-58
lines changed

pkg/internal/tests/cross-tests/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Currently, SDKv2 cross-testing supports:
4949
- [Configure](./configure.go)
5050

5151
Cross-tests can be written **either by** specify both the Terraform value *and* the Pulumi value **or** by
52-
specifying *only* the Terraform value and specifying `crosstests.InferPulumiValue()` for the Pulumi value.
52+
specifying *only* the Terraform value and letting the framework infer an equivalent Pulumi value.
5353

5454
``` go
5555
// A cross-test that infers the Pulumi value.
@@ -60,7 +60,6 @@ crosstests.Create(t,
6060
cty.ObjectVal(map[string]cty.Value{
6161
"f0": cty.BoolVal(true),
6262
}),
63-
crosstests.InferPulumiValue(),
6463
)
6564
```
6665

@@ -73,9 +72,9 @@ crosstests.Create(t,
7372
cty.ObjectVal(map[string]cty.Value{
7473
"f0": cty.BoolVal(true),
7574
}),
76-
resource.PropertyMap{
75+
crosstests.CreatePulumiConfig(resource.PropertyMap{
7776
"f0": resource.NewProperty(true),
78-
},
77+
}),
7978
)
8079
```
8180

pkg/internal/tests/cross-tests/adapt.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,9 @@ import (
3333
"github.com/pulumi/pulumi-terraform-bridge/v3/unstable/logging"
3434
)
3535

36-
// InferPulumiValue is a special marker value that tells cross-tests to infer the Pulumi
37-
// value from the TF value.
38-
func InferPulumiValue() resource.PropertyMap {
39-
return resource.PropertyMap{
40-
inferPulumiValueKey: resource.NewProperty(&inferPulumiValueSecret),
41-
}
42-
}
43-
44-
const inferPulumiValueKey = "inferPulumiValue"
45-
46-
var inferPulumiValueSecret = resource.Secret{}
47-
48-
func isInferPulumiMarker(m resource.PropertyMap) bool {
49-
v, ok := m[inferPulumiValueKey]
50-
return ok && v.IsSecret() && v.SecretValue() == &inferPulumiValueSecret
51-
}
52-
5336
// inferPulumiValue generates a Pulumi value that is semantically equivalent to v.
5437
//
55-
// inferPulumiValue takes into account schema information. [InferPulumiValue] is the
56-
// marker value that instructs [crosstests] to invoke [inferPulumiValue].
38+
// inferPulumiValue takes into account schema information.
5739
func inferPulumiValue(t T, schema shim.SchemaMap, infos map[string]*info.Schema, v cty.Value) resource.PropertyMap {
5840
if v.IsNull() {
5941
return nil

pkg/internal/tests/cross-tests/configure.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import (
3232
)
3333

3434
func MakeConfigure(
35-
provider map[string]*schema.Schema, tfConfig cty.Value, puConfig resource.PropertyMap,
35+
provider map[string]*schema.Schema, tfConfig cty.Value,
3636
options ...ConfigureOption,
3737
) func(*testing.T) {
3838
return func(t *testing.T) {
3939
t.Parallel()
40-
Configure(t, provider, tfConfig, puConfig, options...)
40+
Configure(t, provider, tfConfig, options...)
4141
}
4242
}
4343

@@ -49,7 +49,7 @@ func MakeConfigure(
4949
// on Plugin Framework based resources, see
5050
// github.com/pulumi/pulumi-terraform-bridge/pkg/pf/tests/internal/cross-tests.
5151
func Configure(
52-
t T, provider map[string]*schema.Schema, tfConfig cty.Value, puConfig resource.PropertyMap,
52+
t T, provider map[string]*schema.Schema, tfConfig cty.Value,
5353
options ...ConfigureOption,
5454
) {
5555

@@ -58,7 +58,10 @@ func Configure(
5858
f(&opts)
5959
}
6060

61-
if isInferPulumiMarker(puConfig) {
61+
var puConfig resource.PropertyMap
62+
if opts.puConfig != nil {
63+
puConfig = *opts.puConfig
64+
} else {
6265
puConfig = inferPulumiValue(t,
6366
shimv2.NewSchemaMap(provider),
6467
opts.resourceInfo.GetFields(),
@@ -146,13 +149,19 @@ func Configure(
146149

147150
type configureOpts struct {
148151
resourceInfo *info.Resource
152+
puConfig *resource.PropertyMap
149153
}
150154

151-
// An option that can be used to customize [Create].
155+
// An option that can be used to customize [Configure].
152156
type ConfigureOption func(*configureOpts)
153157

154158
// CreateResourceInfo specifies an [info.Resource] to apply to the resource under test.
155159
func ConfigureProviderInfo(info info.Resource) ConfigureOption {
156160
contract.Assertf(info.Tok == "", "cannot set info.Tok, it will not be respected")
157161
return func(o *configureOpts) { o.resourceInfo = &info }
158162
}
163+
164+
// ConfigurePulumiConfig specifies an explicit pulumi value for the configure call.
165+
func ConfigurePulumiConfig(config resource.PropertyMap) ConfigureOption {
166+
return func(o *configureOpts) { o.puConfig = &config }
167+
}

pkg/internal/tests/cross-tests/create.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import (
3232

3333
// MakeCreate is a helper function for calling [Create] in [testing.T.Run] subcases.
3434
func MakeCreate(
35-
resource map[string]*schema.Schema, tfConfig cty.Value, puConfig resource.PropertyMap,
35+
resource map[string]*schema.Schema, tfConfig cty.Value,
3636
options ...CreateOption,
3737
) func(t *testing.T) {
3838
return func(t *testing.T) {
3939
t.Parallel()
40-
Create(t, resource, tfConfig, puConfig, options...)
40+
Create(t, resource, tfConfig, options...)
4141
}
4242
}
4343

@@ -51,7 +51,7 @@ func MakeCreate(
5151
//
5252
// Create *does not* verify the outputs of the resource, only that the provider witnessed the same inputs.
5353
func Create(
54-
t T, resource map[string]*schema.Schema, tfConfig cty.Value, puConfig resource.PropertyMap,
54+
t T, resourceSchema map[string]*schema.Schema, tfConfig cty.Value,
5555
options ...CreateOption,
5656
) {
5757

@@ -60,9 +60,12 @@ func Create(
6060
f(&opts)
6161
}
6262

63-
if isInferPulumiMarker(puConfig) {
63+
var puConfig resource.PropertyMap
64+
if opts.puConfig != nil {
65+
puConfig = *opts.puConfig
66+
} else {
6467
puConfig = inferPulumiValue(t,
65-
shimv2.NewSchemaMap(resource),
68+
shimv2.NewSchemaMap(resourceSchema),
6669
opts.resourceInfo.GetFields(),
6770
tfConfig,
6871
)
@@ -77,7 +80,7 @@ func Create(
7780

7881
makeResource := func(writeTo *result) *schema.Resource {
7982
return &schema.Resource{
80-
Schema: resource,
83+
Schema: resourceSchema,
8184
SchemaVersion: opts.stateUpgrader.schemaVersion,
8285
StateUpgraders: opts.stateUpgrader.stateUpgraders,
8386
Timeouts: opts.timeouts,
@@ -91,7 +94,7 @@ func Create(
9194

9295
tfwd := t.TempDir()
9396
tfd := newTFResDriver(t, tfwd, defProviderShortName, defRtype, makeResource(&tfResult))
94-
tfd.writePlanApply(t, resource, defRtype, "example", tfConfig, lifecycleArgs{})
97+
tfd.writePlanApply(t, resourceSchema, defRtype, "example", tfConfig, lifecycleArgs{})
9598

9699
require.True(t, tfResult.wasSet, "terraform test result was not set")
97100

@@ -119,13 +122,14 @@ func Create(
119122
assert.Equal(t, tfResult.meta, puResult.meta,
120123
"assert that both providers were configured with the same provider metadata")
121124

122-
assertResourceDataEqual(t, resource, tfResult.data, puResult.data)
125+
assertResourceDataEqual(t, resourceSchema, tfResult.data, puResult.data)
123126
}
124127

125128
type createOpts struct {
126129
resourceInfo *info.Resource
127130
stateUpgrader createOptsUpgraders
128131
timeouts *schema.ResourceTimeout
132+
puConfig *resource.PropertyMap
129133
}
130134

131135
type createOptsUpgraders struct {
@@ -153,3 +157,8 @@ func CreateStateUpgrader(schemaVersion int, upgraders []schema.StateUpgrader) Cr
153157
func CreateTimeout(timeouts *schema.ResourceTimeout) CreateOption {
154158
return func(o *createOpts) { o.timeouts = timeouts }
155159
}
160+
161+
// CreatePulumiConfig specifies an explicit config value in Pulumi's value space.
162+
func CreatePulumiConfig(config resource.PropertyMap) CreateOption {
163+
return func(o *createOpts) { o.puConfig = &config }
164+
}

pkg/internal/tests/cross-tests/rapid_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ func TestCreateInputsConvergence(outerT *testing.T) {
8787
Create(rT,
8888
tv.schemaMap,
8989
coalesceInputs(rT, tv.schemaMap, config),
90-
InferPulumiValue(),
9190
)
9291
})
9392
}

pkg/tfbridge/tests/provider_configure_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,29 @@ func TestConfigureSimpleValues(t *testing.T) {
2828
"f0": {Type: schema.TypeString, Required: true},
2929
}, cty.ObjectVal(map[string]cty.Value{
3030
"f0": cty.StringVal("v0"),
31-
}), crosstests.InferPulumiValue()))
31+
})))
3232

3333
t.Run("bool", crosstests.MakeConfigure(map[string]*schema.Schema{
3434
"f0": {Type: schema.TypeBool, Required: true},
3535
"f1": {Type: schema.TypeBool, Required: true},
3636
}, cty.ObjectVal(map[string]cty.Value{
3737
"f0": cty.BoolVal(false),
3838
"f1": cty.BoolVal(true),
39-
}), crosstests.InferPulumiValue()))
39+
})))
4040

4141
t.Run("int", crosstests.MakeConfigure(map[string]*schema.Schema{
4242
"f0": {Type: schema.TypeInt, Required: true},
4343
}, cty.ObjectVal(map[string]cty.Value{
4444
"f0": cty.NumberIntVal(123456),
45-
}), crosstests.InferPulumiValue()))
45+
})))
4646

4747
t.Run("float64", func(t *testing.T) {
4848
t.Skip("TODO: Float64 does not pass cross-tests")
4949
crosstests.Configure(t, map[string]*schema.Schema{
5050
"f0": {Type: schema.TypeFloat, Required: true},
5151
}, cty.ObjectVal(map[string]cty.Value{
5252
"f0": cty.NumberFloatVal(123.456),
53-
}), crosstests.InferPulumiValue())
53+
}))
5454
})
5555
}
5656

@@ -59,7 +59,7 @@ func TestConfigureSimpleSecretValues(t *testing.T) {
5959
"f0": {Type: schema.TypeString, Required: true},
6060
}, cty.ObjectVal(map[string]cty.Value{
6161
"f0": cty.StringVal("v0"),
62-
}), resource.PropertyMap{
62+
}), crosstests.ConfigurePulumiConfig(resource.PropertyMap{
6363
"f0": resource.MakeSecret(resource.NewProperty("v0")),
64-
}))
64+
})))
6565
}

pkg/tfbridge/tests/provider_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ func TestIntToStringOverride(t *testing.T) {
5151
cty.ObjectVal(map[string]cty.Value{
5252
"f0": cty.NumberIntVal(largeInt),
5353
}),
54-
resource.PropertyMap{
54+
crosstests.CreatePulumiConfig(resource.PropertyMap{
5555
"f0": resource.NewProperty(strconv.FormatInt(largeInt, 10)),
56-
},
56+
}),
5757
crosstests.CreateResourceInfo(info.Resource{Fields: map[string]*info.Schema{
5858
"f0": {Type: "string"},
5959
}}),
@@ -124,7 +124,6 @@ func TestInputsConfigModeEqual(t *testing.T) {
124124
},
125125
},
126126
tc.config,
127-
crosstests.InferPulumiValue(),
128127
)
129128
})
130129
}
@@ -151,7 +150,6 @@ func TestStateFunc(t *testing.T) {
151150
cty.ObjectVal(map[string]cty.Value{
152151
"test": cty.StringVal("hello"),
153152
}),
154-
crosstests.InferPulumiValue(),
155153
)
156154
}
157155

@@ -172,7 +170,6 @@ func TestInputsUnspecifiedMaxItemsOne(t *testing.T) {
172170
},
173171
},
174172
cty.ObjectVal(map[string]cty.Value{}),
175-
crosstests.InferPulumiValue(),
176173
)
177174
}
178175

@@ -192,7 +189,6 @@ func TestOptionalSetNotSpecified(t *testing.T) {
192189
},
193190
},
194191
cty.ObjectVal(map[string]cty.Value{}),
195-
crosstests.InferPulumiValue(),
196192
)
197193
}
198194

@@ -217,7 +213,6 @@ func TestInputsEqualEmptyList(t *testing.T) {
217213
cty.ObjectVal(map[string]cty.Value{
218214
"f0": cty.ListValEmpty(cty.String),
219215
}),
220-
crosstests.InferPulumiValue(),
221216
)
222217
})
223218
}
@@ -245,7 +240,6 @@ func TestCreateDoesNotInvokeStateUpgraders(t *testing.T) {
245240
cty.ObjectVal(map[string]cty.Value{
246241
"f0": cty.StringVal("default"),
247242
}),
248-
crosstests.InferPulumiValue(),
249243
crosstests.CreateStateUpgrader(1, []schema.StateUpgrader{
250244
{
251245
Type: resource().CoreConfigSchema().ImpliedType(),
@@ -270,7 +264,6 @@ func TestTimeouts(t *testing.T) {
270264
},
271265
},
272266
cty.EmptyObjectVal,
273-
crosstests.InferPulumiValue(),
274267
crosstests.CreateTimeout(&schema.ResourceTimeout{
275268
Create: schema.DefaultTimeout(time.Duration(120)),
276269
}),
@@ -297,7 +290,6 @@ func TestMap(t *testing.T) {
297290
"key2": cty.StringVal("val2"),
298291
}),
299292
}),
300-
crosstests.InferPulumiValue(),
301293
)
302294
}
303295

@@ -315,7 +307,6 @@ func TestEmptySetOfEmptyObjects(t *testing.T) {
315307
cty.ObjectVal(map[string]cty.Value{
316308
"d3f0": cty.SetValEmpty(cty.EmptyObject),
317309
}),
318-
crosstests.InferPulumiValue(),
319310
)
320311
}
321312

@@ -332,7 +323,6 @@ func TestInputsEmptyString(t *testing.T) {
332323
cty.ObjectVal(map[string]cty.Value{
333324
"f0": cty.StringVal(""),
334325
}),
335-
crosstests.InferPulumiValue(),
336326
)
337327
}
338328

@@ -384,7 +374,6 @@ func TestInputsNestedBlocksEmpty(t *testing.T) {
384374
},
385375
},
386376
tc.config,
387-
crosstests.InferPulumiValue(),
388377
))
389378
}
390379
}
@@ -410,7 +399,6 @@ func TestExplicitNilList(t *testing.T) {
410399
},
411400
},
412401
cty.ObjectVal(map[string]cty.Value{"f0": cty.NullVal(cty.List(cty.Map(cty.Number)))}),
413-
crosstests.InferPulumiValue(),
414402
)
415403
}
416404

@@ -466,7 +454,6 @@ func TestInputsEmptyCollections(t *testing.T) {
466454
},
467455
},
468456
cty.EmptyObjectVal,
469-
crosstests.InferPulumiValue(),
470457
))
471458
}
472459
}

0 commit comments

Comments
 (0)