Merge pull request #11751 from wy65701436/fixes-11744

fix return code on getting non exist manifest
This commit is contained in:
Daniel Jiang 2020-04-27 12:06:37 +08:00 committed by GitHub
commit ef008fd4cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 6 deletions

View File

@ -14,7 +14,9 @@
package lib
import "context"
import (
"context"
)
type contextKey string

View File

@ -1,7 +1,6 @@
package contenttrust
import (
"fmt"
"net/http"
"github.com/goharbor/harbor/src/controller/artifact"
@ -36,7 +35,7 @@ func Middleware() func(http.Handler) http.Handler {
none := lib.ArtifactInfo{}
af := lib.GetArtifactInfo(ctx)
if af == none {
return fmt.Errorf("artifactinfo middleware required before this middleware")
return errors.New("artifactinfo middleware required before this middleware").WithCode(errors.NotFoundCode)
}
if len(af.Digest) == 0 {
art, err := artifact.Ctl.GetByReference(ctx, af.Repository, af.Reference, nil)

View File

@ -131,6 +131,14 @@ func (suite *MiddlewareTestSuite) TestContentTrustDisabled() {
suite.Equal(rr.Code, http.StatusOK)
}
func (suite *MiddlewareTestSuite) TestNoneArtifact() {
req := httptest.NewRequest("GET", "/v1/library/photon/manifests/nonexist", nil)
rr := httptest.NewRecorder()
Middleware()(suite.next).ServeHTTP(rr, req)
suite.Equal(rr.Code, http.StatusNotFound)
}
func (suite *MiddlewareTestSuite) TestAuthenticatedUserPulling() {
mock.OnAnything(suite.artifactController, "GetByReference").Return(suite.artifact, nil)
mock.OnAnything(suite.projectController, "GetByName").Return(suite.project, nil)

View File

@ -40,7 +40,7 @@ func handlePush(req *http.Request) error {
none := lib.ArtifactInfo{}
art := lib.GetArtifactInfo(req.Context())
if art == none {
return errors.New("cannot get the manifest information from request context")
return errors.New("cannot get the manifest information from request context").WithCode(errors.NotFoundCode)
}
af, err := artifact.Ctl.GetByReference(req.Context(), art.Repository, art.Tag, &artifact.Option{

View File

@ -48,7 +48,7 @@ func Middleware() func(http.Handler) http.Handler {
none := lib.ArtifactInfo{}
info := lib.GetArtifactInfo(ctx)
if info == none {
return fmt.Errorf("artifactinfo middleware required before this middleware")
return errors.New("artifactinfo middleware required before this middleware").WithCode(errors.NotFoundCode)
}
art, err := artifactController.GetByReference(ctx, info.Repository, info.Reference, nil)

View File

@ -127,7 +127,7 @@ func (suite *MiddlewareTestSuite) TestNoArtifactInfo() {
rr := httptest.NewRecorder()
Middleware()(suite.next).ServeHTTP(rr, req)
suite.Equal(rr.Code, http.StatusInternalServerError)
suite.Equal(rr.Code, http.StatusNotFound)
}
func (suite *MiddlewareTestSuite) TestGetArtifactFailed() {