Ejemplo n.º 1
0
func (uis *UIServer) testLog(w http.ResponseWriter, r *http.Request) {
	logId := mux.Vars(r)["log_id"]
	var testLog *model.TestLog
	var err error

	if logId != "" { // direct link to a log document by its ID
		testLog, err = model.FindOneTestLogById(logId)
		if err != nil {
			uis.LoggedError(w, r, http.StatusInternalServerError, err)
			return
		}
	} else {
		taskID := mux.Vars(r)["task_id"]
		testName := mux.Vars(r)["test_name"]
		taskExecutionsAsString := mux.Vars(r)["task_execution"]
		taskExec, err := strconv.Atoi(taskExecutionsAsString)
		if err != nil {
			http.Error(w, "task execution num must be an int", http.StatusBadRequest)
			return
		}

		testLog, err = model.FindOneTestLog(testName, taskID, taskExec)
		if err != nil {
			uis.LoggedError(w, r, http.StatusInternalServerError, err)
			return
		}
	}

	if testLog == nil {
		http.Error(w, "not found", http.StatusNotFound)
		return
	}

	displayLogs := make(chan model.LogMessage)
	go func() {
		for _, line := range testLog.Lines {
			displayLogs <- model.LogMessage{
				Type:     model.TaskLogPrefix,
				Severity: model.LogInfoPrefix,
				Version:  evergreen.LogmessageCurrentVersion,
				Message:  line,
			}
		}
		close(displayLogs)
	}()

	template := "task_log.html"

	if (r.FormValue("raw") == "1") || (r.Header.Get("Content-type") == "text/plain") {
		template = "task_log_raw.html"
		w.Header().Set("Content-Type", "text/plain")
	}

	uis.WriteHTML(w, http.StatusOK, struct {
		Data chan model.LogMessage
		User *user.DBUser
	}{displayLogs, GetUser(r)}, "base", template)
}
Ejemplo n.º 2
0
func TestAttachRawResults(t *testing.T) {
	resetTasks(t)
	testConfig := evergreen.TestConfig()
	cwd := testutil.GetDirectoryOfFile()
	Convey("With attachResults plugin installed into plugin registry", t, func() {
		registry := plugin.NewSimpleRegistry()
		attachPlugin := &AttachPlugin{}
		err := registry.Register(attachPlugin)
		testutil.HandleTestingErr(err, t, "Couldn't register plugin: %v")

		server, err := service.CreateTestServer(testConfig, nil, plugin.APIPlugins, true)
		testutil.HandleTestingErr(err, t, "Couldn't set up testing server")
		httpCom := plugintest.TestAgentCommunicator("mocktaskid", "mocktasksecret", server.URL)
		configFile := filepath.Join(cwd, "testdata", "plugin_attach_results_raw.yml")
		resultsLoc := filepath.Join(cwd, "testdata", "plugin_attach_results_raw.json")
		taskConfig, err := plugintest.CreateTestConfig(configFile, t)
		testutil.HandleTestingErr(err, t, "failed to create test config: %v")
		taskConfig.WorkDir = "."
		sliceAppender := &evergreen.SliceAppender{[]*slogger.Log{}}
		logger := agentutil.NewTestLogger(sliceAppender)

		Convey("when attaching a raw log ", func() {
			for _, projTask := range taskConfig.Project.Tasks {
				So(len(projTask.Commands), ShouldNotEqual, 0)
				for _, command := range projTask.Commands {

					pluginCmds, err := registry.GetCommands(command, taskConfig.Project.Functions)
					testutil.HandleTestingErr(err, t, "Couldn't get plugin command: %v")
					So(pluginCmds, ShouldNotBeNil)
					So(err, ShouldBeNil)
					// create a plugin communicator
					pluginCom := &comm.TaskJSONCommunicator{pluginCmds[0].Plugin(), httpCom}
					err = pluginCmds[0].Execute(logger, pluginCom, taskConfig, make(chan bool))
					So(err, ShouldBeNil)
					Convey("when retrieving task", func() {
						// fetch the task
						testTask, err := task.FindOne(task.ById(httpCom.TaskId))
						testutil.HandleTestingErr(err, t, "Couldn't find task")
						So(testTask, ShouldNotBeNil)

						Convey("test results should match and raw log should be in appropriate collection", func() {

							reportFile, err := os.Open(resultsLoc)
							testutil.HandleTestingErr(err, t, "Couldn't open report file: '%v'", err)
							results := &task.TestResults{}
							err = util.ReadJSONInto(reportFile, results)
							testutil.HandleTestingErr(err, t, "Couldn't read report file: '%v'", err)

							testResults := *results
							So(len(testResults.Results), ShouldEqual, 3)
							So(len(testTask.TestResults), ShouldEqual, 3)
							firstResult := testTask.TestResults[0]
							So(firstResult.LogRaw, ShouldEqual, "")
							So(firstResult.LogId, ShouldNotEqual, "")

							testLog, err := model.FindOneTestLogById(firstResult.LogId)
							So(err, ShouldBeNil)
							So(testLog.Lines[0], ShouldEqual, testResults.Results[0].LogRaw)

							Convey("both URL and raw log should be stored appropriately if both exist", func() {
								urlResult := testTask.TestResults[2]
								So(urlResult.LogRaw, ShouldEqual, "")
								So(urlResult.URL, ShouldNotEqual, "")
								So(urlResult.LogId, ShouldNotEqual, "")

								testLog, err := model.FindOneTestLogById(urlResult.LogId)
								So(err, ShouldBeNil)
								So(testLog.Lines[0], ShouldEqual, testResults.Results[2].LogRaw)
							})
						})
					})

				}
			}
		})
	})
}