core: basic test of EvalVariableBlock

This previously lacked tests altogether. This new test verifies the
"happy path", ensuring that both literal and computed values pass through
correctly into the VariableValues map.
This commit is contained in:
Martin Atkins 2017-04-04 11:31:58 -07:00
parent c1c5c9a2f6
commit 28d6d913e4
2 changed files with 77 additions and 1 deletions

View File

@ -114,7 +114,6 @@ type EvalVariableBlock struct {
VariableValues map[string]interface{} VariableValues map[string]interface{}
} }
// TODO: test
func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) { func (n *EvalVariableBlock) Eval(ctx EvalContext) (interface{}, error) {
// Clear out the existing mapping // Clear out the existing mapping
for k, _ := range n.VariableValues { for k, _ := range n.VariableValues {

View File

@ -3,6 +3,8 @@ package terraform
import ( import (
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/terraform/config"
) )
func TestCoerceMapVariable(t *testing.T) { func TestCoerceMapVariable(t *testing.T) {
@ -140,3 +142,78 @@ func TestCoerceMapVariable(t *testing.T) {
} }
} }
} }
func TestEvalVariableBlock(t *testing.T) {
rc, err := config.NewRawConfig(map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []map[string]interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed_map": map[string]interface{}{},
"computed_list_of_maps": []map[string]interface{}{
map[string]interface{}{},
},
// No computed_list right now, because that isn't currently supported:
// EvalVariableBlock assumes the final step of the path will always
// be a map.
})
if err != nil {
t.Fatalf("config.NewRawConfig failed: %s", err)
}
cfg := NewResourceConfig(rc)
cfg.ComputedKeys = []string{
"computed",
"computed_map.foo",
"computed_list_of_maps.0.foo",
}
n := &EvalVariableBlock{
VariableValues: map[string]interface{}{
// Should be cleared out on Eval
"should_be_deleted": true,
},
Config: &cfg,
}
ctx := &MockEvalContext{}
val, err := n.Eval(ctx)
if err != nil {
t.Fatalf("n.Eval failed: %s", err)
}
if val != nil {
t.Fatalf("n.Eval returned non-nil result: %#v", val)
}
got := n.VariableValues
want := map[string]interface{}{
"known": "foo",
"known_list": []interface{}{"foo"},
"known_map": map[string]interface{}{
"foo": "foo",
},
"known_list_of_maps": []interface{}{
map[string]interface{}{
"foo": "foo",
},
},
"computed": config.UnknownVariableValue,
"computed_map": map[string]interface{}{
"foo": config.UnknownVariableValue,
},
"computed_list_of_maps": []interface{}{
map[string]interface{}{
"foo": config.UnknownVariableValue,
},
},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Incorrect variables\ngot: %#v\nwant: %#v", got, want)
}
}