Skip to content

Commit

Permalink
refactor: unify the mock file generation
Browse files Browse the repository at this point in the history
1. Mock remote interface for distribution manifest by mockery package
feature.
2. Refactor hand-generated mock files to automated management
   generation.
3. Clean useless mocks.

Signed-off-by: chlins <chlins.zhang@gmail.com>
  • Loading branch information
chlins committed Aug 5, 2024
1 parent 295e075 commit c6c3726
Show file tree
Hide file tree
Showing 25 changed files with 923 additions and 652 deletions.
81 changes: 63 additions & 18 deletions src/.mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ packages:
Controller:
config:
dir: testing/controller/artifact
github.com/goharbor/harbor/src/controller/artifact/processor:
interfaces:
Processor:
config:
dir: testing/pkg/processor
github.com/goharbor/harbor/src/controller/artifact/annotation:
interfaces:
Parser:
config:
dir: testing/pkg/parser
outpkg: parser
github.com/goharbor/harbor/src/controller/blob:
interfaces:
Controller:
Expand Down Expand Up @@ -188,6 +199,11 @@ packages:
Manager:
config:
dir: testing/pkg/artifact
github.com/goharbor/harbor/src/pkg/artifactrash:
interfaces:
Manager:
config:
dir: testing/pkg/artifactrash
github.com/goharbor/harbor/src/pkg/blob:
interfaces:
Manager:
Expand Down Expand Up @@ -218,6 +234,11 @@ packages:
Handler:
config:
dir: testing/pkg/scan
github.com/goharbor/harbor/src/pkg/scan/postprocessors:
interfaces:
NativeScanReportConverter:
config:
dir: testing/pkg/scan/postprocessors
github.com/goharbor/harbor/src/pkg/scan/report:
interfaces:
Manager:
Expand All @@ -238,7 +259,7 @@ packages:
dir: pkg/scheduler
outpkg: scheduler
mockname: mockDAO
filename: mock_dao_test.go
filename: mock_dao_test.go
inpackage: True
Scheduler:
config:
Expand Down Expand Up @@ -342,6 +363,14 @@ packages:
DAO:
config:
dir: testing/pkg/immutable/dao
github.com/goharbor/harbor/src/pkg/immutable/match:
interfaces:
ImmutableTagMatcher:
config:
dir: testing/pkg/immutable
filename: matcher.go
outpkg: immutable
mockname: FakeMatcher
github.com/goharbor/harbor/src/pkg/ldap:
interfaces:
Manager:
Expand Down Expand Up @@ -505,20 +534,36 @@ packages:
Manager:
config:
dir: testing/pkg/securityhub

















