github.com/tarrant/terraform@v0.3.8-0.20150402012457-f68c9eee638e/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  	}
    59  
    60  	for tn, tc := range cases {
    61  		out, err := ParseResourceAddress(tc.Input)
    62  		if err != nil {
    63  			t.Fatalf("unexpected err: %#v", err)
    64  		}
    65  
    66  		if !reflect.DeepEqual(out, tc.Expected) {
    67  			t.Fatalf("bad: %q\n\nexpected:\n%#v\n\ngot:\n%#v", tn, tc.Expected, out)
    68  		}
    69  	}
    70  }
    71  
    72  func TestResourceAddressEquals(t *testing.T) {
    73  	cases := map[string]struct {
    74  		Address *ResourceAddress
    75  		Other   interface{}
    76  		Expect  bool
    77  	}{
    78  		"basic match": {
    79  			Address: &ResourceAddress{
    80  				Type:         "aws_instance",
    81  				Name:         "foo",
    82  				InstanceType: TypePrimary,
    83  				Index:        0,
    84  			},
    85  			Other: &ResourceAddress{
    86  				Type:         "aws_instance",
    87  				Name:         "foo",
    88  				InstanceType: TypePrimary,
    89  				Index:        0,
    90  			},
    91  			Expect: true,
    92  		},
    93  		"address does not set index": {
    94  			Address: &ResourceAddress{
    95  				Type:         "aws_instance",
    96  				Name:         "foo",
    97  				InstanceType: TypePrimary,
    98  				Index:        -1,
    99  			},
   100  			Other: &ResourceAddress{
   101  				Type:         "aws_instance",
   102  				Name:         "foo",
   103  				InstanceType: TypePrimary,
   104  				Index:        3,
   105  			},
   106  			Expect: true,
   107  		},
   108  		"other does not set index": {
   109  			Address: &ResourceAddress{
   110  				Type:         "aws_instance",
   111  				Name:         "foo",
   112  				InstanceType: TypePrimary,
   113  				Index:        3,
   114  			},
   115  			Other: &ResourceAddress{
   116  				Type:         "aws_instance",
   117  				Name:         "foo",
   118  				InstanceType: TypePrimary,
   119  				Index:        -1,
   120  			},
   121  			Expect: true,
   122  		},
   123  		"neither sets index": {
   124  			Address: &ResourceAddress{
   125  				Type:         "aws_instance",
   126  				Name:         "foo",
   127  				InstanceType: TypePrimary,
   128  				Index:        -1,
   129  			},
   130  			Other: &ResourceAddress{
   131  				Type:         "aws_instance",
   132  				Name:         "foo",
   133  				InstanceType: TypePrimary,
   134  				Index:        -1,
   135  			},
   136  			Expect: true,
   137  		},
   138  		"different type": {
   139  			Address: &ResourceAddress{
   140  				Type:         "aws_instance",
   141  				Name:         "foo",
   142  				InstanceType: TypePrimary,
   143  				Index:        0,
   144  			},
   145  			Other: &ResourceAddress{
   146  				Type:         "aws_vpc",
   147  				Name:         "foo",
   148  				InstanceType: TypePrimary,
   149  				Index:        0,
   150  			},
   151  			Expect: false,
   152  		},
   153  		"different name": {
   154  			Address: &ResourceAddress{
   155  				Type:         "aws_instance",
   156  				Name:         "foo",
   157  				InstanceType: TypePrimary,
   158  				Index:        0,
   159  			},
   160  			Other: &ResourceAddress{
   161  				Type:         "aws_instance",
   162  				Name:         "bar",
   163  				InstanceType: TypePrimary,
   164  				Index:        0,
   165  			},
   166  			Expect: false,
   167  		},
   168  		"different instance type": {
   169  			Address: &ResourceAddress{
   170  				Type:         "aws_instance",
   171  				Name:         "foo",
   172  				InstanceType: TypePrimary,
   173  				Index:        0,
   174  			},
   175  			Other: &ResourceAddress{
   176  				Type:         "aws_instance",
   177  				Name:         "foo",
   178  				InstanceType: TypeTainted,
   179  				Index:        0,
   180  			},
   181  			Expect: false,
   182  		},
   183  		"different index": {
   184  			Address: &ResourceAddress{
   185  				Type:         "aws_instance",
   186  				Name:         "foo",
   187  				InstanceType: TypePrimary,
   188  				Index:        0,
   189  			},
   190  			Other: &ResourceAddress{
   191  				Type:         "aws_instance",
   192  				Name:         "foo",
   193  				InstanceType: TypePrimary,
   194  				Index:        1,
   195  			},
   196  			Expect: false,
   197  		},
   198  	}
   199  
   200  	for tn, tc := range cases {
   201  		actual := tc.Address.Equals(tc.Other)
   202  		if actual != tc.Expect {
   203  			t.Fatalf("%q: expected equals: %t, got %t for:\n%#v\n%#v",
   204  				tn, tc.Expect, actual, tc.Address, tc.Other)
   205  		}
   206  	}
   207  }