2022-07-12 07:58:04 -05:00
|
|
|
package search //nolint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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-07-12 07:58:04 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"zotregistry.io/zot/pkg/extensions/search/common"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
|
|
|
"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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("GetImageTagsWithTimestamp fail", func() {
|
|
|
|
mockOlum := mocks.OciLayoutUtilsMock{
|
|
|
|
GetImageTagsWithTimestampFn: func(repo string) ([]common.TagInfo, error) {
|
|
|
|
return []common.TagInfo{}, ErrTestError
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
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
|
|
|
}
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
globalSearch([]string{"repo1"}, "test", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
|
|
|
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-08-02 10:58:30 -05:00
|
|
|
globalSearch([]string{"repo1/name"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
globalSearch([]string{"repo1/name"}, "name", "tag", mockOlum, 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-01-19 10:57:10 -05:00
|
|
|
Images: []common.Image{
|
2022-07-12 07:58:04 -05:00
|
|
|
{
|
|
|
|
Tag: "latest",
|
|
|
|
Layers: []common.Layer{
|
|
|
|
{
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
globalSearch([]string{"repo1"}, "name", "tag", mockOlum, log.NewLogger("debug", ""))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|