helper/resource: can compose TestCheckFuncs

This commit is contained in:
Mitchell Hashimoto 2014-07-10 13:29:38 -07:00
parent 8229758806
commit 4a3d51f40e
2 changed files with 71 additions and 1 deletions

View File

@ -219,6 +219,23 @@ func testStep(
return state, err
}
// ComposeTestCheckFunc 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.
func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
return func(s *terraform.State) error {
for _, f := range fs {
if err := f(s); err != nil {
return err
}
}
return nil
}
}
// TestT is the interface used to handle the test lifecycle of a test.
//
// Users should just use a *testing.T object, which implements this.

View File

@ -110,7 +110,7 @@ func TestTest_preCheck(t *testing.T) {
mt := new(mockT)
Test(mt, TestCase{
PreCheck: func() { called = true },
PreCheck: func() { called = true },
})
if !called {
@ -159,6 +159,59 @@ func TestTest_stepError(t *testing.T) {
}
}
func TestComposeTestCheckFunc(t *testing.T) {
cases := []struct {
F []TestCheckFunc
Result string
}{
{
F: []TestCheckFunc{
func(*terraform.State) error { return nil },
},
Result: "",
},
{
F: []TestCheckFunc{
func(*terraform.State) error {
return fmt.Errorf("error")
},
func(*terraform.State) error { return nil },
},
Result: "error",
},
{
F: []TestCheckFunc{
func(*terraform.State) error { return nil },
func(*terraform.State) error {
return fmt.Errorf("error")
},
},
Result: "error",
},
{
F: []TestCheckFunc{
func(*terraform.State) error { return nil },
func(*terraform.State) error { return nil },
},
Result: "",
},
}
for i, tc := range cases {
f := ComposeTestCheckFunc(tc.F...)
err := f(nil)
if err == nil {
err = fmt.Errorf("")
}
if tc.Result != err.Error() {
t.Fatalf("Case %d bad: %s", i, err)
}
}
}
// mockT implements TestT for testing
type mockT struct {
ErrorCalled bool