Skip to content

Commit 7cc82df

Browse files
committed
Full fidelity SDKv2 crosstest.Create equality
Supply `tfprotov5.ApplyResourceChangeRequest.ProviderMeta` when we call `ApplyResourceChange` in SDKv2 providers. This gets us to a byte for byte identical result for SDKv2 `crosstest.Create`. This allows us to simplify (and strengthen) the comparison to general equality. Related to #2521
1 parent 8bb538e commit 7cc82df

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

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

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

1616
import (
1717
"context"
18+
"reflect"
1819
"testing"
1920

21+
"github.com/google/go-cmp/cmp"
2022
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
2123
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2224
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
@@ -118,11 +120,22 @@ func Create(
118120
require.True(t, puResult.wasSet, "pulumi test was not set")
119121

120122
// Compare the result
121-
122-
assert.Equal(t, tfResult.meta, puResult.meta,
123-
"assert that both providers were configured with the same provider metadata")
124-
125-
assertResourceDataEqual(t, resourceSchema, tfResult.data, puResult.data)
123+
if assert.True(t, tfResult.wasSet) && assert.True(t, puResult.wasSet) {
124+
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+
}
138+
}
126139
}
127140

128141
type createOpts struct {

pkg/tfshim/sdk-v2/provider2.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,11 +637,26 @@ func (s *grpcServer) ApplyResourceChange(
637637
if err != nil {
638638
return nil, err
639639
}
640+
641+
var providerMetaVal []byte
642+
if providerMeta != nil {
643+
providerMetaVal, err = msgpack.Marshal(*providerMeta, providerMeta.Type())
644+
if err != nil {
645+
return nil, err
646+
}
647+
} else {
648+
providerMetaVal, err = msgpack.Marshal(cty.NullVal(cty.EmptyObject), cty.EmptyObject)
649+
if err != nil {
650+
return nil, err
651+
}
652+
}
653+
640654
req := &tfprotov5.ApplyResourceChangeRequest{
641655
TypeName: typeName,
642656
Config: &tfprotov5.DynamicValue{MsgPack: configVal},
643657
PriorState: &tfprotov5.DynamicValue{MsgPack: priorStateVal},
644658
PlannedState: &tfprotov5.DynamicValue{MsgPack: plannedStateVal},
659+
ProviderMeta: &tfprotov5.DynamicValue{MsgPack: providerMetaVal},
645660
}
646661
if len(plannedMeta) > 0 {
647662
plannedPrivate, err := json.Marshal(plannedMeta)

0 commit comments

Comments
 (0)