helper/resource: add PreCheck

This commit is contained in:
Mitchell Hashimoto 2014-07-10 13:12:47 -07:00
parent 01b58669b0
commit 83f73e63aa
3 changed files with 25 additions and 2 deletions

View File

@ -10,9 +10,8 @@ import (
) )
func TestAccVpc(t *testing.T) { func TestAccVpc(t *testing.T) {
testAccPreCheck(t)
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders, Providers: testAccProviders,
CheckDestroy: testAccCheckVpcDestroy, CheckDestroy: testAccCheckVpcDestroy,
Steps: []resource.TestStep{ Steps: []resource.TestStep{

View File

@ -27,6 +27,12 @@ type TestCheckFunc func(*terraform.State) error
// When the destroy plan is executed, the config from the last TestStep // When the destroy plan is executed, the config from the last TestStep
// is used to plan it. // is used to plan it.
type TestCase struct { type TestCase struct {
// PreCheck, if non-nil, will be called before any test steps are
// executed. It will only be executed in the case that the steps
// would run, so it can be used for some validation before running
// acceptance tests, such as verifying that keys are setup.
PreCheck func()
// Provider is the ResourceProvider that will be under test. // Provider is the ResourceProvider that will be under test.
Providers map[string]terraform.ResourceProvider Providers map[string]terraform.ResourceProvider
@ -89,6 +95,11 @@ func Test(t TestT, c TestCase) {
return return
} }
// Run the PreCheck if we have it
if c.PreCheck != nil {
c.PreCheck()
}
// Build our context options that we can // Build our context options that we can
ctxProviders := make(map[string]terraform.ResourceProviderFactory) ctxProviders := make(map[string]terraform.ResourceProviderFactory)
for k, p := range c.Providers { for k, p := range c.Providers {

View File

@ -105,6 +105,19 @@ func TestTest_noEnv(t *testing.T) {
} }
} }
func TestTest_preCheck(t *testing.T) {
called := false
mt := new(mockT)
Test(mt, TestCase{
PreCheck: func() { called = true },
})
if !called {
t.Fatal("precheck should be called")
}
}
func TestTest_stepError(t *testing.T) { func TestTest_stepError(t *testing.T) {
mp := testProvider() mp := testProvider()
mp.ApplyReturn = &terraform.ResourceState{ mp.ApplyReturn = &terraform.ResourceState{