helper/resource: Consolidate unit test override

I noticed we had two mechanisms for unit test override. One that dropped
a sentinel into the env var, and another with a struct member on
TestCase. This consolidates the two, using the cleaner struct member
internal mechanism and the nicer `resource.UnitTest()` entry point.
This commit is contained in:
Paul Hinze 2016-07-01 13:08:43 -05:00
parent ab92fa4dff
commit 5656d7388c
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 11 additions and 27 deletions

View File

@ -9,10 +9,9 @@ import (
) )
func TestState_basic(t *testing.T) { func TestState_basic(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.UnitTest(t, resource.TestCase{
OverrideEnvVar: true, PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders,
Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: testAccState_basic, Config: testAccState_basic,
@ -26,10 +25,9 @@ func TestState_basic(t *testing.T) {
} }
func TestState_complexOutputs(t *testing.T) { func TestState_complexOutputs(t *testing.T) {
resource.Test(t, resource.TestCase{ resource.UnitTest(t, resource.TestCase{
OverrideEnvVar: true, PreCheck: func() { testAccPreCheck(t) },
PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders,
Providers: testAccProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
{ {
Config: testAccState_complexOutputs, Config: testAccState_complexOutputs,

View File

@ -21,10 +21,6 @@ import (
const TestEnvVar = "TF_ACC" const TestEnvVar = "TF_ACC"
// UnitTestOverride is a value that when set in TestEnvVar indicates that this
// is a unit test borrowing the acceptance testing framework.
const UnitTestOverride = "UnitTestOverride"
// TestCheckFunc is the callback type used with acceptance tests to check // TestCheckFunc is the callback type used with acceptance tests to check
// the state of a resource. The state passed in is the latest state known, // the state of a resource. The state passed in is the latest state known,
// or in the case of being after a destroy, it is the last known state when // or in the case of being after a destroy, it is the last known state when
@ -40,12 +36,12 @@ type ImportStateCheckFunc func([]*terraform.InstanceState) 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 {
// OverrideEnvVar allows a test to run regardless of the TF_ACC // IsUnitTest allows a test to run regardless of the TF_ACC
// environment variable. This should be used with care - only for // environment variable. This should be used with care - only for
// fast tests on local resources (e.g. remote state with a local // fast tests on local resources (e.g. remote state with a local
// backend) but can be used to increase confidence in correct // backend) but can be used to increase confidence in correct
// operation of Terraform without waiting for a full acctest run. // operation of Terraform without waiting for a full acctest run.
OverrideEnvVar bool IsUnitTest bool
// PreCheck, if non-nil, will be called before any test steps are // PreCheck, if non-nil, will be called before any test steps are
// executed. It will only be executed in the case that the steps // executed. It will only be executed in the case that the steps
@ -190,15 +186,13 @@ func Test(t TestT, c TestCase) {
// We only run acceptance tests if an env var is set because they're // We only run acceptance tests if an env var is set because they're
// slow and generally require some outside configuration. You can opt out // slow and generally require some outside configuration. You can opt out
// of this with OverrideEnvVar on individual TestCases. // of this with OverrideEnvVar on individual TestCases.
if os.Getenv(TestEnvVar) == "" && !c.OverrideEnvVar { if os.Getenv(TestEnvVar) == "" && !c.IsUnitTest {
t.Skip(fmt.Sprintf( t.Skip(fmt.Sprintf(
"Acceptance tests skipped unless env '%s' set", "Acceptance tests skipped unless env '%s' set",
TestEnvVar)) TestEnvVar))
return return
} }
isUnitTest := (os.Getenv(TestEnvVar) == UnitTestOverride)
logWriter, err := logging.LogOutput() logWriter, err := logging.LogOutput()
if err != nil { if err != nil {
t.Error(fmt.Errorf("error setting up logging: %s", err)) t.Error(fmt.Errorf("error setting up logging: %s", err))
@ -206,7 +200,7 @@ func Test(t TestT, c TestCase) {
log.SetOutput(logWriter) log.SetOutput(logWriter)
// We require verbose mode so that the user knows what is going on. // We require verbose mode so that the user knows what is going on.
if !testTesting && !testing.Verbose() && !isUnitTest && !c.OverrideEnvVar { if !testTesting && !testing.Verbose() && !c.IsUnitTest {
t.Fatal("Acceptance tests must be run with the -v flag on tests") t.Fatal("Acceptance tests must be run with the -v flag on tests")
return return
} }
@ -326,15 +320,7 @@ func Test(t TestT, c TestCase) {
// normal unit test suite. This should only be used for resource that don't // normal unit test suite. This should only be used for resource that don't
// have any external dependencies. // have any external dependencies.
func UnitTest(t TestT, c TestCase) { func UnitTest(t TestT, c TestCase) {
oldEnv := os.Getenv(TestEnvVar) c.IsUnitTest = true
if err := os.Setenv(TestEnvVar, UnitTestOverride); err != nil {
t.Fatal(err)
}
defer func() {
if err := os.Setenv(TestEnvVar, oldEnv); err != nil {
t.Fatal(err)
}
}()
Test(t, c) Test(t, c)
} }