Skip to content

Commit

Permalink
Full fidelity SDKv2 crosstest.Create equality
Browse files Browse the repository at this point in the history
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
  • Loading branch information
iwahbe committed Jan 17, 2025
1 parent 8bb538e commit 7cc82df
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
23 changes: 18 additions & 5 deletions pkg/internal/tests/cross-tests/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ package crosstests

import (
"context"
"reflect"
"testing"

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

// Compare the result

assert.Equal(t, tfResult.meta, puResult.meta,
"assert that both providers were configured with the same provider metadata")

assertResourceDataEqual(t, resourceSchema, tfResult.data, puResult.data)
if assert.True(t, tfResult.wasSet) && assert.True(t, puResult.wasSet) {
assert.Equal(t, tfResult.meta, puResult.meta, "meta")
// Use cmp to check if data is equal. We need to use cmp instead of
// `assert`'s default `reflect.DeepEqual` because cmp treats identical
// function pointers as equal, but `reflect.DeepEqual` does not.
opts := []cmp.Option{
cmp.Exporter(func(reflect.Type) bool { return true }),
cmp.Comparer(func(x, y schema.SchemaStateFunc) bool {
return reflect.ValueOf(x).Pointer() == reflect.ValueOf(y).Pointer()
}),
}
if !cmp.Equal(tfResult.data, puResult.data, opts...) {
t.Logf("Diff: %s", cmp.Diff(tfResult.data, puResult.data, opts...))
t.Fail()
}
}
}

type createOpts struct {
Expand Down
15 changes: 15 additions & 0 deletions pkg/tfshim/sdk-v2/provider2.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,11 +637,26 @@ func (s *grpcServer) ApplyResourceChange(
if err != nil {
return nil, err
}

var providerMetaVal []byte
if providerMeta != nil {
providerMetaVal, err = msgpack.Marshal(*providerMeta, providerMeta.Type())
if err != nil {
return nil, err
}
} else {
providerMetaVal, err = msgpack.Marshal(cty.NullVal(cty.EmptyObject), cty.EmptyObject)
if err != nil {
return nil, err
}
}

req := &tfprotov5.ApplyResourceChangeRequest{
TypeName: typeName,
Config: &tfprotov5.DynamicValue{MsgPack: configVal},
PriorState: &tfprotov5.DynamicValue{MsgPack: priorStateVal},
PlannedState: &tfprotov5.DynamicValue{MsgPack: plannedStateVal},
ProviderMeta: &tfprotov5.DynamicValue{MsgPack: providerMetaVal},
}
if len(plannedMeta) > 0 {
plannedPrivate, err := json.Marshal(plannedMeta)
Expand Down

0 comments on commit 7cc82df

Please sign in to comment.