diff --git a/helper/resource/testing.go b/helper/resource/testing.go index d7de1a030..205547eff 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -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( diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index d1c967bbd..8712c8794 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -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{