From 55c1bf7f791d2eb0730f2d271a620224976a8f8e Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 10 Jul 2014 10:30:41 -0700 Subject: [PATCH] helper/resource: more tests --- helper/resource/testing.go | 4 +++ helper/resource/testing_test.go | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 84a287e683..48866bce79 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -22,6 +22,9 @@ type TestCheckFunc func(*terraform.State) error // TestCase is a single acceptance test case used to test the apply/destroy // lifecycle of a resource in a specific configuration. +// +// When the destroy plan is executed, the config from the last TestStep +// is used to plan it. type TestCase struct { // Provider is the ResourceProvider that will be under test. Providers map[string]terraform.ResourceProvider @@ -99,6 +102,7 @@ func Test(t TestT, c TestCase) { for i, step := range c.Steps { var err error state, err = testStep(opts, state, step) + println(fmt.Sprintf("FOO: %#v", state)) if err != nil { t.Error(fmt.Sprintf( "Step %d error: %s", i, err)) diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 33fc2727de..9df8aad13a 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -1,6 +1,7 @@ package resource import ( + "fmt" "os" "testing" @@ -94,6 +95,7 @@ func TestTest_noEnv(t *testing.T) { if err := os.Setenv(TestEnvVar, ""); err != nil { t.Fatalf("err: %s", err) } + defer os.Setenv(TestEnvVar, "1") mt := new(mockT) Test(mt, TestCase{}) @@ -103,6 +105,47 @@ func TestTest_noEnv(t *testing.T) { } } +func TestTest_stepError(t *testing.T) { + mp := testProvider() + mp.ApplyReturn = &terraform.ResourceState{ + ID: "foo", + } + + checkDestroy := false + + checkDestroyFn := func(*terraform.State) error { + checkDestroy = true + return nil + } + + checkStepFn := func(*terraform.State) error { + return fmt.Errorf("error") + } + + mt := new(mockT) + Test(mt, TestCase{ + Providers: map[string]terraform.ResourceProvider{ + "test": mp, + }, + CheckDestroy: checkDestroyFn, + Steps: []TestStep{ + TestStep{ + Config: testConfigStr, + Check: checkStepFn, + }, + }, + }) + + if !mt.failed() { + t.Fatal("test should've failed") + } + t.Logf("Fail message: %s", mt.failMessage()) + + if !checkDestroy { + t.Fatal("didn't call check for destroy") + } +} + // mockT implements TestT for testing type mockT struct { ErrorCalled bool @@ -151,6 +194,13 @@ func (t *mockT) failMessage() string { func testProvider() *terraform.MockResourceProvider { mp := new(terraform.MockResourceProvider) + mp.DiffReturn = &terraform.ResourceDiff{ + Attributes: map[string]*terraform.ResourceAttrDiff{ + "foo": &terraform.ResourceAttrDiff{ + New: "bar", + }, + }, + } mp.ResourcesReturn = []terraform.ResourceType{ terraform.ResourceType{Name: "test_instance"}, }