mirror of
https://github.com/goharbor/harbor
synced 2025-04-25 08:23:53 +00:00
fix conformance test ErrorsCodes failure (#11961)
fixes #11945 When client calls PUT with an invalid digtest, Harbor has to give a 400 instead of 500. Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
parent
8a944e0181
commit
b1793e795c
@ -23,6 +23,8 @@ const (
|
||||
ViolateForeignKeyConstraintCode = "VIOLATE_FOREIGN_KEY_CONSTRAINT"
|
||||
// DIGESTINVALID ...
|
||||
DIGESTINVALID = "DIGEST_INVALID"
|
||||
// MANIFESTINVALID ...
|
||||
MANIFESTINVALID = "MANIFEST_INVALID"
|
||||
)
|
||||
|
||||
// NotFoundError is error for the case of object not found
|
||||
|
@ -30,6 +30,7 @@ var (
|
||||
codeMap = map[string]int{
|
||||
errors.BadRequestCode: http.StatusBadRequest,
|
||||
errors.DIGESTINVALID: http.StatusBadRequest,
|
||||
errors.MANIFESTINVALID: http.StatusBadRequest,
|
||||
errors.UnAuthorizedCode: http.StatusUnauthorized,
|
||||
errors.ForbiddenCode: http.StatusForbidden,
|
||||
errors.DENIED: http.StatusForbidden,
|
||||
|
@ -19,6 +19,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"github.com/goharbor/harbor/src/controller/blob"
|
||||
"github.com/goharbor/harbor/src/lib/errors"
|
||||
"github.com/goharbor/harbor/src/lib/log"
|
||||
"github.com/goharbor/harbor/src/pkg/blob/models"
|
||||
"github.com/goharbor/harbor/src/pkg/types"
|
||||
@ -42,7 +43,7 @@ func putManifestResources(r *http.Request, reference, referenceID string) (types
|
||||
manifest, descriptor, err := unmarshalManifest(r)
|
||||
if err != nil {
|
||||
logger.Errorf("unmarshal manifest failed, error: %v", err)
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "unmarshal manifest failed").WithCode(errors.MANIFESTINVALID)
|
||||
}
|
||||
|
||||
exist, err := blobController.Exist(r.Context(), descriptor.Digest.String(), blob.IsAssociatedWithProject(projectID))
|
||||
|
@ -16,8 +16,10 @@ package quota
|
||||
|
||||
import (
|
||||
"context"
|
||||
std_err "errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/docker/distribution/manifest/schema2"
|
||||
@ -272,6 +274,25 @@ func (suite *PutManifestMiddlewareTestSuite) TestResourcesWarning() {
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *PutManifestMiddlewareTestSuite) TestPutInvalid() {
|
||||
unmarshalManifest = func(r *http.Request) (distribution.Manifest, distribution.Descriptor, error) {
|
||||
return nil, distribution.Descriptor{}, std_err.New("json: cannot unmarshal string into Go value of type map")
|
||||
}
|
||||
|
||||
mock.OnAnything(suite.quotaController, "IsEnabled").Return(true, nil)
|
||||
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/v2/library/photon/manifests/sha256:totallywrong", strings.NewReader("YmxhYmxhYmxh"))
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
PutManifestMiddleware()(next).ServeHTTP(rr, req)
|
||||
suite.Equal(http.StatusBadRequest, rr.Code)
|
||||
|
||||
}
|
||||
|
||||
func TestPutManifestMiddlewareTestSuite(t *testing.T) {
|
||||
suite.Run(t, &PutManifestMiddlewareTestSuite{})
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user