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"
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.
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
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
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
48 // map of sweepers that have ran, and the success/fail status based on any error
50 var sweeperRunList map[string]bool
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
58 // Name for sweeper. Must be unique to be ran by the Sweeper Runner
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
66 // Sweeper function that when invoked sweeps the Provider of specific
72 sweeperFuncs = make(map[string]*Sweeper)
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)
85 sweeperFuncs[name] = s
88 func TestMain(m *testing.M) {
91 // parse flagSweep contents for regions to run
92 regions := strings.Split(*flagSweep, ",")
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{}
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)
108 log.Printf("Sweeper Tests ran:\n")
109 for s, _ := range sweeperRunList {
110 fmt.Printf("\t- %s\n", s)
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
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
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 {
152 log.Printf("[DEBUG] Sweeper (%s) has dependency (%s), but that sweeper was not found", s.Name, dep)
156 if _, ok := sweeperRunList[s.Name]; ok {
157 log.Printf("[DEBUG] Sweeper (%s) already ran in region (%s)", s.Name, region)
163 sweeperRunList[s.Name] = true
165 sweeperRunList[s.Name] = false
171 const TestEnvVar = "TF_ACC"
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 {
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
184 type TestCheckFunc func(*terraform.State) error
186 // ImportStateCheckFunc is the check function for ImportState tests
187 type ImportStateCheckFunc func([]*terraform.InstanceState) error
189 // TestCase is a single acceptance test case used to test the apply/destroy
190 // lifecycle of a resource in a specific configuration.
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.
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.
208 // Providers is the ResourceProvider that will be under test.
210 // Alternately, ProviderFactories can be specified for the providers
211 // that are valid. This takes priority over Providers.
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
218 // PreventPostDestroyRefresh can be set to true for cases where data sources
219 // are tested alongside real resources
220 PreventPostDestroyRefresh bool
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
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.
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.
235 // IDRefreshName is the name of the resource to check. This will
236 // default to the first non-nil primary resource in the state.
238 // IDRefreshIgnore is a list of configuration keys that will be ignored.
240 IDRefreshIgnore []string
243 // TestStep is a single apply sequence of a test, done within the
244 // context of a state.
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.
254 // This is only required if the test mode settings below say it is
255 // for the mode you're using.
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"
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 //---------------------------------------------------------------
271 //---------------------------------------------------------------
272 // Plan, Apply testing
273 //---------------------------------------------------------------
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`.
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.
284 // If an error is returned, the test will fail. In this case, a
285 // destroy plan will still be attempted.
287 // If this is nil, no check is done on this step.
290 // Destroy will create a destroy plan if set to true.
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
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
300 ExpectError *regexp.Regexp
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
307 // PreventPostDestroyRefresh can be set to true for cases where data sources
308 // are tested alongside real resources
309 PreventPostDestroyRefresh bool
311 //---------------------------------------------------------------
312 // ImportState testing
313 //---------------------------------------------------------------
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.
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.
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
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
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.
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
348 // Test performs an acceptance test on a resource.
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.
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
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 {
364 "Acceptance tests skipped unless env '%s' set",
369 logWriter, err := logging.LogOutput()
371 t.Error(fmt.Errorf("error setting up logging: %s", err))
373 log.SetOutput(logWriter)
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")
381 // Run the PreCheck if we have it
382 if c.PreCheck != nil {
386 ctxProviders, err := testProviderFactories(c)
390 opts := terraform.ContextOpts{Providers: ctxProviders}
392 // A single state variable to track the lifecycle, starting with no state
393 var state *terraform.State
395 // Go through each step and run it
396 var idRefreshCheck *terraform.ResourceState
397 idRefresh := c.IDRefreshName != ""
399 for i, step := range c.Steps {
401 log.Printf("[WARN] Test: Executing step %d", i)
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)
410 "unknown test mode for step. Please see TestStep docs\n\n%#v",
414 // If there was an error, exit
416 // Perhaps we expected an error? Check if it matches
417 if step.ExpectError != nil {
418 if !step.ExpectError.MatchString(err.Error()) {
421 "Step %d, expected error:\n\n%s\n\nTo match:\n\n%s\n\n",
422 i, err, step.ExpectError))
428 "Step %d error: %s", i, err))
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 {
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 {
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)
460 "[ERROR] Test: ID-only test failed: %s", err))
467 // If we never checked an id-only refresh, it is a failure.
469 if !errored && len(c.Steps) > 0 && idRefreshCheck == nil {
470 t.Error("ID-only refresh check never ran.")
474 // If we have a state, then run the destroy
476 lastStep := c.Steps[len(c.Steps)-1]
477 destroyStep := TestStep{
478 Config: lastStep.Config,
479 Check: c.CheckDestroy,
481 PreventPostDestroyRefresh: c.PreventPostDestroyRefresh,
484 log.Printf("[WARN] Test: Executing destroy step")
485 state, err := testStep(opts, state, destroyStep)
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",
495 log.Printf("[WARN] Skipping destroy test since there is no state.")
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)
509 // add any fixed providers
510 for k, p := range c.Providers {
511 ctxProviders[k] = terraform.ResourceProviderFactoryFixed(p)
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
522 if p, ok := p.(TestProvider); ok {
525 return nil, fmt.Errorf("[ERROR] failed to reset provider %q: %s", k, err)
530 return ctxProviders, nil
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) {
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") == "" {
548 name := fmt.Sprintf("%s.foo", r.Type)
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{
555 Primary: &terraform.InstanceState{
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)
569 // Initialize the context
572 ctx, err := terraform.NewContext(&opts)
576 if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
578 estrs := make([]string, len(es))
579 for i, e := range es {
583 "Configuration is invalid.\n\nWarnings: %#v\n\nErrors: %#v",
587 log.Printf("[WARN] Config warnings: %#v", ws)
591 state, err = ctx.Refresh()
593 return fmt.Errorf("Error refreshing: %s", err)
596 // Verify attribute equivalence.
597 actualR := state.RootModule().Resources[name]
599 return fmt.Errorf("Resource gone!")
601 if actualR.Primary == nil {
602 return fmt.Errorf("Resource has no primary instance")
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) {
613 for k, _ := range expected {
614 if strings.HasPrefix(k, v) {
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 {
629 spewConf := spew.NewDefaultConfig()
630 spewConf.SortKeys = true
632 "Attributes not equivalent. Difference is shown below. Top is actual, bottom is expected."+
634 spewConf.Sdump(actual), spewConf.Sdump(expected))
641 opts terraform.ContextOpts,
642 step TestStep) (*module.Tree, error) {
643 if step.PreConfig != nil {
647 cfgPath, err := ioutil.TempDir("", "tf-test")
649 return nil, fmt.Errorf(
650 "Error creating temporary directory for config: %s", err)
652 defer os.RemoveAll(cfgPath)
654 // Write the configuration
655 cfgF, err := os.Create(filepath.Join(cfgPath, "main.tf"))
657 return nil, fmt.Errorf(
658 "Error creating temporary file for config: %s", err)
661 _, err = io.Copy(cfgF, strings.NewReader(step.Config))
664 return nil, fmt.Errorf(
665 "Error creating temporary file for config: %s", err)
668 // Parse the configuration
669 mod, err := module.NewTreeModule("", cfgPath)
671 return nil, fmt.Errorf(
672 "Error loading configuration: %s", err)
676 modStorage := &getter.FolderStorage{
677 StorageDir: filepath.Join(cfgPath, ".tfmodules"),
679 err = mod.Load(modStorage, module.GetModeGet)
681 return nil, fmt.Errorf("Error downloading modules: %s", err)
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")
692 for _, m := range state.Modules {
693 if len(m.Resources) > 0 {
694 if v, ok := m.Resources[c.ResourceName]; ok {
700 return nil, fmt.Errorf(
701 "Resource specified by ResourceName couldn't be found: %s", c.ResourceName)
704 // ComposeTestCheckFunc lets you compose multiple TestCheckFuncs into
705 // a single TestCheckFunc.
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)
721 // ComposeAggregateTestCheckFunc lets you compose multiple TestCheckFuncs into
722 // a single TestCheckFunc.
724 // As a user testing their provider, this lets you decompose your checks
725 // into smaller pieces more easily.
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
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))
739 return result.ErrorOrNil()
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)
754 if val, ok := is.Attributes[key]; ok && val != "" {
758 return fmt.Errorf("%s: Attribute '%s' expected to be set", name, key)
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)
771 if v, ok := is.Attributes[key]; !ok || v != value {
773 return fmt.Errorf("%s: Attribute '%s' not found", name, key)
777 "%s: Attribute '%s' expected %#v, got %#v",
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)
797 if _, ok := is.Attributes[key]; ok {
798 return fmt.Errorf("%s: Attribute '%s' found when not expected", name, key)
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)
814 if !r.MatchString(is.Attributes[key]) {
816 "%s: Attribute '%s' didn't match %q, got %#v",
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)
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)
844 vFirst, ok := isFirst.Attributes[keyFirst]
846 return fmt.Errorf("%s: Attribute '%s' not found", nameFirst, keyFirst)
849 isSecond, err := primaryInstanceState(s, nameSecond)
853 vSecond, ok := isSecond.Attributes[keySecond]
855 return fmt.Errorf("%s: Attribute '%s' not found", nameSecond, keySecond)
858 if vFirst != vSecond {
860 "%s: Attribute '%s' expected %#v, got %#v",
871 // TestCheckOutput checks an output in the Terraform configuration
872 func TestCheckOutput(name, value string) TestCheckFunc {
873 return func(s *terraform.State) error {
875 rs, ok := ms.Outputs[name]
877 return fmt.Errorf("Not found: %s", name)
880 if rs.Value != value {
882 "Output '%s': expected %#v, got %#v",
892 func TestMatchOutput(name string, r *regexp.Regexp) TestCheckFunc {
893 return func(s *terraform.State) error {
895 rs, ok := ms.Outputs[name]
897 return fmt.Errorf("Not found: %s", name)
900 if !r.MatchString(rs.Value.(string)) {
902 "Output '%s': %#v didn't match %q",
912 // TestT is the interface used to handle the test lifecycle of a test.
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{})
921 // This is set to true by unit tests to alter some behavior
922 var testTesting = false
924 // primaryInstanceState returns the primary instance state for the given resource name.
925 func primaryInstanceState(s *terraform.State, name string) (*terraform.InstanceState, error) {
927 rs, ok := ms.Resources[name]
929 return nil, fmt.Errorf("Not found: %s", name)
934 return nil, fmt.Errorf("No primary instance: %s", name)