Commit 6378ac2a authored by Nick Thomas's avatar Nick Thomas

Merge branch 'id-refactor-artifacts-upload-test' into 'master'

Refactor internal/artifacts/artifacts_upload_test.go

See merge request gitlab-org/gitlab-workhorse!491
parents 65ce8016 3dfc4a8e
...@@ -53,7 +53,7 @@ func testUploadArtifactsFromTestZip(t *testing.T, ts *httptest.Server) *httptest ...@@ -53,7 +53,7 @@ func testUploadArtifactsFromTestZip(t *testing.T, ts *httptest.Server) *httptest
archiveData, _ := createTestZipArchive(t) archiveData, _ := createTestZipArchive(t)
contentBuffer, contentType := createTestMultipartForm(t, archiveData) contentBuffer, contentType := createTestMultipartForm(t, archiveData)
return testUploadArtifacts(contentType, &contentBuffer, t, ts) return testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
} }
func TestUploadHandlerSendingToExternalStorage(t *testing.T) { func TestUploadHandlerSendingToExternalStorage(t *testing.T) {
...@@ -135,7 +135,7 @@ func TestUploadHandlerSendingToExternalStorage(t *testing.T) { ...@@ -135,7 +135,7 @@ func TestUploadHandlerSendingToExternalStorage(t *testing.T) {
defer ts.Close() defer ts.Close()
contentBuffer, contentType := createTestMultipartForm(t, archiveData) contentBuffer, contentType := createTestMultipartForm(t, archiveData)
response := testUploadArtifacts(contentType, &contentBuffer, t, ts) response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.AssertResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent) testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent)
assert.Equal(t, 1, storeServerCalled, "store should be called only once") assert.Equal(t, 1, storeServerCalled, "store should be called only once")
...@@ -307,7 +307,7 @@ func TestUploadHandlerMultipartUploadSizeLimit(t *testing.T) { ...@@ -307,7 +307,7 @@ func TestUploadHandlerMultipartUploadSizeLimit(t *testing.T) {
defer ts.Close() defer ts.Close()
contentBuffer, contentType := createTestMultipartForm(t, make([]byte, uploadSize)) contentBuffer, contentType := createTestMultipartForm(t, make([]byte, uploadSize))
response := testUploadArtifacts(contentType, &contentBuffer, t, ts) response := testUploadArtifacts(t, contentType, ts.URL+Path, &contentBuffer)
testhelper.AssertResponseCode(t, response, http.StatusRequestEntityTooLarge) testhelper.AssertResponseCode(t, response, http.StatusRequestEntityTooLarge)
// Poll because AbortMultipartUpload is async // Poll because AbortMultipartUpload is async
......
...@@ -5,7 +5,6 @@ import ( ...@@ -5,7 +5,6 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"mime/multipart" "mime/multipart"
...@@ -32,11 +31,12 @@ const ( ...@@ -32,11 +31,12 @@ const (
MetadataHeaderKey = "Metadata-Status" MetadataHeaderKey = "Metadata-Status"
MetadataHeaderPresent = "present" MetadataHeaderPresent = "present"
MetadataHeaderMissing = "missing" MetadataHeaderMissing = "missing"
Path = "/url/path"
) )
func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProcessor func(w http.ResponseWriter, r *http.Request)) *httptest.Server { func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProcessor func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/url/path/authorize", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc(Path+"/authorize", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" { if r.Method != "POST" {
t.Fatal("Expected POST request") t.Fatal("Expected POST request")
} }
...@@ -49,7 +49,7 @@ func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProc ...@@ -49,7 +49,7 @@ func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProc
} }
w.Write(data) w.Write(data)
}) })
mux.HandleFunc("/url/path", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc(Path, func(w http.ResponseWriter, r *http.Request) {
opts := filestore.GetOpts(&authResponse) opts := filestore.GetOpts(&authResponse)
if r.Method != "POST" { if r.Method != "POST" {
...@@ -110,14 +110,42 @@ func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProc ...@@ -110,14 +110,42 @@ func testArtifactsUploadServer(t *testing.T, authResponse api.Response, bodyProc
return testhelper.TestServerWithHandler(nil, mux.ServeHTTP) return testhelper.TestServerWithHandler(nil, mux.ServeHTTP)
} }
func testUploadArtifacts(contentType string, body io.Reader, t *testing.T, ts *httptest.Server) *httptest.ResponseRecorder { type testServer struct {
httpRequest, err := http.NewRequest("POST", ts.URL+"/url/path", body) url string
if err != nil { writer *multipart.Writer
t.Fatal(err) buffer *bytes.Buffer
fileWriter io.Writer
cleanup func()
}
func setupWithTmpPath(t *testing.T, filename string, bodyProcessor func(w http.ResponseWriter, r *http.Request)) *testServer {
tempPath, err := ioutil.TempDir("", "uploads")
require.NoError(t, err)
ts := testArtifactsUploadServer(t, api.Response{TempPath: tempPath}, bodyProcessor)
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
fileWriter, err := writer.CreateFormFile(filename, "my.file")
require.NotNil(t, fileWriter)
require.NoError(t, err)
cleanup := func() {
ts.Close()
require.NoError(t, os.RemoveAll(tempPath))
require.NoError(t, writer.Close())
} }
return &testServer{url: ts.URL + Path, writer: writer, buffer: &buffer, fileWriter: fileWriter, cleanup: cleanup}
}
func testUploadArtifacts(t *testing.T, contentType, url string, body io.Reader) *httptest.ResponseRecorder {
httpRequest, err := http.NewRequest("POST", url, body)
require.NoError(t, err)
httpRequest.Header.Set("Content-Type", contentType) httpRequest.Header.Set("Content-Type", contentType)
response := httptest.NewRecorder() response := httptest.NewRecorder()
parsedURL := helper.URLMustParse(ts.URL) parsedURL := helper.URLMustParse(url)
roundTripper := roundtripper.NewTestBackendRoundTripper(parsedURL) roundTripper := roundtripper.NewTestBackendRoundTripper(parsedURL)
testhelper.ConfigureSecret() testhelper.ConfigureSecret()
apiClient := api.NewAPI(parsedURL, "123", roundTripper) apiClient := api.NewAPI(parsedURL, "123", roundTripper)
...@@ -127,13 +155,7 @@ func testUploadArtifacts(contentType string, body io.Reader, t *testing.T, ts *h ...@@ -127,13 +155,7 @@ func testUploadArtifacts(contentType string, body io.Reader, t *testing.T, ts *h
} }
func TestUploadHandlerAddingMetadata(t *testing.T) { func TestUploadHandlerAddingMetadata(t *testing.T) {
tempPath, err := ioutil.TempDir("", "uploads") s := setupWithTmpPath(t, "file",
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempPath)
ts := testArtifactsUploadServer(t, api.Response{TempPath: tempPath},
func(w http.ResponseWriter, r *http.Request) { func(w http.ResponseWriter, r *http.Request) {
jwtToken, err := jwt.Parse(r.Header.Get(upload.RewrittenFieldsHeader), testhelper.ParseJWT) jwtToken, err := jwt.Parse(r.Header.Get(upload.RewrittenFieldsHeader), testhelper.ParseJWT)
require.NoError(t, err) require.NoError(t, err)
...@@ -145,102 +167,49 @@ func TestUploadHandlerAddingMetadata(t *testing.T) { ...@@ -145,102 +167,49 @@ func TestUploadHandlerAddingMetadata(t *testing.T) {
require.Contains(t, rewrittenFields, "metadata") require.Contains(t, rewrittenFields, "metadata")
}, },
) )
defer ts.Close() defer s.cleanup()
var buffer bytes.Buffer archive := zip.NewWriter(s.fileWriter)
writer := multipart.NewWriter(&buffer) file, err := archive.Create("test.file")
file, err := writer.CreateFormFile("file", "my.file") require.NotNil(t, file)
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
archive := zip.NewWriter(file)
defer archive.Close()
fileInArchive, err := archive.Create("test.file") require.NoError(t, archive.Close())
if err != nil { require.NoError(t, s.writer.Close())
t.Fatal(err)
}
fmt.Fprint(fileInArchive, "test")
archive.Close()
writer.Close()
response := testUploadArtifacts(writer.FormDataContentType(), &buffer, t, ts) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.AssertResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent) testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderPresent)
} }
func TestUploadHandlerForUnsupportedArchive(t *testing.T) { func TestUploadHandlerForUnsupportedArchive(t *testing.T) {
tempPath, err := ioutil.TempDir("", "uploads") s := setupWithTmpPath(t, "file", nil)
if err != nil { defer s.cleanup()
t.Fatal(err) require.NoError(t, s.writer.Close())
}
defer os.RemoveAll(tempPath)
ts := testArtifactsUploadServer(t, api.Response{TempPath: tempPath}, nil) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
defer ts.Close()
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
file, err := writer.CreateFormFile("file", "my.file")
if err != nil {
t.Fatal(err)
}
fmt.Fprint(file, "test")
writer.Close()
response := testUploadArtifacts(writer.FormDataContentType(), &buffer, t, ts)
testhelper.AssertResponseCode(t, response, http.StatusOK) testhelper.AssertResponseCode(t, response, http.StatusOK)
testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderMissing) testhelper.AssertResponseHeader(t, response, MetadataHeaderKey, MetadataHeaderMissing)
} }
func TestUploadHandlerForMultipleFiles(t *testing.T) { func TestUploadHandlerForMultipleFiles(t *testing.T) {
tempPath, err := ioutil.TempDir("", "uploads") s := setupWithTmpPath(t, "file", nil)
if err != nil { defer s.cleanup()
t.Fatal(err)
}
defer os.RemoveAll(tempPath)
ts := testArtifactsUploadServer(t, api.Response{TempPath: tempPath}, nil)
defer ts.Close()
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
file, err := writer.CreateFormFile("file", "my.file")
if err != nil {
t.Fatal(err)
}
fmt.Fprint(file, "test")
file, err = writer.CreateFormFile("file", "my.file") file, err := s.writer.CreateFormFile("file", "my.file")
if err != nil { require.NotNil(t, file)
t.Fatal(err) require.NoError(t, err)
} require.NoError(t, s.writer.Close())
fmt.Fprint(file, "test")
writer.Close()
response := testUploadArtifacts(writer.FormDataContentType(), &buffer, t, ts) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.AssertResponseCode(t, response, http.StatusInternalServerError)
} }
func TestUploadFormProcessing(t *testing.T) { func TestUploadFormProcessing(t *testing.T) {
tempPath, err := ioutil.TempDir("", "uploads") s := setupWithTmpPath(t, "metadata", nil)
if err != nil { defer s.cleanup()
t.Fatal(err) require.NoError(t, s.writer.Close())
}
defer os.RemoveAll(tempPath)
ts := testArtifactsUploadServer(t, api.Response{TempPath: tempPath}, nil)
defer ts.Close()
var buffer bytes.Buffer
writer := multipart.NewWriter(&buffer)
file, err := writer.CreateFormFile("metadata", "my.file")
if err != nil {
t.Fatal(err)
}
fmt.Fprint(file, "test")
writer.Close()
response := testUploadArtifacts(writer.FormDataContentType(), &buffer, t, ts) response := testUploadArtifacts(t, s.writer.FormDataContentType(), s.url, s.buffer)
testhelper.AssertResponseCode(t, response, http.StatusInternalServerError) testhelper.AssertResponseCode(t, response, http.StatusInternalServerError)
} }
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