-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanup_test.go
1384 lines (1163 loc) · 43.4 KB
/
cleanup_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 2014-2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state_test
import (
"bytes"
"sort"
"time"
"github.com/juju/charm/v7"
"github.com/juju/errors"
"github.com/juju/names/v4"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/juju/caas"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/status"
"github.com/juju/juju/resource/resourcetesting"
"github.com/juju/juju/state"
"github.com/juju/juju/state/stateenvirons"
"github.com/juju/juju/state/storage"
"github.com/juju/juju/state/testing"
corestorage "github.com/juju/juju/storage"
"github.com/juju/juju/testing/factory"
)
type CleanupSuite struct {
ConnSuite
storageBackend *state.StorageBackend
}
var _ = gc.Suite(&CleanupSuite{})
func (s *CleanupSuite) SetUpTest(c *gc.C) {
s.ConnSuite.SetUpTest(c)
s.assertDoesNotNeedCleanup(c)
var err error
s.storageBackend, err = state.NewStorageBackend(s.State)
c.Assert(err, jc.ErrorIsNil)
}
func (s *CleanupSuite) TestCleanupDyingApplicationNoUnits(c *gc.C) {
mysql := s.AddTestingApplication(c, "mysql", s.AddTestingCharm(c, "mysql"))
c.Assert(mysql.Destroy(), jc.ErrorIsNil)
c.Assert(mysql.Refresh(), jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupDyingApplicationUnits(c *gc.C) {
// Create a application with some units.
mysql := s.AddTestingApplication(c, "mysql", s.AddTestingCharm(c, "mysql"))
units := make([]*state.Unit, 3)
for i := range units {
unit, err := mysql.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
units[i] = unit
}
preventUnitDestroyRemove(c, units[0])
s.assertDoesNotNeedCleanup(c)
// Destroy the application and check the units are unaffected, but a cleanup
// has been scheduled.
err := mysql.Destroy()
c.Assert(err, jc.ErrorIsNil)
for _, unit := range units {
err := unit.Refresh()
c.Assert(err, jc.ErrorIsNil)
}
s.assertNeedsCleanup(c)
// Run the cleanup, and check that units are all destroyed as appropriate.
s.assertCleanupRuns(c)
err = units[0].Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(units[0].Life(), gc.Equals, state.Dying)
err = units[1].Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
err = units[2].Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// Run a final cleanup to clear the cleanup scheduled for the unit that
// became dying.
s.assertCleanupCount(c, 1)
}
func (s *CleanupSuite) TestCleanupDyingApplicationCharm(c *gc.C) {
// Create a application and a charm.
ch := s.AddTestingCharm(c, "mysql")
mysql := s.AddTestingApplication(c, "mysql", ch)
// Create a dummy archive blob.
stor := storage.NewStorage(s.State.ModelUUID(), s.State.MongoSession())
storagePath := "dummy-path"
err := stor.Put(storagePath, bytes.NewReader([]byte("data")), 4)
c.Assert(err, jc.ErrorIsNil)
// Destroy the application and check that a cleanup has been scheduled.
err = mysql.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Run the cleanup, and check that the charm is removed.
s.assertCleanupRuns(c)
_, _, err = stor.Get(storagePath)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupRemoteApplication(c *gc.C) {
app, err := s.State.AddRemoteApplication(state.AddRemoteApplicationParams{
Name: "remote-app",
SourceModel: names.NewModelTag("test"),
Token: "token",
})
c.Assert(err, jc.ErrorIsNil)
err = app.Destroy()
c.Assert(err, jc.ErrorIsNil)
// Removed immediately since there are no relations yet.
s.assertDoesNotNeedCleanup(c)
_, err = s.State.RemoteApplication("remote-app")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupRemoteApplicationWithRelations(c *gc.C) {
mysqlEps := []charm.Relation{
{
Interface: "mysql",
Name: "db",
Role: charm.RoleProvider,
Scope: charm.ScopeGlobal,
},
}
remoteApp, err := s.State.AddRemoteApplication(state.AddRemoteApplicationParams{
Name: "mysql",
SourceModel: s.Model.ModelTag(),
Token: "t0",
Endpoints: mysqlEps,
})
c.Assert(err, jc.ErrorIsNil)
wordpress := s.AddTestingApplication(c, "wordpress", s.AddTestingCharm(c, "wordpress"))
eps, err := s.State.InferEndpoints("wordpress", "mysql")
c.Assert(err, jc.ErrorIsNil)
_, err = s.State.AddRelation(eps[0], eps[1])
c.Assert(err, jc.ErrorIsNil)
c.Assert(remoteApp.Refresh(), jc.ErrorIsNil)
c.Assert(wordpress.Refresh(), jc.ErrorIsNil)
err = remoteApp.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Run the cleanup, and check that the remote app is removed.
s.assertCleanupRuns(c)
_, err = s.State.RemoteApplication("mysql")
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupControllerModels(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a non-empty hosted model.
otherSt := s.Factory.MakeModel(c, nil)
defer otherSt.Close()
factory.NewFactory(otherSt, s.StatePool).MakeApplication(c, nil)
otherModel, err := otherSt.Model()
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Destroy the controller and check the model is unaffected, but a
// cleanup for the model and applications has been scheduled.
controllerModel, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
err = controllerModel.Destroy(state.DestroyModelParams{
DestroyHostedModels: true,
})
c.Assert(err, jc.ErrorIsNil)
// Two cleanups should be scheduled. One to destroy the hosted
// models, the other to destroy the controller model's
// applications.
s.assertCleanupCount(c, 1)
err = otherModel.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(otherModel.Life(), gc.Equals, state.Dying)
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupModelMachines(c *gc.C) {
// Create a controller machine, and manual and non-manual
// workload machine.
stateMachine, err := s.State.AddMachine("quantal", state.JobManageModel)
c.Assert(err, jc.ErrorIsNil)
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
manualMachine, err := s.State.AddOneMachine(state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
InstanceId: "inst-ance",
Nonce: "manual:foo",
})
c.Assert(err, jc.ErrorIsNil)
// Create a relation with a unit in scope and assigned to the hosted machine.
pr := newPeerRelation(c, s.State)
err = pr.u0.AssignToMachine(machine)
c.Assert(err, jc.ErrorIsNil)
preventPeerUnitsDestroyRemove(c, pr)
err = pr.ru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Destroy model, check cleanup queued.
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
force := true
err = model.Destroy(state.DestroyModelParams{Force: &force})
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Clean up, and check that the unit has been removed...
// There are 4 jobs for the destroy and then the model
// cleanup task queues another set because force is used.
s.assertCleanupCountDirty(c, 4)
assertRemoved(c, pr.u0)
// ...and the unit has departed relation scope...
assertNotJoined(c, pr.ru0)
// ...and the machine has been removed (since model destroy does a
// force-destroy on the machine).
c.Assert(machine.Refresh(), jc.Satisfies, errors.IsNotFound)
assertLife(c, manualMachine, state.Dying)
assertLife(c, stateMachine, state.Alive)
}
func (s *CleanupSuite) TestCleanupModelApplications(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a application with some units.
mysql := s.AddTestingApplication(c, "mysql", s.AddTestingCharm(c, "mysql"))
units := make([]*state.Unit, 3)
for i := range units {
unit, err := mysql.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
units[i] = unit
}
s.assertDoesNotNeedCleanup(c)
// Destroy the model and check the application and units are
// unaffected, but a cleanup for the application has been scheduled.
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
err = model.Destroy(state.DestroyModelParams{})
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
s.assertCleanupRuns(c)
err = mysql.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(mysql.Life(), gc.Equals, state.Dying)
for _, unit := range units {
err = unit.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(unit.Life(), gc.Equals, state.Alive)
}
// The first cleanup removes the units, which schedules
// the application to be removed. This removes the application
// queing up a change for actions and charms.
s.assertCleanupCount(c, 3)
for _, unit := range units {
err = unit.Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
// Now we should have all the cleanups done
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupRelationSettings(c *gc.C) {
// Create a relation with a unit in scope.
pr := newPeerRelation(c, s.State)
preventPeerUnitsDestroyRemove(c, pr)
rel := pr.ru0.Relation()
err := pr.ru0.EnterScope(map[string]interface{}{"some": "settings"})
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Destroy the application, check the relation's still around.
err = pr.app.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupCount(c, 2)
err = rel.Refresh()
c.Assert(err, jc.ErrorIsNil)
c.Assert(rel.Life(), gc.Equals, state.Dying)
// The unit leaves scope, triggering relation removal.
err = pr.ru0.LeaveScope()
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Settings are not destroyed yet...
settings, err := pr.ru1.ReadSettings("riak/0")
c.Assert(err, jc.ErrorIsNil)
c.Assert(settings, gc.DeepEquals, map[string]interface{}{"some": "settings"})
// ...but they are on cleanup.
s.assertCleanupCount(c, 1)
_, err = pr.ru1.ReadSettings("riak/0")
c.Assert(err, gc.ErrorMatches, `cannot read settings for unit "riak/0" in relation "riak:ring": unit "riak/0": settings not found`)
}
func (s *CleanupSuite) TestCleanupModelBranches(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
// Create a branch.
c.Assert(s.Model.AddBranch(newBranchName, newBranchCreator), jc.ErrorIsNil)
branches, err := s.State.Branches()
c.Assert(err, jc.ErrorIsNil)
c.Check(branches, gc.HasLen, 1)
s.assertDoesNotNeedCleanup(c)
// Destroy the model and check the branches unaffected, but a cleanup for
// the branches has been scheduled.
model, err := s.State.Model()
c.Assert(err, jc.ErrorIsNil)
err = model.Destroy(state.DestroyModelParams{})
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
s.assertCleanupCount(c, 1)
s.assertCleanupRuns(c)
err = model.Refresh()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupCount(c, 0)
_, err = s.Model.Branch(newBranchName)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
branches, err = s.State.Branches()
c.Assert(err, jc.ErrorIsNil)
c.Check(branches, gc.HasLen, 0)
// Now we should have all the cleanups done
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestDestroyControllerMachineErrors(c *gc.C) {
manager, err := s.State.AddMachine("quantal", state.JobManageModel)
c.Assert(err, jc.ErrorIsNil)
node, err := s.State.ControllerNode(manager.Id())
c.Assert(err, jc.ErrorIsNil)
node.SetHasVote(true)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = manager.Destroy()
c.Assert(err, gc.ErrorMatches, "controller 0 is the only controller")
s.assertDoesNotNeedCleanup(c)
assertLife(c, manager, state.Alive)
}
const dontWait = time.Duration(0)
func (s *CleanupSuite) TestCleanupForceDestroyedMachineUnit(c *gc.C) {
// Create a machine.
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
err = machine.SetProvisioned("inst-id", "", "fake_nonce", nil)
c.Assert(err, jc.ErrorIsNil)
// Create a relation with a unit in scope and assigned to the machine.
pr := newPeerRelation(c, s.State)
err = pr.u0.AssignToMachine(machine)
c.Assert(err, jc.ErrorIsNil)
preventPeerUnitsDestroyRemove(c, pr)
err = pr.ru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Force machine destruction, check cleanup queued.
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Clean up, and check that the unit has been removed...
s.assertCleanupCountDirty(c, 2)
assertRemoved(c, pr.u0)
// ...and the unit has departed relation scope...
assertNotJoined(c, pr.ru0)
// ...but that the machine remains, and is Dead, ready for removal by the
// provisioner.
assertLife(c, machine, state.Dead)
}
func (s *CleanupSuite) TestCleanupForceDestroyedControllerMachine(c *gc.C) {
machine, err := s.State.AddMachine("quantal", state.JobManageModel)
c.Assert(err, jc.ErrorIsNil)
node, err := s.State.ControllerNode(machine.Id())
c.Assert(err, jc.ErrorIsNil)
err = node.SetHasVote(true)
c.Assert(err, jc.ErrorIsNil)
changes, err := s.State.EnableHA(3, constraints.Value{}, "quantal", nil)
c.Assert(err, jc.ErrorIsNil)
c.Check(changes.Added, gc.HasLen, 2)
c.Check(changes.Removed, gc.HasLen, 0)
c.Check(changes.Maintained, gc.HasLen, 1)
c.Check(changes.Converted, gc.HasLen, 0)
for _, mid := range changes.Added {
m, err := s.State.Machine(mid)
c.Assert(err, jc.ErrorIsNil)
node, err := s.State.ControllerNode(m.Id())
c.Assert(err, jc.ErrorIsNil)
c.Assert(node.SetHasVote(true), jc.ErrorIsNil)
}
s.assertDoesNotNeedCleanup(c)
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
// The machine should no longer want the vote, should be forced to not have the vote, and forced to not be a
// controller member anymore
c.Assert(machine.Refresh(), jc.ErrorIsNil)
c.Check(machine.Life(), gc.Equals, state.Dying)
node, err = s.State.ControllerNode(machine.Id())
c.Assert(err, jc.ErrorIsNil)
c.Check(node.WantsVote(), jc.IsFalse)
c.Check(node.HasVote(), jc.IsTrue)
c.Check(machine.Jobs(), jc.DeepEquals, []state.MachineJob{state.JobManageModel})
controllerIds, err := s.State.ControllerIds()
c.Assert(err, jc.ErrorIsNil)
c.Check(controllerIds, gc.DeepEquals, append([]string{machine.Id()}, changes.Added...))
// ForceDestroy still won't kill the controller if it is flagged as having a vote
// We don't see the error because it is logged, but not returned.
s.assertCleanupRuns(c)
c.Assert(node.SetHasVote(false), jc.ErrorIsNil)
// However, if we remove the vote, it can be cleaned up.
// ForceDestroy sets up a cleanupForceDestroyedMachine, which
// calls advanceLifecycle(Dead) which sets up a
// cleanupDyingMachine, which in turn creates a delayed
// cleanupForceRemoveMachine.
// Run the first two.
s.assertCleanupCountDirty(c, 2)
// After we've run the cleanup for the controller machine, the machine should be dead, and it should not be
// present in the other documents.
assertLife(c, machine, state.Dead)
controllerIds, err = s.State.ControllerIds()
c.Assert(err, jc.ErrorIsNil)
sort.Strings(controllerIds)
sort.Strings(changes.Added)
// Only the machines that were added should still be part of the controller
c.Check(controllerIds, gc.DeepEquals, changes.Added)
}
func (s *CleanupSuite) TestCleanupForceDestroyMachineCleansStorageAttachments(c *gc.C) {
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = machine.SetProvisioned("inst-id", "", "fake_nonce", nil)
c.Assert(err, jc.ErrorIsNil)
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"data": makeStorageCons("loop", 1024, 1),
}
application := s.AddTestingApplicationWithStorage(c, "storage-block", ch, storage)
u, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
err = u.AssignToMachine(machine)
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// this tag matches the storage instance created for the unit above.
storageTag := names.NewStorageTag("data/0")
sa, err := s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(sa.Life(), gc.Equals, state.Alive)
// destroy machine and run cleanups
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
// Run cleanups to remove the unit and make the machine dead.
s.assertCleanupCountDirty(c, 2)
// After running the cleanups, the storage attachment should
// have been removed; the storage instance should be floating,
// and will be removed along with the machine.
_, err = s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
si, err := s.storageBackend.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
_, hasOwner := si.Owner()
c.Assert(hasOwner, jc.IsFalse)
// Check that the unit has been removed.
assertRemoved(c, u)
s.Clock.Advance(time.Minute)
// Check that the last cleanup to remove the machine runs.
s.assertCleanupCount(c, 1)
}
func (s *CleanupSuite) TestCleanupForceDestroyedMachineWithContainer(c *gc.C) {
// Create a machine with a container.
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
err = machine.SetProvisioned("inst-id", "", "fake_nonce", nil)
c.Assert(err, jc.ErrorIsNil)
container, err := s.State.AddMachineInsideMachine(state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
}, machine.Id(), instance.LXD)
c.Assert(err, jc.ErrorIsNil)
err = container.SetProvisioned("inst-id", "", "fake_nonce", nil)
c.Assert(err, jc.ErrorIsNil)
// Create active units (in relation scope, with subordinates).
prr := newProReqRelation(c, &s.ConnSuite, charm.ScopeContainer, machine, container)
prr.allEnterScope(c)
preventProReqUnitsDestroyRemove(c, prr)
s.assertDoesNotNeedCleanup(c)
// Force removal of the top-level machine.
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// And do it again, just to check that the second cleanup doc for the same
// machine doesn't cause problems down the line.
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
// Clean up, and check that the container has been removed...
s.assertCleanupCountDirty(c, 2)
err = container.Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// ...and so have all the units...
assertRemoved(c, prr.pu0)
assertRemoved(c, prr.pu1)
assertRemoved(c, prr.ru0)
assertRemoved(c, prr.ru1)
// ...and none of the units have left relation scopes occupied...
assertNotInScope(c, prr.pru0)
assertNotInScope(c, prr.pru1)
assertNotInScope(c, prr.rru0)
assertNotInScope(c, prr.rru1)
// ...but that the machine remains, and is Dead, ready for removal by the
// provisioner.
assertLife(c, machine, state.Dead)
}
func (s *CleanupSuite) TestForceDestroyMachineSchedulesRemove(c *gc.C) {
machine, err := s.State.AddMachine("quantal", state.JobHostUnits)
c.Assert(err, jc.ErrorIsNil)
err = machine.SetProvisioned("inst-id", "", "fake_nonce", nil)
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = machine.ForceDestroy(time.Minute)
c.Assert(err, jc.ErrorIsNil)
s.assertNeedsCleanup(c)
s.assertCleanupRuns(c)
assertLifeIs(c, machine, state.Dead)
// Running a cleanup pass succeeds but doesn't get rid of cleanups
// because there's a scheduled one.
s.assertCleanupRuns(c)
assertLifeIs(c, machine, state.Dead)
s.assertNeedsCleanup(c)
s.Clock.Advance(time.Minute)
s.assertCleanupCount(c, 1)
err = machine.Refresh()
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupDyingUnit(c *gc.C) {
// Create active unit, in a relation.
prr := newProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
preventProReqUnitsDestroyRemove(c, prr)
err := prr.pru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
// Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
err = prr.pu0.Destroy()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Refresh()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.pu0, state.Dying)
s.assertNeedsCleanup(c)
// Check it's reported in scope until cleaned up.
assertJoined(c, prr.pru0)
s.assertCleanupCount(c, 1)
assertInScope(c, prr.pru0)
assertNotJoined(c, prr.pru0)
// Destroy the relation, and check it sticks around...
err = prr.rel.Destroy()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.rel, state.Dying)
// ...until the unit is removed, and really leaves scope.
err = prr.pu0.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Remove()
c.Assert(err, jc.ErrorIsNil)
assertNotInScope(c, prr.pru0)
assertRemoved(c, prr.rel)
}
func (s *CleanupSuite) TestCleanupDyingUnitAlreadyRemoved(c *gc.C) {
// Create active unit, in a relation.
prr := newProReqRelation(c, &s.ConnSuite, charm.ScopeGlobal)
preventProReqUnitsDestroyRemove(c, prr)
err := prr.pru0.EnterScope(nil)
c.Assert(err, jc.ErrorIsNil)
// Destroy provider unit 0; check it's Dying, and a cleanup has been scheduled.
err = prr.pu0.Destroy()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Refresh()
c.Assert(err, jc.ErrorIsNil)
assertLife(c, prr.pu0, state.Dying)
s.assertNeedsCleanup(c)
// Remove the unit, and the relation.
err = prr.pu0.EnsureDead()
c.Assert(err, jc.ErrorIsNil)
err = prr.pu0.Remove()
c.Assert(err, jc.ErrorIsNil)
err = prr.rel.Destroy()
c.Assert(err, jc.ErrorIsNil)
assertRemoved(c, prr.rel)
// Check the cleanup still runs happily.
s.assertCleanupCount(c, 1)
s.assertCleanupRuns(c)
}
func (s *CleanupSuite) TestCleanupActions(c *gc.C) {
// Create a application with a unit.
dummy := s.AddTestingApplication(c, "dummy", s.AddTestingCharm(c, "dummy"))
unit, err := dummy.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
operationID, err := s.Model.EnqueueOperation("a test")
c.Assert(err, jc.ErrorIsNil)
// Add a couple actions to the unit
_, err = unit.AddAction(operationID, "snapshot", nil)
c.Assert(err, jc.ErrorIsNil)
_, err = unit.AddAction(operationID, "snapshot", nil)
c.Assert(err, jc.ErrorIsNil)
// make sure unit still has actions
actions, err := unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 2)
// destroy unit and run cleanups
err = dummy.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
// make sure unit still has actions, after first cleanup pass
actions, err = unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 2)
// second cleanup pass
s.assertCleanupRuns(c)
// make sure unit has no actions, after second cleanup pass
actions, err = unit.PendingActions()
c.Assert(err, jc.ErrorIsNil)
c.Assert(len(actions), gc.Equals, 0)
// Application has been cleaned up, but now we cleanup the charm
c.Assert(dummy.Refresh(), jc.Satisfies, errors.IsNotFound)
s.assertCleanupRuns(c)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupWithCompletedActions(c *gc.C) {
for _, status := range []state.ActionStatus{
state.ActionCompleted,
state.ActionCancelled,
state.ActionAborted,
state.ActionFailed,
} {
// Create a application with a unit.
dummy := s.AddTestingApplication(c, "dummy", s.AddTestingCharm(c, "dummy"))
unit, err := dummy.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
// Add a completed action to the unit.
operationID, err := s.Model.EnqueueOperation("a test")
c.Assert(err, jc.ErrorIsNil)
action, err := unit.AddAction(operationID, "snapshot", nil)
c.Assert(err, jc.ErrorIsNil)
action, err = action.Finish(state.ActionResults{
Status: status,
Message: "done",
})
c.Assert(err, jc.ErrorIsNil)
c.Assert(action.Status(), gc.Equals, status)
// Destroy application and run cleanups.
err = dummy.Destroy()
c.Assert(err, jc.ErrorIsNil)
// First cleanup marks all units of the application as dying.
// Second cleanup clear pending actions.
s.assertCleanupCount(c, 3)
}
}
func (s *CleanupSuite) TestCleanupStorageAttachments(c *gc.C) {
s.assertDoesNotNeedCleanup(c)
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"data": makeStorageCons("loop", 1024, 1),
}
application := s.AddTestingApplicationWithStorage(c, "storage-block", ch, storage)
u, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// this tag matches the storage instance created for the unit above.
storageTag := names.NewStorageTag("data/0")
sa, err := s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(sa.Life(), gc.Equals, state.Alive)
// destroy unit and run cleanups; the storage should be detached
err = u.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
// After running the cleanup, the attachment should be removed
// (short-circuited, because volume was never attached).
_, err = s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupStorageInstances(c *gc.C) {
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"allecto": makeStorageCons("modelscoped-block", 1024, 1),
}
application := s.AddTestingApplicationWithStorage(c, "storage-block", ch, storage)
u, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// this tag matches the storage instance created for the unit above.
storageTag := names.NewStorageTag("allecto/0")
si, err := s.storageBackend.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Alive)
// destroy storage instance and run cleanups
err = s.storageBackend.DestroyStorageInstance(storageTag, true, false, dontWait)
c.Assert(err, jc.ErrorIsNil)
si, err = s.storageBackend.StorageInstance(storageTag)
c.Assert(err, jc.ErrorIsNil)
c.Assert(si.Life(), gc.Equals, state.Dying)
sa, err := s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(sa.Life(), gc.Equals, state.Alive)
s.assertCleanupRuns(c)
// After running the cleanup, the attachment should be removed
// (short-circuited, because volume was never attached).
_, err = s.storageBackend.StorageAttachment(storageTag, u.UnitTag())
c.Assert(err, jc.Satisfies, errors.IsNotFound)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupMachineStorage(c *gc.C) {
ch := s.AddTestingCharm(c, "storage-block")
storage := map[string]state.StorageConstraints{
"data": makeStorageCons("modelscoped", 1024, 1),
}
application := s.AddTestingApplicationWithStorage(c, "storage-block", ch, storage)
unit, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
err = s.State.AssignUnit(unit, state.AssignCleanEmpty)
c.Assert(err, jc.ErrorIsNil)
machineId, err := unit.AssignedMachineId()
c.Assert(err, jc.ErrorIsNil)
machine, err := s.State.Machine(machineId)
c.Assert(err, jc.ErrorIsNil)
// Destroy the application, so we can destroy the machine.
err = unit.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
err = application.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
// destroy machine and run cleanups; the volume attachment
// should be marked dying.
err = machine.Destroy()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
vas, err := s.storageBackend.MachineVolumeAttachments(machine.MachineTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(vas, gc.HasLen, 1)
c.Assert(vas[0].Life(), gc.Equals, state.Dying)
// check no cleanups
s.assertDoesNotNeedCleanup(c)
}
func (s *CleanupSuite) TestCleanupCAASApplicationWithStorage(c *gc.C) {
s.assertCleanupCAASEntityWithStorage(c, func(st *state.State, app *state.Application) error {
op := app.DestroyOperation()
op.DestroyStorage = true
return st.ApplyOperation(op)
})
}
func (s *CleanupSuite) TestCleanupCAASUnitWithStorage(c *gc.C) {
s.assertCleanupCAASEntityWithStorage(c, func(st *state.State, app *state.Application) error {
units, err := app.AllUnits()
if err != nil {
return err
}
op := units[0].DestroyOperation()
op.DestroyStorage = true
return st.ApplyOperation(op)
})
}
func (s *CleanupSuite) assertCleanupCAASEntityWithStorage(c *gc.C, deleteOp func(*state.State, *state.Application) error) {
st := s.Factory.MakeCAASModel(c, nil)
defer st.Close()
sb, err := state.NewStorageBackend(st)
c.Assert(err, jc.ErrorIsNil)
model, err := st.Model()
c.Assert(err, jc.ErrorIsNil)
broker, err := stateenvirons.GetNewCAASBrokerFunc(caas.New)(model)
c.Assert(err, jc.ErrorIsNil)
registry := stateenvirons.NewStorageProviderRegistry(broker)
s.policy = testing.MockPolicy{
GetStorageProviderRegistry: func() (corestorage.ProviderRegistry, error) {
return registry, nil
},
}
ch := state.AddTestingCharmForSeries(c, st, "kubernetes", "storage-filesystem")
storCons := map[string]state.StorageConstraints{
"data": makeStorageCons("", 1024, 1),
}
application := state.AddTestingApplicationWithStorage(c, st, "storage-filesystem", ch, storCons)
unit, err := application.AddUnit(state.AddUnitParams{})
c.Assert(err, jc.ErrorIsNil)
fs, err := sb.AllFilesystems()
c.Assert(err, jc.ErrorIsNil)
c.Assert(fs, gc.HasLen, 1)
fas, err := sb.UnitFilesystemAttachments(unit.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(fas, gc.HasLen, 1)
err = deleteOp(st, application)
c.Assert(err, jc.ErrorIsNil)
err = application.Destroy()
c.Assert(err, jc.ErrorIsNil)
for i := 0; i < 4; i++ {
err = st.Cleanup()
c.Assert(err, jc.ErrorIsNil)
}
// check no cleanups
state.AssertNoCleanups(c, st)
fas, err = sb.UnitFilesystemAttachments(unit.UnitTag())
c.Assert(err, jc.ErrorIsNil)
c.Assert(fas, gc.HasLen, 0)
fs, err = sb.AllFilesystems()
c.Assert(err, jc.ErrorIsNil)
c.Assert(fs, gc.HasLen, 0)
}
func (s *CleanupSuite) TestCleanupVolumeAttachments(c *gc.C) {
_, err := s.State.AddOneMachine(state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Volumes: []state.HostVolumeParams{{
Volume: state.VolumeParams{Pool: "loop", Size: 1024},
}},
})
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = s.storageBackend.DestroyVolume(names.NewVolumeTag("0/0"), false)
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
attachment, err := s.storageBackend.VolumeAttachment(names.NewMachineTag("0"), names.NewVolumeTag("0/0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(attachment.Life(), gc.Equals, state.Dying)
}
func (s *CleanupSuite) TestCleanupFilesystemAttachments(c *gc.C) {
_, err := s.State.AddOneMachine(state.MachineTemplate{
Series: "quantal",
Jobs: []state.MachineJob{state.JobHostUnits},
Filesystems: []state.HostFilesystemParams{{
Filesystem: state.FilesystemParams{Pool: "rootfs", Size: 1024},
}},
})
c.Assert(err, jc.ErrorIsNil)
s.assertDoesNotNeedCleanup(c)
err = s.storageBackend.DestroyFilesystem(names.NewFilesystemTag("0/0"), false)
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
attachment, err := s.storageBackend.FilesystemAttachment(names.NewMachineTag("0"), names.NewFilesystemTag("0/0"))
c.Assert(err, jc.ErrorIsNil)
c.Assert(attachment.Life(), gc.Equals, state.Dying)
}
func (s *CleanupSuite) TestCleanupResourceBlob(c *gc.C) {
app := s.AddTestingApplication(c, "wp", s.AddTestingCharm(c, "wordpress"))
data := "ancient-debris"
res := resourcetesting.NewResource(c, nil, "mug", "wp", data).Resource
resources, err := s.State.Resources()
c.Assert(err, jc.ErrorIsNil)
_, err = resources.SetResource("wp", res.Username, res.Resource, bytes.NewBufferString(data))
c.Assert(err, jc.ErrorIsNil)
err = app.Destroy()
c.Assert(err, jc.ErrorIsNil)
path := "application-wp/resources/mug"
stateStorage := storage.NewStorage(s.State.ModelUUID(), s.State.MongoSession())
closer, _, err := stateStorage.Get(path)
c.Assert(err, jc.ErrorIsNil)
err = closer.Close()
c.Assert(err, jc.ErrorIsNil)
s.assertCleanupRuns(c)
_, _, err = stateStorage.Get(path)
c.Assert(err, jc.Satisfies, errors.IsNotFound)
}
func (s *CleanupSuite) TestCleanupResourceBlobHandlesMissing(c *gc.C) {
app := s.AddTestingApplication(c, "wp", s.AddTestingCharm(c, "wordpress"))
data := "ancient-debris"
res := resourcetesting.NewResource(c, nil, "mug", "wp", data).Resource
resources, err := s.State.Resources()
c.Assert(err, jc.ErrorIsNil)
_, err = resources.SetResource("wp", res.Username, res.Resource, bytes.NewBufferString(data))
c.Assert(err, jc.ErrorIsNil)