Skip to content

Commit 80add2e

Browse files
committed
Full fidelity SDKv2 crosstest.Configure equality
Stacked on top of #2840. This switches `crosstest.Configure` over to using a stronger comparison function. Fixes #2521
1 parent cece87c commit 80add2e

File tree

3 files changed

+21
-66
lines changed

3 files changed

+21
-66
lines changed
Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,39 @@
11
package crosstests
22

33
import (
4-
"github.com/hashicorp/go-cty/cty"
4+
"reflect"
5+
6+
"github.com/google/go-cmp/cmp"
57
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6-
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

10-
func FailNotEqual(t T, name string, tfVal, pulVal any) {
11-
t.Logf(name + " not equal!")
12-
t.Logf("TF value %s", tfVal)
13-
t.Logf("PU value %s", pulVal)
14-
t.Fail()
15-
}
16-
17-
func assertCtyValEqual(t T, name string, tfVal, pulVal cty.Value) {
18-
if !tfVal.RawEquals(pulVal) {
19-
FailNotEqual(t, name, tfVal.GoString(), pulVal.GoString())
20-
}
21-
}
22-
2311
func assertValEqual(t T, name string, tfVal, pulVal any) {
2412
// usually plugin-sdk schema types
2513
if hasEqualTfVal, ok := tfVal.(interface{ Equal(interface{}) bool }); ok {
2614
if !hasEqualTfVal.Equal(pulVal) {
27-
FailNotEqual(t, name, tfVal, pulVal)
15+
t.Logf(name + " not equal!")
16+
t.Logf("TF value %s", tfVal)
17+
t.Logf("PU value %s", pulVal)
18+
t.Fail()
2819
}
2920
} else {
3021
require.Equal(t, tfVal, pulVal, "Values for key %s do not match", name)
3122
}
3223
}
3324

34-
func assertResourceDataEqual(t T, resourceSchema map[string]*schema.Schema, tfResult, puResult *schema.ResourceData) {
35-
// TODO[pulumi/pulumi-terraform-bridge#2521]: We are unable to assert that both
36-
// providers were configured with the exact same data. Type information doesn't
37-
// line up in the simple case. This just doesn't work:
38-
//
39-
// assert.Equal(t, tfResult, puResult)
40-
//
41-
// We make do by comparing slices tfResult and puResult.
42-
require.NotNil(t, tfResult)
43-
require.NotNil(t, puResult)
44-
assertCtyValEqual(t, "RawConfig", tfResult.GetRawConfig(), puResult.GetRawConfig())
45-
assertCtyValEqual(t, "RawPlan", tfResult.GetRawPlan(), puResult.GetRawPlan())
46-
assertCtyValEqual(t, "RawState", tfResult.GetRawState(), puResult.GetRawState())
47-
48-
for _, timeout := range []string{
49-
schema.TimeoutCreate,
50-
schema.TimeoutRead,
51-
schema.TimeoutUpdate,
52-
schema.TimeoutDelete,
53-
schema.TimeoutDefault,
54-
} {
55-
assert.Equal(t, tfResult.Timeout(timeout), puResult.Timeout(timeout), "timeout %s", timeout)
25+
func assertResourceDataEqual(t T, tfResult, puResult *schema.ResourceData) {
26+
// Use cmp to check if data is equal. We need to use cmp instead of
27+
// `assert`'s default `reflect.DeepEqual` because cmp treats identical
28+
// function pointers as equal, but `reflect.DeepEqual` does not.
29+
opts := []cmp.Option{
30+
cmp.Exporter(func(reflect.Type) bool { return true }),
31+
cmp.Comparer(func(x, y schema.SchemaStateFunc) bool {
32+
return reflect.ValueOf(x).Pointer() == reflect.ValueOf(y).Pointer()
33+
}),
5634
}
57-
58-
for k := range resourceSchema {
59-
// TODO: make this recursive
60-
tfVal := tfResult.Get(k)
61-
pulVal := puResult.Get(k)
62-
63-
tfChangeValOld, tfChangeValNew := tfResult.GetChange(k)
64-
pulChangeValOld, pulChangeValNew := puResult.GetChange(k)
65-
66-
assertValEqual(t, k, tfVal, pulVal)
67-
assertValEqual(t, k+" Change Old", tfChangeValOld, pulChangeValOld)
68-
assertValEqual(t, k+" Change New", tfChangeValNew, pulChangeValNew)
35+
if !cmp.Equal(tfResult, puResult, opts...) {
36+
t.Logf("Diff: %s", cmp.Diff(tfResult, puResult, opts...))
37+
t.Fail()
6938
}
7039
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func Configure(
148148
}
149149
require.True(t, puResult.resourceCreated, "pulumi resource result was not set")
150150

151-
assertResourceDataEqual(t, provider, tfResult.data, puResult.data)
151+
assertResourceDataEqual(t, tfResult.data, puResult.data)
152152
}
153153

154154
type configureOpts struct {

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ package crosstests
1515

1616
import (
1717
"context"
18-
"reflect"
1918
"testing"
2019

21-
"github.com/google/go-cmp/cmp"
2220
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2321
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2422
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
@@ -122,19 +120,7 @@ func Create(
122120
// Compare the result
123121
if assert.True(t, tfResult.wasSet) && assert.True(t, puResult.wasSet) {
124122
assert.Equal(t, tfResult.meta, puResult.meta, "meta")
125-
// Use cmp to check if data is equal. We need to use cmp instead of
126-
// `assert`'s default `reflect.DeepEqual` because cmp treats identical
127-
// function pointers as equal, but `reflect.DeepEqual` does not.
128-
opts := []cmp.Option{
129-
cmp.Exporter(func(reflect.Type) bool { return true }),
130-
cmp.Comparer(func(x, y schema.SchemaStateFunc) bool {
131-
return reflect.ValueOf(x).Pointer() == reflect.ValueOf(y).Pointer()
132-
}),
133-
}
134-
if !cmp.Equal(tfResult.data, puResult.data, opts...) {
135-
t.Logf("Diff: %s", cmp.Diff(tfResult.data, puResult.data, opts...))
136-
t.Fail()
137-
}
123+
assertResourceDataEqual(t, tfResult.data, puResult.data)
138124
}
139125
}
140126

0 commit comments

Comments
 (0)