github.com/pulumi/terraform@v1.4.0/pkg/addrs/resource_test.go (about)

     1  package addrs
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  )
     7  
     8  func TestResourceEqual_true(t *testing.T) {
     9  	resources := []Resource{
    10  		{
    11  			Mode: ManagedResourceMode,
    12  			Type: "a",
    13  			Name: "b",
    14  		},
    15  		{
    16  			Mode: DataResourceMode,
    17  			Type: "a",
    18  			Name: "b",
    19  		},
    20  	}
    21  	for _, r := range resources {
    22  		t.Run(r.String(), func(t *testing.T) {
    23  			if !r.Equal(r) {
    24  				t.Fatalf("expected %#v to be equal to itself", r)
    25  			}
    26  		})
    27  	}
    28  }
    29  
    30  func TestResourceEqual_false(t *testing.T) {
    31  	testCases := []struct {
    32  		left  Resource
    33  		right Resource
    34  	}{
    35  		{
    36  			Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
    37  			Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
    38  		},
    39  		{
    40  			Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
    41  			Resource{Mode: ManagedResourceMode, Type: "b", Name: "b"},
    42  		},
    43  		{
    44  			Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
    45  			Resource{Mode: ManagedResourceMode, Type: "a", Name: "c"},
    46  		},
    47  	}
    48  	for _, tc := range testCases {
    49  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
    50  			if tc.left.Equal(tc.right) {
    51  				t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
    52  			}
    53  
    54  			if tc.right.Equal(tc.left) {
    55  				t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestResourceInstanceEqual_true(t *testing.T) {
    62  	resources := []ResourceInstance{
    63  		{
    64  			Resource: Resource{
    65  				Mode: ManagedResourceMode,
    66  				Type: "a",
    67  				Name: "b",
    68  			},
    69  			Key: IntKey(0),
    70  		},
    71  		{
    72  			Resource: Resource{
    73  				Mode: DataResourceMode,
    74  				Type: "a",
    75  				Name: "b",
    76  			},
    77  			Key: StringKey("x"),
    78  		},
    79  	}
    80  	for _, r := range resources {
    81  		t.Run(r.String(), func(t *testing.T) {
    82  			if !r.Equal(r) {
    83  				t.Fatalf("expected %#v to be equal to itself", r)
    84  			}
    85  		})
    86  	}
    87  }
    88  
    89  func TestResourceInstanceEqual_false(t *testing.T) {
    90  	testCases := []struct {
    91  		left  ResourceInstance
    92  		right ResourceInstance
    93  	}{
    94  		{
    95  			ResourceInstance{
    96  				Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
    97  				Key:      IntKey(0),
    98  			},
    99  			ResourceInstance{
   100  				Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
   101  				Key:      IntKey(0),
   102  			},
   103  		},
   104  		{
   105  			ResourceInstance{
   106  				Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
   107  				Key:      IntKey(0),
   108  			},
   109  			ResourceInstance{
   110  				Resource: Resource{Mode: ManagedResourceMode, Type: "b", Name: "b"},
   111  				Key:      IntKey(0),
   112  			},
   113  		},
   114  		{
   115  			ResourceInstance{
   116  				Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
   117  				Key:      IntKey(0),
   118  			},
   119  			ResourceInstance{
   120  				Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "c"},
   121  				Key:      IntKey(0),
   122  			},
   123  		},
   124  		{
   125  			ResourceInstance{
   126  				Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
   127  				Key:      IntKey(0),
   128  			},
   129  			ResourceInstance{
   130  				Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
   131  				Key:      StringKey("0"),
   132  			},
   133  		},
   134  	}
   135  	for _, tc := range testCases {
   136  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
   137  			if tc.left.Equal(tc.right) {
   138  				t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
   139  			}
   140  
   141  			if tc.right.Equal(tc.left) {
   142  				t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestAbsResourceInstanceEqual_true(t *testing.T) {
   149  	managed := Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"}
   150  	data := Resource{Mode: DataResourceMode, Type: "a", Name: "b"}
   151  
   152  	foo, diags := ParseModuleInstanceStr("module.foo")
   153  	if len(diags) > 0 {
   154  		t.Fatalf("unexpected diags: %s", diags.Err())
   155  	}
   156  	foobar, diags := ParseModuleInstanceStr("module.foo[1].module.bar")
   157  	if len(diags) > 0 {
   158  		t.Fatalf("unexpected diags: %s", diags.Err())
   159  	}
   160  
   161  	instances := []AbsResourceInstance{
   162  		managed.Instance(IntKey(0)).Absolute(foo),
   163  		data.Instance(IntKey(0)).Absolute(foo),
   164  		managed.Instance(StringKey("a")).Absolute(foobar),
   165  	}
   166  	for _, r := range instances {
   167  		t.Run(r.String(), func(t *testing.T) {
   168  			if !r.Equal(r) {
   169  				t.Fatalf("expected %#v to be equal to itself", r)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  func TestAbsResourceInstanceEqual_false(t *testing.T) {
   176  	managed := Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"}
   177  	data := Resource{Mode: DataResourceMode, Type: "a", Name: "b"}
   178  
   179  	foo, diags := ParseModuleInstanceStr("module.foo")
   180  	if len(diags) > 0 {
   181  		t.Fatalf("unexpected diags: %s", diags.Err())
   182  	}
   183  	foobar, diags := ParseModuleInstanceStr("module.foo[1].module.bar")
   184  	if len(diags) > 0 {
   185  		t.Fatalf("unexpected diags: %s", diags.Err())
   186  	}
   187  
   188  	testCases := []struct {
   189  		left  AbsResourceInstance
   190  		right AbsResourceInstance
   191  	}{
   192  		{
   193  			managed.Instance(IntKey(0)).Absolute(foo),
   194  			data.Instance(IntKey(0)).Absolute(foo),
   195  		},
   196  		{
   197  			managed.Instance(IntKey(0)).Absolute(foo),
   198  			managed.Instance(IntKey(0)).Absolute(foobar),
   199  		},
   200  		{
   201  			managed.Instance(IntKey(0)).Absolute(foo),
   202  			managed.Instance(StringKey("0")).Absolute(foo),
   203  		},
   204  	}
   205  	for _, tc := range testCases {
   206  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
   207  			if tc.left.Equal(tc.right) {
   208  				t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
   209  			}
   210  
   211  			if tc.right.Equal(tc.left) {
   212  				t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
   213  			}
   214  		})
   215  	}
   216  }
   217  
   218  func TestAbsResourceUniqueKey(t *testing.T) {
   219  	resourceAddr1 := Resource{
   220  		Mode: ManagedResourceMode,
   221  		Type: "a",
   222  		Name: "b1",
   223  	}.Absolute(RootModuleInstance)
   224  	resourceAddr2 := Resource{
   225  		Mode: ManagedResourceMode,
   226  		Type: "a",
   227  		Name: "b2",
   228  	}.Absolute(RootModuleInstance)
   229  	resourceAddr3 := Resource{
   230  		Mode: ManagedResourceMode,
   231  		Type: "a",
   232  		Name: "in_module",
   233  	}.Absolute(RootModuleInstance.Child("boop", NoKey))
   234  
   235  	tests := []struct {
   236  		Reciever  AbsResource
   237  		Other     UniqueKeyer
   238  		WantEqual bool
   239  	}{
   240  		{
   241  			resourceAddr1,
   242  			resourceAddr1,
   243  			true,
   244  		},
   245  		{
   246  			resourceAddr1,
   247  			resourceAddr2,
   248  			false,
   249  		},
   250  		{
   251  			resourceAddr1,
   252  			resourceAddr3,
   253  			false,
   254  		},
   255  		{
   256  			resourceAddr3,
   257  			resourceAddr3,
   258  			true,
   259  		},
   260  		{
   261  			resourceAddr1,
   262  			resourceAddr1.Instance(NoKey),
   263  			false, // no-key instance key is distinct from its resource even though they have the same String result
   264  		},
   265  		{
   266  			resourceAddr1,
   267  			resourceAddr1.Instance(IntKey(1)),
   268  			false,
   269  		},
   270  	}
   271  
   272  	for _, test := range tests {
   273  		t.Run(fmt.Sprintf("%s matches %T %s?", test.Reciever, test.Other, test.Other), func(t *testing.T) {
   274  			rKey := test.Reciever.UniqueKey()
   275  			oKey := test.Other.UniqueKey()
   276  
   277  			gotEqual := rKey == oKey
   278  			if gotEqual != test.WantEqual {
   279  				t.Errorf(
   280  					"wrong result\nreceiver: %s\nother:    %s (%T)\ngot:  %t\nwant: %t",
   281  					test.Reciever, test.Other, test.Other,
   282  					gotEqual, test.WantEqual,
   283  				)
   284  			}
   285  		})
   286  	}
   287  }
   288  
   289  func TestConfigResourceEqual_true(t *testing.T) {
   290  	resources := []ConfigResource{
   291  		{
   292  			Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
   293  			Module:   RootModule,
   294  		},
   295  		{
   296  			Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
   297  			Module:   RootModule,
   298  		},
   299  		{
   300  			Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"},
   301  			Module:   Module{"foo"},
   302  		},
   303  		{
   304  			Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"},
   305  			Module:   Module{"foo"},
   306  		},
   307  	}
   308  	for _, r := range resources {
   309  		t.Run(r.String(), func(t *testing.T) {
   310  			if !r.Equal(r) {
   311  				t.Fatalf("expected %#v to be equal to itself", r)
   312  			}
   313  		})
   314  	}
   315  }
   316  
   317  func TestConfigResourceEqual_false(t *testing.T) {
   318  	managed := Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"}
   319  	data := Resource{Mode: DataResourceMode, Type: "a", Name: "b"}
   320  
   321  	foo := Module{"foo"}
   322  	foobar := Module{"foobar"}
   323  	testCases := []struct {
   324  		left  ConfigResource
   325  		right ConfigResource
   326  	}{
   327  		{
   328  			ConfigResource{Resource: managed, Module: foo},
   329  			ConfigResource{Resource: data, Module: foo},
   330  		},
   331  		{
   332  			ConfigResource{Resource: managed, Module: foo},
   333  			ConfigResource{Resource: managed, Module: foobar},
   334  		},
   335  	}
   336  	for _, tc := range testCases {
   337  		t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) {
   338  			if tc.left.Equal(tc.right) {
   339  				t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right)
   340  			}
   341  
   342  			if tc.right.Equal(tc.left) {
   343  				t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left)
   344  			}
   345  		})
   346  	}
   347  }