Merge pull request #15957 from hashicorp/helper-test-step-skipfunc

helper/resource: Add TestStep.SkipFunc
This commit is contained in:
Radek Simko 2017-08-30 21:09:31 +02:00 committed by GitHub
commit 6bcc58ceba
2 changed files with 56 additions and 1 deletions

View File

@ -308,6 +308,10 @@ type TestStep struct {
// are tested alongside real resources
PreventPostDestroyRefresh bool
// SkipFunc is called before applying config, but after PreConfig
// This is useful for defining test steps with platform-dependent checks
SkipFunc func() (bool, error)
//---------------------------------------------------------------
// ImportState testing
//---------------------------------------------------------------
@ -398,7 +402,18 @@ func Test(t TestT, c TestCase) {
errored := false
for i, step := range c.Steps {
var err error
log.Printf("[WARN] Test: Executing step %d", i)
log.Printf("[DEBUG] Test: Executing step %d", i)
if step.SkipFunc != nil {
skip, err := step.SkipFunc()
if err != nil {
t.Fatal(err)
}
if skip {
log.Printf("[WARN] Skipping step %d", i)
continue
}
}
if step.Config == "" && !step.ImportState {
err = fmt.Errorf(

View File

@ -389,6 +389,46 @@ func TestTest_preCheck(t *testing.T) {
}
}
func TestTest_skipFunc(t *testing.T) {
preCheckCalled := false
skipped := false
mp := testProvider()
mp.ApplyReturn = &terraform.InstanceState{
ID: "foo",
}
checkStepFn := func(*terraform.State) error {
return fmt.Errorf("error")
}
mt := new(mockT)
Test(mt, TestCase{
Providers: map[string]terraform.ResourceProvider{
"test": mp,
},
PreCheck: func() { preCheckCalled = true },
Steps: []TestStep{
{
Config: testConfigStr,
Check: checkStepFn,
SkipFunc: func() (bool, error) { skipped = true; return true, nil },
},
},
})
if mt.failed() {
t.Fatal("Expected check to be skipped")
}
if !preCheckCalled {
t.Fatal("precheck should be called")
}
if !skipped {
t.Fatal("SkipFunc should be called")
}
}
func TestTest_stepError(t *testing.T) {
mp := testProvider()
mp.ApplyReturn = &terraform.InstanceState{