Skip to content

Commit

Permalink
Reinstating TestStep.mergedConfig() method as terraform and provider …
Browse files Browse the repository at this point in the history
…blocks are only written when using TestStep.Config. The expectation is that when using TestStep.ConfigDirectoy, the terraform files within the configuration directory will specify the terraform and/or provider blocks as necessary (#150)
  • Loading branch information
bendbennett committed Jul 20, 2023
1 parent 0966142 commit ce6b5a3
Show file tree
Hide file tree
Showing 6 changed files with 731 additions and 538 deletions.
23 changes: 12 additions & 11 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,15 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

var testCaseProviderConfig string
var testStepProviderConfig string
hasTerraformBlock, err := cfg.HasTerraformBlock(ctx)

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

hasProviderBlock, err := cfg.HasProviderBlock(ctx)

Expand All @@ -408,18 +415,12 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
t.Fatalf("Error determining whether configuration contains provider block: %s", err)
}

if c.hasProviders(ctx) {
testCaseProviderConfig = c.providerConfig(ctx, hasProviderBlock)
} else {
testStepProviderConfig = step.providerConfig(ctx, hasProviderBlock)
}
mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)

appliedCfg, err = teststep.Configuration(
teststep.ConfigurationRequest{
Directory: step.ConfigDirectory,
Raw: step.Config,
TestCaseProviderConfig: testCaseProviderConfig,
TestStepProviderConfig: testStepProviderConfig,
Directory: step.ConfigDirectory,
Raw: mergedConfig,
},
)

Expand Down
23 changes: 12 additions & 11 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return fmt.Errorf("Error creating config: %w", err)
}

var testCaseProviderConfig string
var testStepProviderConfig string
hasTerraformBlock, err := cfg.HasTerraformBlock(ctx)

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

hasProviderBlock, err := cfg.HasProviderBlock(ctx)

Expand All @@ -45,18 +52,12 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
t.Fatalf("Error determining whether configuration contains provider block: %s", err)
}

if c.hasProviders(ctx) {
testCaseProviderConfig = c.providerConfig(ctx, hasProviderBlock)
} else {
testStepProviderConfig = step.providerConfig(ctx, hasProviderBlock)
}
mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)

config, err := teststep.Configuration(
teststep.ConfigurationRequest{
Directory: step.ConfigDirectory,
Raw: step.Config,
TestCaseProviderConfig: testCaseProviderConfig,
TestStepProviderConfig: testStepProviderConfig,
Directory: step.ConfigDirectory,
Raw: mergedConfig,
},
)

Expand Down
34 changes: 34 additions & 0 deletions helper/resource/teststep_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ 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.
// This is only necessary when using TestStep.Config.
//
// When TestStep.ConfigDirectory is used, the expectation is that the
// Terraform configuration files will specify a terraform configuration
// block and/or provider blocks as necessary.
func (s TestStep) mergedConfig(ctx context.Context, testCase TestCase, configHasTerraformBlock, configHasProviderBlock bool) string {
var config strings.Builder

// Prevent issues with existing configurations containing the terraform
// configuration block.
if configHasTerraformBlock {
config.WriteString(s.Config)

return config.String()
}

if testCase.hasProviders(ctx) {
config.WriteString(testCase.providerConfig(ctx, configHasProviderBlock))
} else {
config.WriteString(s.providerConfig(ctx, configHasProviderBlock))
}

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
Loading

0 comments on commit ce6b5a3

Please sign in to comment.