Skip to content
Snippets Groups Projects
Unverified Commit 703ab079 authored by Tomasz Maczukin's avatar Tomasz Maczukin
Browse files

Add raw build trace download handler

parent 768967cc
No related branches found
No related tags found
1 merge request!44Add raw build trace download handler
Pipeline #
Loading
Loading
@@ -53,6 +53,8 @@ type Response struct {
Archive string `json:"archive"`
// Entry is a filename inside the archive point to file that needs to be extracted
Entry string `json:"entry"`
// TraceFile is a filename of build trace file
TraceFile string `json:"trace_file"`
}
 
// singleJoiningSlash is taken from reverseproxy.go:NewSingleHostReverseProxy
Loading
Loading
package builds
import (
"bufio"
"errors"
"fmt"
"io"
"net/http"
"os"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
)
func readRawTrace(traceFile string, headers http.Header, output io.Writer) error {
file, err := os.Open(traceFile)
defer file.Close()
if err != nil {
return err
}
reader := bufio.NewReader(file)
headers.Set("Content-Type", "text/plain; charset=utf-8")
_, err = io.Copy(output, reader)
if err != nil {
return fmt.Errorf("Copy stdout of %v: %v", traceFile, err)
}
return nil
}
func RawTrace(myApi *api.API) http.Handler {
return myApi.PreAuthorizeHandler(func(writer http.ResponseWriter, req *http.Request, api *api.Response) {
if api.TraceFile == "" {
helper.Fail500(writer, errors.New("RawTrace: TraceFile is empty"))
return
}
err := readRawTrace(api.TraceFile, writer.Header(), writer)
if os.IsNotExist(err) {
http.NotFound(writer, req)
return
} else if err != nil {
helper.Fail500(writer, fmt.Errorf("RawTrace: %v", err))
}
}, "")
}
package builds
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/testhelper"
)
func testRawTraceDownloadServer(t *testing.T, traceFile string) *httptest.Server {
mux := http.NewServeMux()
mux.HandleFunc("/url/path", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Fatal("Expected GET request")
}
w.Header().Set("Content-Type", "application/json")
data, err := json.Marshal(&api.Response{
TraceFile: traceFile,
})
if err != nil {
t.Fatal(err)
}
w.Write(data)
})
return testhelper.TestServerWithHandler(nil, mux.ServeHTTP)
}
func testDownloadRawTrace(t *testing.T, ts *httptest.Server) *httptest.ResponseRecorder {
httpRequest, err := http.NewRequest("GET", ts.URL+"/url/path", nil)
if err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
apiClient := api.NewAPI(helper.URLMustParse(ts.URL), "123", nil)
RawTrace(apiClient).ServeHTTP(response, httpRequest)
return response
}
func TestDownloadRawTrace(t *testing.T) {
tempFile, err := ioutil.TempFile("", "build_trace")
if err != nil {
t.Fatal(err)
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
fmt.Fprint(tempFile, "BUILD TRACE")
ts := testRawTraceDownloadServer(t, tempFile.Name())
defer ts.Close()
response := testDownloadRawTrace(t, ts)
testhelper.AssertResponseCode(t, response, 200)
testhelper.AssertResponseHeader(t, response,
"Content-Type",
"text/plain; charset=utf-8")
testhelper.AssertResponseBody(t, response, "BUILD TRACE")
}
func TestRawTraceFromInvalidFile(t *testing.T) {
ts := testRawTraceDownloadServer(t, "path/to/non/existing/file")
defer ts.Close()
response := testDownloadRawTrace(t, ts)
testhelper.AssertResponseCode(t, response, 404)
}
func TestIncompleteApiResponse(t *testing.T) {
ts := testRawTraceDownloadServer(t, "")
defer ts.Close()
response := testDownloadRawTrace(t, ts)
testhelper.AssertResponseCode(t, response, 500)
}
Loading
Loading
@@ -6,6 +6,7 @@ import (
 
apipkg "gitlab.com/gitlab-org/gitlab-workhorse/internal/api"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/artifacts"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/builds"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/git"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/lfs"
proxypkg "gitlab.com/gitlab-org/gitlab-workhorse/internal/proxy"
Loading
Loading
@@ -61,6 +62,9 @@ func (u *Upstream) configureRoutes() {
route{"GET", regexp.MustCompile(projectPattern + `builds/[0-9]+/artifacts/file/`), contentEncodingHandler(artifacts.DownloadArtifact(api))},
route{"POST", regexp.MustCompile(ciAPIPattern + `v1/builds/[0-9]+/artifacts\z`), contentEncodingHandler(artifacts.UploadArtifacts(api, proxy))},
 
// CI RAW build trace
route{"GET", regexp.MustCompile(projectPattern + `builds/[0-9]+/raw\z`), contentEncodingHandler(builds.RawTrace(api))},
// Explicitly proxy API requests
route{"", regexp.MustCompile(apiPattern), proxy},
route{"", regexp.MustCompile(ciAPIPattern), proxy},
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment