helper/resource: more tests

This commit is contained in:
Mitchell Hashimoto 2014-07-10 10:30:41 -07:00
parent e0fbd48afd
commit 55c1bf7f79
2 changed files with 54 additions and 0 deletions

View File

@ -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))

View File

@ -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"},
}