github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/helper/resource/testing.go (about)

     1  package resource
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"log"
     9  	"os"
    10  	"path/filepath"
    11  	"reflect"
    12  	"regexp"
    13  	"strings"
    14  	"testing"
    15  
    16  	"github.com/davecgh/go-spew/spew"
    17  	"github.com/hashicorp/go-getter"
    18  	"github.com/hashicorp/go-multierror"
    19  	"github.com/hashicorp/terraform/config/module"
    20  	"github.com/hashicorp/terraform/helper/logging"
    21  	"github.com/hashicorp/terraform/terraform"
    22  )
    23  
    24  // flagSweep is a flag available when running tests on the command line. It
    25  // contains a comma seperated list of regions to for the sweeper functions to
    26  // run in.  This flag bypasses the normal Test path and instead runs functions designed to
    27  // clean up any leaked resources a testing environment could have created. It is
    28  // a best effort attempt, and relies on Provider authors to implement "Sweeper"
    29  // methods for resources.
    30  
    31  // Adding Sweeper methods with AddTestSweepers will
    32  // construct a list of sweeper funcs to be called here. We iterate through
    33  // regions provided by the sweep flag, and for each region we iterate through the
    34  // tests, and exit on any errors. At time of writing, sweepers are ran
    35  // sequentially, however they can list dependencies to be ran first. We track
    36  // the sweepers that have been ran, so as to not run a sweeper twice for a given
    37  // region.
    38  //
    39  // WARNING:
    40  // Sweepers are designed to be destructive. You should not use the -sweep flag
    41  // in any environment that is not strictly a test environment. Resources will be
    42  // destroyed.
    43  
    44  var flagSweep = flag.String("sweep", "", "List of Regions to run available Sweepers")
    45  var flagSweepRun = flag.String("sweep-run", "", "Comma seperated list of Sweeper Tests to run")
    46  var sweeperFuncs map[string]*Sweeper
    47  
    48  // map of sweepers that have ran, and the success/fail status based on any error
    49  // raised
    50  var sweeperRunList map[string]bool
    51  
    52  // type SweeperFunc is a signature for a function that acts as a sweeper. It
    53  // accepts a string for the region that the sweeper is to be ran in. This
    54  // function must be able to construct a valid client for that region.
    55  type SweeperFunc func(r string) error
    56  
    57  type Sweeper struct {
    58  	// Name for sweeper. Must be unique to be ran by the Sweeper Runner
    59  	Name string
    60  
    61  	// Dependencies list the const names of other Sweeper functions that must be ran
    62  	// prior to running this Sweeper. This is an ordered list that will be invoked
    63  	// recursively at the helper/resource level
    64  	Dependencies []string
    65  
    66  	// Sweeper function that when invoked sweeps the Provider of specific
    67  	// resources
    68  	F SweeperFunc
    69  }
    70  
    71  func init() {
    72  	sweeperFuncs = make(map[string]*Sweeper)
    73  }
    74  
    75  // AddTestSweepers function adds a given name and Sweeper configuration
    76  // pair to the internal sweeperFuncs map. Invoke this function to register a
    77  // resource sweeper to be available for running when the -sweep flag is used
    78  // with `go test`. Sweeper names must be unique to help ensure a given sweeper
    79  // is only ran once per run.
    80  func AddTestSweepers(name string, s *Sweeper) {
    81  	if _, ok := sweeperFuncs[name]; ok {
    82  		log.Fatalf("[ERR] Error adding (%s) to sweeperFuncs: function already exists in map", name)
    83  	}
    84  
    85  	sweeperFuncs[name] = s
    86  }
    87  
    88  func TestMain(m *testing.M) {
    89  	flag.Parse()
    90  	if *flagSweep != "" {
    91  		// parse flagSweep contents for regions to run
    92  		regions := strings.Split(*flagSweep, ",")
    93  
    94  		// get filtered list of sweepers to run based on sweep-run flag
    95  		sweepers := filterSweepers(*flagSweepRun, sweeperFuncs)
    96  		for _, region := range regions {
    97  			region = strings.TrimSpace(region)
    98  			// reset sweeperRunList for each region
    99  			sweeperRunList = map[string]bool{}
   100  
   101  			log.Printf("[DEBUG] Running Sweepers for region (%s):\n", region)
   102  			for _, sweeper := range sweepers {
   103  				if err := runSweeperWithRegion(region, sweeper); err != nil {
   104  					log.Fatalf("[ERR] error running (%s): %s", sweeper.Name, err)
   105  				}
   106  			}
   107  
   108  			log.Printf("Sweeper Tests ran:\n")
   109  			for s, _ := range sweeperRunList {
   110  				fmt.Printf("\t- %s\n", s)
   111  			}
   112  		}
   113  	} else {
   114  		os.Exit(m.Run())
   115  	}
   116  }
   117  
   118  // filterSweepers takes a comma seperated string listing the names of sweepers
   119  // to be ran, and returns a filtered set from the list of all of sweepers to
   120  // run based on the names given.
   121  func filterSweepers(f string, source map[string]*Sweeper) map[string]*Sweeper {
   122  	filterSlice := strings.Split(strings.ToLower(f), ",")
   123  	if len(filterSlice) == 1 && filterSlice[0] == "" {
   124  		// if the filter slice is a single element of "" then no sweeper list was
   125  		// given, so just return the full list
   126  		return source
   127  	}
   128  
   129  	sweepers := make(map[string]*Sweeper)
   130  	for name, sweeper := range source {
   131  		for _, s := range filterSlice {
   132  			if strings.Contains(strings.ToLower(name), s) {
   133  				sweepers[name] = sweeper
   134  			}
   135  		}
   136  	}
   137  	return sweepers
   138  }
   139  
   140  // runSweeperWithRegion recieves a sweeper and a region, and recursively calls
   141  // itself with that region for every dependency found for that sweeper. If there
   142  // are no dependencies, invoke the contained sweeper fun with the region, and
   143  // add the success/fail status to the sweeperRunList.
   144  func runSweeperWithRegion(region string, s *Sweeper) error {
   145  	for _, dep := range s.Dependencies {
   146  		if depSweeper, ok := sweeperFuncs[dep]; ok {
   147  			log.Printf("[DEBUG] Sweeper (%s) has dependency (%s), running..", s.Name, dep)
   148  			if err := runSweeperWithRegion(region, depSweeper); err != nil {
   149  				return err
   150  			}
   151  		} else {
   152  			log.Printf("[DEBUG] Sweeper (%s) has dependency (%s), but that sweeper was not found", s.Name, dep)
   153  		}
   154  	}
   155  
   156  	if _, ok := sweeperRunList[s.Name]; ok {
   157  		log.Printf("[DEBUG] Sweeper (%s) already ran in region (%s)", s.Name, region)
   158  		return nil
   159  	}
   160  
   161  	runE := s.F(region)
   162  	if runE == nil {
   163  		sweeperRunList[s.Name] = true
   164  	} else {
   165  		sweeperRunList[s.Name] = false
   166  	}
   167  
   168  	return runE
   169  }
   170  
   171  const TestEnvVar = "TF_ACC"
   172  
   173  // TestProvider can be implemented by any ResourceProvider to provide custom
   174  // reset functionality at the start of an acceptance test.
   175  // The helper/schema Provider implements this interface.
   176  type TestProvider interface {
   177  	TestReset() error
   178  }
   179  
   180  // TestCheckFunc is the callback type used with acceptance tests to check
   181  // the state of a resource. The state passed in is the latest state known,
   182  // or in the case of being after a destroy, it is the last known state when
   183  // it was created.
   184  type TestCheckFunc func(*terraform.State) error
   185  
   186  // ImportStateCheckFunc is the check function for ImportState tests
   187  type ImportStateCheckFunc func([]*terraform.InstanceState) error
   188  
   189  // TestCase is a single acceptance test case used to test the apply/destroy
   190  // lifecycle of a resource in a specific configuration.
   191  //
   192  // When the destroy plan is executed, the config from the last TestStep
   193  // is used to plan it.
   194  type TestCase struct {
   195  	// IsUnitTest allows a test to run regardless of the TF_ACC
   196  	// environment variable. This should be used with care - only for
   197  	// fast tests on local resources (e.g. remote state with a local
   198  	// backend) but can be used to increase confidence in correct
   199  	// operation of Terraform without waiting for a full acctest run.
   200  	IsUnitTest bool
   201  
   202  	// PreCheck, if non-nil, will be called before any test steps are
   203  	// executed. It will only be executed in the case that the steps
   204  	// would run, so it can be used for some validation before running
   205  	// acceptance tests, such as verifying that keys are setup.
   206  	PreCheck func()
   207  
   208  	// Providers is the ResourceProvider that will be under test.
   209  	//
   210  	// Alternately, ProviderFactories can be specified for the providers
   211  	// that are valid. This takes priority over Providers.
   212  	//
   213  	// The end effect of each is the same: specifying the providers that
   214  	// are used within the tests.
   215  	Providers         map[string]terraform.ResourceProvider
   216  	ProviderFactories map[string]terraform.ResourceProviderFactory
   217  
   218  	// PreventPostDestroyRefresh can be set to true for cases where data sources
   219  	// are tested alongside real resources
   220  	PreventPostDestroyRefresh bool
   221  
   222  	// CheckDestroy is called after the resource is finally destroyed
   223  	// to allow the tester to test that the resource is truly gone.
   224  	CheckDestroy TestCheckFunc
   225  
   226  	// Steps are the apply sequences done within the context of the
   227  	// same state. Each step can have its own check to verify correctness.
   228  	Steps []TestStep
   229  
   230  	// The settings below control the "ID-only refresh test." This is
   231  	// an enabled-by-default test that tests that a refresh can be
   232  	// refreshed with only an ID to result in the same attributes.
   233  	// This validates completeness of Refresh.
   234  	//
   235  	// IDRefreshName is the name of the resource to check. This will
   236  	// default to the first non-nil primary resource in the state.
   237  	//
   238  	// IDRefreshIgnore is a list of configuration keys that will be ignored.
   239  	IDRefreshName   string
   240  	IDRefreshIgnore []string
   241  }
   242  
   243  // TestStep is a single apply sequence of a test, done within the
   244  // context of a state.
   245  //
   246  // Multiple TestSteps can be sequenced in a Test to allow testing
   247  // potentially complex update logic. In general, simply create/destroy
   248  // tests will only need one step.
   249  type TestStep struct {
   250  	// ResourceName should be set to the name of the resource
   251  	// that is being tested. Example: "aws_instance.foo". Various test
   252  	// modes use this to auto-detect state information.
   253  	//
   254  	// This is only required if the test mode settings below say it is
   255  	// for the mode you're using.
   256  	ResourceName string
   257  
   258  	// PreConfig is called before the Config is applied to perform any per-step
   259  	// setup that needs to happen. This is called regardless of "test mode"
   260  	// below.
   261  	PreConfig func()
   262  
   263  	//---------------------------------------------------------------
   264  	// Test modes. One of the following groups of settings must be
   265  	// set to determine what the test step will do. Ideally we would've
   266  	// used Go interfaces here but there are now hundreds of tests we don't
   267  	// want to re-type so instead we just determine which step logic
   268  	// to run based on what settings below are set.
   269  	//---------------------------------------------------------------
   270  
   271  	//---------------------------------------------------------------
   272  	// Plan, Apply testing
   273  	//---------------------------------------------------------------
   274  
   275  	// Config a string of the configuration to give to Terraform. If this
   276  	// is set, then the TestCase will execute this step with the same logic
   277  	// as a `terraform apply`.
   278  	Config string
   279  
   280  	// Check is called after the Config is applied. Use this step to
   281  	// make your own API calls to check the status of things, and to
   282  	// inspect the format of the ResourceState itself.
   283  	//
   284  	// If an error is returned, the test will fail. In this case, a
   285  	// destroy plan will still be attempted.
   286  	//
   287  	// If this is nil, no check is done on this step.
   288  	Check TestCheckFunc
   289  
   290  	// Destroy will create a destroy plan if set to true.
   291  	Destroy bool
   292  
   293  	// ExpectNonEmptyPlan can be set to true for specific types of tests that are
   294  	// looking to verify that a diff occurs
   295  	ExpectNonEmptyPlan bool
   296  
   297  	// ExpectError allows the construction of test cases that we expect to fail
   298  	// with an error. The specified regexp must match against the error for the
   299  	// test to pass.
   300  	ExpectError *regexp.Regexp
   301  
   302  	// PlanOnly can be set to only run `plan` with this configuration, and not
   303  	// actually apply it. This is useful for ensuring config changes result in
   304  	// no-op plans
   305  	PlanOnly bool
   306  
   307  	// PreventPostDestroyRefresh can be set to true for cases where data sources
   308  	// are tested alongside real resources
   309  	PreventPostDestroyRefresh bool
   310  
   311  	//---------------------------------------------------------------
   312  	// ImportState testing
   313  	//---------------------------------------------------------------
   314  
   315  	// ImportState, if true, will test the functionality of ImportState
   316  	// by importing the resource with ResourceName (must be set) and the
   317  	// ID of that resource.
   318  	ImportState bool
   319  
   320  	// ImportStateId is the ID to perform an ImportState operation with.
   321  	// This is optional. If it isn't set, then the resource ID is automatically
   322  	// determined by inspecting the state for ResourceName's ID.
   323  	ImportStateId string
   324  
   325  	// ImportStateIdPrefix is the prefix added in front of ImportStateId.
   326  	// This can be useful in complex import cases, where more than one
   327  	// attribute needs to be passed on as the Import ID. Mainly in cases
   328  	// where the ID is not known, and a known prefix needs to be added to
   329  	// the unset ImportStateId field.
   330  	ImportStateIdPrefix string
   331  
   332  	// ImportStateCheck checks the results of ImportState. It should be
   333  	// used to verify that the resulting value of ImportState has the
   334  	// proper resources, IDs, and attributes.
   335  	ImportStateCheck ImportStateCheckFunc
   336  
   337  	// ImportStateVerify, if true, will also check that the state values
   338  	// that are finally put into the state after import match for all the
   339  	// IDs returned by the Import.
   340  	//
   341  	// ImportStateVerifyIgnore are fields that should not be verified to
   342  	// be equal. These can be set to ephemeral fields or fields that can't
   343  	// be refreshed and don't matter.
   344  	ImportStateVerify       bool
   345  	ImportStateVerifyIgnore []string
   346  }
   347  
   348  // Test performs an acceptance test on a resource.
   349  //
   350  // Tests are not run unless an environmental variable "TF_ACC" is
   351  // set to some non-empty value. This is to avoid test cases surprising
   352  // a user by creating real resources.
   353  //
   354  // Tests will fail unless the verbose flag (`go test -v`, or explicitly
   355  // the "-test.v" flag) is set. Because some acceptance tests take quite
   356  // long, we require the verbose flag so users are able to see progress
   357  // output.
   358  func Test(t TestT, c TestCase) {
   359  	// We only run acceptance tests if an env var is set because they're
   360  	// slow and generally require some outside configuration. You can opt out
   361  	// of this with OverrideEnvVar on individual TestCases.
   362  	if os.Getenv(TestEnvVar) == "" && !c.IsUnitTest {
   363  		t.Skip(fmt.Sprintf(
   364  			"Acceptance tests skipped unless env '%s' set",
   365  			TestEnvVar))
   366  		return
   367  	}
   368  
   369  	logWriter, err := logging.LogOutput()
   370  	if err != nil {
   371  		t.Error(fmt.Errorf("error setting up logging: %s", err))
   372  	}
   373  	log.SetOutput(logWriter)
   374  
   375  	// We require verbose mode so that the user knows what is going on.
   376  	if !testTesting && !testing.Verbose() && !c.IsUnitTest {
   377  		t.Fatal("Acceptance tests must be run with the -v flag on tests")
   378  		return
   379  	}
   380  
   381  	// Run the PreCheck if we have it
   382  	if c.PreCheck != nil {
   383  		c.PreCheck()
   384  	}
   385  
   386  	ctxProviders, err := testProviderFactories(c)
   387  	if err != nil {
   388  		t.Fatal(err)
   389  	}
   390  	opts := terraform.ContextOpts{Providers: ctxProviders}
   391  
   392  	// A single state variable to track the lifecycle, starting with no state
   393  	var state *terraform.State
   394  
   395  	// Go through each step and run it
   396  	var idRefreshCheck *terraform.ResourceState
   397  	idRefresh := c.IDRefreshName != ""
   398  	errored := false
   399  	for i, step := range c.Steps {
   400  		var err error
   401  		log.Printf("[WARN] Test: Executing step %d", i)
   402  
   403  		// Determine the test mode to execute
   404  		if step.Config != "" {
   405  			state, err = testStepConfig(opts, state, step)
   406  		} else if step.ImportState {
   407  			state, err = testStepImportState(opts, state, step)
   408  		} else {
   409  			err = fmt.Errorf(
   410  				"unknown test mode for step. Please see TestStep docs\n\n%#v",
   411  				step)
   412  		}
   413  
   414  		// If there was an error, exit
   415  		if err != nil {
   416  			// Perhaps we expected an error? Check if it matches
   417  			if step.ExpectError != nil {
   418  				if !step.ExpectError.MatchString(err.Error()) {
   419  					errored = true
   420  					t.Error(fmt.Sprintf(
   421  						"Step %d, expected error:\n\n%s\n\nTo match:\n\n%s\n\n",
   422  						i, err, step.ExpectError))
   423  					break
   424  				}
   425  			} else {
   426  				errored = true
   427  				t.Error(fmt.Sprintf(
   428  					"Step %d error: %s", i, err))
   429  				break
   430  			}
   431  		}
   432  
   433  		// If we've never checked an id-only refresh and our state isn't
   434  		// empty, find the first resource and test it.
   435  		if idRefresh && idRefreshCheck == nil && !state.Empty() {
   436  			// Find the first non-nil resource in the state
   437  			for _, m := range state.Modules {
   438  				if len(m.Resources) > 0 {
   439  					if v, ok := m.Resources[c.IDRefreshName]; ok {
   440  						idRefreshCheck = v
   441  					}
   442  
   443  					break
   444  				}
   445  			}
   446  
   447  			// If we have an instance to check for refreshes, do it
   448  			// immediately. We do it in the middle of another test
   449  			// because it shouldn't affect the overall state (refresh
   450  			// is read-only semantically) and we want to fail early if
   451  			// this fails. If refresh isn't read-only, then this will have
   452  			// caught a different bug.
   453  			if idRefreshCheck != nil {
   454  				log.Printf(
   455  					"[WARN] Test: Running ID-only refresh check on %s",
   456  					idRefreshCheck.Primary.ID)
   457  				if err := testIDOnlyRefresh(c, opts, step, idRefreshCheck); err != nil {
   458  					log.Printf("[ERROR] Test: ID-only test failed: %s", err)
   459  					t.Error(fmt.Sprintf(
   460  						"[ERROR] Test: ID-only test failed: %s", err))
   461  					break
   462  				}
   463  			}
   464  		}
   465  	}
   466  
   467  	// If we never checked an id-only refresh, it is a failure.
   468  	if idRefresh {
   469  		if !errored && len(c.Steps) > 0 && idRefreshCheck == nil {
   470  			t.Error("ID-only refresh check never ran.")
   471  		}
   472  	}
   473  
   474  	// If we have a state, then run the destroy
   475  	if state != nil {
   476  		lastStep := c.Steps[len(c.Steps)-1]
   477  		destroyStep := TestStep{
   478  			Config:                    lastStep.Config,
   479  			Check:                     c.CheckDestroy,
   480  			Destroy:                   true,
   481  			PreventPostDestroyRefresh: c.PreventPostDestroyRefresh,
   482  		}
   483  
   484  		log.Printf("[WARN] Test: Executing destroy step")
   485  		state, err := testStep(opts, state, destroyStep)
   486  		if err != nil {
   487  			t.Error(fmt.Sprintf(
   488  				"Error destroying resource! WARNING: Dangling resources\n"+
   489  					"may exist. The full state and error is shown below.\n\n"+
   490  					"Error: %s\n\nState: %s",
   491  				err,
   492  				state))
   493  		}
   494  	} else {
   495  		log.Printf("[WARN] Skipping destroy test since there is no state.")
   496  	}
   497  }
   498  
   499  // testProviderFactories is a helper to build the ResourceProviderFactory map
   500  // with pre instantiated ResourceProviders, so that we can reset them for the
   501  // test, while only calling the factory function once.
   502  // Any errors are stored so that they can be returned by the factory in
   503  // terraform to match non-test behavior.
   504  func testProviderFactories(c TestCase) (map[string]terraform.ResourceProviderFactory, error) {
   505  	ctxProviders := c.ProviderFactories // make(map[string]terraform.ResourceProviderFactory)
   506  	if ctxProviders == nil {
   507  		ctxProviders = make(map[string]terraform.ResourceProviderFactory)
   508  	}
   509  	// add any fixed providers
   510  	for k, p := range c.Providers {
   511  		ctxProviders[k] = terraform.ResourceProviderFactoryFixed(p)
   512  	}
   513  
   514  	// reset the providers if needed
   515  	for k, pf := range ctxProviders {
   516  		// we can ignore any errors here, if we don't have a provider to reset
   517  		// the error will be handled later
   518  		p, err := pf()
   519  		if err != nil {
   520  			return nil, err
   521  		}
   522  		if p, ok := p.(TestProvider); ok {
   523  			err := p.TestReset()
   524  			if err != nil {
   525  				return nil, fmt.Errorf("[ERROR] failed to reset provider %q: %s", k, err)
   526  			}
   527  		}
   528  	}
   529  
   530  	return ctxProviders, nil
   531  }
   532  
   533  // UnitTest is a helper to force the acceptance testing harness to run in the
   534  // normal unit test suite. This should only be used for resource that don't
   535  // have any external dependencies.
   536  func UnitTest(t TestT, c TestCase) {
   537  	c.IsUnitTest = true
   538  	Test(t, c)
   539  }
   540  
   541  func testIDOnlyRefresh(c TestCase, opts terraform.ContextOpts, step TestStep, r *terraform.ResourceState) error {
   542  	// TODO: We guard by this right now so master doesn't explode. We
   543  	// need to remove this eventually to make this part of the normal tests.
   544  	if os.Getenv("TF_ACC_IDONLY") == "" {
   545  		return nil
   546  	}
   547  
   548  	name := fmt.Sprintf("%s.foo", r.Type)
   549  
   550  	// Build the state. The state is just the resource with an ID. There
   551  	// are no attributes. We only set what is needed to perform a refresh.
   552  	state := terraform.NewState()
   553  	state.RootModule().Resources[name] = &terraform.ResourceState{
   554  		Type: r.Type,
   555  		Primary: &terraform.InstanceState{
   556  			ID: r.Primary.ID,
   557  		},
   558  	}
   559  
   560  	// Create the config module. We use the full config because Refresh
   561  	// doesn't have access to it and we may need things like provider
   562  	// configurations. The initial implementation of id-only checks used
   563  	// an empty config module, but that caused the aforementioned problems.
   564  	mod, err := testModule(opts, step)
   565  	if err != nil {
   566  		return err
   567  	}
   568  
   569  	// Initialize the context
   570  	opts.Module = mod
   571  	opts.State = state
   572  	ctx, err := terraform.NewContext(&opts)
   573  	if err != nil {
   574  		return err
   575  	}
   576  	if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
   577  		if len(es) > 0 {
   578  			estrs := make([]string, len(es))
   579  			for i, e := range es {
   580  				estrs[i] = e.Error()
   581  			}
   582  			return fmt.Errorf(
   583  				"Configuration is invalid.\n\nWarnings: %#v\n\nErrors: %#v",
   584  				ws, estrs)
   585  		}
   586  
   587  		log.Printf("[WARN] Config warnings: %#v", ws)
   588  	}
   589  
   590  	// Refresh!
   591  	state, err = ctx.Refresh()
   592  	if err != nil {
   593  		return fmt.Errorf("Error refreshing: %s", err)
   594  	}
   595  
   596  	// Verify attribute equivalence.
   597  	actualR := state.RootModule().Resources[name]
   598  	if actualR == nil {
   599  		return fmt.Errorf("Resource gone!")
   600  	}
   601  	if actualR.Primary == nil {
   602  		return fmt.Errorf("Resource has no primary instance")
   603  	}
   604  	actual := actualR.Primary.Attributes
   605  	expected := r.Primary.Attributes
   606  	// Remove fields we're ignoring
   607  	for _, v := range c.IDRefreshIgnore {
   608  		for k, _ := range actual {
   609  			if strings.HasPrefix(k, v) {
   610  				delete(actual, k)
   611  			}
   612  		}
   613  		for k, _ := range expected {
   614  			if strings.HasPrefix(k, v) {
   615  				delete(expected, k)
   616  			}
   617  		}
   618  	}
   619  
   620  	if !reflect.DeepEqual(actual, expected) {
   621  		// Determine only the different attributes
   622  		for k, v := range expected {
   623  			if av, ok := actual[k]; ok && v == av {
   624  				delete(expected, k)
   625  				delete(actual, k)
   626  			}
   627  		}
   628  
   629  		spewConf := spew.NewDefaultConfig()
   630  		spewConf.SortKeys = true
   631  		return fmt.Errorf(
   632  			"Attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
   633  				"\n\n%s\n\n%s",
   634  			spewConf.Sdump(actual), spewConf.Sdump(expected))
   635  	}
   636  
   637  	return nil
   638  }
   639  
   640  func testModule(
   641  	opts terraform.ContextOpts,
   642  	step TestStep) (*module.Tree, error) {
   643  	if step.PreConfig != nil {
   644  		step.PreConfig()
   645  	}
   646  
   647  	cfgPath, err := ioutil.TempDir("", "tf-test")
   648  	if err != nil {
   649  		return nil, fmt.Errorf(
   650  			"Error creating temporary directory for config: %s", err)
   651  	}
   652  	defer os.RemoveAll(cfgPath)
   653  
   654  	// Write the configuration
   655  	cfgF, err := os.Create(filepath.Join(cfgPath, "main.tf"))
   656  	if err != nil {
   657  		return nil, fmt.Errorf(
   658  			"Error creating temporary file for config: %s", err)
   659  	}
   660  
   661  	_, err = io.Copy(cfgF, strings.NewReader(step.Config))
   662  	cfgF.Close()
   663  	if err != nil {
   664  		return nil, fmt.Errorf(
   665  			"Error creating temporary file for config: %s", err)
   666  	}
   667  
   668  	// Parse the configuration
   669  	mod, err := module.NewTreeModule("", cfgPath)
   670  	if err != nil {
   671  		return nil, fmt.Errorf(
   672  			"Error loading configuration: %s", err)
   673  	}
   674  
   675  	// Load the modules
   676  	modStorage := &getter.FolderStorage{
   677  		StorageDir: filepath.Join(cfgPath, ".tfmodules"),
   678  	}
   679  	err = mod.Load(modStorage, module.GetModeGet)
   680  	if err != nil {
   681  		return nil, fmt.Errorf("Error downloading modules: %s", err)
   682  	}
   683  
   684  	return mod, nil
   685  }
   686  
   687  func testResource(c TestStep, state *terraform.State) (*terraform.ResourceState, error) {
   688  	if c.ResourceName == "" {
   689  		return nil, fmt.Errorf("ResourceName must be set in TestStep")
   690  	}
   691  
   692  	for _, m := range state.Modules {
   693  		if len(m.Resources) > 0 {
   694  			if v, ok := m.Resources[c.ResourceName]; ok {
   695  				return v, nil
   696  			}
   697  		}
   698  	}
   699  
   700  	return nil, fmt.Errorf(
   701  		"Resource specified by ResourceName couldn't be found: %s", c.ResourceName)
   702  }
   703  
   704  // ComposeTestCheckFunc lets you compose multiple TestCheckFuncs into
   705  // a single TestCheckFunc.
   706  //
   707  // As a user testing their provider, this lets you decompose your checks
   708  // into smaller pieces more easily.
   709  func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
   710  	return func(s *terraform.State) error {
   711  		for i, f := range fs {
   712  			if err := f(s); err != nil {
   713  				return fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)
   714  			}
   715  		}
   716  
   717  		return nil
   718  	}
   719  }
   720  
   721  // ComposeAggregateTestCheckFunc lets you compose multiple TestCheckFuncs into
   722  // a single TestCheckFunc.
   723  //
   724  // As a user testing their provider, this lets you decompose your checks
   725  // into smaller pieces more easily.
   726  //
   727  // Unlike ComposeTestCheckFunc, ComposeAggergateTestCheckFunc runs _all_ of the
   728  // TestCheckFuncs and aggregates failures.
   729  func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc {
   730  	return func(s *terraform.State) error {
   731  		var result *multierror.Error
   732  
   733  		for i, f := range fs {
   734  			if err := f(s); err != nil {
   735  				result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err))
   736  			}
   737  		}
   738  
   739  		return result.ErrorOrNil()
   740  	}
   741  }
   742  
   743  // TestCheckResourceAttrSet is a TestCheckFunc which ensures a value
   744  // exists in state for the given name/key combination. It is useful when
   745  // testing that computed values were set, when it is not possible to
   746  // know ahead of time what the values will be.
   747  func TestCheckResourceAttrSet(name, key string) TestCheckFunc {
   748  	return func(s *terraform.State) error {
   749  		is, err := primaryInstanceState(s, name)
   750  		if err != nil {
   751  			return err
   752  		}
   753  
   754  		if val, ok := is.Attributes[key]; ok && val != "" {
   755  			return nil
   756  		}
   757  
   758  		return fmt.Errorf("%s: Attribute '%s' expected to be set", name, key)
   759  	}
   760  }
   761  
   762  // TestCheckResourceAttr is a TestCheckFunc which validates
   763  // the value in state for the given name/key combination.
   764  func TestCheckResourceAttr(name, key, value string) TestCheckFunc {
   765  	return func(s *terraform.State) error {
   766  		is, err := primaryInstanceState(s, name)
   767  		if err != nil {
   768  			return err
   769  		}
   770  
   771  		if v, ok := is.Attributes[key]; !ok || v != value {
   772  			if !ok {
   773  				return fmt.Errorf("%s: Attribute '%s' not found", name, key)
   774  			}
   775  
   776  			return fmt.Errorf(
   777  				"%s: Attribute '%s' expected %#v, got %#v",
   778  				name,
   779  				key,
   780  				value,
   781  				v)
   782  		}
   783  
   784  		return nil
   785  	}
   786  }
   787  
   788  // TestCheckNoResourceAttr is a TestCheckFunc which ensures that
   789  // NO value exists in state for the given name/key combination.
   790  func TestCheckNoResourceAttr(name, key string) TestCheckFunc {
   791  	return func(s *terraform.State) error {
   792  		is, err := primaryInstanceState(s, name)
   793  		if err != nil {
   794  			return err
   795  		}
   796  
   797  		if _, ok := is.Attributes[key]; ok {
   798  			return fmt.Errorf("%s: Attribute '%s' found when not expected", name, key)
   799  		}
   800  
   801  		return nil
   802  	}
   803  }
   804  
   805  // TestMatchResourceAttr is a TestCheckFunc which checks that the value
   806  // in state for the given name/key combination matches the given regex.
   807  func TestMatchResourceAttr(name, key string, r *regexp.Regexp) TestCheckFunc {
   808  	return func(s *terraform.State) error {
   809  		is, err := primaryInstanceState(s, name)
   810  		if err != nil {
   811  			return err
   812  		}
   813  
   814  		if !r.MatchString(is.Attributes[key]) {
   815  			return fmt.Errorf(
   816  				"%s: Attribute '%s' didn't match %q, got %#v",
   817  				name,
   818  				key,
   819  				r.String(),
   820  				is.Attributes[key])
   821  		}
   822  
   823  		return nil
   824  	}
   825  }
   826  
   827  // TestCheckResourceAttrPtr is like TestCheckResourceAttr except the
   828  // value is a pointer so that it can be updated while the test is running.
   829  // It will only be dereferenced at the point this step is run.
   830  func TestCheckResourceAttrPtr(name string, key string, value *string) TestCheckFunc {
   831  	return func(s *terraform.State) error {
   832  		return TestCheckResourceAttr(name, key, *value)(s)
   833  	}
   834  }
   835  
   836  // TestCheckResourceAttrPair is a TestCheckFunc which validates that the values
   837  // in state for a pair of name/key combinations are equal.
   838  func TestCheckResourceAttrPair(nameFirst, keyFirst, nameSecond, keySecond string) TestCheckFunc {
   839  	return func(s *terraform.State) error {
   840  		isFirst, err := primaryInstanceState(s, nameFirst)
   841  		if err != nil {
   842  			return err
   843  		}
   844  		vFirst, ok := isFirst.Attributes[keyFirst]
   845  		if !ok {
   846  			return fmt.Errorf("%s: Attribute '%s' not found", nameFirst, keyFirst)
   847  		}
   848  
   849  		isSecond, err := primaryInstanceState(s, nameSecond)
   850  		if err != nil {
   851  			return err
   852  		}
   853  		vSecond, ok := isSecond.Attributes[keySecond]
   854  		if !ok {
   855  			return fmt.Errorf("%s: Attribute '%s' not found", nameSecond, keySecond)
   856  		}
   857  
   858  		if vFirst != vSecond {
   859  			return fmt.Errorf(
   860  				"%s: Attribute '%s' expected %#v, got %#v",
   861  				nameFirst,
   862  				keyFirst,
   863  				vSecond,
   864  				vFirst)
   865  		}
   866  
   867  		return nil
   868  	}
   869  }
   870  
   871  // TestCheckOutput checks an output in the Terraform configuration
   872  func TestCheckOutput(name, value string) TestCheckFunc {
   873  	return func(s *terraform.State) error {
   874  		ms := s.RootModule()
   875  		rs, ok := ms.Outputs[name]
   876  		if !ok {
   877  			return fmt.Errorf("Not found: %s", name)
   878  		}
   879  
   880  		if rs.Value != value {
   881  			return fmt.Errorf(
   882  				"Output '%s': expected %#v, got %#v",
   883  				name,
   884  				value,
   885  				rs)
   886  		}
   887  
   888  		return nil
   889  	}
   890  }
   891  
   892  func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc {
   893  	return func(s *terraform.State) error {
   894  		ms := s.RootModule()
   895  		rs, ok := ms.Outputs[name]
   896  		if !ok {
   897  			return fmt.Errorf("Not found: %s", name)
   898  		}
   899  
   900  		if !r.MatchString(rs.Value.(string)) {
   901  			return fmt.Errorf(
   902  				"Output '%s': %#v didn't match %q",
   903  				name,
   904  				rs,
   905  				r.String())
   906  		}
   907  
   908  		return nil
   909  	}
   910  }
   911  
   912  // TestT is the interface used to handle the test lifecycle of a test.
   913  //
   914  // Users should just use a *testing.T object, which implements this.
   915  type TestT interface {
   916  	Error(args ...interface{})
   917  	Fatal(args ...interface{})
   918  	Skip(args ...interface{})
   919  }
   920  
   921  // This is set to true by unit tests to alter some behavior
   922  var testTesting = false
   923  
   924  // primaryInstanceState returns the primary instance state for the given resource name.
   925  func primaryInstanceState(s *terraform.State, name string) (*terraform.InstanceState, error) {
   926  	ms := s.RootModule()
   927  	rs, ok := ms.Resources[name]
   928  	if !ok {
   929  		return nil, fmt.Errorf("Not found: %s", name)
   930  	}
   931  
   932  	is := rs.Primary
   933  	if is == nil {
   934  		return nil, fmt.Errorf("No primary instance: %s", name)
   935  	}
   936  
   937  	return is, nil
   938  }