github.com/adrian-bl/terraform@v0.7.0-rc2.0.20160705220747-de0a34fc3517/config/interpolate_test.go (about)

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