0
Fork 0
mirror of https://github.com/project-zot/zot.git synced 2025-01-06 22:40:28 -05:00
zot/pkg/meta/common/common_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

227 lines
6.4 KiB
Go

package common_test
import (
"errors"
"testing"
"time"
"github.com/opencontainers/go-digest"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
. "github.com/smartystreets/goconvey/convey"
"zotregistry.io/zot/pkg/meta/common"
"zotregistry.io/zot/pkg/meta/repodb"
"zotregistry.io/zot/pkg/test/mocks"
)
var ErrTestError = errors.New("test error")
func TestUtils(t *testing.T) {
Convey("GetReferredSubject", t, func() {
_, err := common.GetReferredSubject([]byte("bad json"))
So(err, ShouldNotBeNil)
})
Convey("MatchesArtifactTypes", t, func() {
res := common.MatchesArtifactTypes("", nil)
So(res, ShouldBeTrue)
res = common.MatchesArtifactTypes("type", []string{"someOtherType"})
So(res, ShouldBeFalse)
})
Convey("CheckImageLastUpdated", t, func() {
Convey("No image checked, it doesn't have time", func() {
repoLastUpdated := time.Time{}
isSigned := false
noImageChecked := true
manifestFilterData := repodb.FilterData{
DownloadCount: 10,
LastUpdated: time.Time{},
IsSigned: true,
}
repoLastUpdated, noImageChecked, isSigned = common.CheckImageLastUpdated(repoLastUpdated, isSigned, noImageChecked,
manifestFilterData)
So(repoLastUpdated, ShouldResemble, manifestFilterData.LastUpdated)
So(isSigned, ShouldEqual, manifestFilterData.IsSigned)
So(noImageChecked, ShouldEqual, false)
})
Convey("First image checked, it has time", func() {
repoLastUpdated := time.Time{}
isSigned := false
noImageChecked := true
manifestFilterData := repodb.FilterData{
DownloadCount: 10,
LastUpdated: time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC),
IsSigned: true,
}
repoLastUpdated, noImageChecked, isSigned = common.CheckImageLastUpdated(repoLastUpdated, isSigned, noImageChecked,
manifestFilterData)
So(repoLastUpdated, ShouldResemble, manifestFilterData.LastUpdated)
So(isSigned, ShouldEqual, manifestFilterData.IsSigned)
So(noImageChecked, ShouldEqual, false)
})
Convey("Not first image checked, current image is newer", func() {
repoLastUpdated := time.Date(2000, 1, 1, 1, 1, 1, 1, time.UTC)
isSigned := true
noImageChecked := false
manifestFilterData := repodb.FilterData{
DownloadCount: 10,
LastUpdated: time.Date(2023, 1, 1, 1, 1, 1, 1, time.UTC),
IsSigned: false,
}
repoLastUpdated, noImageChecked, isSigned = common.CheckImageLastUpdated(repoLastUpdated, isSigned,
noImageChecked, manifestFilterData)
So(repoLastUpdated, ShouldResemble, manifestFilterData.LastUpdated)
So(isSigned, ShouldEqual, manifestFilterData.IsSigned)
So(noImageChecked, ShouldEqual, false)
})
Convey("Not first image checked, current image is older", func() {
repoLastUpdated := time.Date(2024, 1, 1, 1, 1, 1, 1, time.UTC)
isSigned := false
noImageChecked := false
manifestFilterData := repodb.FilterData{
DownloadCount: 10,
LastUpdated: time.Date(2022, 1, 1, 1, 1, 1, 1, time.UTC),
IsSigned: true,
}
updatedRepoLastUpdated, noImageChecked, isSigned := common.CheckImageLastUpdated(repoLastUpdated, isSigned,
noImageChecked,
manifestFilterData)
So(updatedRepoLastUpdated, ShouldResemble, repoLastUpdated)
So(isSigned, ShouldEqual, false)
So(noImageChecked, ShouldEqual, false)
})
})
Convey("SignatureAlreadyExists", t, func() {
res := common.SignatureAlreadyExists(
[]repodb.SignatureInfo{{SignatureManifestDigest: "digest"}},
repodb.SignatureMetadata{SignatureDigest: "digest"},
)
So(res, ShouldEqual, true)
res = common.SignatureAlreadyExists(
[]repodb.SignatureInfo{{SignatureManifestDigest: "digest"}},
repodb.SignatureMetadata{SignatureDigest: "digest2"},
)
So(res, ShouldEqual, false)
})
Convey("FilterDataByRepo", t, func() {
Convey("Errors", func() {
// Unmarshal index data error
_, _, err := common.FilterDataByRepo(
[]repodb.RepoMetadata{{
Tags: map[string]repodb.Descriptor{
"tag": {
Digest: "indexDigest",
MediaType: ispec.MediaTypeImageIndex,
},
},
}},
map[string]repodb.ManifestMetadata{},
map[string]repodb.IndexData{
"indexDigest": {
IndexBlob: []byte("bad blob"),
},
},
)
So(err, ShouldNotBeNil)
})
})
Convey("FetchDataForRepos", t, func() {
Convey("Errors", func() {
// Unmarshal index data error
_, _, err := common.FetchDataForRepos(
mocks.RepoDBMock{
GetIndexDataFn: func(indexDigest digest.Digest) (repodb.IndexData, error) {
return repodb.IndexData{
IndexBlob: []byte("bad blob"),
}, nil
},
},
[]repodb.RepoMetadata{{
Tags: map[string]repodb.Descriptor{
"tag": {
Digest: "indexDigest",
MediaType: ispec.MediaTypeImageIndex,
},
},
}},
)
So(err, ShouldNotBeNil)
})
})
}
func TestFetchDataForRepos(t *testing.T) {
Convey("GetReferredSubject", t, func() {
mockRepoDB := mocks.RepoDBMock{}
Convey("GetManifestData errors", func() {
mockRepoDB.GetManifestDataFn = func(manifestDigest digest.Digest) (repodb.ManifestData, error) {
return repodb.ManifestData{}, ErrTestError
}
_, _, err := common.FetchDataForRepos(mockRepoDB, []repodb.RepoMetadata{
{
Tags: map[string]repodb.Descriptor{
"tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageManifest},
},
},
})
So(err, ShouldNotBeNil)
})
Convey("GetIndexData errors", func() {
mockRepoDB.GetIndexDataFn = func(indexDigest digest.Digest) (repodb.IndexData, error) {
return repodb.IndexData{}, ErrTestError
}
_, _, err := common.FetchDataForRepos(mockRepoDB, []repodb.RepoMetadata{
{
Tags: map[string]repodb.Descriptor{
"tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageIndex},
},
},
})
So(err, ShouldNotBeNil)
})
Convey("GetIndexData ok, GetManifestData errors", func() {
mockRepoDB.GetIndexDataFn = func(indexDigest digest.Digest) (repodb.IndexData, error) {
return repodb.IndexData{
IndexBlob: []byte(`{
"manifests": [
{"digest": "dig1"}
]
}`),
}, nil
}
mockRepoDB.GetManifestDataFn = func(manifestDigest digest.Digest) (repodb.ManifestData, error) {
return repodb.ManifestData{}, ErrTestError
}
_, _, err := common.FetchDataForRepos(mockRepoDB, []repodb.RepoMetadata{
{
Tags: map[string]repodb.Descriptor{
"tag1": {Digest: "dig1", MediaType: ispec.MediaTypeImageIndex},
},
},
})
So(err, ShouldNotBeNil)
})
})
}