fix catalog pagenation issue

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
wang yan 2020-02-07 18:04:54 +08:00
parent 65dc54b059
commit beb7664b33
4 changed files with 359 additions and 4 deletions

View File

@ -75,6 +75,7 @@ func (r *repositoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
// handle the pagination
resRepos := repoNames
repoNamesLen := len(repoNames)
// with "last", get items form lastEntryIndex+1 to lastEntryIndex+maxEntries
// without "last", get items from 0 to maxEntries'
if lastEntry != "" {
@ -84,11 +85,23 @@ func (r *repositoryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
serror.SendError(w, err)
return
}
resRepos = repoNames[lastEntryIndex+1 : lastEntryIndex+maxEntries]
if lastEntryIndex+1+maxEntries > repoNamesLen {
resRepos = repoNames[lastEntryIndex+1 : repoNamesLen]
} else {
resRepos = repoNames[lastEntryIndex+1 : lastEntryIndex+1+maxEntries]
}
} else {
if maxEntries > repoNamesLen {
maxEntries = repoNamesLen
}
resRepos = repoNames[0:maxEntries]
}
if len(resRepos) == 0 {
r.sendResponse(w, req, resRepos)
return
}
// compare the last item to define whether return the link header.
// if equals, means that there is no more items in DB. Do not need to give the link header.
if repoNames[len(repoNames)-1] != resRepos[len(resRepos)-1] {

View File

@ -14,4 +14,146 @@
package registry
// TODO
import (
"encoding/json"
"github.com/goharbor/harbor/src/api/repository"
"github.com/goharbor/harbor/src/common/models"
repotesting "github.com/goharbor/harbor/src/testing/api/repository"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
"testing"
)
type catalogTestSuite struct {
suite.Suite
originalRepoCtl repository.Controller
repoCtl *repotesting.FakeController
}
func (c *catalogTestSuite) SetupSuite() {
c.originalRepoCtl = repository.Ctl
}
func (c *catalogTestSuite) SetupTest() {
c.repoCtl = &repotesting.FakeController{}
repository.Ctl = c.repoCtl
}
func (c *catalogTestSuite) TearDownTest() {
}
func (c *catalogTestSuite) TearDownSuite() {
repository.Ctl = c.originalRepoCtl
}
func (c *catalogTestSuite) TestCatalog() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("List").Return(2, []*models.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
},
{
RepositoryID: 2,
Name: "busybox",
},
}, nil)
w = httptest.NewRecorder()
newRepositoryHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Repositories []string `json:"repositories"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(2, len(ctlg.Repositories))
}
func (c *catalogTestSuite) TestCatalogPaginationN1() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("List").Return(2, []*models.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
},
{
RepositoryID: 2,
Name: "busybox",
},
}, nil)
w = httptest.NewRecorder()
newRepositoryHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Repositories []string `json:"repositories"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(1, len(ctlg.Repositories))
c.Equal("busybox", ctlg.Repositories[0])
}
func (c *catalogTestSuite) TestCatalogPaginationN2() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?n=3", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("List").Return(2, []*models.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
},
{
RepositoryID: 2,
Name: "busybox",
},
}, nil)
w = httptest.NewRecorder()
newRepositoryHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Repositories []string `json:"repositories"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(2, len(ctlg.Repositories))
c.Equal("hello-world", ctlg.Repositories[1])
}
func (c *catalogTestSuite) TestCatalogPaginationN3() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/_catalog?last=busybox&n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("List").Return(2, []*models.RepoRecord{
{
RepositoryID: 1,
Name: "hello-world",
},
{
RepositoryID: 2,
Name: "busybox",
},
}, nil)
w = httptest.NewRecorder()
newRepositoryHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Repositories []string `json:"repositories"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(1, len(ctlg.Repositories))
c.Equal("hello-world", ctlg.Repositories[0])
}
func TestCatalogTestSuite(t *testing.T) {
suite.Run(t, &catalogTestSuite{})
}

View File

