github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/terraform/resource_address_test.go (about)

     1  package terraform
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestParseResourceAddress(t *testing.T) {
     9  	cases := map[string]struct {
    10  		Input    string
    11  		Expected *ResourceAddress
    12  	}{
    13  		"implicit primary, no specific index": {
    14  			Input: "aws_instance.foo",
    15  			Expected: &ResourceAddress{
    16  				Type:         "aws_instance",
    17  				Name:         "foo",
    18  				InstanceType: TypePrimary,
    19  				Index:        -1,
    20  			},
    21  		},
    22  		"implicit primary, explicit index": {
    23  			Input: "aws_instance.foo[2]",
    24  			Expected: &ResourceAddress{
    25  				Type:         "aws_instance",
    26  				Name:         "foo",
    27  				InstanceType: TypePrimary,
    28  				Index:        2,
    29  			},
    30  		},
    31  		"explicit primary, explicit index": {
    32  			Input: "aws_instance.foo.primary[2]",
    33  			Expected: &ResourceAddress{
    34  				Type:         "aws_instance",
    35  				Name:         "foo",
    36  				InstanceType: TypePrimary,
    37  				Index:        2,
    38  			},
    39  		},
    40  		"tainted": {
    41  			Input: "aws_instance.foo.tainted",
    42  			Expected: &ResourceAddress{
    43  				Type:         "aws_instance",
    44  				Name:         "foo",
    45  				InstanceType: TypeTainted,
    46  				Index:        -1,
    47  			},
    48  		},
    49  		"deposed": {
    50  			Input: "aws_instance.foo.deposed",
    51  			Expected: &ResourceAddress{
    52  				Type:         "aws_instance",
    53  				Name:         "foo",
    54  				InstanceType: TypeDeposed,
    55  				Index:        -1,
    56  			},
    57  		},
    58  		"with a hyphen": {
    59  			Input: "aws_instance.foo-bar",
    60  			Expected: &ResourceAddress{
    61  				Type:         "aws_instance",
    62  				Name:         "foo-bar",
    63  				InstanceType: TypePrimary,
    64  				Index:        -1,
    65  			},
    66  		},
    67  		"in a module": {
    68  			Input: "module.child.aws_instance.foo",
    69  			Expected: &ResourceAddress{
    70  				Path:         []string{"child"},
    71  				Type:         "aws_instance",
    72  				Name:         "foo",
    73  				InstanceType: TypePrimary,
    74  				Index:        -1,
    75  			},
    76  		},
    77  		"nested modules": {
    78  			Input: "module.a.module.b.module.forever.aws_instance.foo",
    79  			Expected: &ResourceAddress{
    80  				Path:         []string{"a", "b", "forever"},
    81  				Type:         "aws_instance",
    82  				Name:         "foo",
    83  				InstanceType: TypePrimary,
    84  				Index:        -1,
    85  			},
    86  		},
    87  		"just a module": {
    88  			Input: "module.a",
    89  			Expected: &ResourceAddress{
    90  				Path:         []string{"a"},
    91  				Type:         "",
    92  				Name:         "",
    93  				InstanceType: TypePrimary,
    94  				Index:        -1,
    95  			},
    96  		},
    97  		"just a nested module": {
    98  			Input: "module.a.module.b",
    99  			Expected: &ResourceAddress{
   100  				Path:         []string{"a", "b"},
   101  				Type:         "",
   102  				Name:         "",
   103  				InstanceType: TypePrimary,
   104  				Index:        -1,
   105  			},
   106  		},
   107  	}
   108  
   109  	for tn, tc := range cases {
   110  		out, err := ParseResourceAddress(tc.Input)
   111  		if err != nil {
   112  			t.Fatalf("unexpected err: %#v", err)
   113  		}
   114  
   115  		if !reflect.DeepEqual(out, tc.Expected) {
   116  			t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
   117  		}
   118  	}
   119  }
   120  
   121  func TestResourceAddressEquals(t *testing.T) {
   122  	cases := map[string]struct {
   123  		Address *ResourceAddress
   124  		Other   interface{}
   125  		Expect  bool
   126  	}{
   127  		"basic match": {
   128  			Address: &ResourceAddress{
   129  				Type:         "aws_instance",
   130  				Name:         "foo",
   131  				InstanceType: TypePrimary,
   132  				Index:        0,
   133  			},
   134  			Other: &ResourceAddress{
   135  				Type:         "aws_instance",
   136  				Name:         "foo",
   137  				InstanceType: TypePrimary,
   138  				Index:        0,
   139  			},
   140  			Expect: true,
   141  		},
   142  		"address does not set index": {
   143  			Address: &ResourceAddress{
   144  				Type:         "aws_instance",
   145  				Name:         "foo",
   146  				InstanceType: TypePrimary,
   147  				Index:        -1,
   148  			},
   149  			Other: &ResourceAddress{
   150  				Type:         "aws_instance",
   151  				Name:         "foo",
   152  				InstanceType: TypePrimary,
   153  				Index:        3,
   154  			},
   155  			Expect: true,
   156  		},
   157  		"other does not set index": {
   158  			Address: &ResourceAddress{
   159  				Type:         "aws_instance",
   160  				Name:         "foo",
   161  				InstanceType: TypePrimary,
   162  				Index:        3,
   163  			},
   164  			Other: &ResourceAddress{
   165  				Type:         "aws_instance",
   166  				Name:         "foo",
   167  				InstanceType: TypePrimary,
   168  				Index:        -1,
   169  			},
   170  			Expect: true,
   171  		},
   172  		"neither sets index": {
   173  			Address: &ResourceAddress{
   174  				Type:         "aws_instance",
   175  				Name:         "foo",
   176  				InstanceType: TypePrimary,
   177  				Index:        -1,
   178  			},
   179  			Other: &ResourceAddress{
   180  				Type:         "aws_instance",
   181  				Name:         "foo",
   182  				InstanceType: TypePrimary,
   183  				Index:        -1,
   184  			},
   185  			Expect: true,
   186  		},
   187  		"different type": {
   188  			Address: &ResourceAddress{
   189  				Type:         "aws_instance",
   190  				Name:         "foo",
   191  				InstanceType: TypePrimary,
   192  				Index:        0,
   193  			},
   194  			Other: &ResourceAddress{
   195  				Type:         "aws_vpc",
   196  				Name:         "foo",
   197  				InstanceType: TypePrimary,
   198  				Index:        0,
   199  			},
   200  			Expect: false,
   201  		},
   202  		"different name": {
   203  			Address: &ResourceAddress{
   204  				Type:         "aws_instance",
   205  				Name:         "foo",
   206  				InstanceType: TypePrimary,
   207  				Index:        0,
   208  			},
   209  			Other: &ResourceAddress{
   210  				Type:         "aws_instance",
   211  				Name:         "bar",
   212  				InstanceType: TypePrimary,
   213  				Index:        0,
   214  			},
   215  			Expect: false,
   216  		},
   217  		"different instance type": {
   218  			Address: &ResourceAddress{
   219  				Type:         "aws_instance",
   220  				Name:         "foo",
   221  				InstanceType: TypePrimary,
   222  				Index:        0,
   223  			},
   224  			Other: &ResourceAddress{
   225  				Type:         "aws_instance",
   226  				Name:         "foo",
   227  				InstanceType: TypeTainted,
   228  				Index:        0,
   229  			},
   230  			Expect: false,
   231  		},
   232  		"different index": {
   233  			Address: &ResourceAddress{
   234  				Type:         "aws_instance",
   235  				Name:         "foo",
   236  				InstanceType: TypePrimary,
   237  				Index:        0,
   238  			},
   239  			Other: &ResourceAddress{
   240  				Type:         "aws_instance",
   241  				Name:         "foo",
   242  				InstanceType: TypePrimary,
   243  				Index:        1,
   244  			},
   245  			Expect: false,
   246  		},
   247  		"module address matches address of resource inside module": {
   248  			Address: &ResourceAddress{
   249  				Path:         []string{"a", "b"},
   250  				Type:         "",
   251  				Name:         "",
   252  				InstanceType: TypePrimary,
   253  				Index:        -1,
   254  			},
   255  			Other: &ResourceAddress{
   256  				Path:         []string{"a", "b"},
   257  				Type:         "aws_instance",
   258  				Name:         "foo",
   259  				InstanceType: TypePrimary,
   260  				Index:        0,
   261  			},
   262  			Expect: true,
   263  		},
   264  		"module address doesn't match resource outside module": {
   265  			Address: &ResourceAddress{
   266  				Path:         []string{"a", "b"},
   267  				Type:         "",
   268  				Name:         "",
   269  				InstanceType: TypePrimary,
   270  				Index:        -1,
   271  			},
   272  			Other: &ResourceAddress{
   273  				Path:         []string{"a"},
   274  				Type:         "aws_instance",
   275  				Name:         "foo",
   276  				InstanceType: TypePrimary,
   277  				Index:        0,
   278  			},
   279  			Expect: false,
   280  		},
   281  		"nil path vs empty path should match": {
   282  			Address: &ResourceAddress{
   283  				Path:         []string{},
   284  				Type:         "aws_instance",
   285  				Name:         "foo",
   286  				InstanceType: TypePrimary,
   287  				Index:        -1,
   288  			},
   289  			Other: &ResourceAddress{
   290  				Path:         nil,
   291  				Type:         "aws_instance",
   292  				Name:         "foo",
   293  				InstanceType: TypePrimary,
   294  				Index:        0,
   295  			},
   296  			Expect: true,
   297  		},
   298  	}
   299  
   300  	for tn, tc := range cases {
   301  		actual := tc.Address.Equals(tc.Other)
   302  		if actual != tc.Expect {
   303  			t.Fatalf("%q: expected equals: %t, got %t for:\n%#v\n%#v",
   304  				tn, tc.Expect, actual, tc.Address, tc.Other)
   305  		}
   306  	}
   307  }