forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_test.go
More file actions
1237 lines (1115 loc) · 35.2 KB
/
state_test.go
File metadata and controls
1237 lines (1115 loc) · 35.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package states
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/go-test/deep"
"github.com/google/go-cmp/cmp"
"github.com/zclconf/go-cty/cty"
"github.com/hashicorp/terraform/internal/addrs"
)
func TestState(t *testing.T) {
// This basic tests exercises the main mutation methods to construct
// a state. It is not fully comprehensive, so other tests should visit
// more esoteric codepaths.
state := NewState()
rootModule := state.RootModule()
if rootModule == nil {
t.Errorf("root module is nil; want valid object")
}
state.SetOutputValue(
addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar value"), false,
)
state.SetOutputValue(
addrs.OutputValue{Name: "secret"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("secret value"), true,
)
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "baz",
}.Instance(addrs.IntKey(0)),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
// State silently ignores attempts to write to non-root outputs, because
// historically we did track those here but these days we track them in
// namedvals.State instead, and we're being gracious to existing callers
// that might not know yet that they need to treat root module output
// values in a special way.
childModule := state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
state.SetOutputValue(addrs.OutputValue{Name: "pizza"}.Absolute(childModule.Addr), cty.StringVal("hawaiian"), false)
multiModA := state.EnsureModule(addrs.RootModuleInstance.Child("multi", addrs.StringKey("a")))
state.SetOutputValue(addrs.OutputValue{Name: "pizza"}.Absolute(multiModA.Addr), cty.StringVal("cheese"), false)
multiModB := state.EnsureModule(addrs.RootModuleInstance.Child("multi", addrs.StringKey("b")))
state.SetOutputValue(addrs.OutputValue{Name: "pizza"}.Absolute(multiModB.Addr), cty.StringVal("sausage"), false)
want := &State{
RootOutputValues: map[string]*OutputValue{
"bar": {
Addr: addrs.AbsOutputValue{
OutputValue: addrs.OutputValue{
Name: "bar",
},
},
Value: cty.StringVal("bar value"),
Sensitive: false,
},
"secret": {
Addr: addrs.AbsOutputValue{
OutputValue: addrs.OutputValue{
Name: "secret",
},
},
Value: cty.StringVal("secret value"),
Sensitive: true,
},
},
Modules: map[string]*Module{
"": {
Addr: addrs.RootModuleInstance,
Resources: map[string]*Resource{
"test_thing.baz": {
Addr: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "baz",
}.Absolute(addrs.RootModuleInstance),
Instances: map[addrs.InstanceKey]*ResourceInstance{
addrs.IntKey(0): {
Current: &ResourceInstanceObjectSrc{
SchemaVersion: 1,
Status: ObjectReady,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
Deposed: map[DeposedKey]*ResourceInstanceObjectSrc{},
},
},
ProviderConfig: addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
},
},
},
"module.child": {
Addr: addrs.RootModuleInstance.Child("child", addrs.NoKey),
Resources: map[string]*Resource{},
},
`module.multi["a"]`: {
Addr: addrs.RootModuleInstance.Child("multi", addrs.StringKey("a")),
Resources: map[string]*Resource{},
},
`module.multi["b"]`: {
Addr: addrs.RootModuleInstance.Child("multi", addrs.StringKey("b")),
Resources: map[string]*Resource{},
},
},
}
{
// Our structure goes deep, so we need to temporarily override the
// deep package settings to ensure that we visit the full structure.
oldDeepDepth := deep.MaxDepth
oldDeepCompareUnexp := deep.CompareUnexportedFields
deep.MaxDepth = 50
deep.CompareUnexportedFields = true
defer func() {
deep.MaxDepth = oldDeepDepth
deep.CompareUnexportedFields = oldDeepCompareUnexp
}()
}
if diff := cmp.Diff(want.String(), state.String()); diff != "" {
t.Errorf("wrong result\n%s", diff)
}
}
func TestStateDeepCopyObject(t *testing.T) {
obj := &ResourceInstanceObject{
Value: cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("id"),
}),
Private: []byte("private"),
Status: ObjectReady,
Dependencies: []addrs.ConfigResource{
{
Module: addrs.RootModule,
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_instance",
Name: "bar",
},
},
},
CreateBeforeDestroy: true,
}
objCopy := obj.DeepCopy()
if !reflect.DeepEqual(obj, objCopy) {
t.Fatalf("not equal\n%#v\n%#v", obj, objCopy)
}
}
func TestStateDeepCopy(t *testing.T) {
state := NewState()
rootModule := state.RootModule()
if rootModule == nil {
t.Fatalf("root module is nil; want valid object")
}
state.SetOutputValue(
addrs.OutputValue{Name: "bar"}.Absolute(rootModule.Addr),
cty.StringVal("bar value"), false,
)
state.SetOutputValue(
addrs.OutputValue{Name: "secret"}.Absolute(rootModule.Addr),
cty.StringVal("secret value"), true,
)
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "baz",
}.Instance(addrs.IntKey(0)),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
Private: []byte("private data"),
Dependencies: []addrs.ConfigResource{},
CreateBeforeDestroy: true,
// these may or may not be copied, but should not affect equality of
// the resources.
decodeValueCache: cty.ObjectVal(map[string]cty.Value{
"woozles": cty.StringVal("confuzles"),
}),
decodeIdentityCache: cty.DynamicVal,
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "bar",
}.Instance(addrs.IntKey(0)),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
// Sensitive path at "woozles"
AttrSensitivePaths: []cty.Path{
cty.GetAttrPath("woozles"),
},
Private: []byte("private data"),
Dependencies: []addrs.ConfigResource{
{
Module: addrs.RootModule,
Resource: addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "baz",
},
},
},
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
state.EnsureModule(addrs.RootModuleInstance.Child("child", addrs.NoKey))
stateCopy := state.DeepCopy()
if !state.Equal(stateCopy) {
t.Fatalf("\nexpected:\n%q\ngot:\n%q\n", state, stateCopy)
}
// this is implied by the above, but has previously used a different
// codepath for comparison.
if !state.ManagedResourcesEqual(stateCopy) {
t.Fatalf("\nexpected managed resources to be equal:\n%q\ngot:\n%q\n", state, stateCopy)
}
// remove the cached values and ensure equality still holds
for _, mod := range stateCopy.Modules {
for _, res := range mod.Resources {
for _, inst := range res.Instances {
inst.Current.decodeValueCache = cty.NilVal
inst.Current.decodeIdentityCache = cty.NilVal
}
}
}
if !state.Equal(stateCopy) {
t.Fatalf("\nexpected:\n%q\ngot:\n%q\n", state, stateCopy)
}
if !state.ManagedResourcesEqual(stateCopy) {
t.Fatalf("\nexpected managed resources to be equal:\n%q\ngot:\n%q\n", state, stateCopy)
}
}
func TestStateHasResourceInstanceObjects(t *testing.T) {
providerConfig := addrs.AbsProviderConfig{
Module: addrs.RootModule,
Provider: addrs.MustParseProviderSourceString("test/test"),
}
childModuleProviderConfig := addrs.AbsProviderConfig{
Module: addrs.RootModule.Child("child"),
Provider: addrs.MustParseProviderSourceString("test/test"),
}
tests := map[string]struct {
Setup func(ss *SyncState)
Want bool
}{
"empty": {
func(ss *SyncState) {},
false,
},
"one current, ready object in root module": {
func(ss *SyncState) {
ss.SetResourceInstanceCurrent(
mustAbsResourceAddr("test.foo").Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectReady,
},
providerConfig,
)
},
true,
},
"one current, ready object in child module": {
func(ss *SyncState) {
ss.SetResourceInstanceCurrent(
mustAbsResourceAddr("module.child.test.foo").Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectReady,
},
childModuleProviderConfig,
)
},
true,
},
"one current, tainted object in root module": {
func(ss *SyncState) {
ss.SetResourceInstanceCurrent(
mustAbsResourceAddr("test.foo").Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectTainted,
},
providerConfig,
)
},
true,
},
"one deposed, ready object in root module": {
func(ss *SyncState) {
ss.SetResourceInstanceDeposed(
mustAbsResourceAddr("test.foo").Instance(addrs.NoKey),
DeposedKey("uhoh"),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectTainted,
},
providerConfig,
)
},
true,
},
"one empty resource husk in root module": {
func(ss *SyncState) {
// Current Terraform doesn't actually create resource husks
// as part of its everyday work, so this is a "should never
// happen" case but we'll test to make sure we're robust to
// it anyway, because this was a historical bug blocking
// "terraform workspace delete" and similar.
ss.SetResourceInstanceCurrent(
mustAbsResourceAddr("test.foo").Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectTainted,
},
providerConfig,
)
s := ss.Lock()
delete(s.Modules[""].Resources["test.foo"].Instances, addrs.NoKey)
ss.Unlock()
done := make(chan struct{})
go func() {
ss.Lock()
ss.Unlock()
close(done)
}()
select {
case <-done:
// OK: lock was released
case <-time.After(500 * time.Millisecond):
t.Fatalf("Unlock did not release SyncState lock (timed out acquiring lock again)")
}
},
false,
},
"one current data resource object in root module": {
func(ss *SyncState) {
ss.SetResourceInstanceCurrent(
mustAbsResourceAddr("data.test.foo").Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
AttrsJSON: []byte(`{}`),
Status: ObjectReady,
},
providerConfig,
)
},
false, // data resources aren't managed resources, so they don't count
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
state := BuildState(test.Setup)
got := state.HasManagedResourceInstanceObjects()
if got != test.Want {
t.Errorf("wrong result\nstate content: (using legacy state string format; might not be comprehensive)\n%s\n\ngot: %t\nwant: %t", state, got, test.Want)
}
})
}
}
func TestStateHasRootOutputValues(t *testing.T) {
tests := map[string]struct {
Setup func(ss *SyncState)
Want bool
}{
"empty": {
func(ss *SyncState) {},
false,
},
"one output value": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar value"), false,
)
},
true,
},
"one secret output value": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "secret"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("secret value"), true,
)
},
true,
},
// The output value tests below are in other modules and do not persist between runs.
// Terraform Core tracks them internally and does not expose them in any
// artifacts that survive between executions.
"one output value in child module": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance.Child("child", addrs.NoKey)),
cty.StringVal("bar value"), false,
)
},
false,
},
"one output value in multi module": {
func(ss *SyncState) {
ss.state.EnsureModule(addrs.RootModuleInstance.Child("multi", addrs.StringKey("a")))
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance.Child("multi", addrs.StringKey("a"))),
cty.StringVal("bar"), false,
)
},
false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
state := BuildState(test.Setup)
got := state.HasRootOutputValues()
if got != test.Want {
t.Errorf("wrong result\nstate content: (using legacy state string format; might not be comprehensive)\n%s\n\ngot: %t\nwant: %t", state, got, test.Want)
}
})
}
}
func TestStateRootOutputValuesEqual(t *testing.T) {
tests := map[string]struct {
SetupA func(ss *SyncState)
SetupB func(ss *SyncState)
Want bool
}{
"both empty": {
func(ss *SyncState) {},
func(ss *SyncState) {},
true,
},
"identical outputs": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"), false,
)
},
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"), false,
)
},
true,
},
"different values same key": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"), false,
)
},
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("baz"), false,
)
},
false,
},
"different sensitivity same value": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"), false,
)
},
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("bar"), true,
)
},
false,
},
"different keys same count": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("val"), false,
)
},
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("val"), false,
)
},
false,
},
"different count": {
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("val"), false,
)
},
func(ss *SyncState) {
ss.SetOutputValue(
addrs.OutputValue{Name: "foo"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("val"), false,
)
ss.SetOutputValue(
addrs.OutputValue{Name: "bar"}.Absolute(addrs.RootModuleInstance),
cty.StringVal("val2"), false,
)
},
false,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
stateA := BuildState(test.SetupA)
stateB := BuildState(test.SetupB)
got := stateA.RootOutputValuesEqual(stateB)
if got != test.Want {
t.Errorf("wrong result for stateA.RootOutputValuesEqual(stateB)\ngot: %t\nwant: %t", got, test.Want)
}
})
}
}
func TestState_MoveAbsResource(t *testing.T) {
// Set up a starter state for the embedded tests, which should start from a copy of this state.
state := NewState()
rootModule := state.RootModule()
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "foo",
}.Instance(addrs.IntKey(0)),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Absolute(addrs.RootModuleInstance)
t.Run("basic move", func(t *testing.T) {
s := state.DeepCopy()
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "bar"}.Absolute(addrs.RootModuleInstance)
s.MoveAbsResource(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
if len(s.RootModule().Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(state.RootModule().Resources))
}
got := s.Resource(dst)
if got.Addr.Resource != dst.Resource {
t.Fatalf("dst resource not in state")
}
})
t.Run("move to new module", func(t *testing.T) {
s := state.DeepCopy()
dstModule := addrs.RootModuleInstance.Child("kinder", addrs.StringKey("one"))
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "bar"}.Absolute(dstModule)
s.MoveAbsResource(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
if s.Module(dstModule) == nil {
t.Fatalf("child module %s not in state", dstModule.String())
}
if len(s.Module(dstModule).Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(s.Module(dstModule).Resources))
}
got := s.Resource(dst)
if got.Addr.Resource != dst.Resource {
t.Fatalf("dst resource not in state")
}
})
t.Run("from a child module to root", func(t *testing.T) {
s := state.DeepCopy()
srcModule := addrs.RootModuleInstance.Child("kinder", addrs.NoKey)
cm := s.EnsureModule(srcModule)
cm.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "child",
}.Instance(addrs.IntKey(0)), // Moving the AbsResouce moves all instances
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
cm.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "child",
}.Instance(addrs.IntKey(1)), // Moving the AbsResouce moves all instances
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(srcModule)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(addrs.RootModuleInstance)
s.MoveAbsResource(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
// The child module should have been removed after removing its only resource
if s.Module(srcModule) != nil {
t.Fatalf("child module %s was not removed from state after mv", srcModule.String())
}
if len(s.RootModule().Resources) != 2 {
t.Fatalf("wrong number of resources in state; expected 2, found %d", len(s.RootModule().Resources))
}
if len(s.Resource(dst).Instances) != 2 {
t.Fatalf("wrong number of resource instances for dst, got %d expected 2", len(s.Resource(dst).Instances))
}
got := s.Resource(dst)
if got.Addr.Resource != dst.Resource {
t.Fatalf("dst resource not in state")
}
})
t.Run("module to new module", func(t *testing.T) {
s := NewState()
srcModule := addrs.RootModuleInstance.Child("kinder", addrs.StringKey("exists"))
dstModule := addrs.RootModuleInstance.Child("kinder", addrs.StringKey("new"))
cm := s.EnsureModule(srcModule)
cm.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "child",
}.Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(srcModule)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(dstModule)
s.MoveAbsResource(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
// The child module should have been removed after removing its only resource
if s.Module(srcModule) != nil {
t.Fatalf("child module %s was not removed from state after mv", srcModule.String())
}
gotMod := s.Module(dstModule)
if len(gotMod.Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(gotMod.Resources))
}
got := s.Resource(dst)
if got.Addr.Resource != dst.Resource {
t.Fatalf("dst resource not in state")
}
})
t.Run("module to new module", func(t *testing.T) {
s := NewState()
srcModule := addrs.RootModuleInstance.Child("kinder", addrs.StringKey("exists"))
dstModule := addrs.RootModuleInstance.Child("kinder", addrs.StringKey("new"))
cm := s.EnsureModule(srcModule)
cm.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "child",
}.Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(srcModule)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "child"}.Absolute(dstModule)
s.MoveAbsResource(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
// The child module should have been removed after removing its only resource
if s.Module(srcModule) != nil {
t.Fatalf("child module %s was not removed from state after mv", srcModule.String())
}
gotMod := s.Module(dstModule)
if len(gotMod.Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(gotMod.Resources))
}
got := s.Resource(dst)
if got.Addr.Resource != dst.Resource {
t.Fatalf("dst resource not in state")
}
})
}
func TestState_MaybeMoveAbsResource(t *testing.T) {
state := NewState()
rootModule := state.RootModule()
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "foo",
}.Instance(addrs.IntKey(0)),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Absolute(addrs.RootModuleInstance)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "bar"}.Absolute(addrs.RootModuleInstance)
// First move, success
t.Run("first move", func(t *testing.T) {
moved := state.MaybeMoveAbsResource(src, dst)
if !moved {
t.Fatal("wrong result")
}
})
// Trying to move a resource that doesn't exist in state to a resource which does exist should be a noop.
t.Run("noop", func(t *testing.T) {
moved := state.MaybeMoveAbsResource(src, dst)
if moved {
t.Fatal("wrong result")
}
})
}
func TestState_MoveAbsResourceInstance(t *testing.T) {
state := NewState()
rootModule := state.RootModule()
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "foo",
}.Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
// src resource from the state above
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
t.Run("resource to resource instance", func(t *testing.T) {
s := state.DeepCopy()
// For a little extra fun, move a resource to a resource instance: test_thing.foo to test_thing.foo[1]
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Instance(addrs.IntKey(1)).Absolute(addrs.RootModuleInstance)
s.MoveAbsResourceInstance(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
if len(s.RootModule().Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(state.RootModule().Resources))
}
got := s.ResourceInstance(dst)
if got == nil {
t.Fatalf("dst resource not in state")
}
})
t.Run("move to new module", func(t *testing.T) {
s := state.DeepCopy()
// test_thing.foo to module.kinder.test_thing.foo["baz"]
dstModule := addrs.RootModuleInstance.Child("kinder", addrs.NoKey)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Instance(addrs.IntKey(1)).Absolute(dstModule)
s.MoveAbsResourceInstance(src, dst)
if s.Empty() {
t.Fatal("unexpected empty state")
}
if s.Module(dstModule) == nil {
t.Fatalf("child module %s not in state", dstModule.String())
}
if len(s.Module(dstModule).Resources) != 1 {
t.Fatalf("wrong number of resources in state; expected 1, found %d", len(s.Module(dstModule).Resources))
}
got := s.ResourceInstance(dst)
if got == nil {
t.Fatalf("dst resource not in state")
}
})
}
func TestState_MaybeMoveAbsResourceInstance(t *testing.T) {
state := NewState()
rootModule := state.RootModule()
rootModule.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "foo",
}.Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
// For a little extra fun, let's go from a resource to a resource instance: test_thing.foo to test_thing.bar[1]
src := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance)
dst := addrs.Resource{Mode: addrs.ManagedResourceMode, Type: "test_thing", Name: "foo"}.Instance(addrs.IntKey(1)).Absolute(addrs.RootModuleInstance)
// First move, success
t.Run("first move", func(t *testing.T) {
moved := state.MaybeMoveAbsResourceInstance(src, dst)
if !moved {
t.Fatal("wrong result")
}
got := state.ResourceInstance(dst)
if got == nil {
t.Fatal("destination resource instance not in state")
}
})
// Moving a resource instance that doesn't exist in state to a resource which does exist should be a noop.
t.Run("noop", func(t *testing.T) {
moved := state.MaybeMoveAbsResourceInstance(src, dst)
if moved {
t.Fatal("wrong result")
}
})
}
func TestState_MoveModuleInstance(t *testing.T) {
state := NewState()
srcModule := addrs.RootModuleInstance.Child("kinder", addrs.NoKey)
m := state.EnsureModule(srcModule)
m.SetResourceInstanceCurrent(
addrs.Resource{
Mode: addrs.ManagedResourceMode,
Type: "test_thing",
Name: "foo",
}.Instance(addrs.NoKey),
&ResourceInstanceObjectSrc{
Status: ObjectReady,
SchemaVersion: 1,
AttrsJSON: []byte(`{"woozles":"confuzles"}`),
},
addrs.AbsProviderConfig{
Provider: addrs.NewDefaultProvider("test"),
Module: addrs.RootModule,
},
)
dstModule := addrs.RootModuleInstance.Child("child", addrs.IntKey(3))
state.MoveModuleInstance(srcModule, dstModule)
// srcModule should have been removed, dstModule should exist and have one resource
if len(state.Modules) != 2 { // kinder[3] and root
t.Fatalf("wrong number of modules in state. Expected 2, got %d", len(state.Modules))
}
got := state.Module(dstModule)
if got == nil {
t.Fatal("dstModule not found")
}
gone := state.Module(srcModule)
if gone != nil {
t.Fatal("srcModule not removed from state")
}
r := got.Resource(mustAbsResourceAddr("test_thing.foo").Resource)
if r.Addr.Module.String() != dstModule.String() {