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

Expose internal SDKv2 Diff cross-test interface #2720

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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
39 changes: 39 additions & 0 deletions pkg/internal/tests/cross-tests/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package crosstests

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/zclconf/go-cty/cty"

crosstestsimpl "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests/impl"
)

type diffOpts struct {
deleteBeforeReplace bool
}

// An option that can be used to customize [Diff].
type DiffOption func(*diffOpts)

// DiffDeleteBeforeReplace specifies whether to delete the resource before replacing it.
func DiffDeleteBeforeReplace(deleteBeforeReplace bool) DiffOption {
return func(o *diffOpts) { o.deleteBeforeReplace = deleteBeforeReplace }
}

func Diff(
t T, resource *schema.Resource, config1, config2 map[string]cty.Value, opts ...DiffOption,
Copy link
Member

Choose a reason for hiding this comment

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

Create and Configure both take a map[string]*schema.Schema instead of a *schema.Resource. This is to intentionally constrain the inputs to Diff to only options that can be exercised in Diff based tests.

We will probably want an option for schema.Resource.CustomizeDiff. We don't want users to be able to set schema.Resource.Importer.

Copy link
Contributor Author

@VenelinMartinov VenelinMartinov Dec 12, 2024

Choose a reason for hiding this comment

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

Yeah, this unfortunately limits the expressiveness of the test too - for example custom Create and Update methods might be required in a Diff test for implementing Computed properties. Instead of re-implementing the API with options we should have a simple check for the banned inputs. In the case of Diff tests, I don't think there are any schema inputs which should be banned - this only applies to, for example, custom Create in Create cross-tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see what Resource.Importer has to do with Diff test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see a good reason why we would choose to add a new interface there for specifying schemas instead of reusing the default TF SDK one - it works well enough and allows us to copy-paste provider examples and avoids unnecessary complexity.

I can add refactoring the other cross-test interfaces to my TODO list

Copy link
Member

Choose a reason for hiding this comment

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

I'm on the side of being more permissive here in the internal test code.

Copy link
Member

Choose a reason for hiding this comment

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

I don't see a good reason why we would choose to add a new interface there for specifying schemas instead of reusing the default TF SDK one

Because it keeps the tests intentional. We only add properties that can actually be tested.

it works well enough and allows us to copy-paste provider examples and avoids unnecessary complexity.

I'd rather we didn't copy-paste whole resources to test only a small part of them.

I don't see what Resource.Importer has to do with Diff test

Nothing at all, but I can set .Importer on a *schema.Resource and pass that to crosstests.Diff with this design. I'd rather that was a compiler error. I use that example because I remember someone setting .Importer for a create and asserting that the .Importer didn't panic.

I won't block the PR on this, but IMO passing *schema.Resource directly makes the tests easier to write but harder to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

passing *schema.Resource directly makes the tests easier to write but harder to read.

I don't agree about readability either. I think the benefit of options is that it is harder to get the Schema wrong. Given that there's very few people who actually write these type of tests, I think it's worth keeping it simple for now and evolving the interface once we find a need to.

Let's revisit once we have a concrete problem we are solving with the extra interface.

) crosstestsimpl.DiffResult {
o := &diffOpts{}
for _, opt := range opts {
opt(o)
}

config1Cty := cty.ObjectVal(config1)
config2Cty := cty.ObjectVal(config2)

return runDiffCheck(t, diffTestCase{
Resource: resource,
Config1: config1Cty,
Config2: config2Cty,
DeleteBeforeReplace: o.deleteBeforeReplace,
})
}
Loading