]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blobdiff - vendor/github.com/hashicorp/terraform/helper/resource/testing_config.go
Merge pull request #27 from terraform-providers/go-modules-2019-02-22
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / helper / resource / testing_config.go
index 537a11c34ae327709f48edc935e11742dfd210c7..033f1266d6c72eb1054f5e8ce5415f42f77c19bc 100644 (file)
@@ -1,10 +1,12 @@
 package resource
 
 import (
+       "errors"
        "fmt"
        "log"
        "strings"
 
+       "github.com/hashicorp/errwrap"
        "github.com/hashicorp/terraform/terraform"
 )
 
@@ -20,6 +22,14 @@ func testStep(
        opts terraform.ContextOpts,
        state *terraform.State,
        step TestStep) (*terraform.State, error) {
+       // Pre-taint any resources that have been defined in Taint, as long as this
+       // is not a destroy step.
+       if !step.Destroy {
+               if err := testStepTaint(state, step); err != nil {
+                       return state, err
+               }
+       }
+
        mod, err := testModule(opts, step)
        if err != nil {
                return state, err
@@ -33,17 +43,12 @@ func testStep(
        if err != nil {
                return state, fmt.Errorf("Error initializing context: %s", err)
        }
-       if ws, es := ctx.Validate(); len(ws) > 0 || len(es) > 0 {
-               if len(es) > 0 {
-                       estrs := make([]string, len(es))
-                       for i, e := range es {
-                               estrs[i] = e.Error()
-                       }
-                       return state, fmt.Errorf(
-                               "Configuration is invalid.\n\nWarnings: %#v\n\nErrors: %#v",
-                               ws, estrs)
+       if diags := ctx.Validate(); len(diags) > 0 {
+               if diags.HasErrors() {
+                       return nil, errwrap.Wrapf("config is invalid: {{err}}", diags.Err())
                }
-               log.Printf("[WARN] Config warnings: %#v", ws)
+
+               log.Printf("[WARN] Config warnings:\n%s", diags)
        }
 
        // Refresh!
@@ -158,3 +163,19 @@ func testStep(
        // Made it here? Good job test step!
        return state, nil
 }
+
+func testStepTaint(state *terraform.State, step TestStep) error {
+       for _, p := range step.Taint {
+               m := state.RootModule()
+               if m == nil {
+                       return errors.New("no state")
+               }
+               rs, ok := m.Resources[p]
+               if !ok {
+                       return fmt.Errorf("resource %q not found in state", p)
+               }
+               log.Printf("[WARN] Test: Explicitly tainting resource %q", p)
+               rs.Taint()
+       }
+       return nil
+}