Verified Commit 3efd6d4a authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Update the format of /debug/jobs/list output

parent 74085754
......@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strings"
"regexp"
"sync"
"gitlab.com/gitlab-org/gitlab-runner/common"
......@@ -269,15 +270,23 @@ func (b *buildsHelper) Collect(ch chan<- prometheus.Metric) {
}
}
func CreateJobURL(projectURL string, jobID int) string {
r := regexp.MustCompile("(\\.git$)?")
URL := r.ReplaceAllString(projectURL, "")
return fmt.Sprintf("%s/-/jobs/%d", URL, jobID)
}
func (b *buildsHelper) ListJobsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
for _, job := range b.builds {
url := CreateJobURL(job.RepoCleanURL(), job.ID)
fmt.Fprintf(
w,
"id=%d url=%s state=%s stage=%s executor_stage=%s\n",
job.ID, job.RepoCleanURL(),
job.CurrentState, job.CurrentStage, job.CurrentExecutorStage(),
"url=%s state=%s stage=%s executor_stage=%s\n",
url, job.CurrentState, job.CurrentStage, job.CurrentExecutorStage(),
)
}
}
package commands
import (
"bytes"
"fmt"
"net/http"
"testing"
"github.com/prometheus/client_golang/prometheus"
......@@ -152,6 +155,72 @@ func getTestBuild() *common.Build {
build.ID = testBuildCurrentID
build.Runner = &runner
build.JobInfo = jobInfo
build.GitInfo = common.GitInfo{
RepoURL: "https://gitlab.example.com/my-namespace/my-project.git",
}
return build
}
type fakeResponseWriter struct {
output *bytes.Buffer
header http.Header
}
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) {}
type listJobsHandlerTest struct {
build *common.Build
expectedOutput string
}
func TestBuildsHandler_ListJobsHandler(t *testing.T) {
build := getTestBuild()
tests := map[string]listJobsHandlerTest{
"no jobs": {
build: nil,
expectedOutput: "",
},
"job exists": {
build: build,
expectedOutput: fmt.Sprintf("url=https://gitlab.example.com/my-namespace/my-project/-/jobs/%d", build.ID),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
writer := &fakeResponseWriter{
output: &bytes.Buffer{},
header: http.Header{},
}
b := &buildsHelper{}
b.addBuild(test.build)
b.ListJobsHandler(writer, &http.Request{})
if test.expectedOutput == "" {
assert.Empty(t, writer.output.String())
} else {
assert.Contains(t, writer.output.String(), test.expectedOutput)
}
})
}
}
func TestCreateJobURL(t *testing.T) {
testCases := map[string]string{
"http://gitlab.example.com/my-namespace/my-project.git": "http://gitlab.example.com/my-namespace/my-project/-/jobs/1",
"http://gitlab.example.com/my-namespace/my-project": "http://gitlab.example.com/my-namespace/my-project/-/jobs/1",
"http://gitlab.example.com/my-namespace/my.git.project.git": "http://gitlab.example.com/my-namespace/my.git.project/-/jobs/1",
"http://gitlab.example.com/my-namespace/my.git.project": "http://gitlab.example.com/my-namespace/my.git.project/-/jobs/1",
}
for URL, expectedURL := range testCases {
jobURL := CreateJobURL(URL, 1)
assert.Equal(t, expectedURL, jobURL)
}
}
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