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