github.com/goharbor/harbor/src/pkg/tag:
interfaces:
Manager:
config:
dir: testing/pkg/tag
github.com/goharbor/harbor/src/pkg/p2p/preheat/policy:
interfaces:
Manager:
config:
dir: testing/pkg/p2p/preheat/policy
github.com/goharbor/harbor/src/pkg/p2p/preheat/instance:
interfaces:
Manager:
config:
dir: testing/pkg/p2p/preheat/instance
github.com/goharbor/harbor/src/pkg/chart:
interfaces:
Operator:
config:
dir: testing/pkg/chart
# registryctl related mocks
github.com/goharbor/harbor/src/registryctl/client:
interfaces:
Client:
config:
dir: testing/registryctl
outpkg: registryctl
# remote interfaces
github.com/docker/distribution:
interfaces:
Manifest:
config:
dir: testing/pkg/distribution
8 changes: 4 additions & 4 deletions src/controller/artifact/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type controllerTestSuite struct {
ctl *controller
repoMgr *repotesting.Manager
artMgr *arttesting.Manager
artrashMgr *artrashtesting.FakeManager
artrashMgr *artrashtesting.Manager
blobMgr *blob.Manager
tagCtl *tagtesting.FakeController
labelMgr *label.Manager
Expand All @@ -80,7 +80,7 @@ type controllerTestSuite struct {
func (c *controllerTestSuite) SetupTest() {
c.repoMgr = &repotesting.Manager{}
c.artMgr = &arttesting.Manager{}
c.artrashMgr = &artrashtesting.FakeManager{}
c.artrashMgr = &artrashtesting.Manager{}
c.blobMgr = &blob.Manager{}
c.tagCtl = &tagtesting.FakeController{}
c.labelMgr = &label.Manager{}
Expand Down Expand Up @@ -476,7 +476,7 @@ func (c *controllerTestSuite) TestDeleteDeeply() {
},
}, nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
c.artrashMgr.On("Create").Return(0, nil)
c.artrashMgr.On("Create", mock.Anything, mock.Anything).Return(int64(0), nil)
c.accMgr.On("List", mock.Anything, mock.Anything).Return([]accessorymodel.Accessory{}, nil)
err = c.ctl.deleteDeeply(orm.NewContext(nil, &ormtesting.FakeOrmer{}), 1, false, false)
c.Require().Nil(err)
Expand Down Expand Up @@ -534,7 +534,7 @@ func (c *controllerTestSuite) TestDeleteDeeply() {
c.blobMgr.On("List", mock.Anything, mock.Anything).Return(nil, nil)
c.blobMgr.On("CleanupAssociationsForProject", mock.Anything, mock.Anything, mock.Anything).Return(nil)
c.repoMgr.On("Get", mock.Anything, mock.Anything).Return(&repomodel.RepoRecord{}, nil)
c.artrashMgr.On("Create").Return(0, nil)
c.artrashMgr.On("Create", mock.Anything, mock.Anything).Return(int64(0), nil)
err = c.ctl.deleteDeeply(orm.NewContext(nil, &ormtesting.FakeOrmer{}), 1, true, true)
c.Require().Nil(err)

Expand Down
6 changes: 3 additions & 3 deletions src/controller/artifact/processor/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ type processorTestSuite struct {
suite.Suite
processor *processor
regCli *registry.Client
chartOptr *chart.FakeOpertaor
chartOptr *chart.Operator
}

func (p *processorTestSuite) SetupTest() {
p.regCli = &registry.Client{}
p.chartOptr = &chart.FakeOpertaor{}
p.chartOptr = &chart.Operator{}
p.processor = &processor{
chartOperator: p.chartOptr,
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func (p *processorTestSuite) TestAbstractAddition() {
p.Require().Nil(err)
p.regCli.On("PullManifest", mock.Anything, mock.Anything).Return(manifest, "", nil)
p.regCli.On("PullBlob", mock.Anything, mock.Anything).Return(int64(0), io.NopCloser(strings.NewReader(chartYaml)), nil)
p.chartOptr.On("GetDetails").Return(chartDetails, nil)
p.chartOptr.On("GetDetails", mock.Anything).Return(chartDetails, nil)

// values.yaml
addition, err := p.processor.AbstractAddition(nil, artifact, AdditionTypeValues)
Expand Down
8 changes: 4 additions & 4 deletions src/controller/p2p/preheat/controllor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ type preheatSuite struct {
suite.Suite
ctx context.Context
controller Controller
fakeInstanceMgr *instance.FakeManager
fakePolicyMgr *pmocks.FakeManager
fakeInstanceMgr *instance.Manager
fakePolicyMgr *pmocks.Manager
fakeScheduler *smocks.Scheduler
mockInstanceServer *httptest.Server
fakeExecutionMgr *tmocks.ExecutionManager
}

func TestPreheatSuite(t *testing.T) {
t.Log("Start TestPreheatSuite")
fakeInstanceMgr := &instance.FakeManager{}
fakePolicyMgr := &pmocks.FakeManager{}
fakeInstanceMgr := &instance.Manager{}
fakePolicyMgr := &pmocks.Manager{}
fakeScheduler := &smocks.Scheduler{}
fakeExecutionMgr := &tmocks.ExecutionManager{}

Expand Down
4 changes: 2 additions & 2 deletions src/controller/p2p/preheat/enforcer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (suite *EnforcerTestSuite) SetupSuite() {
suite.server.StartTLS()

fakePolicies := mockPolicies()
fakePolicyManager := &policy.FakeManager{}
fakePolicyManager := &policy.Manager{}
fakePolicyManager.On("Get",
context.TODO(),
mock.AnythingOfType("int64")).
Expand Down Expand Up @@ -130,7 +130,7 @@ func (suite *EnforcerTestSuite) SetupSuite() {
},
}, nil)

fakeInstanceMgr := &instance.FakeManager{}
fakeInstanceMgr := &instance.Manager{}
fakeInstanceMgr.On("Get",
context.TODO(),
mock.AnythingOfType("int64"),
Expand Down
5 changes: 3 additions & 2 deletions src/controller/scan/base_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type ControllerTestSuite struct {
reportMgr *reporttesting.Manager
ar artifact.Controller
c *basicController
reportConverter *postprocessorstesting.ScanReportV1ToV2Converter
reportConverter *postprocessorstesting.NativeScanReportConverter
cache *mockcache.Cache
}

Expand Down Expand Up @@ -339,7 +339,7 @@ func (suite *ControllerTestSuite) SetupSuite() {

execMgr: suite.execMgr,
taskMgr: suite.taskMgr,
reportConverter: &postprocessorstesting.ScanReportV1ToV2Converter{},
reportConverter: &postprocessorstesting.NativeScanReportConverter{},
cache: func() cache.Cache { return suite.cache },
}
mock.OnAnything(suite.scanHandler, "JobVendorType").Return("IMAGE_SCAN")
Expand Down Expand Up @@ -486,6 +486,7 @@ func (suite *ControllerTestSuite) TestScanControllerGetReport() {
{ExtraAttrs: suite.makeExtraAttrs(int64(1), "rp-uuid-001")},
}, nil).Once()
mock.OnAnything(suite.accessoryMgr, "List").Return(nil, nil)
mock.OnAnything(suite.c.reportConverter, "FromRelationalSchema").Return("", nil)
rep, err := suite.c.GetReport(ctx, suite.artifact, []string{v1.MimeTypeNativeReport})
require.NoError(suite.T(), err)
assert.Equal(suite.T(), 1, len(rep))
Expand Down
4 changes: 2 additions & 2 deletions src/controller/scan/callback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type CallbackTestSuite struct {
scanCtl Controller

taskMgr *tasktesting.Manager
reportConverter *postprocessorstesting.ScanReportV1ToV2Converter
reportConverter *postprocessorstesting.NativeScanReportConverter
}

func (suite *CallbackTestSuite) SetupSuite() {
Expand All @@ -69,7 +69,7 @@ func (suite *CallbackTestSuite) SetupSuite() {
suite.taskMgr = &tasktesting.Manager{}
taskMgr = suite.taskMgr

suite.reportConverter = &postprocessorstesting.ScanReportV1ToV2Converter{}
suite.reportConverter = &postprocessorstesting.NativeScanReportConverter{}

suite.scanCtl = &basicController{
makeCtx: context.TODO,
Expand Down
4 changes: 2 additions & 2 deletions src/controller/securityhub/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ControllerTestSuite struct {
c *controller
scannerMgr *scannerMock.Manager
secHubMgr *securityMock.Manager
tagMgr *tagMock.FakeManager
tagMgr *tagMock.Manager
}

// TestController is the entry of controller test suite
Expand All @@ -56,7 +56,7 @@ func TestController(t *testing.T) {
func (suite *ControllerTestSuite) SetupTest() {
suite.secHubMgr = &securityMock.Manager{}
suite.scannerMgr = &scannerMock.Manager{}
suite.tagMgr = &tagMock.FakeManager{}
suite.tagMgr = &tagMock.Manager{}

suite.c = &controller{
secHubMgr: suite.secHubMgr,
Expand Down
Loading

0 comments on commit c6c3726

Please sign in to comment.