helper/resource: add PreCheck
This commit is contained in:
parent
01b58669b0
commit
83f73e63aa
|
@ -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{
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
|
@ -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{
|
||||||
|
|
Loading…
Reference in New Issue