kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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 TestConfigResourceEqual_true(t *testing.T) { 219 resources := []ConfigResource{ 220 { 221 Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"}, 222 Module: RootModule, 223 }, 224 { 225 Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"}, 226 Module: RootModule, 227 }, 228 { 229 Resource: Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"}, 230 Module: Module{"foo"}, 231 }, 232 { 233 Resource: Resource{Mode: DataResourceMode, Type: "a", Name: "b"}, 234 Module: Module{"foo"}, 235 }, 236 } 237 for _, r := range resources { 238 t.Run(r.String(), func(t *testing.T) { 239 if !r.Equal(r) { 240 t.Fatalf("expected %#v to be equal to itself", r) 241 } 242 }) 243 } 244 } 245 246 func TestConfigResourceEqual_false(t *testing.T) { 247 managed := Resource{Mode: ManagedResourceMode, Type: "a", Name: "b"} 248 data := Resource{Mode: DataResourceMode, Type: "a", Name: "b"} 249 250 foo := Module{"foo"} 251 foobar := Module{"foobar"} 252 testCases := []struct { 253 left ConfigResource 254 right ConfigResource 255 }{ 256 { 257 ConfigResource{Resource: managed, Module: foo}, 258 ConfigResource{Resource: data, Module: foo}, 259 }, 260 { 261 ConfigResource{Resource: managed, Module: foo}, 262 ConfigResource{Resource: managed, Module: foobar}, 263 }, 264 } 265 for _, tc := range testCases { 266 t.Run(fmt.Sprintf("%s = %s", tc.left, tc.right), func(t *testing.T) { 267 if tc.left.Equal(tc.right) { 268 t.Fatalf("expected %#v not to be equal to %#v", tc.left, tc.right) 269 } 270 271 if tc.right.Equal(tc.left) { 272 t.Fatalf("expected %#v not to be equal to %#v", tc.right, tc.left) 273 } 274 }) 275 } 276 }