From 4a3d51f40e4b831f930537e753f9af50c88bd7a8 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Jul 2014 13:29:38 -0700 Subject: [PATCH] helper/resource: can compose TestCheckFuncs --- helper/resource/testing.go | 17 ++++++++++ helper/resource/testing_test.go | 55 ++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 92b755852..4222fbd0e 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -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. diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 81f6c8987..7a096149e 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -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