2022-07-12 07:58:04 -05:00
|
|
|
package search //nolint
|
|
|
|
|
|
|
|
import (
|
2022-08-16 03:57:09 -05:00
|
|
|
"context"
|
2022-09-30 12:32:32 -05:00
|
|
|
"encoding/json"
|
2022-07-12 07:58:04 -05:00
|
|
|
"errors"
|
2022-08-16 03:57:09 -05:00
|
|
|
"os"
|
2022-07-12 07:58:04 -05:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
"github.com/99designs/gqlgen/graphql"
|
2022-08-02 10:58:30 -05:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
2022-07-12 07:58:04 -05:00
|
|
|
godigest "github.com/opencontainers/go-digest"
|
2022-08-02 10:58:30 -05:00
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2022-08-16 03:57:09 -05:00
|
|
|
"github.com/rs/zerolog"
|
2022-07-12 07:58:04 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2022-08-16 03:57:09 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/monitoring"
|
2022-07-12 07:58:04 -05:00
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2022-08-16 03:57:09 -05:00
|
|
|
localCtx "zotregistry.io/zot/pkg/requestcontext"
|
2022-09-30 12:35:16 -05:00
|
|
|
"zotregistry.io/zot/pkg/storage/local"
|
2022-07-12 07:58:04 -05:00
|
|
|
"zotregistry.io/zot/pkg/test/mocks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrTestError = errors.New("TestError")
|
|
|
|
|
|
|
|
func TestGlobalSearch(t *testing.T) {
|
|
|
|
Convey("globalSearch", t, func() {
|
|
|
|
Convey("GetRepoLastUpdated fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
2022-07-22 15:01:38 -05:00
|
|
|
GetRepoLastUpdatedFn: func(repo string) (common.TagInfo, error) {
|
|
|
|
return common.TagInfo{}, ErrTestError
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("GetImageTagsWithTimestamp fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageTagsWithTimestampFn: func(repo string) ([]common.TagInfo, error) {
|
|
|
|
return []common.TagInfo{}, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
Convey("GetImageManifests fail", func() {
|
2022-07-12 07:58:04 -05:00
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
2022-08-02 10:58:30 -05:00
|
|
|
GetImageManifestsFn: func(name string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{}, ErrTestError
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
2022-07-12 07:58:04 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
|
2022-08-02 10:58:30 -05:00
|
|
|
Convey("Manifests given, bad image blob manifest", func() {
|
2022-07-12 07:58:04 -05:00
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
2022-08-02 10:58:30 -05:00
|
|
|
GetImageManifestsFn: func(name string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
Digest: "digest",
|
|
|
|
Size: -1,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "this is a bad format",
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
2022-08-02 10:58:30 -05:00
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return v1.Manifest{}, ErrTestError
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
2022-08-02 10:58:30 -05:00
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-08-02 10:58:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Manifests given, no manifest tag", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageManifestsFn: func(name string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
Digest: "digest",
|
|
|
|
Size: -1,
|
|
|
|
},
|
|
|
|
}, nil
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
2022-08-02 10:58:30 -05:00
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
2022-08-02 10:58:30 -05:00
|
|
|
|
2022-09-28 13:39:54 -05:00
|
|
|
globalSearch([]string{"repo1"}, "test", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-08-02 10:58:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Global search success, no tag", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetRepoLastUpdatedFn: func(repo string) (common.TagInfo, error) {
|
|
|
|
return common.TagInfo{
|
|
|
|
Digest: "sha256:855b1556a45637abf05c63407437f6f305b4627c4361fb965a78e5731999c0c7",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
GetImageManifestsFn: func(name string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
2022-07-12 07:58:04 -05:00
|
|
|
{
|
2022-08-02 10:58:30 -05:00
|
|
|
Digest: "sha256:855b1556a45637abf05c63407437f6f305b4627c4361fb965a78e5731999c0c7",
|
|
|
|
Size: -1,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "this is a bad format",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return v1.Manifest{
|
|
|
|
Layers: []v1.Descriptor{
|
|
|
|
{
|
|
|
|
Size: 0,
|
|
|
|
Digest: v1.Hash{},
|
|
|
|
},
|
2022-07-12 07:58:04 -05:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
globalSearch([]string{"repo1/name"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-08-02 10:58:30 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Manifests given, bad image config info", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageManifestsFn: func(name string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
Digest: "digest",
|
|
|
|
Size: -1,
|
|
|
|
Annotations: map[string]string{
|
|
|
|
ispec.AnnotationRefName: "this is a bad format",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, manifestDigest godigest.Digest) (ispec.Image, error) {
|
|
|
|
return ispec.Image{}, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
globalSearch([]string{"repo1/name"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Tag given, no layer match", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetExpandedRepoInfoFn: func(name string) (common.RepoInfo, error) {
|
|
|
|
return common.RepoInfo{
|
2022-09-22 14:08:58 -05:00
|
|
|
ImageSummaries: []common.ImageSummary{
|
2022-07-12 07:58:04 -05:00
|
|
|
{
|
|
|
|
Tag: "latest",
|
2022-09-30 12:32:32 -05:00
|
|
|
Layers: []common.LayerSummary{
|
2022-07-12 07:58:04 -05:00
|
|
|
{
|
|
|
|
Size: "100",
|
|
|
|
Digest: "sha256:855b1556a45637abf05c63407437f6f305b4627c4361fb965a78e5731999c0c7",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
GetImageManifestSizeFn: func(repo string, manifestDigest godigest.Digest) int64 {
|
|
|
|
return 100
|
|
|
|
},
|
|
|
|
GetImageConfigSizeFn: func(repo string, manifestDigest godigest.Digest) int64 {
|
|
|
|
return 100
|
|
|
|
},
|
|
|
|
GetImageTagsWithTimestampFn: func(repo string) ([]common.TagInfo, error) {
|
|
|
|
return []common.TagInfo{
|
|
|
|
{
|
|
|
|
Name: "test",
|
|
|
|
Digest: "test",
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-07-12 07:58:04 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-13 09:20:44 -05:00
|
|
|
func TestRepoListWithNewestImage(t *testing.T) {
|
|
|
|
Convey("repoListWithNewestImage", t, func() {
|
|
|
|
Convey("GetImageManifests fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageManifestsFn: func(image string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{}, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := graphql.WithResponseContext(context.Background(), graphql.DefaultErrorPresenter, graphql.Recover)
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
_, err := repoListWithNewestImage(ctx, []string{"repo1"}, mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-09-13 09:20:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
errs := graphql.GetErrors(ctx)
|
|
|
|
So(errs, ShouldNotBeEmpty)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("GetImageBlobManifest fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return v1.Manifest{}, ErrTestError
|
|
|
|
},
|
|
|
|
GetImageManifestsFn: func(image string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
|
|
|
Size: int64(0),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := graphql.WithResponseContext(context.Background(), graphql.DefaultErrorPresenter, graphql.Recover)
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
_, err := repoListWithNewestImage(ctx, []string{"repo1"}, mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-09-13 09:20:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
errs := graphql.GetErrors(ctx)
|
|
|
|
So(errs, ShouldNotBeEmpty)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("GetImageConfigInfo fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageManifestsFn: func(image string) ([]ispec.Descriptor, error) {
|
|
|
|
return []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
|
|
|
Size: int64(0),
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, manifestDigest godigest.Digest) (ispec.Image, error) {
|
|
|
|
return ispec.Image{
|
|
|
|
Author: "test",
|
|
|
|
}, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := graphql.WithResponseContext(context.Background(), graphql.DefaultErrorPresenter, graphql.Recover)
|
2022-09-28 13:39:54 -05:00
|
|
|
mockCve := mocks.CveInfoMock{}
|
|
|
|
_, err := repoListWithNewestImage(ctx, []string{"repo1"}, mockOlum, mockCve, log.NewLogger("debug", ""))
|
2022-09-13 09:20:44 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
errs := graphql.GetErrors(ctx)
|
|
|
|
So(errs, ShouldNotBeEmpty)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-16 03:57:09 -05:00
|
|
|
func TestUserAvailableRepos(t *testing.T) {
|
|
|
|
Convey("Type assertion fails", t, func() {
|
|
|
|
var invalid struct{}
|
|
|
|
|
|
|
|
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
dir := t.TempDir()
|
|
|
|
metrics := monitoring.NewMetricsServer(false, log)
|
2022-09-30 12:35:16 -05:00
|
|
|
defaultStore := local.NewImageStore(dir, false, 0, false, false, log, metrics, nil)
|
2022-08-16 03:57:09 -05:00
|
|
|
|
|
|
|
repoList, err := defaultStore.GetRepositories()
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
key := localCtx.GetContextKey()
|
|
|
|
ctx = context.WithValue(ctx, key, invalid)
|
|
|
|
|
|
|
|
repos, err := userAvailableRepos(ctx, repoList)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
So(repos, ShouldBeEmpty)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:58:04 -05:00
|
|
|
func TestMatching(t *testing.T) {
|
|
|
|
pine := "pine"
|
|
|
|
|
|
|
|
Convey("Perfect Matching", t, func() {
|
|
|
|
query := "alpine"
|
|
|
|
score := calculateImageMatchingScore("alpine", strings.Index("alpine", query), true)
|
|
|
|
So(score, ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Partial Matching", t, func() {
|
|
|
|
query := pine
|
|
|
|
score := calculateImageMatchingScore("alpine", strings.Index("alpine", query), true)
|
|
|
|
So(score, ShouldEqual, 2)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Complex Partial Matching", t, func() {
|
|
|
|
query := pine
|
|
|
|
score := calculateImageMatchingScore("repo/test/alpine", strings.Index("alpine", query), true)
|
|
|
|
So(score, ShouldEqual, 2)
|
|
|
|
|
|
|
|
query = pine
|
|
|
|
score = calculateImageMatchingScore("repo/alpine/test", strings.Index("alpine", query), true)
|
|
|
|
So(score, ShouldEqual, 2)
|
|
|
|
|
|
|
|
query = pine
|
|
|
|
score = calculateImageMatchingScore("alpine/repo/test", strings.Index("alpine", query), true)
|
|
|
|
So(score, ShouldEqual, 2)
|
|
|
|
|
|
|
|
query = pine
|
|
|
|
score = calculateImageMatchingScore("alpine/repo/test", strings.Index("alpine", query), false)
|
|
|
|
So(score, ShouldEqual, 12)
|
|
|
|
})
|
|
|
|
}
|
2022-09-30 12:32:32 -05:00
|
|
|
|
|
|
|
func TestExtractImageDetails(t *testing.T) {
|
|
|
|
Convey("repoListWithNewestImage", t, func() {
|
|
|
|
// log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
|
|
|
content := []byte("this is a blob5")
|
|
|
|
testLogger := log.NewLogger("debug", "")
|
|
|
|
layerDigest := godigest.FromBytes(content)
|
|
|
|
config := ispec.Image{
|
|
|
|
Architecture: "amd64",
|
|
|
|
OS: "linux",
|
|
|
|
RootFS: ispec.RootFS{
|
|
|
|
Type: "layers",
|
|
|
|
DiffIDs: []godigest.Digest{},
|
|
|
|
},
|
|
|
|
Author: "some author",
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
authzCtxKey := localCtx.GetContextKey()
|
|
|
|
ctx = context.WithValue(ctx, authzCtxKey,
|
|
|
|
localCtx.AccessControlContext{
|
|
|
|
GlobPatterns: map[string]bool{"*": true, "**": true},
|
|
|
|
Username: "jane_doe",
|
|
|
|
})
|
|
|
|
configBlobContent, _ := json.MarshalIndent(&config, "", "\t")
|
|
|
|
configDigest := godigest.FromBytes(configBlobContent)
|
|
|
|
|
|
|
|
localTestManifest := ispec.Manifest{
|
|
|
|
Config: ispec.Descriptor{
|
|
|
|
MediaType: "application/vnd.oci.image.config.v1+json",
|
|
|
|
Digest: configDigest,
|
|
|
|
Size: int64(len(configBlobContent)),
|
|
|
|
},
|
|
|
|
Layers: []ispec.Descriptor{
|
|
|
|
{
|
|
|
|
MediaType: "application/vnd.oci.image.layer.v1.tar",
|
|
|
|
Digest: layerDigest,
|
|
|
|
Size: int64(len(content)),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
localTestDigestTry, _ := json.Marshal(localTestManifest)
|
|
|
|
localTestDigest := godigest.FromBytes(localTestDigestTry)
|
|
|
|
localTestManifestV1 := v1.Manifest{
|
|
|
|
Config: v1.Descriptor{
|
|
|
|
Digest: v1.Hash{
|
|
|
|
Algorithm: "sha256",
|
|
|
|
Hex: configDigest.Encoded(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Layers: []v1.Descriptor{
|
|
|
|
{
|
|
|
|
Size: 4,
|
|
|
|
Digest: v1.Hash{
|
|
|
|
Algorithm: "sha256",
|
|
|
|
Hex: layerDigest.Encoded(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
Convey("extractImageDetails good workflow", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return localTestManifestV1, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, digest godigest.Digest) (
|
|
|
|
ispec.Image, error,
|
|
|
|
) {
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
GetImageManifestFn: func(repo string, tag string) (
|
|
|
|
ispec.Manifest, string, error,
|
|
|
|
) {
|
|
|
|
return localTestManifest, localTestDigest.String(), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resDigest, resManifest, resIspecImage, resErr := extractImageDetails(ctx,
|
|
|
|
mockOlum, "zot-test", "latest", testLogger)
|
|
|
|
So(string(resDigest), ShouldContainSubstring, "sha256:d004018b9f")
|
|
|
|
So(resManifest.Config.Digest.String(), ShouldContainSubstring, configDigest.Encoded())
|
|
|
|
|
|
|
|
So(resIspecImage.Architecture, ShouldContainSubstring, "amd64")
|
|
|
|
So(resErr, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("extractImageDetails bad ispec.ImageManifest", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return localTestManifestV1, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, digest godigest.Digest) (
|
|
|
|
ispec.Image, error,
|
|
|
|
) {
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
GetImageManifestFn: func(repo string, tag string) (
|
|
|
|
ispec.Manifest, string, error,
|
|
|
|
) {
|
|
|
|
// localTestManifest = nil
|
|
|
|
return ispec.Manifest{}, localTestDigest.String() + "aaa", ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resDigest, resManifest, resIspecImage, resErr := extractImageDetails(ctx,
|
|
|
|
mockOlum, "zot-test", "latest", testLogger)
|
|
|
|
So(resErr, ShouldEqual, ErrTestError)
|
|
|
|
So(string(resDigest), ShouldEqual, "")
|
|
|
|
So(resManifest, ShouldBeNil)
|
|
|
|
|
|
|
|
So(resIspecImage, ShouldBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("extractImageDetails bad ImageBlobManifest", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return localTestManifestV1, ErrTestError
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, digest godigest.Digest) (
|
|
|
|
ispec.Image, error,
|
|
|
|
) {
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
GetImageManifestFn: func(repo string, tag string) (
|
|
|
|
ispec.Manifest, string, error,
|
|
|
|
) {
|
|
|
|
return localTestManifest, localTestDigest.String(), nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resDigest, resManifest, resIspecImage, resErr := extractImageDetails(ctx,
|
|
|
|
mockOlum, "zot-test", "latest", testLogger)
|
|
|
|
So(string(resDigest), ShouldEqual, "")
|
|
|
|
So(resManifest, ShouldBeNil)
|
|
|
|
|
|
|
|
So(resIspecImage, ShouldBeNil)
|
|
|
|
So(resErr, ShouldEqual, ErrTestError)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("extractImageDetails bad imageConfig", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return localTestManifestV1, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, digest godigest.Digest) (
|
|
|
|
ispec.Image, error,
|
|
|
|
) {
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
GetImageManifestFn: func(repo string, tag string) (
|
|
|
|
ispec.Manifest, string, error,
|
|
|
|
) {
|
|
|
|
return localTestManifest, localTestDigest.String(), ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resDigest, resManifest, resIspecImage, resErr := extractImageDetails(ctx,
|
|
|
|
mockOlum, "zot-test", "latest", testLogger)
|
|
|
|
So(string(resDigest), ShouldEqual, "")
|
|
|
|
So(resManifest, ShouldBeNil)
|
|
|
|
|
|
|
|
So(resIspecImage, ShouldBeNil)
|
|
|
|
So(resErr, ShouldEqual, ErrTestError)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("extractImageDetails without proper authz", func() {
|
|
|
|
ctx = context.WithValue(ctx, authzCtxKey,
|
|
|
|
localCtx.AccessControlContext{
|
|
|
|
GlobPatterns: map[string]bool{},
|
|
|
|
Username: "jane_doe",
|
|
|
|
})
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageBlobManifestFn: func(imageDir string, digest godigest.Digest) (v1.Manifest, error) {
|
|
|
|
return localTestManifestV1, nil
|
|
|
|
},
|
|
|
|
GetImageConfigInfoFn: func(repo string, digest godigest.Digest) (
|
|
|
|
ispec.Image, error,
|
|
|
|
) {
|
|
|
|
return config, nil
|
|
|
|
},
|
|
|
|
GetImageManifestFn: func(repo string, tag string) (
|
|
|
|
ispec.Manifest, string, error,
|
|
|
|
) {
|
|
|
|
return localTestManifest, localTestDigest.String(), ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resDigest, resManifest, resIspecImage, resErr := extractImageDetails(ctx,
|
|
|
|
mockOlum, "zot-test", "latest", testLogger)
|
|
|
|
So(string(resDigest), ShouldEqual, "")
|
|
|
|
So(resManifest, ShouldBeNil)
|
|
|
|
|
|
|
|
So(resIspecImage, ShouldBeNil)
|
|
|
|
So(resErr, ShouldNotBeNil)
|
|
|
|
So(strings.ToLower(resErr.Error()), ShouldContainSubstring, "unauthorized access")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|