Skip to content

Commit

Permalink
Adding validation to ensure state checks are only defined for config …
Browse files Browse the repository at this point in the history
…(apply) tests (#266)
  • Loading branch information
bendbennett committed Jan 15, 2024
1 parent f67b3e7 commit eed2a12
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
8 changes: 7 additions & 1 deletion helper/resource/teststep_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// testStepValidateRequest contains data for the (TestStep).validate() method.
type testStepValidateRequest struct {
// StepConfiguration contains the TestStep configuration derived from
// TestStep.Config or TestStep.ConfigDirectory.
// TestStep.Config, TestStep.ConfigDirectory, or TestStep.ConfigFile.
StepConfiguration teststep.Config

// StepNumber is the index of the TestStep in the TestCase.Steps.
Expand Down Expand Up @@ -235,5 +235,11 @@ func (s TestStep) validate(ctx context.Context, req testStepValidateRequest) err
return err
}

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

return nil
}
10 changes: 10 additions & 0 deletions helper/resource/teststep_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ func TestTestStepValidate(t *testing.T) {
testStepValidateRequest: testStepValidateRequest{TestCaseHasProviders: true},
expectedError: errors.New("TestStep ConfigPlanChecks.PostApplyPostRefresh must only be specified with Config"),
},
"configstatechecks-not-config-mode": {
testStep: TestStep{
ConfigStateChecks: ConfigStateChecks{
&stateCheckSpy{},
},
RefreshState: true,
},
testStepValidateRequest: testStepValidateRequest{TestCaseHasProviders: true},
expectedError: errors.New("TestStep ConfigStateChecks must only be specified with Config"),
},
"refreshplanchecks-postrefresh-not-refresh-mode": {
testStep: TestStep{
RefreshPlanChecks: RefreshPlanChecks{
Expand Down

0 comments on commit eed2a12

Please sign in to comment.