1. check is repository exist later at filterTags.

2. test case for image not found.

Signed-off-by: 慕薇疯魔 <kfanjian@gmail.com>
This commit is contained in:
慕薇疯魔 2019-04-17 13:34:04 +08:00
parent 7196abc77b
commit bf28e9779b
2 changed files with 24 additions and 36 deletions

View File

@ -15,10 +15,8 @@
package native
import (
"net/http"
"strings"
common_http "github.com/goharbor/harbor/src/common/http"
adp "github.com/goharbor/harbor/src/replication/adapter"
"github.com/goharbor/harbor/src/replication/model"
"github.com/goharbor/harbor/src/replication/util"
@ -42,7 +40,7 @@ func (n native) FetchImages(filters []*model.Filter) ([]*model.Resource, error)
return nil, err
}
resources := []*model.Resource{}
var resources []*model.Resource
for _, repository := range repositories {
tags, err := n.filterTags(repository, tagFilterPattern)
if err != nil {
@ -70,17 +68,8 @@ func (n native) filterRepositories(pattern string) ([]string, error) {
// if the pattern contains no "*" and "?", it is a specific repository name
// just to make sure the repository exists
if len(pattern) > 0 && !strings.ContainsAny(pattern, "*?") {
_, err := n.ListTag(pattern)
// the repository exists
if err == nil {
return []string{pattern}, nil
}
// the repository doesn't exist
if e, ok := err.(*common_http.Error); ok && e.Code == http.StatusNotFound {
return nil, nil
}
// other error
return nil, err
// check is repository exist later at filterTags.
return []string{pattern}, nil
}
// search repositories from catalog api
repositories, err := n.Catalog()
@ -113,7 +102,7 @@ func (n native) filterTags(repository, pattern string) ([]string, error) {
return tags, nil
}
result := []string{}
var result []string
for _, tag := range tags {
match, err := util.Match(pattern, tag)
if err != nil {

View File

@ -70,27 +70,26 @@ func Test_native_FetchImages(t *testing.T) {
want []*model.Resource
wantErr bool
}{
// TODO: discuss: should we report error if not found in the source native registry.
// {
// name: "repository not exist",
// filters: []*model.Filter{
// {
// Type: model.FilterTypeName,
// Value: "b1",
// },
// },
// wantErr: true,
// },
// {
// name: "tag not exist",
// filters: []*model.Filter{
// {
// Type: model.FilterTypeTag,
// Value: "c",
// },
// },
// wantErr: true,
// },
{
name: "repository not exist",
filters: []*model.Filter{
{
Type: model.FilterTypeName,
Value: "b1",
},
},
wantErr: false,
},
{
name: "tag not exist",
filters: []*model.Filter{
{
Type: model.FilterTypeTag,
Value: "this_tag_not_exist_in_the_mock_server",
},
},
wantErr: false,
},
{
name: "no filters",
filters: []*model.Filter{},