-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
snapshot_test.go
1048 lines (886 loc) 路 34.3 KB
/
snapshot_test.go
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 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backend
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/pulumi/pulumi/pkg/v3/resource/deploy"
"github.com/pulumi/pulumi/pkg/v3/resource/stack"
"github.com/pulumi/pulumi/pkg/v3/secrets"
"github.com/pulumi/pulumi/pkg/v3/secrets/b64"
"github.com/pulumi/pulumi/pkg/v3/version"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/config"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
)
type MockRegisterResourceEvent struct {
deploy.SourceEvent
}
func (m MockRegisterResourceEvent) Goal() *resource.Goal { return nil }
func (m MockRegisterResourceEvent) Done(result *deploy.RegisterResult) {}
type MockStackPersister struct {
SavedSnapshots []*deploy.Snapshot
}
func (m *MockStackPersister) Save(snap *deploy.Snapshot) error {
m.SavedSnapshots = append(m.SavedSnapshots, snap)
return nil
}
func (m *MockStackPersister) SecretsManager() secrets.Manager {
return b64.NewBase64SecretsManager()
}
func (m *MockStackPersister) LastSnap() *deploy.Snapshot {
return m.SavedSnapshots[len(m.SavedSnapshots)-1]
}
func MockSetup(t *testing.T, baseSnap *deploy.Snapshot) (*SnapshotManager, *MockStackPersister) {
err := baseSnap.VerifyIntegrity()
if !assert.NoError(t, err) {
t.FailNow()
}
sp := &MockStackPersister{}
return NewSnapshotManager(sp, baseSnap), sp
}
func NewResourceWithDeps(name string, deps []resource.URN) *resource.State {
return &resource.State{
Type: tokens.Type("test"),
URN: resource.URN(name),
Inputs: make(resource.PropertyMap),
Outputs: make(resource.PropertyMap),
Dependencies: deps,
}
}
func NewResourceWithInputs(name string, inputs resource.PropertyMap) *resource.State {
return &resource.State{
Type: tokens.Type("test"),
URN: resource.URN(name),
Inputs: inputs,
Outputs: make(resource.PropertyMap),
Dependencies: []resource.URN{},
}
}
func NewResource(name string, deps ...resource.URN) *resource.State {
return NewResourceWithDeps(name, deps)
}
func NewSnapshot(resources []*resource.State) *deploy.Snapshot {
return deploy.NewSnapshot(deploy.Manifest{
Time: time.Now(),
Version: version.Version,
Plugins: nil,
}, b64.NewBase64SecretsManager(), resources, nil)
}
func TestIdenticalSames(t *testing.T) {
t.Parallel()
sameState := NewResource("a-unique-urn")
snap := NewSnapshot([]*resource.State{
sameState,
})
manager, sp := MockSetup(t, snap)
// The engine generates a SameStep on sameState.
engineGeneratedSame := NewResource(string(sameState.URN))
same := deploy.NewSameStep(nil, nil, sameState, engineGeneratedSame)
mutation, err := manager.BeginMutation(same)
assert.NoError(t, err)
// No mutation was made
assert.Empty(t, sp.SavedSnapshots)
err = mutation.End(same, true)
assert.NoError(t, err)
// Identical sames do not cause a snapshot mutation as part of `End`.
assert.Empty(t, sp.SavedSnapshots)
// Close must write the snapshot.
err = manager.Close()
assert.NoError(t, err)
assert.NotEmpty(t, sp.SavedSnapshots)
assert.NotEmpty(t, sp.SavedSnapshots[0].Resources)
// Our same resource should be the first entry in the snapshot list.
inSnapshot := sp.SavedSnapshots[0].Resources[0]
assert.Equal(t, sameState.URN, inSnapshot.URN)
}
func TestSamesWithEmptyDependencies(t *testing.T) {
t.Parallel()
res := NewResourceWithDeps("a-unique-urn-resource-a", nil)
snap := NewSnapshot([]*resource.State{
res,
})
manager, sp := MockSetup(t, snap)
resUpdated := NewResourceWithDeps(string(res.URN), []resource.URN{})
same := deploy.NewSameStep(nil, nil, res, resUpdated)
mutation, err := manager.BeginMutation(same)
assert.NoError(t, err)
err = mutation.End(same, true)
assert.NoError(t, err)
assert.Len(t, sp.SavedSnapshots, 0, "expected no snapshots to be saved for same step")
}
func TestSamesWithEmptyArraysInInputs(t *testing.T) {
t.Parallel()
// Model reading from state file
state := map[string]interface{}{"defaults": []interface{}{}}
inputs, err := stack.DeserializeProperties(state, config.NopDecrypter, config.NopEncrypter)
assert.NoError(t, err)
res := NewResourceWithInputs("a-unique-urn-resource-a", inputs)
snap := NewSnapshot([]*resource.State{
res,
})
manager, sp := MockSetup(t, snap)
// Model passing into and back out of RPC layer (e.g. via `Check`)
marshalledInputs, err := plugin.MarshalProperties(inputs, plugin.MarshalOptions{})
assert.NoError(t, err)
inputsUpdated, err := plugin.UnmarshalProperties(marshalledInputs, plugin.MarshalOptions{})
assert.NoError(t, err)
resUpdated := NewResourceWithInputs(string(res.URN), inputsUpdated)
same := deploy.NewSameStep(nil, nil, res, resUpdated)
mutation, err := manager.BeginMutation(same)
assert.NoError(t, err)
err = mutation.End(same, true)
assert.NoError(t, err)
assert.Len(t, sp.SavedSnapshots, 0, "expected no snapshots to be saved for same step")
}
// This test challenges the naive approach of mutating resources
// that are the targets of Same steps in-place by changing the dependencies
// of two resources in the snapshot, which is perfectly legal in our system
// (and in fact is done by the `dependency_steps` integration test as well).
//
// The correctness of the `snap` function in snapshot.go is tested here.
func TestSamesWithDependencyChanges(t *testing.T) {
t.Parallel()
resourceA := NewResource("a-unique-urn-resource-a")
resourceB := NewResource("a-unique-urn-resource-b", resourceA.URN)
// The setup: the snapshot contains two resources, A and B, where
// B depends on A. We're going to begin a mutation in which B no longer
// depends on A and appears first in program order.
snap := NewSnapshot([]*resource.State{
resourceA,
resourceB,
})
manager, sp := MockSetup(t, snap)
resourceBUpdated := NewResource(string(resourceB.URN))
// note: no dependencies
resourceAUpdated := NewResource(string(resourceA.URN), resourceBUpdated.URN)
// note: now depends on B
// The engine first generates a Same for b:
bSame := deploy.NewSameStep(nil, nil, resourceB, resourceBUpdated)
mutation, err := manager.BeginMutation(bSame)
assert.NoError(t, err)
err = mutation.End(bSame, true)
assert.NoError(t, err)
// The snapshot should now look like this:
// snapshot
// resources
// b
// a
// where b does not depend on anything and neither does a.
firstSnap := sp.SavedSnapshots[0]
assert.Len(t, firstSnap.Resources, 2)
assert.Equal(t, resourceB.URN, firstSnap.Resources[0].URN)
assert.Len(t, firstSnap.Resources[0].Dependencies, 0)
assert.Equal(t, resourceA.URN, firstSnap.Resources[1].URN)
assert.Len(t, firstSnap.Resources[1].Dependencies, 0)
// The engine then generates a Same for a:
aSame := deploy.NewSameStep(nil, nil, resourceA, resourceAUpdated)
mutation, err = manager.BeginMutation(aSame)
assert.NoError(t, err)
err = mutation.End(aSame, true)
assert.NoError(t, err)
// The snapshot should now look like this:
// snapshot
// resources
// b
// a
// where b does not depend on anything and a depends on b.
secondSnap := sp.SavedSnapshots[1]
assert.Len(t, secondSnap.Resources, 2)
assert.Equal(t, resourceB.URN, secondSnap.Resources[0].URN)
assert.Len(t, secondSnap.Resources[0].Dependencies, 0)
assert.Equal(t, resourceA.URN, secondSnap.Resources[1].URN)
assert.Len(t, secondSnap.Resources[1].Dependencies, 1)
assert.Equal(t, resourceB.URN, secondSnap.Resources[1].Dependencies[0])
}
// This test checks that we only write the Checkpoint once whether or
// not there are important changes when asked to via
// pulumiSkipCheckpointsEnvVar.
//
//nolint:paralleltest // mutates environment variables
func TestWriteCheckpointOnceUnsafe(t *testing.T) {
t.Setenv("PULUMI_EXPERIMENTAL", "1")
t.Setenv(pulumiSkipCheckpointsEnvVar, "1")
provider := NewResource("urn:pulumi:foo::bar::pulumi:providers:pkgUnsafe::provider")
provider.Custom, provider.Type, provider.ID = true, "pulumi:providers:pkgUnsafe", "id"
resourceP := NewResource("a-unique-urn-resource-p")
resourceA := NewResource("a-unique-urn-resource-a")
snap := NewSnapshot([]*resource.State{
provider,
resourceP,
resourceA,
})
manager, sp := MockSetup(t, snap)
// Generate a same for the provider.
provUpdated := NewResource(string(provider.URN))
provUpdated.Custom, provUpdated.Type = true, provider.Type
provSame := deploy.NewSameStep(nil, nil, provider, provUpdated)
mutation, err := manager.BeginMutation(provSame)
assert.NoError(t, err)
_, _, err = provSame.Apply(false)
assert.NoError(t, err)
err = mutation.End(provSame, true)
assert.NoError(t, err)
// The engine generates a meaningful change, the DEFAULT behavior is that a snapshot is written:
pUpdated := NewResource(string(resourceP.URN))
pUpdated.Protect = !resourceP.Protect
pSame := deploy.NewSameStep(nil, nil, resourceP, pUpdated)
mutation, err = manager.BeginMutation(pSame)
assert.NoError(t, err)
err = mutation.End(pSame, true)
assert.NoError(t, err)
// The engine generates a meaningful change, the DEFAULT behavior is that a snapshot is written:
aUpdated := NewResource(string(resourceA.URN))
aUpdated.Protect = !resourceA.Protect
aSame := deploy.NewSameStep(nil, nil, resourceA, aUpdated)
mutation, err = manager.BeginMutation(aSame)
assert.NoError(t, err)
err = mutation.End(aSame, true)
assert.NoError(t, err)
// a `Close()` call is required to write back the snapshots.
// It is called in all of the references to SnapshotManager.
err = manager.Close()
assert.NoError(t, err)
// DEFAULT behavior would cause more than 1 snapshot to be written,
// but the provided flag should only create 1 Snapshot
assert.Len(t, sp.SavedSnapshots, 1)
}
// This test exercises same steps with meaningful changes to properties _other_ than `Dependencies` in order to ensure
// that the snapshot is written.
func TestSamesWithOtherMeaningfulChanges(t *testing.T) {
t.Parallel()
provider := NewResource("urn:pulumi:foo::bar::pulumi:providers:pkgA::provider")
provider.Custom, provider.Type, provider.ID = true, "pulumi:providers:pkgA", "id"
resourceP := NewResource("a-unique-urn-resource-p")
resourceA := NewResource("a-unique-urn-resource-a")
var changes []*resource.State
// Change the "custom" bit.
changes = append(changes, NewResource(string(resourceA.URN)))
changes[0].Custom, changes[0].Provider = true, "urn:pulumi:foo::bar::pulumi:providers:pkgA::provider::id"
// Change the parent.
changes = append(changes, NewResource(string(resourceA.URN)))
changes[1].Parent = resourceP.URN
// Change the "protect" bit.
changes = append(changes, NewResource(string(resourceA.URN)))
changes[2].Protect = !resourceA.Protect
// Change the resource outputs.
changes = append(changes, NewResource(string(resourceA.URN)))
changes[3].Outputs = resource.PropertyMap{"foo": resource.NewStringProperty("bar")}
snap := NewSnapshot([]*resource.State{
provider,
resourceP,
resourceA,
})
for _, c := range changes {
manager, sp := MockSetup(t, snap)
// Generate a same for the provider.
provUpdated := NewResource(string(provider.URN))
provUpdated.Custom, provUpdated.Type = true, provider.Type
provSame := deploy.NewSameStep(nil, nil, provider, provUpdated)
mutation, err := manager.BeginMutation(provSame)
assert.NoError(t, err)
_, _, err = provSame.Apply(false)
assert.NoError(t, err)
err = mutation.End(provSame, true)
assert.NoError(t, err)
assert.Empty(t, sp.SavedSnapshots)
// The engine generates a Same for p. This is not a meaningful change, so the snapshot is not written.
pUpdated := NewResource(string(resourceP.URN))
pSame := deploy.NewSameStep(nil, nil, resourceP, pUpdated)
mutation, err = manager.BeginMutation(pSame)
assert.NoError(t, err)
err = mutation.End(pSame, true)
assert.NoError(t, err)
assert.Empty(t, sp.SavedSnapshots)
// The engine generates a Same for a. Because this is a meaningful change, the snapshot is written:
aSame := deploy.NewSameStep(nil, nil, resourceA, c)
mutation, err = manager.BeginMutation(aSame)
assert.NoError(t, err)
err = mutation.End(aSame, true)
assert.NoError(t, err)
assert.NotEmpty(t, sp.SavedSnapshots)
assert.NotEmpty(t, sp.SavedSnapshots[0].Resources)
inSnapshot := sp.SavedSnapshots[0].Resources[2]
assert.Equal(t, c, inSnapshot)
err = manager.Close()
assert.NoError(t, err)
}
// Set up a second provider and change the resource's provider reference.
provider2 := NewResource("urn:pulumi:foo::bar::pulumi:providers:pkgA::provider2")
provider2.Custom, provider2.Type, provider2.ID = true, "pulumi:providers:pkgA", "id2"
resourceA.Custom, resourceA.ID, resourceA.Provider =
true, "id", "urn:pulumi:foo::bar::pulumi:providers:pkgA::provider::id"
snap = NewSnapshot([]*resource.State{
provider,
provider2,
resourceA,
})
changes = []*resource.State{NewResource(string(resourceA.URN))}
changes[0].Custom, changes[0].Provider = true, "urn:pulumi:foo::bar::pulumi:providers:pkgA::provider2::id2"
for _, c := range changes {
manager, sp := MockSetup(t, snap)
// Generate sames for the providers.
provUpdated := NewResource(string(provider.URN))
provUpdated.Custom, provUpdated.Type = true, provider.Type
provSame := deploy.NewSameStep(nil, nil, provider, provUpdated)
mutation, err := manager.BeginMutation(provSame)
assert.NoError(t, err)
_, _, err = provSame.Apply(false)
assert.NoError(t, err)
err = mutation.End(provSame, true)
assert.NoError(t, err)
assert.Empty(t, sp.SavedSnapshots)
// The engine generates a Same for p. This is not a meaningful change, so the snapshot is not written.
prov2Updated := NewResource(string(provider2.URN))
prov2Updated.Custom, prov2Updated.Type = true, provider.Type
prov2Same := deploy.NewSameStep(nil, nil, provider2, prov2Updated)
mutation, err = manager.BeginMutation(prov2Same)
assert.NoError(t, err)
_, _, err = prov2Same.Apply(false)
assert.NoError(t, err)
err = mutation.End(prov2Same, true)
assert.NoError(t, err)
assert.Empty(t, sp.SavedSnapshots)
// The engine generates a Same for a. Because this is a meaningful change, the snapshot is written:
aSame := deploy.NewSameStep(nil, nil, resourceA, c)
mutation, err = manager.BeginMutation(aSame)
assert.NoError(t, err)
_, _, err = aSame.Apply(false)
assert.NoError(t, err)
err = mutation.End(aSame, true)
assert.NoError(t, err)
assert.NotEmpty(t, sp.SavedSnapshots)
assert.NotEmpty(t, sp.SavedSnapshots[0].Resources)
inSnapshot := sp.SavedSnapshots[0].Resources[2]
assert.Equal(t, c, inSnapshot)
err = manager.Close()
assert.NoError(t, err)
}
}
// This test exercises the merge operation with a particularly vexing deployment
// state that was useful in shaking out bugs.
func TestVexingDeployment(t *testing.T) {
t.Parallel()
// This is the dependency graph we are going for in the base snapshot:
//
// +-+
// +--> |A|
// | +-+
// | ^
// | +-+
// | |B|
// | +-+
// | ^
// | +-+
// +--+ |C| <---+
// +-+ |
// ^ |
// +-+ |
// |D| |
// +-+ |
// |
// +-+ |
// |E| +---+
// +-+
a := NewResource("a")
b := NewResource("b", a.URN)
c := NewResource("c", a.URN, b.URN)
d := NewResource("d", c.URN)
e := NewResource("e", c.URN)
snap := NewSnapshot([]*resource.State{
a,
b,
c,
d,
e,
})
manager, sp := MockSetup(t, snap)
// This is the sequence of events that come out of the engine:
// B - Same, depends on nothing
// C - CreateReplacement, depends on B
// C - Replace
// D - Update, depends on new C
// This produces the following dependency graph in the new snapshot:
// +-+
// +---> |B|
// | +++
// | ^
// | +++
// | |C| <----+
// | +-+ |
// | |
// | +-+ |
// +---+ |C| +-------------> A (not in graph!)
// +-+ |
// |
// +-+ |
// |D| +---+
// +-+
//
// Conceptually, this is a plan that deletes A. However, we have not yet observed the
// deletion of A, presumably because the engine can't know for sure that it's been deleted
// until the eval source completes. Of note in this snapshot is that the replaced C is still in the graph,
// because it has not yet been deleted, and its dependency A is not in the graph because it
// has not been seen.
//
// Since axiomatically we assume that steps come in in a valid topological order of the dependency graph,
// we can logically assume that A is going to be deleted. (If A were not being deleted, it must have been
// the target of a Step that came before C, which depends on it.)
applyStep := func(step deploy.Step) {
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
err = mutation.End(step, true)
if !assert.NoError(t, err) {
t.FailNow()
}
}
// b now depends on nothing
bPrime := NewResource(string(b.URN))
applyStep(deploy.NewSameStep(nil, MockRegisterResourceEvent{}, b, bPrime))
// c now only depends on b
cPrime := NewResource(string(c.URN), bPrime.URN)
// mocking out the behavior of a provider indicating that this resource needs to be deleted
createReplacement := deploy.NewCreateReplacementStep(nil, MockRegisterResourceEvent{}, c, cPrime, nil, nil, nil, true)
replace := deploy.NewReplaceStep(nil, c, cPrime, nil, nil, nil, true)
c.Delete = true
applyStep(createReplacement)
applyStep(replace)
// cPrime now exists, c is now pending deletion
// dPrime now depends on cPrime, which got replaced
dPrime := NewResource(string(d.URN), cPrime.URN)
applyStep(deploy.NewUpdateStep(nil, MockRegisterResourceEvent{}, d, dPrime, nil, nil, nil, nil))
lastSnap := sp.SavedSnapshots[len(sp.SavedSnapshots)-1]
assert.Len(t, lastSnap.Resources, 6)
res := lastSnap.Resources
// Here's what the merged snapshot should look like:
// B should be first, and it should depend on nothing
assert.Equal(t, b.URN, res[0].URN)
assert.Len(t, res[0].Dependencies, 0)
// cPrime should be next, and it should depend on B
assert.Equal(t, c.URN, res[1].URN)
assert.Len(t, res[1].Dependencies, 1)
assert.Equal(t, b.URN, res[1].Dependencies[0])
// d should be next, and it should depend on cPrime
assert.Equal(t, d.URN, res[2].URN)
assert.Len(t, res[2].Dependencies, 1)
assert.Equal(t, c.URN, res[2].Dependencies[0])
// a should be next, and it should depend on nothing
assert.Equal(t, a.URN, res[3].URN)
assert.Len(t, res[3].Dependencies, 0)
// c should be next, it should depend on A and B and should be pending deletion
// this is a critical operation of snap and the crux of this test:
// merge MUST put c after a in the snapshot, despite never having seen a in the current plan
assert.Equal(t, c.URN, res[4].URN)
assert.True(t, res[4].Delete)
assert.Len(t, res[4].Dependencies, 2)
assert.Contains(t, res[4].Dependencies, a.URN)
assert.Contains(t, res[4].Dependencies, b.URN)
// e should be last, it should depend on C and still be live
assert.Equal(t, e.URN, res[5].URN)
assert.Len(t, res[5].Dependencies, 1)
assert.Equal(t, c.URN, res[5].Dependencies[0])
}
func TestDeletion(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewDeleteStep(nil, map[resource.URN]bool{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
err = mutation.End(step, true)
if !assert.NoError(t, err) {
t.FailNow()
}
// the end mutation should mark the resource as "done".
// snap should then not put resourceA in the merged snapshot, since it has been deleted.
lastSnap := sp.SavedSnapshots[len(sp.SavedSnapshots)-1]
assert.Len(t, lastSnap.Resources, 0)
}
func TestFailedDelete(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewDeleteStep(nil, map[resource.URN]bool{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
err = mutation.End(step, false /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// since we marked the mutation as not successful, the snapshot should still contain
// the resource we failed to delete.
lastSnap := sp.SavedSnapshots[len(sp.SavedSnapshots)-1]
assert.Len(t, lastSnap.Resources, 1)
assert.Equal(t, resourceA.URN, lastSnap.Resources[0].URN)
}
func TestRecordingCreateSuccess(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot(nil)
manager, sp := MockSetup(t, snap)
step := deploy.NewCreateStep(nil, &MockRegisterResourceEvent{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the create step mutation should have placed a pending "creating" operation
// into the operations list
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeCreating, snap.PendingOperations[0].Type)
err = mutation.End(step, true /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A successful creation should remove the "creating" operation from the operations list
// and persist the created resource in the snapshot.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
}
func TestRecordingCreateFailure(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot(nil)
manager, sp := MockSetup(t, snap)
step := deploy.NewCreateStep(nil, &MockRegisterResourceEvent{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the create step mutation should have placed a pending "creating" operation
// into the operations list
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeCreating, snap.PendingOperations[0].Type)
err = mutation.End(step, false /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A failed creation should remove the "creating" operation from the operations list
// and not persist the created resource in the snapshot.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 0)
}
func TestRecordingUpdateSuccess(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
resourceA.Inputs["key"] = resource.NewStringProperty("old")
resourceANew := NewResource("a")
resourceANew.Inputs["key"] = resource.NewStringProperty("new")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewUpdateStep(nil, &MockRegisterResourceEvent{}, resourceA, resourceANew, nil, nil, nil, nil)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the update mutation should have placed a pending "updating" operation into
// the operations list, with the resource's new inputs.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeUpdating, snap.PendingOperations[0].Type)
assert.Equal(t, resource.NewStringProperty("new"), snap.PendingOperations[0].Resource.Inputs["key"])
err = mutation.End(step, true /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// Completing the update should place the resource with the new inputs into the snapshot and clear the in
// flight operation.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
assert.Equal(t, resource.NewStringProperty("new"), snap.Resources[0].Inputs["key"])
}
func TestRecordingUpdateFailure(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
resourceA.Inputs["key"] = resource.NewStringProperty("old")
resourceANew := NewResource("a")
resourceANew.Inputs["key"] = resource.NewStringProperty("new")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewUpdateStep(nil, &MockRegisterResourceEvent{}, resourceA, resourceANew, nil, nil, nil, nil)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the update mutation should have placed a pending "updating" operation into
// the operations list, with the resource's new inputs.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeUpdating, snap.PendingOperations[0].Type)
assert.Equal(t, resource.NewStringProperty("new"), snap.PendingOperations[0].Resource.Inputs["key"])
err = mutation.End(step, false /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// Failing the update should keep the old resource with old inputs in the snapshot while clearing the
// in flight operation.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
assert.Equal(t, resource.NewStringProperty("old"), snap.Resources[0].Inputs["key"])
}
func TestRecordingDeleteSuccess(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewDeleteStep(nil, map[resource.URN]bool{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the delete mutation should have placed a pending "deleting" operation into the operations list.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeDeleting, snap.PendingOperations[0].Type)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
err = mutation.End(step, true /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A successful delete should remove the in flight operation and deleted resource from the snapshot.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 0)
}
func TestRecordingDeleteFailure(t *testing.T) {
t.Parallel()
resourceA := NewResource("a")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewDeleteStep(nil, map[resource.URN]bool{}, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the delete mutation should have placed a pending "deleting" operation into the operations list.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeDeleting, snap.PendingOperations[0].Type)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
err = mutation.End(step, false /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A failed delete should remove the in flight operation but leave the resource in the snapshot.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
}
func TestRecordingReadSuccessNoPreviousResource(t *testing.T) {
t.Parallel()
resourceA := NewResource("b")
resourceA.ID = "some-b"
resourceA.External = true
resourceA.Custom = true
snap := NewSnapshot(nil)
manager, sp := MockSetup(t, snap)
step := deploy.NewReadStep(nil, nil, nil, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the read mutation should have placed a pending "reading" operation into the operations list.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeReading, snap.PendingOperations[0].Type)
err = mutation.End(step, true /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A successful read should clear the in flight operation and put the new resource into the snapshot
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
}
func TestRecordingReadSuccessPreviousResource(t *testing.T) {
t.Parallel()
resourceA := NewResource("c")
resourceA.ID = "some-c"
resourceA.External = true
resourceA.Custom = true
resourceA.Inputs["key"] = resource.NewStringProperty("old")
resourceANew := NewResource("c")
resourceANew.ID = "some-other-c"
resourceANew.External = true
resourceANew.Custom = true
resourceANew.Inputs["key"] = resource.NewStringProperty("new")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewReadStep(nil, nil, resourceA, resourceANew)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the read mutation should have placed a pending "reading" operation into the operations list
// with the inputs of the new read
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeReading, snap.PendingOperations[0].Type)
assert.Equal(t, resource.NewStringProperty("new"), snap.PendingOperations[0].Resource.Inputs["key"])
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
assert.Equal(t, resource.NewStringProperty("old"), snap.Resources[0].Inputs["key"])
err = mutation.End(step, true /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A successful read should clear the in flight operation and replace the existing resource in the snapshot.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 1)
assert.Len(t, snap.PendingOperations, 0)
assert.Equal(t, resourceA.URN, snap.Resources[0].URN)
assert.Equal(t, resource.NewStringProperty("new"), snap.Resources[0].Inputs["key"])
}
func TestRecordingReadFailureNoPreviousResource(t *testing.T) {
t.Parallel()
resourceA := NewResource("d")
resourceA.ID = "some-d"
resourceA.External = true
resourceA.Custom = true
snap := NewSnapshot(nil)
manager, sp := MockSetup(t, snap)
step := deploy.NewReadStep(nil, nil, nil, resourceA)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the read mutation should have placed a pending "reading" operation into the operations list.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 1)
assert.Equal(t, resourceA.URN, snap.PendingOperations[0].Resource.URN)
assert.Equal(t, resource.OperationTypeReading, snap.PendingOperations[0].Type)
err = mutation.End(step, false /* successful */)
if !assert.NoError(t, err) {
t.FailNow()
}
// A failed read should clear the in flight operation and leave the snapshot empty.
snap = sp.LastSnap()
assert.Len(t, snap.Resources, 0)
assert.Len(t, snap.PendingOperations, 0)
}
func TestRecordingReadFailurePreviousResource(t *testing.T) {
t.Parallel()
resourceA := NewResource("e")
resourceA.ID = "some-e"
resourceA.External = true
resourceA.Custom = true
resourceA.Inputs["key"] = resource.NewStringProperty("old")
resourceANew := NewResource("e")
resourceANew.ID = "some-new-e"
resourceANew.External = true
resourceANew.Custom = true
resourceANew.Inputs["key"] = resource.NewStringProperty("new")
snap := NewSnapshot([]*resource.State{
resourceA,
})
manager, sp := MockSetup(t, snap)
step := deploy.NewReadStep(nil, nil, resourceA, resourceANew)
mutation, err := manager.BeginMutation(step)
if !assert.NoError(t, err) {
t.FailNow()
}
// Beginning the read mutation should have placed a pending "reading" operation into the operations list
// with the inputs of the new read
snap = sp.LastSnap()