mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
fix(cov): coverage boltdb+dynamo (#2018)
Signed-off-by: Laurentiu Niculae <niculae.laurentiu1@gmail.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/opencontainers/image-spec/specs-go"
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
|
||||
"zotregistry.io/zot/pkg/common"
|
||||
mTypes "zotregistry.io/zot/pkg/meta/types"
|
||||
storageConstants "zotregistry.io/zot/pkg/storage/constants"
|
||||
)
|
||||
@@ -145,7 +146,7 @@ func (img Image) AsImageMeta() mTypes.ImageMeta {
|
||||
MediaType: img.Manifest.MediaType,
|
||||
Digest: img.ManifestDescriptor.Digest,
|
||||
Size: img.ManifestDescriptor.Size,
|
||||
Manifests: []mTypes.ManifestData{
|
||||
Manifests: []mTypes.ManifestMeta{
|
||||
{
|
||||
Size: img.ManifestDescriptor.Size,
|
||||
Digest: img.ManifestDescriptor.Digest,
|
||||
@@ -203,11 +204,14 @@ func CreateRandomVulnerableImageWith() ManifestBuilder {
|
||||
return CreateImageWith().VulnerableLayers().RandomVulnConfig()
|
||||
}
|
||||
|
||||
// CreateFakeTestSignature returns a test signature that is used to mark a image as signed
|
||||
// when creating a test Repo. It won't be recognized as a signature if uploaded to the repository directly.
|
||||
func CreateFakeTestSignature(subject *ispec.Descriptor) Image {
|
||||
return CreateImageWith().RandomLayers(1, 10).DefaultConfig().
|
||||
ArtifactType(TestFakeSignatureArtType).Subject(subject).Build()
|
||||
func CreateMockNotationSignature(subject *ispec.Descriptor) Image {
|
||||
return CreateImageWith().RandomLayers(1, 10).EmptyConfig().Subject(subject).
|
||||
ArtifactType(common.ArtifactTypeNotation).Build()
|
||||
}
|
||||
|
||||
func CreateMockCosignSignature(subject *ispec.Descriptor) Image {
|
||||
return CreateImageWith().RandomLayers(1, 10).EmptyConfig().Subject(subject).
|
||||
ArtifactType(common.ArtifactTypeCosign).Build()
|
||||
}
|
||||
|
||||
type BaseImageBuilder struct {
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"zotregistry.io/zot/pkg/common"
|
||||
. "zotregistry.io/zot/pkg/test/image-utils"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,15 @@ func TestImageBuilder(t *testing.T) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
Convey("Signature images", t, func() {
|
||||
image := CreateDefaultImage()
|
||||
cosign := CreateMockCosignSignature(image.DescriptorRef())
|
||||
So(cosign.Manifest.ArtifactType, ShouldResemble, common.ArtifactTypeCosign)
|
||||
|
||||
notation := CreateMockNotationSignature(image.DescriptorRef())
|
||||
So(notation.Manifest.ArtifactType, ShouldResemble, common.ArtifactTypeNotation)
|
||||
})
|
||||
|
||||
Convey("Test Layer Builders", t, func() {
|
||||
layerBuilder := CreateImageWith()
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ func (mi *MultiarchImage) DigestStr() string {
|
||||
func (mi MultiarchImage) AsImageMeta() mTypes.ImageMeta {
|
||||
index := mi.Index
|
||||
|
||||
manifests := make([]mTypes.ManifestData, 0, len(index.Manifests))
|
||||
manifests := make([]mTypes.ManifestMeta, 0, len(index.Manifests))
|
||||
|
||||
for _, image := range mi.Images {
|
||||
manifests = append(manifests, image.AsImageMeta().Manifests...)
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
package ociutils_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"zotregistry.io/zot/pkg/meta/types"
|
||||
"zotregistry.io/zot/pkg/test/image-utils"
|
||||
"zotregistry.io/zot/pkg/test/mocks"
|
||||
ociutils "zotregistry.io/zot/pkg/test/oci-utils"
|
||||
)
|
||||
|
||||
var ErrTestFail = fmt.Errorf("fail")
|
||||
|
||||
func TestInitializeMetaDBErrors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
Convey("InitializeTestMetaDB", t, func() {
|
||||
metaDB := mocks.MetaDBMock{
|
||||
GetRepoMetaFn: func(ctx context.Context, repo string) (types.RepoMeta, error) {
|
||||
return types.RepoMeta{
|
||||
Statistics: map[string]types.DescriptorStatistics{},
|
||||
Signatures: map[string]types.ManifestSignatures{},
|
||||
Referrers: map[string][]types.ReferrerInfo{},
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
Convey("Multiple repos same name", func() {
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB, ociutils.Repo{Name: "repo"}, ociutils.Repo{Name: "repo"})
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Set Repo Ref fails", func() {
|
||||
metaDB.SetRepoReferenceFn = func(ctx context.Context, repo, reference string, imageMeta types.ImageMeta) error {
|
||||
return ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{Name: "repo", Images: []ociutils.RepoImage{{}}},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Set Repo Ref fails for manifest in index", func() {
|
||||
metaDB.SetRepoReferenceFn = func(ctx context.Context, repo, reference string, imageMeta types.ImageMeta) error {
|
||||
return ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{
|
||||
Name: "repo",
|
||||
MultiArchImages: []ociutils.RepoMultiArchImage{{MultiarchImage: image.CreateRandomMultiarch()}},
|
||||
},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Set Repo Ref fails for index", func() {
|
||||
count := 0
|
||||
metaDB.SetRepoReferenceFn = func(ctx context.Context, repo, reference string, imageMeta types.ImageMeta) error {
|
||||
if count == 1 {
|
||||
return ErrTestFail
|
||||
}
|
||||
|
||||
count++
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
multiarch := image.CreateMultiarchWith().Images([]image.Image{{}}).Build()
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{
|
||||
Name: "repo",
|
||||
MultiArchImages: []ociutils.RepoMultiArchImage{{MultiarchImage: multiarch}},
|
||||
},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Get repo meta errors", func() {
|
||||
metaDB.GetRepoMetaFn = func(ctx context.Context, repo string) (types.RepoMeta, error) {
|
||||
return types.RepoMeta{}, ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{Name: "repo", Images: []ociutils.RepoImage{{}}},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("Set repo meta errors", func() {
|
||||
metaDB.SetRepoMetaFn = func(repo string, repoMeta types.RepoMeta) error {
|
||||
return ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{Name: "repo", Images: []ociutils.RepoImage{{}}},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("ToggleBookmarkRepo errors", func() {
|
||||
metaDB.ToggleBookmarkRepoFn = func(ctx context.Context, repo string) (types.ToggleState, error) {
|
||||
return types.NotChanged, ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{Name: "repo", Images: []ociutils.RepoImage{{}}, IsBookmarked: true},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
Convey("ToggleStarRepo errors", func() {
|
||||
metaDB.ToggleStarRepoFn = func(ctx context.Context, repo string) (types.ToggleState, error) {
|
||||
return types.NotChanged, ErrTestFail
|
||||
}
|
||||
_, err := ociutils.InitializeTestMetaDB(ctx, metaDB,
|
||||
ociutils.Repo{Name: "repo", Images: []ociutils.RepoImage{{}}, IsStarred: true},
|
||||
)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user