From b551981cc7333e9627dd93654fdb31214b5dc475 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Tue, 16 Aug 2016 19:50:04 +0100 Subject: [PATCH] testing: Add ComposeAggregateTestFunc This commit adds a function which composes a series of TestFuncs, but will run all tests before returning an error, unlike ComposeTestFunc. This is useful when verifying contents of state in acceptance tests and it is desirable to see all the failing cases in one run for slow resources. --- helper/resource/testing.go | 23 +++++++++++++++++++++++ helper/resource/testing_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 885207704..5d46e5dc8 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 edb11b7b6..d2e05c0c5 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