mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
Merge pull request #8233 from hashicorp/f-test-compose-aggregate
testing: Add ComposeAggregateTestFunc
This commit is contained in:
commit
e6aa50175c
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user