diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 8852077041..5d46e5dc8a 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -14,6 +14,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/hashicorp/go-getter" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/helper/logging" "github.com/hashicorp/terraform/terraform" @@ -520,6 +521,28 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { } } +// ComposeAggregateTestCheckFunc lets you compose multiple TestCheckFuncs into +// a single TestCheckFunc. +// +// As a user testing their provider, this lets you decompose your checks +// into smaller pieces more easily. +// +// Unlike ComposeTestCheckFunc, ComposeAggergateTestCheckFunc runs _all_ of the +// TestCheckFuncs and aggregates failures. +func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { + return func(s *terraform.State) error { + var result *multierror.Error + + for i, f := range fs { + if err := f(s); err != nil { + result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)) + } + } + + return result.ErrorOrNil() + } +} + // TestCheckResourceAttrSet is a TestCheckFunc which ensures a value // exists in state for the given name/key combination. It is useful when // testing that computed values were set, when it is not possible to diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index edb11b7b60..d2e05c0c52 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -1,11 +1,14 @@ package resource import ( + "errors" "fmt" "os" + "strings" "sync/atomic" "testing" + "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/terraform" ) @@ -352,6 +355,30 @@ func TestTest_stepError(t *testing.T) { } } +func TestComposeAggregateTestCheckFunc(t *testing.T) { + check1 := func(s *terraform.State) error { + return errors.New("Error 1") + } + + check2 := func(s *terraform.State) error { + return errors.New("Error 2") + } + + f := ComposeAggregateTestCheckFunc(check1, check2) + err := f(nil) + if err == nil { + t.Fatalf("Expected errors") + } + + multi := err.(*multierror.Error) + if !strings.Contains(multi.Errors[0].Error(), "Error 1") { + t.Fatalf("Expected Error 1, Got %s", multi.Errors[0]) + } + if !strings.Contains(multi.Errors[1].Error(), "Error 2") { + t.Fatalf("Expected Error 2, Got %s", multi.Errors[1]) + } +} + func TestComposeTestCheckFunc(t *testing.T) { cases := []struct { F []TestCheckFunc