Skip to content

Commit

Permalink
Merge pull request #14285 from heww/cherry-pick-fix-issue-14251
Browse files Browse the repository at this point in the history
[Cherry pick]fix: use clone query in loop of artifact.Iterator func (#14283)
  • Loading branch information
ywk253100 authored Feb 23, 2021
2 parents 6db1a34 + e0b3365 commit ec0ba11
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/controller/artifact/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Iterator(ctx context.Context, chunkSize int, query *q.Query, option *Option
break
}

query.PageNumber++
clone.PageNumber++
}

}()
Expand Down
20 changes: 16 additions & 4 deletions src/controller/artifact/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@ import (
"context"
"testing"

"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/artifact"
artifacttesting "github.com/goharbor/harbor/src/testing/pkg/artifact"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
)

type IteratorTestSuite struct {
suite.Suite

artMgr *artifacttesting.FakeManager
artMgr *artifacttesting.Manager

ctl *controller
originalCtl Controller
}

func (suite *IteratorTestSuite) SetupSuite() {
suite.artMgr = &artifacttesting.FakeManager{}
suite.artMgr = &artifacttesting.Manager{}

suite.originalCtl = Ctl
suite.ctl = &controller{artMgr: suite.artMgr}
Expand All @@ -45,18 +47,28 @@ func (suite *IteratorTestSuite) TeardownSuite() {
}

func (suite *IteratorTestSuite) TestIterator() {
suite.artMgr.On("List").Return([]*artifact.Artifact{
q1 := &q.Query{PageNumber: 1, PageSize: 5, Keywords: map[string]interface{}{}}
suite.artMgr.On("List", mock.Anything, q1).Return([]*artifact.Artifact{
{ID: 1},
{ID: 2},
{ID: 3},
{ID: 4},
{ID: 5},
}, nil)

q2 := &q.Query{PageNumber: 2, PageSize: 5, Keywords: map[string]interface{}{}}
suite.artMgr.On("List", mock.Anything, q2).Return([]*artifact.Artifact{
{ID: 6},
{ID: 7},
{ID: 8},
}, nil)

var artifacts []*Artifact
for art := range Iterator(context.TODO(), 5, nil, nil) {
artifacts = append(artifacts, art)
}

suite.Len(artifacts, 3)
suite.Len(artifacts, 8)
}

func TestIteratorTestSuite(t *testing.T) {
Expand Down
97 changes: 97 additions & 0 deletions src/testing/pkg/artifact/fake_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package artifact

import (
"context"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/artifact"
"github.com/stretchr/testify/mock"
)

// FakeManager is a fake artifact manager that implement src/pkg/artifact.Manager interface
type FakeManager struct {
mock.Mock
}

// Count ...
func (f *FakeManager) Count(ctx context.Context, query *q.Query) (int64, error) {
args := f.Called()
return int64(args.Int(0)), args.Error(1)
}

// List ...
func (f *FakeManager) List(ctx context.Context, query *q.Query) ([]*artifact.Artifact, error) {
args := f.Called()
var artifacts []*artifact.Artifact
if args.Get(0) != nil {
artifacts = args.Get(0).([]*artifact.Artifact)
}
return artifacts, args.Error(1)
}

// Get ...
func (f *FakeManager) Get(ctx context.Context, id int64) (*artifact.Artifact, error) {
args := f.Called()
var art *artifact.Artifact
if args.Get(0) != nil {
art = args.Get(0).(*artifact.Artifact)
}
return art, args.Error(1)
}

// GetByDigest ...
func (f *FakeManager) GetByDigest(ctx context.Context, repository, digest string) (*artifact.Artifact, error) {
args := f.Called()
var art *artifact.Artifact
if args.Get(0) != nil {
art = args.Get(0).(*artifact.Artifact)
}
return art, args.Error(1)
}

// Create ...
func (f *FakeManager) Create(ctx context.Context, artifact *artifact.Artifact) (int64, error) {
args := f.Called()
return int64(args.Int(0)), args.Error(1)
}

// Delete ...
func (f *FakeManager) Delete(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
}

// UpdatePullTime ...
func (f *FakeManager) Update(ctx context.Context, artifact *artifact.Artifact, props ...string) error {
args := f.Called()
return args.Error(0)
}

// ListReferences ...
func (f *FakeManager) ListReferences(ctx context.Context, query *q.Query) ([]*artifact.Reference, error) {
args := f.Called()
var references []*artifact.Reference
if args.Get(0) != nil {
references = args.Get(0).([]*artifact.Reference)
}
return references, args.Error(1)
}

// DeleteReference ...
func (f *FakeManager) DeleteReference(ctx context.Context, id int64) error {
args := f.Called()
return args.Error(0)
}
Loading

0 comments on commit ec0ba11

Please sign in to comment.