Merge pull request #7145 from hashicorp/b-concat-lists-of-maps-panic

core: Fix panic on concat() w/ list of nonprimitives
This commit is contained in:
Paul Hinze 2016-06-12 13:40:46 -05:00 committed by GitHub
commit a4b06d6833
2 changed files with 43 additions and 2 deletions

View File

@ -240,12 +240,18 @@ func interpolationFuncConcat() ast.Function {
// Otherwise variables // Otherwise variables
if argument, ok := arg.([]ast.Variable); ok { if argument, ok := arg.([]ast.Variable); ok {
for _, element := range argument { for _, element := range argument {
finalListElements = append(finalListElements, element.Value.(string)) t := element.Type
switch t {
case ast.TypeString:
finalListElements = append(finalListElements, element.Value.(string))
default:
return nil, fmt.Errorf("concat() does not support lists of %s", t.Printable())
}
} }
continue continue
} }
return nil, fmt.Errorf("arguments to concat() must be a string or list") return nil, fmt.Errorf("arguments to concat() must be a string or list of strings")
} }
return stringSliceToVariableValue(finalListElements), nil return stringSliceToVariableValue(finalListElements), nil

View File

@ -5,6 +5,7 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/hashicorp/hil" "github.com/hashicorp/hil"
@ -226,6 +227,40 @@ func TestInterpolateFuncConcat(t *testing.T) {
}) })
} }
// TODO: This test is split out and calls a private function
// because there's no good way to get a list of maps into the unit
// tests due to GH-7142 - once lists of maps can be expressed properly as
// literals this unit test can be wrapped back into the suite above.
//
// Reproduces crash reported in GH-7030.
func TestInterpolationFuncConcatListOfMaps(t *testing.T) {
listOfMapsOne := ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeMap,
Value: map[string]interface{}{"one": "foo"},
},
},
}
listOfMapsTwo := ast.Variable{
Type: ast.TypeList,
Value: []ast.Variable{
{
Type: ast.TypeMap,
Value: map[string]interface{}{"two": "bar"},
},
},
}
args := []interface{}{listOfMapsOne.Value, listOfMapsTwo.Value}
_, err := interpolationFuncConcat().Callback(args)
if err == nil || !strings.Contains(err.Error(), "concat() does not support lists of type map") {
t.Fatalf("Expected err, got: %v", err)
}
}
func TestInterpolateFuncFile(t *testing.T) { func TestInterpolateFuncFile(t *testing.T) {
tf, err := ioutil.TempFile("", "tf") tf, err := ioutil.TempFile("", "tf")
if err != nil { if err != nil {