Verified Commit 4b3735fb authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Print job duration

parent d1813557
......@@ -283,14 +283,15 @@ func CreateJobURL(projectURL string, jobID int) string {
func (b *buildsHelper) ListJobsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
for _, job := range b.builds {
url := CreateJobURL(job.RepoCleanURL(), job.ID)
fmt.Fprintf(
w,
"url=%s state=%s stage=%s executor_stage=%s\n",
url, job.CurrentState, job.CurrentStage, job.CurrentExecutorStage(),
"url=%s state=%s stage=%s executor_stage=%s duration=%s\n",
url, job.CurrentState, job.CurrentStage, job.CurrentExecutorStage(), job.Duration(),
)
}
}
......@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"net/http"
"regexp"
"testing"
"github.com/prometheus/client_golang/prometheus"
......@@ -163,17 +164,27 @@ func getTestBuild() *common.Build {
}
type fakeResponseWriter struct {
output *bytes.Buffer
header http.Header
output *bytes.Buffer
header http.Header
statusCode int
}
func (w *fakeResponseWriter) Header() http.Header { return w.header }
func (w *fakeResponseWriter) Write(data []byte) (int, error) { return w.output.Write(data) }
func (w *fakeResponseWriter) WriteHeader(statusCode int) {}
func (w *fakeResponseWriter) WriteHeader(statusCode int) { w.statusCode = statusCode }
func newFakeResponseWriter() *fakeResponseWriter {
return &fakeResponseWriter{
output: &bytes.Buffer{},
header: http.Header{},
}
}
type listJobsHandlerTest struct {
build *common.Build
expectedOutput string
expectedOutput []string
expectedRegexp []*regexp.Regexp
expectedStatus int
}
func TestBuildsHandler_ListJobsHandler(t *testing.T) {
......@@ -182,31 +193,41 @@ func TestBuildsHandler_ListJobsHandler(t *testing.T) {
tests := map[string]listJobsHandlerTest{
"no jobs": {
build: nil,
expectedOutput: "",
expectedStatus: http.StatusOK,
},
"job exists": {
build: build,
expectedOutput: fmt.Sprintf("url=https://gitlab.example.com/my-namespace/my-project/-/jobs/%d", build.ID),
build: build,
expectedOutput: []string{
fmt.Sprintf("url=https://gitlab.example.com/my-namespace/my-project/-/jobs/%d", build.ID),
},
expectedRegexp: []*regexp.Regexp{
regexp.MustCompile("duration=[0-9hms.]+"),
},
expectedStatus: http.StatusOK,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
writer := &fakeResponseWriter{
output: &bytes.Buffer{},
header: http.Header{},
}
writer := newFakeResponseWriter()
b := &buildsHelper{}
b.addBuild(test.build)
b.ListJobsHandler(writer, &http.Request{})
if test.expectedOutput == "" {
if len(test.expectedOutput) == 0 && len(test.expectedRegexp) == 0 {
assert.Empty(t, writer.output.String())
} else {
assert.Contains(t, writer.output.String(), test.expectedOutput)
for _, expectedOutput := range test.expectedOutput {
assert.Contains(t, writer.output.String(), expectedOutput)
}
for _, expectedRegexp := range test.expectedRegexp {
assert.Regexp(t, expectedRegexp, writer.output.String())
}
}
assert.Equal(t, test.expectedStatus, writer.statusCode)
})
}
}
......
......@@ -87,6 +87,8 @@ type Build struct {
executorStageResolver func() ExecutorStage
logger BuildLogger
allVariables JobVariables
startedAt time.Time
}
func (b *Build) Log() *logrus.Entry {
......@@ -291,6 +293,8 @@ func (b *Build) run(ctx context.Context, executor Executor) (err error) {
buildFinish <- b.executeScript(runContext, executor)
}()
b.startedAt = time.Now()
// Wait for signals: cancel, timeout, abort or finish
b.Log().Debugln("Waiting for signals...")
select {
......@@ -639,3 +643,7 @@ func (b *Build) GetCacheRequestTimeout() int {
}
return timeout
}
func (b *Build) Duration() time.Duration {
return time.Since(b.startedAt)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment