github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/task_log_test.go (about) 1 package model 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/evergreen-ci/evergreen/db" 8 "github.com/evergreen-ci/evergreen/testutil" 9 . "github.com/smartystreets/goconvey/convey" 10 "gopkg.in/mgo.v2/bson" 11 ) 12 13 var taskLogTestConfig = testutil.TestConfig() 14 15 func init() { 16 db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(taskLogTestConfig)) 17 } 18 19 func cleanUpLogDB() error { 20 session, _, err := db.GetGlobalSessionFactory().GetSession() 21 if err != nil { 22 return err 23 } 24 defer session.Close() 25 _, err = session.DB(TaskLogDB).C(TaskLogCollection).RemoveAll(bson.M{}) 26 return err 27 } 28 29 func TestFindMostRecentTaskLogs(t *testing.T) { 30 31 Convey("When finding the most recent task logs", t, func() { 32 33 testutil.HandleTestingErr(cleanUpLogDB(), t, "Error cleaning up task log"+ 34 " database") 35 36 Convey("the ones with the most recent timestamp should be retrieved,"+ 37 " in backwards time order", func() { 38 39 // insert 10 logs 40 startTime := time.Now() 41 for i := 0; i < 10; i++ { 42 taskLog := &TaskLog{} 43 taskLog.TaskId = "task_id" 44 taskLog.MessageCount = i + 1 45 taskLog.Messages = []LogMessage{} 46 taskLog.Timestamp = startTime.Add( 47 time.Second * time.Duration(i)) 48 So(taskLog.Insert(), ShouldBeNil) 49 } 50 51 // get the last 5 logs from the database 52 fromDB, err := FindMostRecentTaskLogs("task_id", 0, 5) 53 So(err, ShouldBeNil) 54 So(len(fromDB), ShouldEqual, 5) 55 56 // this is kinda hacky, but it works 57 for i := 0; i < 5; i++ { 58 So(fromDB[i].MessageCount, ShouldEqual, 10-i) 59 } 60 61 }) 62 63 }) 64 65 } 66 67 func TestFindTaskLogsBeforeTime(t *testing.T) { 68 69 Convey("When finding task logs before a specified time", t, func() { 70 71 testutil.HandleTestingErr(cleanUpLogDB(), t, "Error cleaning up task log"+ 72 " database") 73 74 Convey("the specified number of task logs should be returned, in"+ 75 " backwards time order, all from before the specified"+ 76 " time", func() { 77 78 startTime := time.Now() 79 // insert 5 logs, 4 before the specified time 80 for i := 0; i < 5; i++ { 81 taskLog := &TaskLog{} 82 taskLog.TaskId = "task_id" 83 taskLog.MessageCount = 1 // to differentiate these 84 taskLog.Messages = []LogMessage{} 85 taskLog.Timestamp = startTime.Add( 86 time.Second * time.Duration(-i)) 87 So(taskLog.Insert(), ShouldBeNil) 88 } 89 // insert 4 logs, 3 after the specified time 90 for i := 0; i < 4; i++ { 91 taskLog := &TaskLog{} 92 taskLog.TaskId = "task_id" 93 taskLog.MessageCount = 0 94 taskLog.Messages = []LogMessage{} 95 taskLog.Timestamp = startTime.Add( 96 time.Second * time.Duration(i)) 97 So(taskLog.Insert(), ShouldBeNil) 98 } 99 100 fromDB, err := FindTaskLogsBeforeTime("task_id", 0, startTime, 10) 101 So(err, ShouldBeNil) 102 So(len(fromDB), ShouldEqual, 4) 103 for _, log := range fromDB { 104 So(log.MessageCount, ShouldEqual, 1) 105 } 106 107 }) 108 109 }) 110 111 } 112 113 func TestAddLogMessage(t *testing.T) { 114 115 Convey("When adding a log message to a task log", t, func() { 116 117 testutil.HandleTestingErr(cleanUpLogDB(), t, "Error cleaning up task log"+ 118 " database") 119 120 Convey("both the in-memory and database copies of the task log should"+ 121 " be updated", func() { 122 123 taskLog := &TaskLog{} 124 taskLog.TaskId = "task_id" 125 taskLog.MessageCount = 0 126 127 taskLog.Messages = []LogMessage{} 128 taskLog.Timestamp = time.Now() 129 130 So(taskLog.Insert(), ShouldBeNil) 131 132 // so that we get the _id 133 taskLogs, err := FindTaskLogsBeforeTime(taskLog.TaskId, 0, 134 time.Now().Add(time.Second), 1) 135 So(err, ShouldBeNil) 136 taskLog = &(taskLogs[0]) 137 138 for i := 0; i < 5; i++ { 139 logMsg := &LogMessage{} 140 logMsg.Message = "Hello" 141 logMsg.Severity = LogDebugPrefix 142 logMsg.Timestamp = time.Now() 143 logMsg.Type = SystemLogPrefix 144 So(taskLog.AddLogMessage(*logMsg), ShouldBeNil) 145 } 146 So(taskLog.MessageCount, ShouldEqual, 5) 147 So(len(taskLog.Messages), ShouldEqual, 5) 148 149 taskLogsFromDB, err := FindTaskLogsBeforeTime(taskLog.TaskId, 0, 150 time.Now().Add(time.Second), 1) 151 So(err, ShouldBeNil) 152 taskLogFromDB := &(taskLogsFromDB[0]) 153 So(err, ShouldBeNil) 154 So(taskLogFromDB.MessageCount, ShouldEqual, 5) 155 So(len(taskLogFromDB.Messages), ShouldEqual, 5) 156 157 }) 158 159 }) 160 161 } 162 163 func TestInsertLogMessage(t *testing.T) { 164 165 Convey("When inserting a log message", t, func() { 166 167 testutil.HandleTestingErr(cleanUpLogDB(), t, "Error cleaning up task log"+ 168 " database") 169 170 Convey("the log message should be added to the most recent task log"+ 171 " document for the task", func() { 172 173 startTime := time.Now() 174 for i := 0; i < MessagesPerLog*2; i++ { 175 logMsg := &LogMessage{} 176 logMsg.Severity = LogDebugPrefix 177 logMsg.Timestamp = startTime.Add(time.Second * time.Duration(i)) 178 logMsg.Type = SystemLogPrefix 179 So(logMsg.Insert("task_id", 0), ShouldBeNil) 180 } 181 182 fromDB, err := FindMostRecentTaskLogs("task_id", 0, 2) 183 So(err, ShouldBeNil) 184 So(len(fromDB), ShouldEqual, 2) 185 186 // since log saving happens as fire-and-forget, we're not entirely 187 // guaranteed that there will be the full number of messages in 188 // each task log document. however, there have to be enough in the 189 // first one (but there could be more) 190 So(fromDB[0].MessageCount >= MessagesPerLog, ShouldBeTrue) 191 So(len(fromDB[0].Messages), ShouldEqual, fromDB[0].MessageCount) 192 193 }) 194 195 }) 196 197 } 198 199 func TestFindMostRecentLogMessages(t *testing.T) { 200 201 Convey("When finding the most recent log messages", t, func() { 202 203 testutil.HandleTestingErr(cleanUpLogDB(), t, "Error cleaning up task log"+ 204 " database") 205 206 Convey("the specified number of log messages should be retrieved,"+ 207 " using the specified severity and log type filters", func() { 208 209 getRandomSeverity := func(i int) string { 210 switch i % 3 { 211 case 0: 212 return LogDebugPrefix 213 case 1: 214 return LogInfoPrefix 215 case 2: 216 return LogWarnPrefix 217 } 218 return LogErrorPrefix 219 } 220 221 getRandomMsgType := func(i int) string { 222 switch i % 2 { 223 case 0: 224 return SystemLogPrefix 225 case 1: 226 return TaskLogPrefix 227 } 228 return LogErrorPrefix 229 } 230 231 startTime := time.Now().Add(time.Second * time.Duration(-1000)) 232 // insert a lot of log messages 233 for i := 0; i < 150; i++ { 234 logMsg := &LogMessage{} 235 logMsg.Severity = getRandomSeverity(i) 236 logMsg.Type = getRandomMsgType(i) 237 logMsg.Message = "Hello" 238 logMsg.Timestamp = startTime.Add(time.Second * time.Duration(i)) 239 So(logMsg.Insert("task_id", 0), ShouldBeNil) 240 } 241 242 // now get the last 10 messages, unfiltered 243 fromDB, err := FindMostRecentLogMessages("task_id", 0, 10, []string{}, 244 []string{}) 245 So(err, ShouldBeNil) 246 So(len(fromDB), ShouldEqual, 10) 247 248 // filter by severity and log type 249 fromDB, err = FindMostRecentLogMessages("task_id", 0, 10, 250 []string{LogDebugPrefix}, []string{SystemLogPrefix}) 251 So(err, ShouldBeNil) 252 So(len(fromDB), ShouldEqual, 10) 253 for _, msgFromDB := range fromDB { 254 So(msgFromDB.Severity, ShouldEqual, LogDebugPrefix) 255 So(msgFromDB.Type, ShouldEqual, SystemLogPrefix) 256 } 257 258 // filter on multiple severities 259 fromDB, err = FindMostRecentLogMessages("task_id", 0, 10, 260 []string{LogDebugPrefix, LogInfoPrefix}, []string{}) 261 So(err, ShouldBeNil) 262 So(len(fromDB), ShouldEqual, 10) 263 for _, logMsg := range fromDB { 264 So(logMsg.Severity != LogDebugPrefix || logMsg.Severity != LogInfoPrefix, 265 ShouldBeTrue) 266 } 267 268 // filter on a non-existent severity 269 fromDB, err = FindMostRecentLogMessages("task_id", 0, 10, []string{""}, 270 []string{}) 271 So(err, ShouldBeNil) 272 So(len(fromDB), ShouldEqual, 0) 273 274 }) 275 276 }) 277 278 }