0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-20 22:52:51 -05:00
zot/pkg/meta/repodb/pagination_test.go
LaurentiuNiculae 9cc990d7ca
feat(repodb): add user related information to repodb (#1317)
Initial code was contributed by Bogdan BIVOLARU <104334+bogdanbiv@users.noreply.github.com>
Moved implementation from a separate db to repodb by Andrei Aaron <aaaron@luxoft.com>

Not done yet:
- run/test dynamodb implementation, only boltdb was tested
- add additional coverage for existing functionality
- add web-based APIs to toggle the stars/bookmarks on/off

Initially graphql mutation was discussed for the missing API but
we decided REST endpoints would be better suited for configuration



feat(userdb): complete functionality for userdb integration

- dynamodb rollback changes to user starred repos in case increasing the total star count fails
- dynamodb increment/decrement repostars in repometa when user stars/unstars a repo
- dynamodb check anonymous user permissions are working as intendend
- common test handle anonymous users
- RepoMeta2RepoSummary set IsStarred and IsBookmarked



feat(userdb): rest api calls for toggling stars/bookmarks on/off



test(userdb): blackbox tests



test(userdb): move preferences tests in a different file with specific build tags



feat(repodb): add is-starred and is-bookmarked fields to repo-meta

- removed duplicated logic for determining if a repo is starred/bookmarked

Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
Co-authored-by: Andrei Aaron <aaaron@luxoft.com>
2023-04-24 11:13:15 -07:00

240 lines
6.7 KiB
Go

package repodb_test
import (
"testing"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
"zotregistry.io/zot/pkg/meta/repodb"
)
func TestPagination(t *testing.T) {
Convey("Repo Pagination", t, func() {
Convey("reset", func() {
pageFinder, err := repodb.NewBaseRepoPageFinder(1, 0, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Reset()
result, _ := pageFinder.Page()
So(result, ShouldBeEmpty)
})
})
Convey("Image Pagination", t, func() {
Convey("create new pageFinder errors", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(-1, 10, repodb.AlphabeticAsc)
So(pageFinder, ShouldBeNil)
So(err, ShouldNotBeNil)
pageFinder, err = repodb.NewBaseImagePageFinder(2, -1, repodb.AlphabeticAsc)
So(pageFinder, ShouldBeNil)
So(err, ShouldNotBeNil)
pageFinder, err = repodb.NewBaseImagePageFinder(2, 1, "wrong sorting criteria")
So(pageFinder, ShouldBeNil)
So(err, ShouldNotBeNil)
})
Convey("Reset", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(1, 0, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Add(repodb.DetailedRepoMeta{})
pageFinder.Reset()
result, _ := pageFinder.Page()
So(result, ShouldBeEmpty)
})
Convey("Page", func() {
Convey("no limit or offset", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(0, 0, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo1",
Tags: map[string]repodb.Descriptor{
"tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageManifest},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo2",
Tags: map[string]repodb.Descriptor{
"Tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageManifest},
"Tag2": {Digest: "dig2", MediaType: ispec.MediaTypeImageManifest},
"Tag3": {Digest: "dig3", MediaType: ispec.MediaTypeImageManifest},
"Tag4": {Digest: "dig4", MediaType: ispec.MediaTypeImageManifest},
},
},
})
_, pageInfo := pageFinder.Page()
So(pageInfo.ItemCount, ShouldEqual, 5)
So(pageInfo.TotalCount, ShouldEqual, 5)
})
Convey("Test 1 limit < len(tags)", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(5, 2, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo1",
Tags: map[string]repodb.Descriptor{
"tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageManifest},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo2",
Tags: map[string]repodb.Descriptor{
"Tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageManifest},
"Tag2": {Digest: "dig2", MediaType: ispec.MediaTypeImageManifest},
"Tag3": {Digest: "dig3", MediaType: ispec.MediaTypeImageManifest},
"Tag4": {Digest: "dig4", MediaType: ispec.MediaTypeImageManifest},
},
},
})
_, pageInfo := pageFinder.Page()
So(pageInfo.ItemCount, ShouldEqual, 3)
So(pageInfo.TotalCount, ShouldEqual, 5)
})
Convey("Test 2 limit < len(tags)", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(5, 2, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo1",
Tags: map[string]repodb.Descriptor{
"tag1": {
Digest: "dig1",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo2",
Tags: map[string]repodb.Descriptor{
"Tag1": {
Digest: "dig1",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag2": {
Digest: "dig2",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag3": {
Digest: "dig3",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag4": {
Digest: "dig4",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo3",
Tags: map[string]repodb.Descriptor{
"Tag11": {
Digest: "dig11",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag12": {
Digest: "dig12",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag13": {
Digest: "dig13",
MediaType: ispec.MediaTypeImageManifest,
},
"Tag14": {
Digest: "dig14",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
result, pageInfo := pageFinder.Page()
So(result[0].Tags, ShouldContainKey, "Tag2")
So(result[0].Tags, ShouldContainKey, "Tag3")
So(result[0].Tags, ShouldContainKey, "Tag4")
So(result[1].Tags, ShouldContainKey, "Tag11")
So(result[1].Tags, ShouldContainKey, "Tag12")
So(pageInfo.ItemCount, ShouldEqual, 5)
So(pageInfo.TotalCount, ShouldEqual, 9)
})
Convey("Test 2 limit > len(tags)", func() {
pageFinder, err := repodb.NewBaseImagePageFinder(3, 0, repodb.AlphabeticAsc)
So(err, ShouldBeNil)
So(pageFinder, ShouldNotBeNil)
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo1",
Tags: map[string]repodb.Descriptor{
"tag1": {
Digest: "dig1",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo2",
Tags: map[string]repodb.Descriptor{
"Tag1": {
Digest: "dig1",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
pageFinder.Add(repodb.DetailedRepoMeta{
RepoMetadata: repodb.RepoMetadata{
Name: "repo3",
Tags: map[string]repodb.Descriptor{
"Tag11": {
Digest: "dig11",
MediaType: ispec.MediaTypeImageManifest,
},
},
},
})
result, _ := pageFinder.Page()
So(result[0].Tags, ShouldContainKey, "tag1")
So(result[1].Tags, ShouldContainKey, "Tag1")
So(result[2].Tags, ShouldContainKey, "Tag11")
})
})
})
}