Skip to content

Commit

Permalink
Add terraform block to config when not already present (#216)
Browse files Browse the repository at this point in the history
* Prefix configuration with empty terraform block if block is missing from config (#215)

* Adding unit tests for AddTerraformBlock() (#215)

* Revert "Adding unit tests for AddTerraformBlock() (#215)"

This reverts commit bd351c0.

* Revert "Prefix configuration with empty terraform block if block is missing from config (#215)"

This reverts commit d417058.

* Broken implementation (#215)

* Removing unneeded addition of provider blocks (#215)

* Modifying TestStep.mergedConfig() to add terraform blocks (#215)

* Linting (#215)

* Only add terraform required provider blocks for TF >= 1.6.* (#215)

* Adding change log entry (#215)

* Fixing nil pointers (#215)

* Remove Terraform version check when adding required providers (#215)

* Remove Terraform version check when adding required providers (#215)

* Revert "Remove Terraform version check when adding required providers (#215)"

This reverts commit b530b61.

* Revert "Remove Terraform version check when adding required providers (#215)"

This reverts commit 711d592.

* Remove addition of required providers that do not specify a source (#215)

* Set minimum Terraform version to v1.0.0 for adding required providers to terraform block (#215)

* Update changelog (#215)
  • Loading branch information
bendbennett authored Nov 30, 2023
1 parent 880e76a commit 222d08e
Show file tree
Hide file tree
Showing 17 changed files with 1,460 additions and 62 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/ENHANCEMENTS-20231130-092645.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: ENHANCEMENTS
body: 'helper/resource: Automatically add `required_providers` configuration to `TestStep.Config`
Terraform language configuration when using Terraform >= 1.0.*'
time: 2023-11-30T09:26:45.606859Z
custom:
Issue: "216"
15 changes: 7 additions & 8 deletions helper/resource/testcase_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-testing/internal/testing/testsdk/providerserver"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

Expand Down Expand Up @@ -223,7 +225,8 @@ provider "test" {}
"test": {},
},
},
expected: `provider "test" {}`,
expected: `
provider "test" {}`,
},
}

Expand Down Expand Up @@ -365,9 +368,7 @@ func TestTest_TestCase_ProtoV5ProviderFactories(t *testing.T) {

Test(&mockT{}, TestCase{
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
"test": func() (tfprotov5.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
},
"test": providerserver.NewProtov5ProviderServer(testprovider.Protov5Provider{}),
},
Steps: []TestStep{
{
Expand Down Expand Up @@ -401,9 +402,7 @@ func TestTest_TestCase_ProtoV6ProviderFactories(t *testing.T) {

Test(&mockT{}, TestCase{
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
"test": func() (tfprotov6.ProviderServer, error) { //nolint:unparam // required signature
return nil, nil
},
"test": providerserver.NewProviderServer(testprovider.Provider{}),
},
Steps: []TestStep{
{
Expand Down Expand Up @@ -438,7 +437,7 @@ func TestTest_TestCase_ProviderFactories(t *testing.T) {
Test(&mockT{}, TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
return nil, nil
return &schema.Provider{}, nil
},
},
Steps: []TestStep{
Expand Down
34 changes: 29 additions & 5 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,23 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest

var testStepConfig teststep.Config

rawCfg, err := step.providerConfig(ctx, hasProviderBlock, helper.TerraformVersion())

if err != nil {
logging.HelperResourceError(ctx,
"TestStep error generating provider configuration",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("TestStep %d/%d error generating provider configuration: %s", stepNumber, len(c.Steps), err)
}

// Return value from step.providerConfig() is assigned to Raw as this was previously being
// passed to wd.SetConfig() directly when the second argument to wd.SetConfig() accepted a
// configuration string.
confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: step.providerConfig(ctx, hasProviderBlock),
Raw: rawCfg,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
Expand Down Expand Up @@ -354,7 +364,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
if cfg != nil {
logging.HelperResourceTrace(ctx, "TestStep is Config mode")

err := testStepNewConfig(ctx, t, c, wd, step, providers, stepIndex)
err := testStepNewConfig(ctx, t, c, wd, step, providers, stepIndex, helper)
if step.ExpectError != nil {
logging.HelperResourceDebug(ctx, "Checking TestStep ExpectError")

Expand Down Expand Up @@ -413,7 +423,15 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)
mergedConfig, err := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())

if err != nil {
logging.HelperResourceError(ctx,
"Error generating merged configuration",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Error generating merged configuration: %s", err)
}

confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
Expand Down Expand Up @@ -469,7 +487,7 @@ func planIsEmpty(plan *tfjson.Plan) bool {
return true
}

func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, r *terraform.ResourceState, providers *providerFactories, stepIndex int) error {
func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, r *terraform.ResourceState, providers *providerFactories, stepIndex int, helper *plugintest.Helper) error {
t.Helper()

// Build the state. The state is just the resource with an ID. There
Expand Down Expand Up @@ -521,11 +539,17 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
t.Fatalf("Error setting import test config: %s", err)
}

rawCfg, err := step.providerConfig(ctx, hasProviderBlock, helper.TerraformVersion())

if err != nil {
t.Fatalf("Error generating import provider config: %s", err)
}

defer func() {
confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
File: step.ConfigFile,
Raw: step.providerConfig(ctx, hasProviderBlock),
Raw: rawCfg,
TestStepConfigRequest: config.TestStepConfigRequest{
StepNumber: stepIndex + 1,
TestName: t.Name(),
Expand Down
16 changes: 12 additions & 4 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-testing/internal/plugintest"
)

func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, providers *providerFactories, stepIndex int) error {
func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugintest.WorkingDir, step TestStep, providers *providerFactories, stepIndex int, helper *plugintest.Helper) error {
t.Helper()

configRequest := teststep.PrepareConfigurationRequest{
Expand Down Expand Up @@ -62,7 +62,15 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
}
}

mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)
mergedConfig, err := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock, helper.TerraformVersion())

if err != nil {
logging.HelperResourceError(ctx,
"Error generating merged configuration",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Error generating merged configuration: %s", err)
}

confRequest := teststep.PrepareConfigurationRequest{
Directory: step.ConfigDirectory,
Expand All @@ -76,7 +84,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint

testStepConfig := teststep.Configuration(confRequest)

err := wd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
err = wd.SetConfig(ctx, testStepConfig, step.ConfigVariables)
if err != nil {
return fmt.Errorf("Error setting config: %w", err)
}
Expand Down Expand Up @@ -335,7 +343,7 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
// this fails. If refresh isn't read-only, then this will have
// caught a different bug.
if idRefreshCheck != nil {
if err := testIDRefresh(ctx, t, c, wd, step, idRefreshCheck, providers, stepIndex); err != nil {
if err := testIDRefresh(ctx, t, c, wd, step, idRefreshCheck, providers, stepIndex, helper); err != nil {
return fmt.Errorf(
"[ERROR] Test: ID-only test failed: %s", err)
}
Expand Down
5 changes: 2 additions & 3 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
"strings"
"testing"

testinginterface "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
testinginterface "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/terraform-plugin-testing/terraform"
)
Expand All @@ -35,7 +34,7 @@ func TestParallelTest(t *testing.T) {
IsUnitTest: true,
ProviderFactories: map[string]func() (*schema.Provider, error){
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
return nil, nil
return &schema.Provider{}, nil
},
},
Steps: []TestStep{
Expand Down
Loading

0 comments on commit 222d08e

Please sign in to comment.