Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Full fidelity SDKv2 crosstest.Create equality #2840

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has cmp compare all un-exported fields.

cmp.Comparer(func(x, y schema.SchemaStateFunc) bool {
return reflect.ValueOf(x).Pointer() == reflect.ValueOf(y).Pointer()
}),
Comment on lines +130 to +132
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implements pointer equality for schema.SchemaStateFunc.

}
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
}
}
Comment on lines +641 to +652
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous compare function wasn't catching this, but the new comparison does. This now more accurately imitates TF.


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
Loading