Skip to content

Commit

Permalink
Adding configurationDirectory and configurationString types (#150)
Browse files Browse the repository at this point in the history
  * Configuration() now returns the Config interface to accommodate the different types that could be returned (e.g., configurationDirectory, configurationString)
  • Loading branch information
bendbennett committed Jul 26, 2023
1 parent 647801c commit 2155db2
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 336 deletions.
75 changes: 44 additions & 31 deletions helper/resource/testing_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

if cfg.HasConfiguration() && !step.Destroy && len(step.Taint) > 0 {
if cfg != nil && !step.Destroy && len(step.Taint) > 0 {
err := testStepTaint(ctx, step, wd)

if err != nil {
Expand Down Expand Up @@ -229,14 +229,18 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
protov6: protov6ProviderFactories(c.ProtoV6ProviderFactories).merge(step.ProtoV6ProviderFactories),
}

hasProviderBlock, err := cfg.HasProviderBlock(ctx)
var hasProviderBlock bool

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

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

var testStepConfig teststep.Config
Expand Down Expand Up @@ -373,7 +377,7 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
continue
}

if cfg.HasConfiguration() {
if cfg != nil {
logging.HelperResourceTrace(ctx, "TestStep is Config mode")

err := testStepNewConfig(ctx, t, c, wd, step, providers, stepIndex)
Expand Down Expand Up @@ -410,24 +414,29 @@ func runNewTest(ctx context.Context, t testing.T, c TestCase, helper *plugintest
}
}

hasTerraformBlock, err := cfg.HasTerraformBlock(ctx)
var hasTerraformBlock bool
var hasProviderBlock bool

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)
}
if cfg != nil {
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)
hasProviderBlock, err = cfg.HasProviderBlock(ctx)

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

mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)
Expand Down Expand Up @@ -527,14 +536,18 @@ func testIDRefresh(ctx context.Context, t testing.T, c TestCase, wd *plugintest.
t.Fatalf("Error creating provider configuration for import test config: %s", err)
}

hasProviderBlock, err := cfg.HasProviderBlock(ctx)
var hasProviderBlock bool

if err != nil {
logging.HelperResourceError(ctx,
"Error determining whether configuration contains provider block for import test config",
map[string]interface{}{logging.KeyError: err},
)
t.Fatalf("Error determining whether configuration contains provider block for import test config: %s", err)
if cfg != nil {
hasProviderBlock, err = cfg.HasProviderBlock(ctx)

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

// Return value from c.ProviderConfig() is assigned to Raw as this was previously being
Expand Down
35 changes: 20 additions & 15 deletions helper/resource/testing_new_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,29 @@ func testStepNewConfig(ctx context.Context, t testing.T, c TestCase, wd *plugint
return fmt.Errorf("Error creating config: %w", err)
}

hasTerraformBlock, err := cfg.HasTerraformBlock(ctx)
var hasTerraformBlock bool
var hasProviderBlock bool

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)
}
if cfg != nil {
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)
hasProviderBlock, err = cfg.HasProviderBlock(ctx)

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

