github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/worker/dblogpruner/worker_test.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package dblogpruner_test 5 6 import ( 7 stdtesting "testing" 8 "time" 9 10 "github.com/juju/loggo" 11 jc "github.com/juju/testing/checkers" 12 gc "gopkg.in/check.v1" 13 "gopkg.in/juju/names.v2" 14 "gopkg.in/mgo.v2" 15 "gopkg.in/mgo.v2/bson" 16 17 "github.com/juju/juju/state" 18 statetesting "github.com/juju/juju/state/testing" 19 "github.com/juju/juju/testing" 20 "github.com/juju/juju/version" 21 "github.com/juju/juju/worker" 22 "github.com/juju/juju/worker/dblogpruner" 23 ) 24 25 func TestPackage(t *stdtesting.T) { 26 testing.MgoTestPackage(t) 27 } 28 29 var _ = gc.Suite(&suite{}) 30 31 type suite struct { 32 statetesting.StateSuite 33 pruner worker.Worker 34 logsColl *mgo.Collection 35 } 36 37 func (s *suite) SetUpTest(c *gc.C) { 38 s.StateSuite.SetUpTest(c) 39 s.logsColl = s.State.MongoSession().DB("logs").C("logs") 40 } 41 42 func (s *suite) StartWorker(c *gc.C, maxLogAge time.Duration, maxCollectionMB int) { 43 params := &dblogpruner.LogPruneParams{ 44 MaxLogAge: maxLogAge, 45 MaxCollectionMB: maxCollectionMB, 46 PruneInterval: time.Millisecond, // Speed up pruning interval for testing 47 } 48 s.pruner = dblogpruner.New(s.State, params) 49 s.AddCleanup(func(*gc.C) { 50 s.pruner.Kill() 51 c.Assert(s.pruner.Wait(), jc.ErrorIsNil) 52 }) 53 } 54 55 func (s *suite) TestPrunesOldLogs(c *gc.C) { 56 maxLogAge := 24 * time.Hour 57 noPruneMB := int(1e9) 58 s.StartWorker(c, maxLogAge, noPruneMB) 59 60 now := time.Now() 61 addLogsToPrune := func(count int) { 62 // Add messages beyond the prune threshold. 63 tPrune := now.Add(-maxLogAge - 1) 64 s.addLogs(c, tPrune, "prune", count) 65 } 66 addLogsToKeep := func(count int) { 67 // Add messages within the prune threshold. 68 s.addLogs(c, now, "keep", count) 69 } 70 for i := 0; i < 10; i++ { 71 addLogsToKeep(5) 72 addLogsToPrune(5) 73 } 74 75 // Wait for all logs with the message "prune" to be removed. 76 for attempt := testing.LongAttempt.Start(); attempt.Next(); { 77 pruneRemaining, err := s.logsColl.Find(bson.M{"x": "prune"}).Count() 78 c.Assert(err, jc.ErrorIsNil) 79 if pruneRemaining == 0 { 80 // All the "keep" messages should still be there. 81 keepCount, err := s.logsColl.Find(bson.M{"x": "keep"}).Count() 82 c.Assert(err, jc.ErrorIsNil) 83 c.Assert(keepCount, gc.Equals, 50) 84 return 85 } 86 } 87 c.Fatal("pruning didn't happen as expected") 88 } 89 90 func (s *suite) TestPrunesLogsBySize(c *gc.C) { 91 startingLogCount := 25000 92 s.addLogs(c, time.Now(), "stuff", startingLogCount) 93 94 noPruneAge := 999 * time.Hour 95 s.StartWorker(c, noPruneAge, 2) 96 97 for attempt := testing.LongAttempt.Start(); attempt.Next(); { 98 count, err := s.logsColl.Count() 99 c.Assert(err, jc.ErrorIsNil) 100 // The space used by MongoDB by the collection isn't that 101 // predictable, so just treat any pruning due to size as 102 // success. 103 if count < startingLogCount { 104 return 105 } 106 } 107 c.Fatal("pruning didn't happen as expected") 108 } 109 110 func (s *suite) addLogs(c *gc.C, t0 time.Time, text string, count int) { 111 dbLogger := state.NewDbLogger(s.State, names.NewMachineTag("0"), version.Current) 112 defer dbLogger.Close() 113 114 for offset := 0; offset < count; offset++ { 115 t := t0.Add(-time.Duration(offset) * time.Second) 116 dbLogger.Log(t, "some.module", "foo.go:42", loggo.INFO, text) 117 } 118 }