helper/resource: Add TestStep.SkipFunc

This commit is contained in:
Radek Simko 2017-08-30 08:59:19 +02:00
parent 61971f2dde
commit db97555e3e
No known key found for this signature in database
GPG Key ID: 6823F3DCCE01BB19
2 changed files with 56 additions and 1 deletions

View File

@ -308,6 +308,10 @@ type TestStep struct {
// are tested alongside real resources // are tested alongside real resources
PreventPostDestroyRefresh bool 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 // ImportState testing
//--------------------------------------------------------------- //---------------------------------------------------------------
@ -398,7 +402,18 @@ func Test(t TestT, c TestCase) {
errored := false errored := false
for i, step := range c.Steps { for i, step := range c.Steps {
var err error 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 { if step.Config == "" && !step.ImportState {
err = fmt.Errorf( 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) { func TestTest_stepError(t *testing.T) {
mp := testProvider() mp := testProvider()
mp.ApplyReturn = &terraform.InstanceState{ mp.ApplyReturn = &terraform.InstanceState{