github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/addrs/resource_test.go (about)

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