@ -104,6 +104,7 @@ func (t *tagHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// handle the pagination
resTags := tagNames
tagNamesLen := len(tagNames)
// with "last", get items form lastEntryIndex+1 to lastEntryIndex+maxEntries
// without "last", get items from 0 to maxEntries'
if lastEntry != "" {
@ -113,11 +114,23 @@ func (t *tagHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
serror.SendError(w, err)
return
}
resTags = tagNames[lastEntryIndex+1 : lastEntryIndex+maxEntries]
if lastEntryIndex+1+maxEntries > tagNamesLen {
resTags = tagNames[lastEntryIndex+1 : tagNamesLen]
} else {
resTags = tagNames[lastEntryIndex+1 : lastEntryIndex+1+maxEntries]
}
} else {
if maxEntries > tagNamesLen {
maxEntries = tagNamesLen
}
resTags = tagNames[0:maxEntries]
}
if len(resTags) == 0 {
t.sendResponse(w, req, resTags)
return
}
// compare the last item to define whether return the link header.
// if equals, means that there is no more items in DB. Do not need to give the link header.
if tagNames[len(tagNames)-1] != resTags[len(resTags)-1] {

View File

@ -14,4 +14,191 @@
package registry
// TODO
import (
"encoding/json"
"github.com/goharbor/harbor/src/api/artifact"
"github.com/goharbor/harbor/src/api/repository"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/pkg/tag/model/tag"
arttesting "github.com/goharbor/harbor/src/testing/api/artifact"
repotesting "github.com/goharbor/harbor/src/testing/api/repository"
"github.com/stretchr/testify/suite"
"net/http"
"net/http/httptest"
"testing"
)
type tagTestSuite struct {
suite.Suite
originalRepoCtl repository.Controller
repoCtl *repotesting.FakeController
originalArtCtl artifact.Controller
artCtl *arttesting.FakeController
}
func (c *tagTestSuite) SetupSuite() {
c.originalArtCtl = artifact.Ctl
c.originalRepoCtl = repository.Ctl
}
func (c *tagTestSuite) SetupTest() {
c.artCtl = &arttesting.FakeController{}
artifact.Ctl = c.artCtl
c.repoCtl = &repotesting.FakeController{}
repository.Ctl = c.repoCtl
}
func (c *tagTestSuite) TearDownTest() {
}
func (c *tagTestSuite) TearDownSuite() {
repository.Ctl = c.originalRepoCtl
artifact.Ctl = c.originalArtCtl
}
func (c *tagTestSuite) TestListTag() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/library/hello-world/tags/list", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
RepositoryID: 1,
Name: "library/hello-world",
}, nil)
c.artCtl.On("ListTags").Return(2, []*artifact.Tag{
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v1",
},
},
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v2",
},
},
}, nil)
w = httptest.NewRecorder()
newTagHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var tagsAPIResponse struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&tagsAPIResponse)
c.Nil(err)
c.Equal(2, len(tagsAPIResponse.Tags))
}
func (c *tagTestSuite) TestListTagPagination1() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)
c.artCtl.On("ListTags").Return(2, []*artifact.Tag{
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v1",
},
},
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v2",
},
},
}, nil)
w = httptest.NewRecorder()
newTagHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(1, len(ctlg.Tags))
c.Equal("v1", ctlg.Tags[0])
}
func (c *tagTestSuite) TestListTagPagination2() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?n=3", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)
c.artCtl.On("ListTags").Return(2, []*artifact.Tag{
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v1",
},
},
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v2",
},
},
}, nil)
w = httptest.NewRecorder()
newTagHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(2, len(ctlg.Tags))
c.Equal("v2", ctlg.Tags[1])
}
func (c *tagTestSuite) TestListTagPagination3() {
c.SetupTest()
req := httptest.NewRequest(http.MethodGet, "/v2/hello-world/tags/list?last=v1&n=1", nil)
var w *httptest.ResponseRecorder
c.repoCtl.On("GetByName").Return(&models.RepoRecord{
RepositoryID: 1,
Name: "hello-world",
}, nil)
c.artCtl.On("ListTags").Return(2, []*artifact.Tag{
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v1",
},
},
{
Tag: tag.Tag{
RepositoryID: 1,
Name: "v2",
},
},
}, nil)
w = httptest.NewRecorder()
newTagHandler().ServeHTTP(w, req)
c.Equal(http.StatusOK, w.Code)
var ctlg struct {
Name string `json:"name"`
Tags []string `json:"tags"`
}
decoder := json.NewDecoder(w.Body)
err := decoder.Decode(&ctlg)
c.Nil(err)
c.Equal(1, len(ctlg.Tags))
c.Equal("v2", ctlg.Tags[0])
}
func TestTagTestSuite(t *testing.T) {
suite.Run(t, &tagTestSuite{})
}