github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/config/interpolate_test.go (about)

     1  package config
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/hil"
     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  			"self.address",
    59  			&SelfVariable{
    60  				Field: "address",
    61  				key:   "self.address",
    62  			},
    63  			false,
    64  		},
    65  	}
    66  
    67  	for i, tc := range cases {
    68  		actual, err := NewInterpolatedVariable(tc.Input)
    69  		if err != nil != tc.Error {
    70  			t.Fatalf("%d. Error: %s", i, err)
    71  		}
    72  		if !reflect.DeepEqual(actual, tc.Result) {
    73  			t.Fatalf("%d bad: %#v", i, actual)
    74  		}
    75  	}
    76  }
    77  
    78  func TestNewResourceVariable(t *testing.T) {
    79  	v, err := NewResourceVariable("foo.bar.baz")
    80  	if err != nil {
    81  		t.Fatalf("err: %s", err)
    82  	}
    83  
    84  	if v.Mode != ManagedResourceMode {
    85  		t.Fatalf("bad: %#v", v)
    86  	}
    87  	if v.Type != "foo" {
    88  		t.Fatalf("bad: %#v", v)
    89  	}
    90  	if v.Name != "bar" {
    91  		t.Fatalf("bad: %#v", v)
    92  	}
    93  	if v.Field != "baz" {
    94  		t.Fatalf("bad: %#v", v)
    95  	}
    96  	if v.Multi {
    97  		t.Fatal("should not be multi")
    98  	}
    99  
   100  	if v.FullKey() != "foo.bar.baz" {
   101  		t.Fatalf("bad: %#v", v)
   102  	}
   103  }
   104  
   105  func TestNewResourceVariableData(t *testing.T) {
   106  	v, err := NewResourceVariable("data.foo.bar.baz")
   107  	if err != nil {
   108  		t.Fatalf("err: %s", err)
   109  	}
   110  
   111  	if v.Mode != DataResourceMode {
   112  		t.Fatalf("bad: %#v", v)
   113  	}
   114  	if v.Type != "foo" {
   115  		t.Fatalf("bad: %#v", v)
   116  	}
   117  	if v.Name != "bar" {
   118  		t.Fatalf("bad: %#v", v)
   119  	}
   120  	if v.Field != "baz" {
   121  		t.Fatalf("bad: %#v", v)
   122  	}
   123  	if v.Multi {
   124  		t.Fatal("should not be multi")
   125  	}
   126  
   127  	if v.FullKey() != "data.foo.bar.baz" {
   128  		t.Fatalf("bad: %#v", v)
   129  	}
   130  }
   131  
   132  func TestNewUserVariable(t *testing.T) {
   133  	v, err := NewUserVariable("var.bar")
   134  	if err != nil {
   135  		t.Fatalf("err: %s", err)
   136  	}
   137  
   138  	if v.Name != "bar" {
   139  		t.Fatalf("bad: %#v", v.Name)
   140  	}
   141  	if v.FullKey() != "var.bar" {
   142  		t.Fatalf("bad: %#v", v)
   143  	}
   144  }
   145  
   146  func TestNewUserVariable_map(t *testing.T) {
   147  	v, err := NewUserVariable("var.bar.baz")
   148  	if err != nil {
   149  		t.Fatalf("err: %s", err)
   150  	}
   151  
   152  	if v.Name != "bar" {
   153  		t.Fatalf("bad: %#v", v.Name)
   154  	}
   155  	if v.Elem != "baz" {
   156  		t.Fatalf("bad: %#v", v.Elem)
   157  	}
   158  	if v.FullKey() != "var.bar.baz" {
   159  		t.Fatalf("bad: %#v", v)
   160  	}
   161  }
   162  
   163  func TestResourceVariable_impl(t *testing.T) {
   164  	var _ InterpolatedVariable = new(ResourceVariable)
   165  }
   166  
   167  func TestResourceVariable_Multi(t *testing.T) {
   168  	v, err := NewResourceVariable("foo.bar.*.baz")
   169  	if err != nil {
   170  		t.Fatalf("err: %s", err)
   171  	}
   172  
   173  	if v.Type != "foo" {
   174  		t.Fatalf("bad: %#v", v)
   175  	}
   176  	if v.Name != "bar" {
   177  		t.Fatalf("bad: %#v", v)
   178  	}
   179  	if v.Field != "baz" {
   180  		t.Fatalf("bad: %#v", v)
   181  	}
   182  	if !v.Multi {
   183  		t.Fatal("should be multi")
   184  	}
   185  }
   186  
   187  func TestResourceVariable_MultiIndex(t *testing.T) {
   188  	cases := []struct {
   189  		Input string
   190  		Index int
   191  		Field string
   192  	}{
   193  		{"foo.bar.*.baz", -1, "baz"},
   194  		{"foo.bar.0.baz", 0, "baz"},
   195  		{"foo.bar.5.baz", 5, "baz"},
   196  	}
   197  
   198  	for _, tc := range cases {
   199  		v, err := NewResourceVariable(tc.Input)
   200  		if err != nil {
   201  			t.Fatalf("err: %s", err)
   202  		}
   203  		if !v.Multi {
   204  			t.Fatalf("should be multi: %s", tc.Input)
   205  		}
   206  		if v.Index != tc.Index {
   207  			t.Fatalf("bad: %d\n\n%s", v.Index, tc.Input)
   208  		}
   209  		if v.Field != tc.Field {
   210  			t.Fatalf("bad: %s\n\n%s", v.Field, tc.Input)
   211  		}
   212  	}
   213  }
   214  
   215  func TestUserVariable_impl(t *testing.T) {
   216  	var _ InterpolatedVariable = new(UserVariable)
   217  }
   218  
   219  func TestDetectVariables(t *testing.T) {
   220  	cases := []struct {
   221  		Input  string
   222  		Result []InterpolatedVariable
   223  	}{
   224  		{
   225  			"foo $${var.foo}",
   226  			nil,
   227  		},
   228  
   229  		{
   230  			"foo ${var.foo}",
   231  			[]InterpolatedVariable{
   232  				&UserVariable{
   233  					Name: "foo",
   234  					key:  "var.foo",
   235  				},
   236  			},
   237  		},
   238  
   239  		{
   240  			"foo ${var.foo} ${var.bar}",
   241  			[]InterpolatedVariable{
   242  				&UserVariable{
   243  					Name: "foo",
   244  					key:  "var.foo",
   245  				},
   246  				&UserVariable{
   247  					Name: "bar",
   248  					key:  "var.bar",
   249  				},
   250  			},
   251  		},
   252  	}
   253  
   254  	for _, tc := range cases {
   255  		ast, err := hil.Parse(tc.Input)
   256  		if err != nil {
   257  			t.Fatalf("%s\n\nInput: %s", err, tc.Input)
   258  		}
   259  
   260  		actual, err := DetectVariables(ast)
   261  		if err != nil {
   262  			t.Fatalf("err: %s", err)
   263  		}
   264  		if !reflect.DeepEqual(actual, tc.Result) {
   265  			t.Fatalf("bad: %#v\n\nInput: %s", actual, tc.Input)
   266  		}
   267  	}
   268  }