mergedConfig := step.mergedConfig(ctx, c, hasTerraformBlock, hasProviderBlock)
Expand Down
6 changes: 2 additions & 4 deletions helper/resource/testing_new_import_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest.Helper, wd *plugintest.WorkingDir, step TestStep, cfg teststep.Config, providers *providerFactories, stepIndex int) error {
t.Helper()

var testStepConfig teststep.Config

testStepConfig, err := teststep.Configuration(
teststep.ConfigurationRequest{
Directory: teststep.Pointer(
Expand Down Expand Up @@ -100,11 +98,11 @@ func testStepNewImportState(ctx context.Context, t testing.T, helper *plugintest
logging.HelperResourceTrace(ctx, fmt.Sprintf("Using import identifier: %s", importId))

// Create working directory for import tests
if !testStepConfig.HasConfiguration() {
if testStepConfig == nil {
logging.HelperResourceTrace(ctx, "Using prior TestStep Config for import")

testStepConfig = cfg
if !testStepConfig.HasConfiguration() {
if testStepConfig == nil {
t.Fatal("Cannot import state with no specified config")
}
}
Expand Down
36 changes: 22 additions & 14 deletions helper/resource/teststep_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ func (s TestStep) hasProviders(ctx context.Context, stepIndex int, testName stri
return false, err
}

cfgHasProviders, err := cfg.HasProviderBlock(ctx)
var cfgHasProviders bool

if err != nil {
return false, err
if cfg != nil {
cfgHasProviders, err = cfg.HasProviderBlock(ctx)

if err != nil {
return false, err
}
}

if cfgHasProviders {
Expand Down Expand Up @@ -115,13 +119,13 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err

logging.HelperResourceTrace(ctx, "Validating TestStep")

if !req.StepConfiguration.HasConfiguration() && !s.ImportState && !s.RefreshState {
if req.StepConfiguration == nil && !s.ImportState && !s.RefreshState {
err := fmt.Errorf("TestStep missing Config or ConfigDirectory or ImportState or RefreshState")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
}

if req.StepConfiguration.HasConfiguration() && s.RefreshState {
if req.StepConfiguration != nil && s.RefreshState {
err := fmt.Errorf("TestStep cannot have Config and RefreshState")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
Expand Down Expand Up @@ -153,13 +157,13 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err
}
}

if req.TestCaseHasExternalProviders && req.StepConfiguration.HasConfigurationFiles() {
if req.TestCaseHasExternalProviders && req.StepConfiguration != nil && req.StepConfiguration.HasConfigurationFiles() {
err := fmt.Errorf("Providers must only be specified within the terraform configuration files when using TestStep.ConfigDirectory")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
}

if s.hasExternalProviders() && req.StepConfiguration.HasConfigurationFiles() {
if s.hasExternalProviders() && req.StepConfiguration != nil && req.StepConfiguration.HasConfigurationFiles() {
err := fmt.Errorf("Providers must only be specified within the terraform configuration files when using TestStep.ConfigDirectory")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
Expand All @@ -179,11 +183,15 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err
return err
}

cfgHasProviderBlock, err := req.StepConfiguration.HasProviderBlock(ctx)
var cfgHasProviderBlock bool

if err != nil {
logging.HelperResourceError(ctx, "TestStep error checking for if configuration has provider block", map[string]interface{}{logging.KeyError: err})
return err
if req.StepConfiguration != nil {
cfgHasProviderBlock, err = req.StepConfiguration.HasProviderBlock(ctx)

if err != nil {
logging.HelperResourceError(ctx, "TestStep error checking for if configuration has provider block", map[string]interface{}{logging.KeyError: err})
return err
}
}

if !req.TestCaseHasProviders && !hasProviders && !cfgHasProviderBlock {
Expand All @@ -201,7 +209,7 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err
}

if len(s.ConfigPlanChecks.PreApply) > 0 {
if !req.StepConfiguration.HasConfiguration() {
if req.StepConfiguration == nil {
err := fmt.Errorf("TestStep ConfigPlanChecks.PreApply must only be specified with Config")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
Expand All @@ -214,13 +222,13 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err
}
}

if len(s.ConfigPlanChecks.PostApplyPreRefresh) > 0 && !req.StepConfiguration.HasConfiguration() {
if len(s.ConfigPlanChecks.PostApplyPreRefresh) > 0 && req.StepConfiguration == nil {
err := fmt.Errorf("TestStep ConfigPlanChecks.PostApplyPreRefresh must only be specified with Config")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
}

if len(s.ConfigPlanChecks.PostApplyPostRefresh) > 0 && !req.StepConfiguration.HasConfiguration() {
if len(s.ConfigPlanChecks.PostApplyPostRefresh) > 0 && req.StepConfiguration == nil {
err := fmt.Errorf("TestStep ConfigPlanChecks.PostApplyPostRefresh must only be specified with Config")
logging.HelperResourceError(ctx, "TestStep validation error", map[string]interface{}{logging.KeyError: err})
return err
Expand Down
8 changes: 5 additions & 3 deletions internal/plugintest/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,12 @@ func (wd *WorkingDir) SetConfig(ctx context.Context, cfg teststep.Config, vars c
wd.configFilename = outFilename

// Write configuration
err = cfg.Write(ctx, wd.baseDir)
if cfg != nil {
err = cfg.Write(ctx, wd.baseDir)

if err != nil {
return err
if err != nil {
return err
}
}

//Write configuration variables
Expand Down
Loading

0 comments on commit 2155db2

Please sign in to comment.