func printLogsForTask(taskId string) { if !Verbose { return } logMessages, err := model.FindMostRecentLogMessages(taskId, 0, 100, []string{}, []string{}) if err != nil { panic(err) return } for i := len(logMessages) - 1; i >= 0; i-- { if logMessages[i].Type == model.SystemLogPrefix { continue } fmt.Println(logMessages[i].Message) } }
func getTaskLogs(taskId string, execution int, limit int, logType string, loggedIn bool) ([]model.LogMessage, error) { logTypeFilter := []string{} if logType != AllLogsType { logTypeFilter = []string{logType} } // auth stuff if !loggedIn { if logType == AllLogsType { logTypeFilter = []string{model.TaskLogPrefix} } if logType == model.AgentLogPrefix || logType == model.SystemLogPrefix { return []model.LogMessage{}, nil } } return model.FindMostRecentLogMessages(taskId, execution, limit, []string{}, logTypeFilter) }
func TestBasicEndpoints(t *testing.T) { setupTlsConfigs(t) for tlsString, tlsConfig := range tlsConfigs { testTask, _, err := setupAPITestData(testConfig, "task", "linux-64", NoPatch, t) testutil.HandleTestingErr(err, t, "Couldn't make test data: %v", err) Convey("With a live api server, agent, and test task over "+tlsString, t, func() { testServer, err := apiserver.CreateTestServer(testConfig, tlsConfig, plugin.APIPlugins, Verbose) testutil.HandleTestingErr(err, t, "Couldn't create apiserver: %v", err) testAgent, err := createAgent(testServer, testTask) testutil.HandleTestingErr(err, t, "failed to create agent: %v") Convey("sending logs should store the log messages properly", func() { msg1 := "task logger initialized!" msg2 := "system logger initialized!" msg3 := "exec logger initialized!" testAgent.logger.LogTask(slogger.INFO, msg1) testAgent.logger.LogSystem(slogger.INFO, msg2) testAgent.logger.LogExecution(slogger.INFO, msg3) time.Sleep(100 * time.Millisecond) testAgent.APILogger.FlushAndWait() // This returns logs in order of NEWEST first. logMessages, err := model.FindMostRecentLogMessages(testTask.Id, 0, 10, []string{}, []string{}) testutil.HandleTestingErr(err, t, "failed to get log msgs") So(logMessages[2].Message, ShouldEndWith, msg1) So(logMessages[1].Message, ShouldEndWith, msg2) So(logMessages[0].Message, ShouldEndWith, msg3) Convey("Task endpoints should work between agent and server", func() { testAgent.StartBackgroundActions(&NoopSignalHandler{}) Convey("calling GetTask should get retrieve same task back", func() { testTaskFromApi, err := testAgent.GetTask() So(err, ShouldBeNil) // ShouldResemble doesn't seem to work here, possibly because of // omitempty? anyways, just assert equality of the important fields So(testTaskFromApi.Id, ShouldEqual, testTask.Id) So(testTaskFromApi.Status, ShouldEqual, testTask.Status) So(testTaskFromApi.HostId, ShouldEqual, testTask.HostId) }) Convey("calling start should flip the task's status to started", func() { err := testAgent.Start("1") testutil.HandleTestingErr(err, t, "Couldn't start task: %v", err) testTask, err := model.FindTask(testTask.Id) testutil.HandleTestingErr(err, t, "Couldn't refresh task from db: %v", err) So(testTask.Status, ShouldEqual, evergreen.TaskStarted) testHost, err := host.FindOne(host.ByRunningTaskId(testTask.Id)) So(err, ShouldBeNil) So(testHost.Id, ShouldEqual, "testHost") So(testHost.RunningTask, ShouldEqual, testTask.Id) }) Convey("calling end() should update task status properly", func() { commandType := model.SystemCommandType description := "random" details := &apimodels.TaskEndDetail{ Description: description, Type: commandType, TimedOut: true, Status: evergreen.TaskSucceeded, } testAgent.End(details) time.Sleep(100 * time.Millisecond) taskUpdate, err := model.FindTask(testTask.Id) So(err, ShouldBeNil) So(taskUpdate.Status, ShouldEqual, evergreen.TaskSucceeded) So(taskUpdate.Details.Description, ShouldEqual, description) So(taskUpdate.Details.Type, ShouldEqual, commandType) So(taskUpdate.Details.TimedOut, ShouldEqual, true) }) Convey("no checkins should trigger timeout signal", func() { testAgent.idleTimeoutWatcher.SetDuration(2 * time.Second) testAgent.idleTimeoutWatcher.CheckIn() // sleep long enough for the timeout watcher to time out time.Sleep(3 * time.Second) timeoutSignal, ok := <-testAgent.signalHandler.idleTimeoutChan So(ok, ShouldBeTrue) So(timeoutSignal, ShouldEqual, IdleTimeout) }) }) }) }) } }