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

helper/resource: Prevent Inconsistent dependency lock file errors when using ExternalProviders outside the hashicorp namespace #1057

Merged
merged 2 commits into from Sep 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1057.txt
@@ -0,0 +1,3 @@
```release-note:bug
helper/resource: Prevented `Inconsistent dependency lock file` errors when using `ExternalProviders` outside the `hashicorp` namespace
```
149 changes: 149 additions & 0 deletions helper/resource/testcase_providers_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -19,6 +20,81 @@ func TestTestCaseProviderConfig(t *testing.T) {
testCase TestCase
expected string
}{
"externalproviders-and-protov5providerfactories": {
testCase: TestCase{
ExternalProviders: map[string]ExternalProvider{
"externaltest": {
Source: "registry.terraform.io/hashicorp/externaltest",
VersionConstraint: "1.2.3",
},
},
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"localtest": nil,
},
},
expected: `
terraform {
required_providers {
externaltest = {
source = "registry.terraform.io/hashicorp/externaltest"
version = "1.2.3"
}
}
}

provider "externaltest" {}
`,
},
"externalproviders-and-protov6providerfactories": {
testCase: TestCase{
ExternalProviders: map[string]ExternalProvider{
"externaltest": {
Source: "registry.terraform.io/hashicorp/externaltest",
VersionConstraint: "1.2.3",
},
},
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"localtest": nil,
},
},
expected: `
terraform {
required_providers {
externaltest = {
source = "registry.terraform.io/hashicorp/externaltest"
version = "1.2.3"
}
}
}

provider "externaltest" {}
`,
},
"externalproviders-and-providerfactories": {
testCase: TestCase{
ExternalProviders: map[string]ExternalProvider{
"externaltest": {
Source: "registry.terraform.io/hashicorp/externaltest",
VersionConstraint: "1.2.3",
},
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"localtest": nil,
},
},
expected: `
terraform {
required_providers {
externaltest = {
source = "registry.terraform.io/hashicorp/externaltest"
version = "1.2.3"
}
}
}

provider "externaltest" {}
`,
},
"externalproviders-missing-source-and-versionconstraint": {
testCase: TestCase{
ExternalProviders: map[string]ExternalProvider{
Expand Down Expand Up @@ -155,6 +231,79 @@ func TestTest_TestCase_ExternalProviders(t *testing.T) {
})
}

func TestTest_TestCase_ExternalProviders_NonHashiCorpNamespace(t *testing.T) {
t.Parallel()

Test(t, TestCase{
ExternalProviders: map[string]ExternalProvider{
// This can be set to any provider outside the hashicorp namespace.
// bflad/scaffoldingtest happens to be a published version of
// terraform-provider-scaffolding-framework.
"scaffoldingtest": {
Source: "registry.terraform.io/bflad/scaffoldingtest",
VersionConstraint: "0.1.0",
},
},
Steps: []TestStep{
{
Config: `resource "scaffoldingtest_example" "test" {}`,
},
},
})
}

func TestTest_TestCase_ExternalProvidersAndProviderFactories_NonHashiCorpNamespace(t *testing.T) {
t.Parallel()

Test(t, TestCase{
ExternalProviders: map[string]ExternalProvider{
// This can be set to any provider outside the hashicorp namespace.
// bflad/scaffoldingtest happens to be a published version of
// terraform-provider-scaffolding-framework.
"scaffoldingtest": {
Source: "registry.terraform.io/bflad/scaffoldingtest",
VersionConstraint: "0.1.0",
},
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"null": func() (*schema.Provider, error) { //nolint:unparam // required signature
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"null_resource": {
CreateContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("test")
return nil
},
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
ReadContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
Schema: map[string]*schema.Schema{
"triggers": {
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Optional: true,
Type: schema.TypeMap,
},
},
},
},
}, nil
},
},
Steps: []TestStep{
{
Config: `
resource "null_resource" "test" {}
resource "scaffoldingtest_example" "test" {}
`,
},
},
})
}

func TestTest_TestCase_ExternalProviders_Error(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion helper/resource/testing_new.go
Expand Up @@ -278,7 +278,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

appliedCfg = step.Config
appliedCfg = step.mergedConfig(ctx, c)

logging.HelperResourceDebug(ctx, "Finished TestStep")

Expand Down
2 changes: 1 addition & 1 deletion helper/resource/testing_new_config.go
Expand Up @@ -16,7 +16,7 @@ import (
func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, providers *providerFactories) error {
t.Helper()

err := wd.SetConfig(ctx, step.Config)
err := wd.SetConfig(ctx, step.mergedConfig(ctx, c))
if err != nil {
return fmt.Errorf("Error setting config: %w", err)
}
Expand Down
25 changes: 25 additions & 0 deletions helper/resource/teststep_providers.go
Expand Up @@ -6,6 +6,31 @@ import (
"strings"
)

// mergedConfig prepends any necessary terraform configuration blocks to the
// TestStep Config.
//
// If there are ExternalProviders configurations in either the TestCase or
// TestStep, the terraform configuration block should be included with the
// step configuration to prevent errors with providers outside the
// registry.terraform.io hostname or outside the hashicorp namespace.
func (s TestStep) mergedConfig(ctx context.Context, testCase TestCase) string {
var config strings.Builder

// Prevent issues with existing configurations containing the terraform
// configuration block.
if !strings.Contains(s.Config, "terraform {") {
if testCase.hasProviders(ctx) {
config.WriteString(testCase.providerConfig(ctx))
} else {
config.WriteString(s.providerConfig(ctx))
}
}

config.WriteString(s.Config)

return config.String()
}

// providerConfig takes the list of providers in a TestStep and returns a
// config with only empty provider blocks. This is useful for Import, where no
// config is provided, but the providers must be defined.
Expand Down