github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/interpolate_funcs_test.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"reflect"
     8  	"testing"
     9  
    10  	"github.com/hashicorp/terraform/config/lang"
    11  	"github.com/hashicorp/terraform/config/lang/ast"
    12  )
    13  
    14  func TestInterpolateFuncConcat(t *testing.T) {
    15  	testFunction(t, testFunctionConfig{
    16  		Cases: []testFunctionCase{
    17  			{
    18  				`${concat("foo", "bar")}`,
    19  				"foobar",
    20  				false,
    21  			},
    22  
    23  			{
    24  				`${concat("foo")}`,
    25  				"foo",
    26  				false,
    27  			},
    28  
    29  			{
    30  				`${concat()}`,
    31  				nil,
    32  				true,
    33  			},
    34  		},
    35  	})
    36  }
    37  
    38  func TestInterpolateFuncFile(t *testing.T) {
    39  	tf, err := ioutil.TempFile("", "tf")
    40  	if err != nil {
    41  		t.Fatalf("err: %s", err)
    42  	}
    43  	path := tf.Name()
    44  	tf.Write([]byte("foo"))
    45  	tf.Close()
    46  	defer os.Remove(path)
    47  
    48  	testFunction(t, testFunctionConfig{
    49  		Cases: []testFunctionCase{
    50  			{
    51  				fmt.Sprintf(`${file("%s")}`, path),
    52  				"foo",
    53  				false,
    54  			},
    55  
    56  			// Invalid path
    57  			{
    58  				`${file("/i/dont/exist")}`,
    59  				nil,
    60  				true,
    61  			},
    62  
    63  			// Too many args
    64  			{
    65  				`${file("foo", "bar")}`,
    66  				nil,
    67  				true,
    68  			},
    69  		},
    70  	})
    71  }
    72  
    73  func TestInterpolateFuncJoin(t *testing.T) {
    74  	testFunction(t, testFunctionConfig{
    75  		Cases: []testFunctionCase{
    76  			{
    77  				`${join(",")}`,
    78  				nil,
    79  				true,
    80  			},
    81  
    82  			{
    83  				`${join(",", "foo")}`,
    84  				"foo",
    85  				false,
    86  			},
    87  
    88  			/*
    89  				TODO
    90  				{
    91  					`${join(",", "foo", "bar")}`,
    92  					"foo,bar",
    93  					false,
    94  				},
    95  			*/
    96  
    97  			{
    98  				fmt.Sprintf(`${join(".", "%s")}`,
    99  					fmt.Sprintf(
   100  						"foo%sbar%sbaz",
   101  						InterpSplitDelim,
   102  						InterpSplitDelim)),
   103  				"foo.bar.baz",
   104  				false,
   105  			},
   106  		},
   107  	})
   108  }
   109  
   110  func TestInterpolateFuncLookup(t *testing.T) {
   111  	testFunction(t, testFunctionConfig{
   112  		Vars: map[string]ast.Variable{
   113  			"var.foo.bar": ast.Variable{
   114  				Value: "baz",
   115  				Type:  ast.TypeString,
   116  			},
   117  		},
   118  		Cases: []testFunctionCase{
   119  			{
   120  				`${lookup("foo", "bar")}`,
   121  				"baz",
   122  				false,
   123  			},
   124  
   125  			// Invalid key
   126  			{
   127  				`${lookup("foo", "baz")}`,
   128  				nil,
   129  				true,
   130  			},
   131  
   132  			// Too many args
   133  			{
   134  				`${lookup("foo", "bar", "baz")}`,
   135  				nil,
   136  				true,
   137  			},
   138  		},
   139  	})
   140  }
   141  
   142  func TestInterpolateFuncElement(t *testing.T) {
   143  	testFunction(t, testFunctionConfig{
   144  		Cases: []testFunctionCase{
   145  			{
   146  				fmt.Sprintf(`${element("%s", "1")}`,
   147  					"foo"+InterpSplitDelim+"baz"),
   148  				"baz",
   149  				false,
   150  			},
   151  
   152  			{
   153  				`${element("foo", "0")}`,
   154  				"foo",
   155  				false,
   156  			},
   157  
   158  			// Invalid index should wrap vs. out-of-bounds
   159  			{
   160  				fmt.Sprintf(`${element("%s", "2")}`,
   161  					"foo"+InterpSplitDelim+"baz"),
   162  				"foo",
   163  				false,
   164  			},
   165  
   166  			// Too many args
   167  			{
   168  				fmt.Sprintf(`${element("%s", "0", "2")}`,
   169  					"foo"+InterpSplitDelim+"baz"),
   170  				nil,
   171  				true,
   172  			},
   173  		},
   174  	})
   175  }
   176  
   177  type testFunctionConfig struct {
   178  	Cases []testFunctionCase
   179  	Vars  map[string]ast.Variable
   180  }
   181  
   182  type testFunctionCase struct {
   183  	Input  string
   184  	Result interface{}
   185  	Error  bool
   186  }
   187  
   188  func testFunction(t *testing.T, config testFunctionConfig) {
   189  	for i, tc := range config.Cases {
   190  		ast, err := lang.Parse(tc.Input)
   191  		if err != nil {
   192  			t.Fatalf("%d: err: %s", i, err)
   193  		}
   194  
   195  		out, _, err := lang.Eval(ast, langEvalConfig(config.Vars))
   196  		if (err != nil) != tc.Error {
   197  			t.Fatalf("%d: err: %s", i, err)
   198  		}
   199  
   200  		if !reflect.DeepEqual(out, tc.Result) {
   201  			t.Fatalf("%d: bad: %#v", i, out)
   202  		}
   203  	}
   204  }