diff --git a/builtin/providers/aws/resource_aws_vpc_test.go b/builtin/providers/aws/resource_aws_vpc_test.go index 30c3fe7ab..57c73a020 100644 --- a/builtin/providers/aws/resource_aws_vpc_test.go +++ b/builtin/providers/aws/resource_aws_vpc_test.go @@ -10,9 +10,8 @@ import ( ) func TestAccVpc(t *testing.T) { - testAccPreCheck(t) - resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckVpcDestroy, Steps: []resource.TestStep{ diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 079724839..fb8ebf9a5 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -27,6 +27,12 @@ type TestCheckFunc func(*terraform.State) error // When the destroy plan is executed, the config from the last TestStep // is used to plan it. 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. Providers map[string]terraform.ResourceProvider @@ -89,6 +95,11 @@ func Test(t TestT, c TestCase) { return } + // Run the PreCheck if we have it + if c.PreCheck != nil { + c.PreCheck() + } + // Build our context options that we can ctxProviders := make(map[string]terraform.ResourceProviderFactory) for k, p := range c.Providers { diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 9df8aad13..81f6c8987 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -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) { mp := testProvider() mp.ApplyReturn = &terraform.ResourceState{