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

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/config/lang"
     8  )
     9  
    10  func TestNewInterpolatedVariable(t *testing.T) {
    11  	cases := []struct {
    12  		Input  string
    13  		Result InterpolatedVariable
    14  		Error  bool
    15  	}{
    16  		{
    17  			"var.foo",
    18  			&UserVariable{
    19  				Name: "foo",
    20  				key:  "var.foo",
    21  			},
    22  			false,
    23  		},
    24  		{
    25  			"module.foo.bar",
    26  			&ModuleVariable{
    27  				Name:  "foo",
    28  				Field: "bar",
    29  				key:   "module.foo.bar",
    30  			},
    31  			false,
    32  		},
    33  		{
    34  			"count.index",
    35  			&CountVariable{
    36  				Type: CountValueIndex,
    37  				key:  "count.index",
    38  			},
    39  			false,
    40  		},
    41  		{
    42  			"count.nope",
    43  			&CountVariable{
    44  				Type: CountValueInvalid,
    45  				key:  "count.nope",
    46  			},
    47  			false,
    48  		},
    49  		{
    50  			"path.module",
    51  			&PathVariable{
    52  				Type: PathValueModule,
    53  				key:  "path.module",
    54  			},
    55  			false,
    56  		},
    57  	}
    58  
    59  	for i, tc := range cases {
    60  		actual, err := NewInterpolatedVariable(tc.Input)
    61  		if (err != nil) != tc.Error {
    62  			t.Fatalf("%d. Error: %s", i, err)
    63  		}
    64  		if !reflect.DeepEqual(actual, tc.Result) {
    65  			t.Fatalf("%d bad: %#v", i, actual)
    66  		}
    67  	}
    68  }
    69  
    70  func TestNewResourceVariable(t *testing.T) {
    71  	v, err := NewResourceVariable("foo.bar.baz")
    72  	if err != nil {
    73  		t.Fatalf("err: %s", err)
    74  	}
    75  
    76  	if v.Type != "foo" {
    77  		t.Fatalf("bad: %#v", v)
    78  	}
    79  	if v.Name != "bar" {
    80  		t.Fatalf("bad: %#v", v)
    81  	}
    82  	if v.Field != "baz" {
    83  		t.Fatalf("bad: %#v", v)
    84  	}
    85  	if v.Multi {
    86  		t.Fatal("should not be multi")
    87  	}
    88  
    89  	if v.FullKey() != "foo.bar.baz" {
    90  		t.Fatalf("bad: %#v", v)
    91  	}
    92  }
    93  
    94  func TestNewUserVariable(t *testing.T) {
    95  	v, err := NewUserVariable("var.bar")
    96  	if err != nil {
    97  		t.Fatalf("err: %s", err)
    98  	}
    99  
   100  	if v.Name != "bar" {
   101  		t.Fatalf("bad: %#v", v.Name)
   102  	}
   103  	if v.FullKey() != "var.bar" {
   104  		t.Fatalf("bad: %#v", v)
   105  	}
   106  }
   107  
   108  func TestNewUserVariable_map(t *testing.T) {
   109  	v, err := NewUserVariable("var.bar.baz")
   110  	if err != nil {
   111  		t.Fatalf("err: %s", err)
   112  	}
   113  
   114  	if v.Name != "bar" {
   115  		t.Fatalf("bad: %#v", v.Name)
   116  	}
   117  	if v.Elem != "baz" {
   118  		t.Fatalf("bad: %#v", v.Elem)
   119  	}
   120  	if v.FullKey() != "var.bar.baz" {
   121  		t.Fatalf("bad: %#v", v)
   122  	}
   123  }
   124  
   125  func TestResourceVariable_impl(t *testing.T) {
   126  	var _ InterpolatedVariable = new(ResourceVariable)
   127  }
   128  
   129  func TestResourceVariable_Multi(t *testing.T) {
   130  	v, err := NewResourceVariable("foo.bar.*.baz")
   131  	if err != nil {
   132  		t.Fatalf("err: %s", err)
   133  	}
   134  
   135  	if v.Type != "foo" {
   136  		t.Fatalf("bad: %#v", v)
   137  	}
   138  	if v.Name != "bar" {
   139  		t.Fatalf("bad: %#v", v)
   140  	}
   141  	if v.Field != "baz" {
   142  		t.Fatalf("bad: %#v", v)
   143  	}
   144  	if !v.Multi {
   145  		t.Fatal("should be multi")
   146  	}
   147  }
   148  
   149  func TestResourceVariable_MultiIndex(t *testing.T) {
   150  	cases := []struct {
   151  		Input string
   152  		Index int
   153  		Field string
   154  	}{
   155  		{"foo.bar.*.baz", -1, "baz"},
   156  		{"foo.bar.0.baz", 0, "baz"},
   157  		{"foo.bar.5.baz", 5, "baz"},
   158  	}
   159  
   160  	for _, tc := range cases {
   161  		v, err := NewResourceVariable(tc.Input)
   162  		if err != nil {
   163  			t.Fatalf("err: %s", err)
   164  		}
   165  		if !v.Multi {
   166  			t.Fatalf("should be multi: %s", tc.Input)
   167  		}
   168  		if v.Index != tc.Index {
   169  			t.Fatalf("bad: %d\n\n%s", v.Index, tc.Input)
   170  		}
   171  		if v.Field != tc.Field {
   172  			t.Fatalf("bad: %s\n\n%s", v.Field, tc.Input)
   173  		}
   174  	}
   175  }
   176  
   177  func TestUserVariable_impl(t *testing.T) {
   178  	var _ InterpolatedVariable = new(UserVariable)
   179  }
   180  
   181  func TestDetectVariables(t *testing.T) {
   182  	cases := []struct {
   183  		Input  string
   184  		Result []InterpolatedVariable
   185  	}{
   186  		{
   187  			"foo $${var.foo}",
   188  			nil,
   189  		},
   190  
   191  		{
   192  			"foo ${var.foo}",
   193  			[]InterpolatedVariable{
   194  				&UserVariable{
   195  					Name: "foo",
   196  					key:  "var.foo",
   197  				},
   198  			},
   199  		},
   200  
   201  		{
   202  			"foo ${var.foo} ${var.bar}",
   203  			[]InterpolatedVariable{
   204  				&UserVariable{
   205  					Name: "foo",
   206  					key:  "var.foo",
   207  				},
   208  				&UserVariable{
   209  					Name: "bar",
   210  					key:  "var.bar",
   211  				},
   212  			},
   213  		},
   214  	}
   215  
   216  	for _, tc := range cases {
   217  		ast, err := lang.Parse(tc.Input)
   218  		if err != nil {
   219  			t.Fatalf("%s\n\nInput: %s", err, tc.Input)
   220  		}
   221  
   222  		actual, err := DetectVariables(ast)
   223  		if err != nil {
   224  			t.Fatalf("err: %s", err)
   225  		}
   226  		if !reflect.DeepEqual(actual, tc.Result) {
   227  			t.Fatalf("bad: %#v\n\nInput: %s", actual, tc.Input)
   228  		}
   229  	}
   230  }