package resource
import (
+ "errors"
"fmt"
"log"
"strings"
+ "github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/terraform"
)
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
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!
// 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
+}