terraform: some tests for the eval nodes

This commit is contained in:
Mitchell Hashimoto 2015-02-03 19:06:12 -05:00
parent 128c07e504
commit ac92c67cba
7 changed files with 142 additions and 6 deletions

View File

@ -19,8 +19,10 @@ type EvalNode interface {
// and not used at runtime.
Args() ([]EvalNode, []EvalType)
// Eval evaluates this node with the given context.
Eval(EvalContext) (interface{}, error)
// Eval evaluates this node with the given context. The second parameter
// are the argument values. These will match in order and 1-1 with the
// results of the Args() return value.
Eval(EvalContext, []interface{}) (interface{}, error)
// Type returns the type that will be returned by this node.
Type() EvalType
@ -31,3 +33,28 @@ type EvalNode interface {
type GraphNodeEvalable interface {
EvalTree() EvalNode
}
// MockEvalContext is a mock version of EvalContext that can be used
// for tests.
type MockEvalContext struct {
InitProviderCalled bool
InitProviderName string
InitProviderProvider ResourceProvider
InitProviderError error
ProviderCalled bool
ProviderName string
ProviderProvider ResourceProvider
}
func (c *MockEvalContext) InitProvider(n string) (ResourceProvider, error) {
c.InitProviderCalled = true
c.InitProviderName = n
return c.InitProviderProvider, c.InitProviderError
}
func (c *MockEvalContext) Provider(n string) ResourceProvider {
c.ProviderCalled = true
c.ProviderName = n
return c.ProviderProvider
}

View File

@ -14,7 +14,8 @@ func (n *EvalInterpolate) Args() ([]EvalNode, []EvalType) {
return nil, nil
}
func (n *EvalInterpolate) Eval(ctx EvalContext) (interface{}, error) {
func (n *EvalInterpolate) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
return nil, nil
}

20
terraform/eval_literal.go Normal file
View File

@ -0,0 +1,20 @@
package terraform
// EvalLiteral is an EvalNode implementation that returns a literal
// value. This is very useful for testing as well as in practice.
type EvalLiteral struct {
Value interface{}
ValueType EvalType
}
func (n *EvalLiteral) Args() ([]EvalNode, []EvalType) {
return nil, nil
}
func (n *EvalLiteral) Eval(EvalContext, []interface{}) (interface{}, error) {
return n.Value, nil
}
func (n *EvalLiteral) Type() EvalType {
return n.ValueType
}

View File

@ -0,0 +1,27 @@
package terraform
import (
"testing"
)
func TestEvalLiteral_impl(t *testing.T) {
var _ EvalNode = new(EvalLiteral)
}
func TestEvalLiteralEval(t *testing.T) {
n := &EvalLiteral{Value: 42}
actual, err := n.Eval(nil, nil)
if err != nil {
t.Fatalf("err: %s", err)
}
if actual != 42 {
t.Fatalf("bad: %#v", actual)
}
}
func TestEvalLiteralType(t *testing.T) {
n := &EvalLiteral{Value: 42, ValueType: EvalTypeConfig}
if actual := n.Type(); actual != EvalTypeConfig {
t.Fatalf("bad: %#v", actual)
}
}

View File

@ -16,7 +16,8 @@ func (n *EvalConfigProvider) Args() ([]EvalNode, []EvalType) {
[]EvalType{EvalTypeResourceProvider, EvalTypeConfig}
}
func (n *EvalConfigProvider) Eval(ctx EvalContext) (interface{}, error) {
func (n *EvalConfigProvider) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
return nil, nil
}
@ -35,7 +36,8 @@ func (n *EvalInitProvider) Args() ([]EvalNode, []EvalType) {
return nil, nil
}
func (n *EvalInitProvider) Eval(ctx EvalContext) (interface{}, error) {
func (n *EvalInitProvider) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
return ctx.InitProvider(n.Name)
}
@ -53,7 +55,8 @@ func (n *EvalGetProvider) Args() ([]EvalNode, []EvalType) {
return nil, nil
}
func (n *EvalGetProvider) Eval(ctx EvalContext) (interface{}, error) {
func (n *EvalGetProvider) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) {
result := ctx.Provider(n.Name)
if result == nil {
return nil, fmt.Errorf("provider %s not initialized", n.Name)

View File

@ -0,0 +1,49 @@
package terraform
import (
"testing"
)
func TestEvalInitProvider_impl(t *testing.T) {
var _ EvalNode = new(EvalInitProvider)
}
func TestEvalInitProvider(t *testing.T) {
n := &EvalInitProvider{Name: "foo"}
provider := &MockResourceProvider{}
ctx := &MockEvalContext{InitProviderProvider: provider}
if actual, err := n.Eval(ctx, nil); err != nil {
t.Fatalf("err: %s", err)
} else if actual != provider {
t.Fatalf("bad: %#v", actual)
}
if !ctx.InitProviderCalled {
t.Fatal("should be called")
}
if ctx.InitProviderName != "foo" {
t.Fatalf("bad: %#v", ctx.InitProviderName)
}
}
func TestEvalGetProvider_impl(t *testing.T) {
var _ EvalNode = new(EvalGetProvider)
}
func TestEvalGetProvider(t *testing.T) {
n := &EvalGetProvider{Name: "foo"}
provider := &MockResourceProvider{}
ctx := &MockEvalContext{ProviderProvider: provider}
if actual, err := n.Eval(ctx, nil); err != nil {
t.Fatalf("err: %s", err)
} else if actual != provider {
t.Fatalf("bad: %#v", actual)
}
if !ctx.ProviderCalled {
t.Fatal("should be called")
}
if ctx.ProviderName != "foo" {
t.Fatalf("bad: %#v", ctx.ProviderName)
}
}

9
terraform/eval_test.go Normal file
View File

@ -0,0 +1,9 @@
package terraform
import (
"testing"
)
func TestMockEvalContext_impl(t *testing.T) {
var _ EvalContext = new(MockEvalContext)
}