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.
This commit is contained in:
parent
782b24833a
commit
b551981cc7
|
@ -14,6 +14,7 @@ import (
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/hashicorp/go-getter"
|
"github.com/hashicorp/go-getter"
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/config/module"
|
"github.com/hashicorp/terraform/config/module"
|
||||||
"github.com/hashicorp/terraform/helper/logging"
|
"github.com/hashicorp/terraform/helper/logging"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"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
|
// TestCheckResourceAttrSet is a TestCheckFunc which ensures a value
|
||||||
// exists in state for the given name/key combination. It is useful when
|
// 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
|
// testing that computed values were set, when it is not possible to
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
package resource
|
package resource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"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) {
|
func TestComposeTestCheckFunc(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
F []TestCheckFunc
|
F []TestCheckFunc
|
||||||
|
|
Loading…
Reference in New Issue