terraform: all eval nodes tested

This commit is contained in:
Mitchell Hashimoto 2015-02-03 19:48:38 -05:00
parent c2df70e499
commit 3f0eb528de
3 changed files with 56 additions and 1 deletions

View File

@ -1,5 +1,9 @@
package terraform package terraform
import (
"github.com/hashicorp/terraform/config"
)
// EvalContext is the interface that is given to eval nodes to execute. // EvalContext is the interface that is given to eval nodes to execute.
type EvalContext interface { type EvalContext interface {
// InitProvider initializes the provider with the given name and // InitProvider initializes the provider with the given name and
@ -9,6 +13,10 @@ type EvalContext interface {
// Provider gets the provider instance with the given name (already // Provider gets the provider instance with the given name (already
// initialized) or returns nil if the provider isn't initialized. // initialized) or returns nil if the provider isn't initialized.
Provider(string) ResourceProvider Provider(string) ResourceProvider
// Interpolate takes the given raw configuration and completes
// the interpolations, returning the processed ResourceConfig.
Interpolate(*config.RawConfig) (*ResourceConfig, error)
} }
// EvalNode is the interface that must be implemented by graph nodes to // EvalNode is the interface that must be implemented by graph nodes to
@ -45,6 +53,11 @@ type MockEvalContext struct {
ProviderCalled bool ProviderCalled bool
ProviderName string ProviderName string
ProviderProvider ResourceProvider ProviderProvider ResourceProvider
InterpolateCalled bool
InterpolateConfig *config.RawConfig
InterpolateConfigResult *ResourceConfig
InterpolateError error
} }
func (c *MockEvalContext) InitProvider(n string) (ResourceProvider, error) { func (c *MockEvalContext) InitProvider(n string) (ResourceProvider, error) {
@ -58,3 +71,10 @@ func (c *MockEvalContext) Provider(n string) ResourceProvider {
c.ProviderName = n c.ProviderName = n
return c.ProviderProvider return c.ProviderProvider
} }
func (c *MockEvalContext) Interpolate(
config *config.RawConfig) (*ResourceConfig, error) {
c.InterpolateCalled = true
c.InterpolateConfig = config
return c.InterpolateConfigResult, c.InterpolateError
}

View File

@ -16,7 +16,7 @@ func (n *EvalInterpolate) Args() ([]EvalNode, []EvalType) {
func (n *EvalInterpolate) Eval( func (n *EvalInterpolate) Eval(
ctx EvalContext, args []interface{}) (interface{}, error) { ctx EvalContext, args []interface{}) (interface{}, error) {
return nil, nil return ctx.Interpolate(n.Config)
} }
func (n *EvalInterpolate) Type() EvalType { func (n *EvalInterpolate) Type() EvalType {

View File

@ -0,0 +1,35 @@
package terraform
import (
"reflect"
"testing"
"github.com/hashicorp/terraform/config"
)
func TestEvalInterpolate_impl(t *testing.T) {
var _ EvalNode = new(EvalInterpolate)
}
func TestEvalInterpolate(t *testing.T) {
config, err := config.NewRawConfig(map[string]interface{}{})
if err != nil {
t.Fatalf("err: %s", err)
}
n := &EvalInterpolate{Config: config}
result := testResourceConfig(t, map[string]interface{}{})
ctx := &MockEvalContext{InterpolateConfigResult: result}
if actual, err := n.Eval(ctx, nil); err != nil {
t.Fatalf("err: %s", err)
} else if actual != result {
t.Fatalf("bad: %#v", actual)
}
if !ctx.InterpolateCalled {
t.Fatal("should be called")
}
if !reflect.DeepEqual(ctx.InterpolateConfig, config) {
t.Fatalf("bad: %#v", ctx.InterpolateConfig)
}
}