Commit 9e1d0676 authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Merge branch '6406-handle-422-artifact-upload' into 'master'

Handle 422 on artifact upload

Closes #6406

See merge request gitlab-org/gitlab-runner!1794
parents 61fe5753 5125700c
......@@ -111,6 +111,8 @@ func (c *ArtifactsUploaderCommand) createAndUpload() error {
return os.ErrPermission
case common.UploadTooLarge:
return errors.New("too large")
case common.UploadUnprocessableEntity:
return errors.New("unprocessable entity")
case common.UploadFailed:
return retryableErr{err: os.ErrInvalid}
default:
......
......@@ -76,6 +76,31 @@ func TestArtifactsUploaderForbidden(t *testing.T) {
assert.Equal(t, 1, network.uploadCalled)
}
func TestArtifactsUploaderUnprocessableEntityNoRetry(t *testing.T) {
network := &testNetwork{
uploadState: common.UploadUnprocessableEntity,
}
cmd := ArtifactsUploaderCommand{
JobCredentials: UploaderCredentials,
network: network,
fileArchiver: fileArchiver{
Paths: []string{artifactsTestArchivedFile},
},
}
writeTestFile(t, artifactsTestArchivedFile)
defer os.Remove(artifactsTestArchivedFile)
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
assert.Panics(t, func() {
cmd.Execute(nil)
})
assert.Equal(t, 1, network.uploadCalled)
}
func TestArtifactsUploaderRetry(t *testing.T) {
network := &testNetwork{
uploadState: common.UploadFailed,
......@@ -227,7 +252,7 @@ func TestArtifactsUploaderRawDoesNotSendMultipleFiles(t *testing.T) {
defer os.Remove(artifactsTestArchivedFile)
writeTestFile(t, artifactsTestArchivedFile2)
defer os.Remove(artifactsTestArchivedFile)
defer os.Remove(artifactsTestArchivedFile2)
removeHook := helpers.MakeFatalToPanic()
defer removeHook()
......
......@@ -5,7 +5,7 @@ import (
"fmt"
"io"
"gitlab.com/gitlab-org/gitlab-runner/helpers/url"
url_helpers "gitlab.com/gitlab-org/gitlab-runner/helpers/url"
)
type UpdateState int
......@@ -41,6 +41,7 @@ const (
UploadTooLarge
UploadForbidden
UploadFailed
UploadUnprocessableEntity
)
const (
......
......@@ -492,6 +492,9 @@ func (n *GitLabClient) UploadRawArtifacts(config common.JobCredentials, reader i
case http.StatusRequestEntityTooLarge:
log.WithField("status", res.Status).Errorln("Uploading artifacts to coordinator...", "too large archive")
return common.UploadTooLarge
case http.StatusUnprocessableEntity:
log.WithField("status", res.Status).Errorln("Uploading artifacts to coordinator...", "unprocessable entity")
return common.UploadUnprocessableEntity
default:
log.WithField("status", res.Status).Warningln("Uploading artifacts to coordinator...", "failed")
return common.UploadFailed
......
......@@ -968,35 +968,46 @@ func TestAbortedPatchTrace(t *testing.T) {
}
func checkTestArtifactsUploadHandlerContent(w http.ResponseWriter, r *http.Request, body string) {
switch body {
case "too-large":
w.WriteHeader(http.StatusRequestEntityTooLarge)
return
cases := map[string]struct {
formValueKey string
statusCode int
}{
"too-large": {
statusCode: http.StatusRequestEntityTooLarge,
},
"content": {
statusCode: http.StatusCreated,
},
"zip": {
statusCode: http.StatusCreated,
formValueKey: "artifact_format",
},
"gzip": {
statusCode: http.StatusCreated,
formValueKey: "artifact_format",
},
"junit": {
statusCode: http.StatusCreated,
formValueKey: "artifact_type",
},
"unprocessable-entity": {
statusCode: http.StatusUnprocessableEntity,
},
}
case "content":
w.WriteHeader(http.StatusCreated)
testCase, ok := cases[body]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
case "zip":
if r.FormValue("artifact_format") == "zip" {
w.WriteHeader(http.StatusCreated)
return
}
case "gzip":
if r.FormValue("artifact_format") == "gzip" {
w.WriteHeader(http.StatusCreated)
return
}
case "junit":
if r.FormValue("artifact_type") == "junit" {
w.WriteHeader(http.StatusCreated)
if len(testCase.formValueKey) > 0 {
if r.FormValue(testCase.formValueKey) != body {
return
}
}
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(testCase.statusCode)
}
func testArtifactsUploadHandler(w http.ResponseWriter, r *http.Request, t *testing.T) {
......@@ -1100,6 +1111,10 @@ func TestArtifactsUpload(t *testing.T) {
state = uploadArtifacts(c, invalidToken, tempFile.Name(), "", ArtifactFormatDefault)
assert.Equal(t, UploadForbidden, state, "Artifacts should be rejected if invalid token")
ioutil.WriteFile(tempFile.Name(), []byte("unprocessable-entity"), 0600)
state = uploadArtifacts(c, config, tempFile.Name(), "", ArtifactFormatDefault)
assert.Equal(t, UploadUnprocessableEntity, state, "Artifacts should be not uploaded, because of unprocessable entity response")
}
func testArtifactsDownloadHandler(w http.ResponseWriter, r *http.Request, t *testing.T) {
......
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