github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/hook_count_test.go (about) 1 package views 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/zclconf/go-cty/cty" 8 9 "github.com/hashicorp/terraform/internal/addrs" 10 "github.com/hashicorp/terraform/internal/plans" 11 "github.com/hashicorp/terraform/internal/states" 12 "github.com/hashicorp/terraform/internal/terraform" 13 14 legacy "github.com/hashicorp/terraform/internal/legacy/terraform" 15 ) 16 17 func TestCountHook_impl(t *testing.T) { 18 var _ terraform.Hook = new(countHook) 19 } 20 21 func TestCountHookPostDiff_DestroyDeposed(t *testing.T) { 22 h := new(countHook) 23 24 resources := map[string]*legacy.InstanceDiff{ 25 "lorem": &legacy.InstanceDiff{DestroyDeposed: true}, 26 } 27 28 for k := range resources { 29 addr := addrs.Resource{ 30 Mode: addrs.ManagedResourceMode, 31 Type: "test_instance", 32 Name: k, 33 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 34 35 h.PostDiff(addr, states.DeposedKey("deadbeef"), plans.Delete, cty.DynamicVal, cty.DynamicVal) 36 } 37 38 expected := new(countHook) 39 expected.ToAdd = 0 40 expected.ToChange = 0 41 expected.ToRemoveAndAdd = 0 42 expected.ToRemove = 1 43 44 if !reflect.DeepEqual(expected, h) { 45 t.Fatalf("Expected %#v, got %#v instead.", expected, h) 46 } 47 } 48 49 func TestCountHookPostDiff_DestroyOnly(t *testing.T) { 50 h := new(countHook) 51 52 resources := map[string]*legacy.InstanceDiff{ 53 "foo": &legacy.InstanceDiff{Destroy: true}, 54 "bar": &legacy.InstanceDiff{Destroy: true}, 55 "lorem": &legacy.InstanceDiff{Destroy: true}, 56 "ipsum": &legacy.InstanceDiff{Destroy: true}, 57 } 58 59 for k := range resources { 60 addr := addrs.Resource{ 61 Mode: addrs.ManagedResourceMode, 62 Type: "test_instance", 63 Name: k, 64 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 65 66 h.PostDiff(addr, states.CurrentGen, plans.Delete, cty.DynamicVal, cty.DynamicVal) 67 } 68 69 expected := new(countHook) 70 expected.ToAdd = 0 71 expected.ToChange = 0 72 expected.ToRemoveAndAdd = 0 73 expected.ToRemove = 4 74 75 if !reflect.DeepEqual(expected, h) { 76 t.Fatalf("Expected %#v, got %#v instead.", expected, h) 77 } 78 } 79 80 func TestCountHookPostDiff_AddOnly(t *testing.T) { 81 h := new(countHook) 82 83 resources := map[string]*legacy.InstanceDiff{ 84 "foo": &legacy.InstanceDiff{ 85 Attributes: map[string]*legacy.ResourceAttrDiff{ 86 "foo": &legacy.ResourceAttrDiff{RequiresNew: true}, 87 }, 88 }, 89 "bar": &legacy.InstanceDiff{ 90 Attributes: map[string]*legacy.ResourceAttrDiff{ 91 "foo": &legacy.ResourceAttrDiff{RequiresNew: true}, 92 }, 93 }, 94 "lorem": &legacy.InstanceDiff{ 95 Attributes: map[string]*legacy.ResourceAttrDiff{ 96 "foo": &legacy.ResourceAttrDiff{RequiresNew: true}, 97 }, 98 }, 99 } 100 101 for k := range resources { 102 addr := addrs.Resource{ 103 Mode: addrs.ManagedResourceMode, 104 Type: "test_instance", 105 Name: k, 106 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 107 108 h.PostDiff(addr, states.CurrentGen, plans.Create, cty.DynamicVal, cty.DynamicVal) 109 } 110 111 expected := new(countHook) 112 expected.ToAdd = 3 113 expected.ToChange = 0 114 expected.ToRemoveAndAdd = 0 115 expected.ToRemove = 0 116 117 if !reflect.DeepEqual(expected, h) { 118 t.Fatalf("Expected %#v, got %#v instead.", expected, h) 119 } 120 } 121 122 func TestCountHookPostDiff_ChangeOnly(t *testing.T) { 123 h := new(countHook) 124 125 resources := map[string]*legacy.InstanceDiff{ 126 "foo": &legacy.InstanceDiff{ 127 Destroy: false, 128 Attributes: map[string]*legacy.ResourceAttrDiff{ 129 "foo": &legacy.ResourceAttrDiff{}, 130 }, 131 }, 132 "bar": &legacy.InstanceDiff{ 133 Destroy: false, 134 Attributes: map[string]*legacy.ResourceAttrDiff{ 135 "foo": &legacy.ResourceAttrDiff{}, 136 }, 137 }, 138 "lorem": &legacy.InstanceDiff{ 139 Destroy: false, 140 Attributes: map[string]*legacy.ResourceAttrDiff{ 141 "foo": &legacy.ResourceAttrDiff{}, 142 }, 143 }, 144 } 145 146 for k := range resources { 147 addr := addrs.Resource{ 148 Mode: addrs.ManagedResourceMode, 149 Type: "test_instance", 150 Name: k, 151 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 152 153 h.PostDiff(addr, states.CurrentGen, plans.Update, cty.DynamicVal, cty.DynamicVal) 154 } 155 156 expected := new(countHook) 157 expected.ToAdd = 0 158 expected.ToChange = 3 159 expected.ToRemoveAndAdd = 0 160 expected.ToRemove = 0 161 162 if !reflect.DeepEqual(expected, h) { 163 t.Fatalf("Expected %#v, got %#v instead.", expected, h) 164 } 165 } 166 167 func TestCountHookPostDiff_Mixed(t *testing.T) { 168 h := new(countHook) 169 170 resources := map[string]plans.Action{ 171 "foo": plans.Delete, 172 "bar": plans.NoOp, 173 "lorem": plans.Update, 174 "ipsum": plans.Delete, 175 } 176 177 for k, a := range resources { 178 addr := addrs.Resource{ 179 Mode: addrs.ManagedResourceMode, 180 Type: "test_instance", 181 Name: k, 182 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 183 184 h.PostDiff(addr, states.CurrentGen, a, cty.DynamicVal, cty.DynamicVal) 185 } 186 187 expected := new(countHook) 188 expected.ToAdd = 0 189 expected.ToChange = 1 190 expected.ToRemoveAndAdd = 0 191 expected.ToRemove = 2 192 193 if !reflect.DeepEqual(expected, h) { 194 t.Fatalf("Expected %#v, got %#v instead.", 195 expected, h) 196 } 197 } 198 199 func TestCountHookPostDiff_NoChange(t *testing.T) { 200 h := new(countHook) 201 202 resources := map[string]*legacy.InstanceDiff{ 203 "foo": &legacy.InstanceDiff{}, 204 "bar": &legacy.InstanceDiff{}, 205 "lorem": &legacy.InstanceDiff{}, 206 "ipsum": &legacy.InstanceDiff{}, 207 } 208 209 for k := range resources { 210 addr := addrs.Resource{ 211 Mode: addrs.ManagedResourceMode, 212 Type: "test_instance", 213 Name: k, 214 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 215 216 h.PostDiff(addr, states.CurrentGen, plans.NoOp, cty.DynamicVal, cty.DynamicVal) 217 } 218 219 expected := new(countHook) 220 expected.ToAdd = 0 221 expected.ToChange = 0 222 expected.ToRemoveAndAdd = 0 223 expected.ToRemove = 0 224 225 if !reflect.DeepEqual(expected, h) { 226 t.Fatalf("Expected %#v, got %#v instead.", 227 expected, h) 228 } 229 } 230 231 func TestCountHookPostDiff_DataSource(t *testing.T) { 232 h := new(countHook) 233 234 resources := map[string]plans.Action{ 235 "foo": plans.Delete, 236 "bar": plans.NoOp, 237 "lorem": plans.Update, 238 "ipsum": plans.Delete, 239 } 240 241 for k, a := range resources { 242 addr := addrs.Resource{ 243 Mode: addrs.DataResourceMode, 244 Type: "test_instance", 245 Name: k, 246 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 247 248 h.PostDiff(addr, states.CurrentGen, a, cty.DynamicVal, cty.DynamicVal) 249 } 250 251 expected := new(countHook) 252 expected.ToAdd = 0 253 expected.ToChange = 0 254 expected.ToRemoveAndAdd = 0 255 expected.ToRemove = 0 256 257 if !reflect.DeepEqual(expected, h) { 258 t.Fatalf("Expected %#v, got %#v instead.", 259 expected, h) 260 } 261 } 262 263 func TestCountHookApply_ChangeOnly(t *testing.T) { 264 h := new(countHook) 265 266 resources := map[string]*legacy.InstanceDiff{ 267 "foo": &legacy.InstanceDiff{ 268 Destroy: false, 269 Attributes: map[string]*legacy.ResourceAttrDiff{ 270 "foo": &legacy.ResourceAttrDiff{}, 271 }, 272 }, 273 "bar": &legacy.InstanceDiff{ 274 Destroy: false, 275 Attributes: map[string]*legacy.ResourceAttrDiff{ 276 "foo": &legacy.ResourceAttrDiff{}, 277 }, 278 }, 279 "lorem": &legacy.InstanceDiff{ 280 Destroy: false, 281 Attributes: map[string]*legacy.ResourceAttrDiff{ 282 "foo": &legacy.ResourceAttrDiff{}, 283 }, 284 }, 285 } 286 287 for k := range resources { 288 addr := addrs.Resource{ 289 Mode: addrs.ManagedResourceMode, 290 Type: "test_instance", 291 Name: k, 292 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 293 294 h.PreApply(addr, states.CurrentGen, plans.Update, cty.DynamicVal, cty.DynamicVal) 295 h.PostApply(addr, states.CurrentGen, cty.DynamicVal, nil) 296 } 297 298 expected := &countHook{pending: make(map[string]plans.Action)} 299 expected.Added = 0 300 expected.Changed = 3 301 expected.Removed = 0 302 303 if !reflect.DeepEqual(expected, h) { 304 t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h) 305 } 306 } 307 308 func TestCountHookApply_DestroyOnly(t *testing.T) { 309 h := new(countHook) 310 311 resources := map[string]*legacy.InstanceDiff{ 312 "foo": &legacy.InstanceDiff{Destroy: true}, 313 "bar": &legacy.InstanceDiff{Destroy: true}, 314 "lorem": &legacy.InstanceDiff{Destroy: true}, 315 "ipsum": &legacy.InstanceDiff{Destroy: true}, 316 } 317 318 for k := range resources { 319 addr := addrs.Resource{ 320 Mode: addrs.ManagedResourceMode, 321 Type: "test_instance", 322 Name: k, 323 }.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance) 324 325 h.PreApply(addr, states.CurrentGen, plans.Delete, cty.DynamicVal, cty.DynamicVal) 326 h.PostApply(addr, states.CurrentGen, cty.DynamicVal, nil) 327 } 328 329 expected := &countHook{pending: make(map[string]plans.Action)} 330 expected.Added = 0 331 expected.Changed = 0 332 expected.Removed = 4 333 334 if !reflect.DeepEqual(expected, h) { 335 t.Fatalf("Expected:\n%#v\nGot:\n%#v\n", expected, h) 336 } 337 }