terraform: more passing tests

This commit is contained in:
Mitchell Hashimoto 2015-02-07 09:53:46 -08:00
parent 5b0004ffc7
commit d0786d20dd
2 changed files with 81 additions and 62 deletions

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform/config/module" "github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/dag" "github.com/hashicorp/terraform/dag"
) )
@ -67,11 +69,20 @@ func (c *Context2) GraphBuilder() GraphBuilder {
// Validate validates the configuration and returns any warnings or errors. // Validate validates the configuration and returns any warnings or errors.
func (c *Context2) Validate() ([]string, []error) { func (c *Context2) Validate() ([]string, []error) {
var warns []string var warns []string
var errs []error var errs error
// Validate the configuration itself // Validate the configuration itself
if err := c.module.Validate(); err != nil { if err := c.module.Validate(); err != nil {
errs = append(errs, err) errs = multierror.Append(errs, err)
}
// This only needs to be done for the root module, since inter-module
// variables are validated in the module tree.
if config := c.module.Config(); config != nil {
// Validate the user variables
if err := smcUserVariables(config, c.variables); len(err) > 0 {
errs = multierror.Append(errs, err...)
}
} }
evalCtx := c.evalContext(walkValidate) evalCtx := c.evalContext(walkValidate)
@ -105,15 +116,23 @@ func (c *Context2) Validate() ([]string, []error) {
verr, ok := err.(*EvalValidateError) verr, ok := err.(*EvalValidateError)
if !ok { if !ok {
errs = append(errs, err) errs = multierror.Append(errs, err)
return return
} }
warns = append(warns, verr.Warnings...) warns = append(warns, verr.Warnings...)
errs = append(errs, verr.Errors...) errs = multierror.Append(errs, verr.Errors...)
}) })
return warns, errs // Get the actual list of errors out. We do this by checking if the
// error is a error wrapper (multierror is one) and grabbing all the
// wrapped errors.
var rerrs []error
if w, ok := errs.(errwrap.Wrapper); ok {
rerrs = w.WrappedErrors()
}
return warns, rerrs
} }
func (c *Context2) evalContext(op walkOperation) *BuiltinEvalContext { func (c *Context2) evalContext(op walkOperation) *BuiltinEvalContext {

View File

@ -167,6 +167,63 @@ func TestContext2Validate_requiredVar(t *testing.T) {
} }
} }
func TestContext2Validate_selfRef(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref")
c := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %s", e)
}
}
func TestContext2Validate_selfRefMulti(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi")
c := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContext2Validate_selfRefMultiAll(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi-all")
c := testContext2(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
/* /*
func TestContextValidate_goodModule(t *testing.T) { func TestContextValidate_goodModule(t *testing.T) {
p := testProvider("aws") p := testProvider("aws")
@ -468,63 +525,6 @@ func TestContextValidate_provisionerConfig_good(t *testing.T) {
} }
} }
func TestContextValidate_selfRef(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref")
c := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_selfRefMulti(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi")
c := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_selfRefMultiAll(t *testing.T) {
p := testProvider("aws")
m := testModule(t, "validate-self-ref-multi-all")
c := testContext(t, &ContextOpts{
Module: m,
Providers: map[string]ResourceProviderFactory{
"aws": testProviderFuncFixed(p),
},
})
w, e := c.Validate()
if len(w) > 0 {
t.Fatalf("bad: %#v", w)
}
if len(e) == 0 {
t.Fatalf("bad: %#v", e)
}
}
func TestContextValidate_varRef(t *testing.T) { func TestContextValidate_varRef(t *testing.T) {
m := testModule(t, "validate-variable-ref") m := testModule(t, "validate-variable-ref")
p := testProvider("aws") p := testProvider("aws")