-
Notifications
You must be signed in to change notification settings - Fork 43
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 crosstests.Create
#2501
Merged
Merged
Expose crosstests.Create
#2501
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2016-2024, Pulumi Corporation. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
|
||
package crosstests | ||
|
||
import ( | ||
"context" | ||
|
||
"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" | ||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/zclconf/go-cty/cty" | ||
|
||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tests/pulcheck" | ||
"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge/info" | ||
) | ||
|
||
// Create validates that a Terraform provider witnesses the same input when: | ||
// - invoked directly with HCL on tfConfig | ||
// - bridged and invoked via Pulumi YAML on puConfig | ||
// | ||
// Create only applies to resources defined with github.com/hashicorp/terraform-plugin-sdk/v2. For cross-tests | ||
// on Plugin Framework based resources, see | ||
// github.com/pulumi/pulumi-terraform-bridge/pkg/pf/tests/internal/cross-tests. | ||
// | ||
// Create *does not* verify the outputs of the resource, only that the provider witnessed the same inputs. | ||
func Create( | ||
t T, resource map[string]*schema.Schema, tfConfig cty.Value, puConfig resource.PropertyMap, | ||
options ...CreateOption, | ||
) { | ||
var opts createOpts | ||
for _, f := range options { | ||
f(&opts) | ||
} | ||
|
||
type result struct { | ||
data *schema.ResourceData | ||
meta any | ||
wasSet bool | ||
} | ||
var tfResult, puResult result | ||
|
||
makeResource := func(writeTo *result) *schema.Resource { | ||
return &schema.Resource{ | ||
Schema: resource, | ||
CreateContext: func(_ context.Context, rd *schema.ResourceData, meta any) diag.Diagnostics { | ||
*writeTo = result{rd, meta, true} | ||
rd.SetId("someid") // CreateContext must pick an ID | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
tfwd := t.TempDir() | ||
tfd := newTFResDriver(t, tfwd, defProviderShortName, defRtype, makeResource(&tfResult)) | ||
tfd.writePlanApply(t, resource, defRtype, "example", tfConfig, lifecycleArgs{}) | ||
|
||
require.True(t, tfResult.wasSet, "terraform test result was not set") | ||
|
||
resMap := map[string]*schema.Resource{defRtype: makeResource(&puResult)} | ||
tfp := &schema.Provider{ResourcesMap: resMap} | ||
bridgedProvider := pulcheck.BridgedProvider( | ||
t, defProviderShortName, tfp, | ||
pulcheck.WithResourceInfo(map[string]*info.Resource{defRtype: opts.resourceInfo}), | ||
) | ||
pd := &pulumiDriver{ | ||
name: defProviderShortName, | ||
pulumiResourceToken: defRtoken, | ||
tfResourceName: defRtype, | ||
} | ||
yamlProgram := pd.generateYAML(t, puConfig) | ||
|
||
pt := pulcheck.PulCheck(t, bridgedProvider, string(yamlProgram)) | ||
|
||
pt.Up(t) | ||
|
||
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") | ||
|
||
// We are unable to assert that both providers were configured with the exact same | ||
// data. Type information doesn't line up in the simple case. This just doesn't work: | ||
// | ||
// assert.Equal(t, tfResult.data, puResult.data) | ||
// | ||
// We make due by comparing raw data. | ||
assertCtyValEqual(t, "RawConfig", tfResult.data.GetRawConfig(), puResult.data.GetRawConfig()) | ||
assertCtyValEqual(t, "RawPlan", tfResult.data.GetRawPlan(), puResult.data.GetRawPlan()) | ||
assertCtyValEqual(t, "RawState", tfResult.data.GetRawState(), puResult.data.GetRawState()) | ||
} | ||
|
||
type createOpts struct { | ||
resourceInfo *info.Resource | ||
} | ||
|
||
// An option that can be used to customize [Create]. | ||
type CreateOption func(*createOpts) | ||
|
||
// Specify an [info.Resource] to apply to the resource under test. | ||
func CreateResourceInfo(info info.Resource) CreateOption { | ||
contract.Assertf(info.Tok == "", "cannot set info.Tok, it will not be respected") | ||
return func(o *createOpts) { o.resourceInfo = &info } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For internal code I wonder sometimes if plain struct options are just easier to work with and discover than functional options. That's a stylistic nit though. It's all good.