Fix tests for: reply "insufficient storage" on disk full or over-quota

This commit is contained in:
Enrico204 2021-09-06 22:13:33 +02:00
parent 9b31f17188
commit fb5d63435a
2 changed files with 19 additions and 5 deletions

View file

@ -5,7 +5,6 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@ -397,7 +396,7 @@ func TestAbortedRequest(t *testing.T) {
// the first request is an upload to a file which blocks while reading the // the first request is an upload to a file which blocks while reading the
// body and then after some data returns an error // body and then after some data returns an error
rd := newDelayedErrorReader(errors.New("injected")) rd := newDelayedErrorReader(io.ErrUnexpectedEOF)
wg.Add(1) wg.Add(1)
go func() { go func() {

View file

@ -85,6 +85,10 @@ func httpMethodNotAllowed(w http.ResponseWriter, allowed []string) {
httpDefaultError(w, http.StatusMethodNotAllowed) httpDefaultError(w, http.StatusMethodNotAllowed)
} }
// errFileContentDoesntMatchHash is the error raised when the file content hash
// doesn't match the hash provided in the URL
var errFileContentDoesntMatchHash = errors.New("file content does not match hash")
// BlobPathRE matches valid blob URI paths with optional object IDs // BlobPathRE matches valid blob URI paths with optional object IDs
var BlobPathRE = regexp.MustCompile(`^/(data|index|keys|locks|snapshots)/([0-9a-f]{64})?$`) var BlobPathRE = regexp.MustCompile(`^/(data|index|keys|locks|snapshots)/([0-9a-f]{64})?$`)
@ -594,7 +598,7 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
// reject if file content doesn't match file name // reject if file content doesn't match file name
if err == nil && hex.EncodeToString(hasher.Sum(nil)) != objectID { if err == nil && hex.EncodeToString(hasher.Sum(nil)) != objectID {
err = fmt.Errorf("file content does not match hash") err = errFileContentDoesntMatchHash
} }
} }
@ -606,10 +610,21 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
log.Print(err) log.Print(err)
} }
var pathError *os.PathError var pathError *os.PathError
if errors.As(err, &pathError) && (pathError.Err == syscall.ENOSPC || pathError.Err == syscall.EDQUOT) { if errors.As(err, &pathError) && (pathError.Err == syscall.ENOSPC ||
pathError.Err == syscall.EDQUOT) {
// The error is disk-related (no space left, no quota left),
// notify the client using the correct HTTP status
httpDefaultError(w, http.StatusInsufficientStorage) httpDefaultError(w, http.StatusInsufficientStorage)
} else if errors.Is(err, errFileContentDoesntMatchHash) ||
errors.Is(err, io.ErrUnexpectedEOF) ||
errors.Is(err, http.ErrMissingBoundary) ||
errors.Is(err, http.ErrNotMultipart) {
// The error is connection-related, send a client-side HTTP status
httpDefaultError(w, http.StatusBadRequest)
} else { } else {
httpDefaultError(w, http.StatusInternalServerError) // Otherwise we have a different internal error, reply with
// server-side HTTP status
h.internalServerError(w, err)
} }
return return
} }