@@ -30,6 +30,7 @@ import (
30
30
pb "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/providerbuilder"
31
31
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfbridge"
32
32
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tfgen"
33
+ "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/assume"
33
34
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/cross-tests"
34
35
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/tfcheck"
35
36
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info"
@@ -50,10 +51,13 @@ import (
50
51
// }
51
52
//
52
53
// For details on the test itself, see [Configure].
53
- func MakeConfigure (schema schema.Schema , tfConfig map [string ]cty.Value , puConfig resource.PropertyMap ) func (t * testing.T ) {
54
+ func MakeConfigure (
55
+ schema schema.Schema , tfConfig map [string ]cty.Value , puConfig resource.PropertyMap ,
56
+ options ... ConfigureOption ,
57
+ ) func (t * testing.T ) {
54
58
return func (t * testing.T ) {
55
59
t .Parallel ()
56
- Configure (t , schema , tfConfig , puConfig )
60
+ Configure (t , schema , tfConfig , puConfig , options ... )
57
61
}
58
62
}
59
63
@@ -81,12 +85,17 @@ func MakeConfigure(schema schema.Schema, tfConfig map[string]cty.Value, puConfig
81
85
// +--------------------+ +---------------------+
82
86
//
83
87
// Configure should be safe to run in parallel.
84
- func Configure (t * testing.T , schema schema.Schema , tfConfig map [string ]cty.Value , puConfig resource.PropertyMap ) {
85
- skipUnlessLinux (t )
88
+ func Configure (
89
+ t TestingT , schema schema.Schema , tfConfig map [string ]cty.Value , puConfig resource.PropertyMap ,
90
+ options ... ConfigureOption ,
91
+ ) {
92
+ assume .TerraformCLI (t )
93
+
94
+ var opts configureOptions
95
+ for _ , o := range options {
96
+ o (& opts )
97
+ }
86
98
87
- // By default, logs only show when they are on a failed test. By logging to
88
- // topLevelT, we can log items to be shown if downstream tests fail.
89
- topLevelT := t
90
99
const providerName = "test"
91
100
92
101
prov := func (config * tfsdk.Config ) * pb.Provider {
@@ -103,8 +112,11 @@ func Configure(t *testing.T, schema schema.Schema, tfConfig map[string]cty.Value
103
112
}
104
113
105
114
var tfOutput , puOutput tfsdk.Config
106
- t .Run ("tf" , func (t * testing.T ) {
107
- defer propageteSkip (topLevelT , t )
115
+ var runOnFail []func (t TestingT )
116
+
117
+ logf := func (msg string , a ... any ) { runOnFail = append (runOnFail , func (t TestingT ) { t .Logf (msg , a ... ) }) }
118
+
119
+ withAugmentedT (t , func (t * augmentedT ) { // --- Run Terraform Provider ---
108
120
var hcl bytes.Buffer
109
121
err := crosstests .WritePF (& hcl ).Provider (schema , providerName , tfConfig )
110
122
require .NoError (t , err )
@@ -120,13 +132,12 @@ resource "` + providerName + `_res" "res" {}
120
132
121
133
driver .Write (t , hcl .String ())
122
134
plan , err := driver .Plan (t )
123
- require .NoError (t , err )
135
+ require .NoError (t , err , "failed to generate TF plan" )
124
136
err = driver .Apply (t , plan )
125
137
require .NoError (t , err )
126
138
})
127
139
128
- t .Run ("bridged" , func (t * testing.T ) {
129
- defer propageteSkip (topLevelT , t )
140
+ withAugmentedT (t , func (t * augmentedT ) { // --- Run Pulumi Provider ---
130
141
dir := t .TempDir ()
131
142
132
143
pulumiYaml := map [string ]any {
@@ -145,7 +156,7 @@ resource "` + providerName + `_res" "res" {}
145
156
146
157
bytes , err := yaml .Marshal (pulumiYaml )
147
158
require .NoError (t , err )
148
- topLevelT . Logf ("Pulumi.yaml:\n %s" , string (bytes ))
159
+ logf ("Pulumi.yaml:\n %s" , string (bytes ))
149
160
err = os .WriteFile (filepath .Join (dir , "Pulumi.yaml" ), bytes , 0600 )
150
161
require .NoError (t , err )
151
162
@@ -187,11 +198,49 @@ resource "` + providerName + `_res" "res" {}
187
198
contract .Ignore (test .Up (t )) // Assert that the update succeeded, but not the result.
188
199
})
189
200
190
- skipCompare := t .Failed () || t .Skipped ()
191
- t .Run ("compare" , func (t * testing.T ) {
192
- if skipCompare {
193
- t .Skipf ("skipping since earlier steps did not complete" )
194
- }
201
+ // --- Compare results -----------------------------
202
+ if opts .testEqual != nil {
203
+ opts .testEqual (t , tfOutput , puOutput )
204
+ } else {
195
205
assert .Equal (t , tfOutput , puOutput )
196
- })
206
+ }
207
+
208
+ if t .Failed () {
209
+ for _ , f := range runOnFail {
210
+ f (t )
211
+ }
212
+ }
213
+ }
214
+
215
+ // An option for configuring [Configure] or [MakeConfigure].
216
+ //
217
+ // Existing options are:
218
+ // - [WithConfigureEquals]
219
+ type ConfigureOption func (* configureOptions )
220
+
221
+ type configureOptions struct {
222
+ testEqual func (t TestingT , tfOutput , puOutput tfsdk.Config )
223
+ }
224
+
225
+ // WithConfigureEqual defines a comparison function for the cross-test.
226
+ //
227
+ // This function is called after both the Terraform and Pulumi portions have run, and is
228
+ // responsible for asserting that the results match.
229
+ //
230
+ // Here are 2 examples:
231
+ //
232
+ // // Assert that both Terraform and Pulumi ran, but do not assert anything about their behavior.
233
+ // WithConfigureEqual(func(t TestingT, tfOutput, puOutput tfsdk.Config) {})
234
+ //
235
+ // // Assert that the underlying provider witnessed saw could not distinguish between
236
+ // // the direct and bridged call (the default behavior).
237
+ // WithConfigureEqual(func(t TestingT, tfOutput, puOutput tfsdk.Config) {
238
+ // assert.Equal(t, tfOutput, puOutput)
239
+ // })
240
+ //
241
+ // WithConfigureEqual should be used only when the direct and bridged providers don't
242
+ // agree, to limit the scope of the test so it can be checked in. In general, usage should
243
+ // be accompanied by a bridge issue to track the discrepancy.
244
+ func WithConfigureEqual (equal func (t TestingT , tfOutput , puOutput tfsdk.Config )) ConfigureOption {
245
+ return func (opts * configureOptions ) { opts .testEqual = equal }
197
246
}
0 commit comments