From 9c7cf639b3c7270252957ff10e001957ec2ecd90 Mon Sep 17 00:00:00 2001 From: James Nugent Date: Sat, 11 Jun 2016 12:55:14 +0100 Subject: [PATCH] testing: Allow acceptance test to run as unit test This commit adds a flag to acceptance tests in order to make appropriately named tests work during `make test` irrespective of the TF_ACC environment variable. This should only be used on tests which are known to be fast. --- helper/resource/testing.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/helper/resource/testing.go b/helper/resource/testing.go index a394d6409..7c4ba174b 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -40,6 +40,13 @@ type ImportStateCheckFunc func([]*terraform.InstanceState) error // When the destroy plan is executed, the config from the last TestStep // is used to plan it. type TestCase struct { + // OverrideEnvVar allows a test to run regardless of the TF_ACC + // environment variable. This should be used with care - only for + // fast tests on local resources (e.g. remote state with a local + // backend) but can be used to increase confidence in correct + // operation of Terraform without waiting for a full acctest run. + OverrideEnvVar bool + // 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 @@ -181,8 +188,9 @@ type TestStep struct { // output. func Test(t TestT, c TestCase) { // We only run acceptance tests if an env var is set because they're - // slow and generally require some outside configuration. - if os.Getenv(TestEnvVar) == "" { + // slow and generally require some outside configuration. You can opt out + // of this with OverrideEnvVar on individual TestCases. + if os.Getenv(TestEnvVar) == "" && !c.OverrideEnvVar { t.Skip(fmt.Sprintf( "Acceptance tests skipped unless env '%s' set", TestEnvVar)) @@ -198,7 +206,7 @@ func Test(t TestT, c TestCase) { log.SetOutput(logWriter) // We require verbose mode so that the user knows what is going on. - if !testTesting && !testing.Verbose() && !isUnitTest { + if !testTesting && !testing.Verbose() && !isUnitTest && !c.OverrideEnvVar { t.Fatal("Acceptance tests must be run with the -v flag on tests") return }