Merge pull request #12548 from heww/gen-and-check-mocks

chore(mocks): add make targets to generate and check mocks
This commit is contained in:
He Weiwei 2020-07-22 11:28:13 +08:00 committed by GitHub
commit 06dc5bf10a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 149 additions and 85 deletions

View File

@ -327,6 +327,29 @@ endef
gen_apis: SWAGGER_IMAGENAME
$(call swagger_generate_server,api/v2.0/swagger.yaml,src/server/v2.0,harbor)
MOCKERY_IMAGENAME=goharbor/mockery
MOCKERY_VERSION=v2.1.0
MOCKERY=$(DOCKERCMD) run --rm -u $(shell id -u):$(shell id -g) -v $(BUILDPATH):$(BUILDPATH) -w $(BUILDPATH) ${MOCKERY_IMAGENAME}:${MOCKERY_VERSION}
MOCKERY_IMAGE_BUILD_CMD=${DOCKERBUILD} -f ${TOOLSPATH}/mockery/Dockerfile --build-arg GOLANG=${GOBUILDIMAGE} --build-arg MOCKERY_VERSION=${MOCKERY_VERSION} -t ${MOCKERY_IMAGENAME}:$(MOCKERY_VERSION) .
MOCKERY_IMAGE:
@if [ "$(shell ${DOCKERIMAGES} -q ${MOCKERY_IMAGENAME}:$(MOCKERY_VERSION) 2> /dev/null)" == "" ]; then \
${MOCKERY_IMAGE_BUILD_CMD} && echo "build mockery image successfully" || (echo "build mockery image failed" && exit 1) ; \
fi
gen_mocks: MOCKERY_IMAGE
${MOCKERY} go generate ./...
mocks_check: gen_mocks
@echo checking mocks...
@res=$$(git status -s src/ | awk '{ printf("%s\n", $$2) }' | egrep .*.go); \
if [ -n "$${res}" ]; then \
echo mocks of the interface are out of date... ; \
echo "$${res}"; \
exit 1; \
fi
export VERSIONS_FOR_PREPARE
versions_prepare:
@echo "$$VERSIONS_FOR_PREPARE" > $(MAKE_PREPARE_PATH)/$(PREPARE_VERSION_NAME)
@ -435,7 +458,7 @@ gosec:
$(GOPATH)/bin/gosec -fmt=json -out=harbor_gas_output.json -quiet ./... | true ; \
fi
go_check: gen_apis misspell gofmt commentfmt golint govet
go_check: gen_apis mocks_check misspell gofmt commentfmt golint govet
gofmt:
@echo checking gofmt...

View File

@ -28,5 +28,6 @@ comment:
ignore:
- "src/vendor"
- "src/testing"
- "src/github.com/goharbor/harbor/src/server/v2.0/restapi/**/*"
- "src/github.com/goharbor/harbor/src/server/v2.0/models"

View File

@ -40,11 +40,11 @@ type schedule struct {
// DAO is the data access object interface for schedule
type DAO interface {
Create(ctx context.Context, schedule *schedule) (id int64, err error)
Create(ctx context.Context, s *schedule) (id int64, err error)
List(ctx context.Context, query *q.Query) (schedules []*schedule, err error)
Get(ctx context.Context, id int64) (schedule *schedule, err error)
Get(ctx context.Context, id int64) (s *schedule, err error)
Delete(ctx context.Context, id int64) (err error)
Update(ctx context.Context, schedule *schedule, props ...string) (err error)
Update(ctx context.Context, s *schedule, props ...string) (err error)
}
type dao struct{}

View File

@ -14,4 +14,4 @@
package scheduler
//go:generate mockery -name DAO -output . -outpkg scheduler -filename mock_dao_test.go -structname mockDAO -inpkg
//go:generate mockery --name DAO --output . --outpkg scheduler --filename mock_dao_test.go --structname mockDAO --inpackage

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scheduler
@ -14,20 +14,20 @@ type mockDAO struct {
mock.Mock
}
// Create provides a mock function with given fields: ctx, schedule
func (_m *mockDAO) Create(ctx context.Context, schd *schedule) (int64, error) {
ret := _m.Called(ctx, schd)
// Create provides a mock function with given fields: ctx, s
func (_m *mockDAO) Create(ctx context.Context, s *schedule) (int64, error) {
ret := _m.Called(ctx, s)
var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, *schedule) int64); ok {
r0 = rf(ctx, schd)
r0 = rf(ctx, s)
} else {
r0 = ret.Get(0).(int64)
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *schedule) error); ok {
r1 = rf(ctx, schd)
r1 = rf(ctx, s)
} else {
r1 = ret.Error(1)
}
@ -95,20 +95,20 @@ func (_m *mockDAO) List(ctx context.Context, query *q.Query) ([]*schedule, error
return r0, r1
}
// Update provides a mock function with given fields: ctx, schedule, props
func (_m *mockDAO) Update(ctx context.Context, schd *schedule, props ...string) error {
// Update provides a mock function with given fields: ctx, s, props
func (_m *mockDAO) Update(ctx context.Context, s *schedule, props ...string) error {
_va := make([]interface{}, len(props))
for _i := range props {
_va[_i] = props[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, schd)
_ca = append(_ca, ctx, s)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, *schedule, ...string) error); ok {
r0 = rf(ctx, schd, props...)
r0 = rf(ctx, s, props...)
} else {
r0 = ret.Error(0)
}

View File

@ -14,20 +14,7 @@
package task
//go:generate mockery -dir ./dao -name TaskDAO -output . -outpkg task -filename mock_task_dao_test.go -structname mockTaskDAO
//go:generate mockery -dir ./dao -name ExecutionDAO -output . -outpkg task -filename mock_execution_dao_test.go -structname mockExecutionDAO
// Need to modify the generated mock code manually to avoid the compile error: https://github.com/vektra/mockery/issues/293
/*
func (_m *mockTaskManager) Create(ctx context.Context, executionID int64, job *Job, extraAttrs ...map[string]interface{}) (int64, error) {
_va := make([]interface{}, len(extraAttrs))
for _i := range extraAttrs {
_va[_i] = extraAttrs[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx, executionID, job)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
...
*/
//go:generate mockery -name Manager -output . -outpkg task -filename mock_task_manager_test.go -structname mockTaskManager -inpkg
//go:generate mockery -dir ../../common/job -name Client -output . -outpkg task -filename mock_jobservice_client_test.go -structname mockJobserviceClient
//go:generate mockery --dir ./dao --name TaskDAO --output . --outpkg task --filename mock_task_dao_test.go --structname mockTaskDAO
//go:generate mockery --dir ./dao --name ExecutionDAO --output . --outpkg task --filename mock_execution_dao_test.go --structname mockExecutionDAO
//go:generate mockery --name Manager --output . --outpkg task --filename mock_task_manager_test.go --structname mockTaskManager --inpackage
//go:generate mockery --dir ../../common/job --name Client --output . --outpkg task --filename mock_jobservice_client_test.go --structname mockJobserviceClient

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package task

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package task

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package task

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package task

View File

@ -14,4 +14,4 @@
package common
//go:generate mockery -case snake -dir ../../common/security -name Context -output ./security -outpkg security
//go:generate mockery --case snake --dir ../../common/security --name Context --output ./security --outpkg security

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package security

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package artifact

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package blob

View File

@ -14,10 +14,10 @@
package controller
//go:generate mockery -case snake -dir ../../controller/artifact -name Controller -output ./artifact -outpkg artifact
//go:generate mockery -case snake -dir ../../controller/blob -name Controller -output ./blob -outpkg blob
//go:generate mockery -case snake -dir ../../controller/project -name Controller -output ./project -outpkg project
//go:generate mockery -case snake -dir ../../controller/quota -name Controller -output ./quota -outpkg quota
//go:generate mockery -case snake -dir ../../controller/scan -name Controller -output ./scan -outpkg scan
//go:generate mockery -case snake -dir ../../controller/scan -name Checker -output ./scan -outpkg scan
//go:generate mockery -case snake -dir ../../controller/scanner -name Controller -output ./scanner -outpkg scanner
//go:generate mockery --case snake --dir ../../controller/artifact --name Controller --output ./artifact --outpkg artifact
//go:generate mockery --case snake --dir ../../controller/blob --name Controller --output ./blob --outpkg blob
//go:generate mockery --case snake --dir ../../controller/project --name Controller --output ./project --outpkg project
//go:generate mockery --case snake --dir ../../controller/quota --name Controller --output ./quota --outpkg quota
//go:generate mockery --case snake --dir ../../controller/scan --name Controller --output ./scan --outpkg scan
//go:generate mockery --case snake --dir ../../controller/scan --name Checker --output ./scan --outpkg scan
//go:generate mockery --case snake --dir ../../controller/scanner --name Controller --output ./scanner --outpkg scanner

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package project

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package quota

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scan

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scan

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scanner

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package blob

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package distribution

View File

@ -14,10 +14,11 @@
package pkg
//go:generate mockery -case snake -dir ../../pkg/blob -name Manager -output ./blob -outpkg blob
//go:generate mockery -case snake -dir ../../vendor/github.com/docker/distribution -name Manifest -output ./distribution -outpkg distribution
//go:generate mockery -case snake -dir ../../pkg/quota -name Manager -output ./quota -outpkg quota
//go:generate mockery -case snake -dir ../../pkg/quota/driver -name Driver -output ./quota/driver -outpkg driver
//go:generate mockery -case snake -dir ../../pkg/scan/report -name Manager -output ./scan/report -outpkg report
//go:generate mockery -case snake -dir ../../pkg/scan/rest/v1 -all -output ./scan/rest/v1 -outpkg v1
//go:generate mockery -case snake -dir ../../pkg/scan/scanner -all -output ./scan/scanner -outpkg scanner
//go:generate mockery --case snake --dir ../../pkg/blob --name Manager --output ./blob --outpkg blob
//go:generate mockery --case snake --dir ../../vendor/github.com/docker/distribution --name Manifest --output ./distribution --outpkg distribution
//go:generate mockery --case snake --dir ../../pkg/quota --name Manager --output ./quota --outpkg quota
//go:generate mockery --case snake --dir ../../pkg/quota/driver --name Driver --output ./quota/driver --outpkg driver
//go:generate mockery --case snake --dir ../../pkg/scan/report --name Manager --output ./scan/report --outpkg report
//go:generate mockery --case snake --dir ../../pkg/scan/rest/v1 --all --output ./scan/rest/v1 --outpkg v1
//go:generate mockery --case snake --dir ../../pkg/scan/scanner --all --output ./scan/scanner --outpkg scanner
//go:generate mockery --case snake --dir ../../pkg/scheduler --name Scheduler --output ./scheduler --outpkg scheduler

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package driver

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package quota

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package report

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package v1

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package v1

View File

@ -0,0 +1,19 @@
// Code generated by mockery v2.1.0. DO NOT EDIT.
package v1
import (
http "net/http"
mock "github.com/stretchr/testify/mock"
)
// RequestResolver is an autogenerated mock type for the RequestResolver type
type RequestResolver struct {
mock.Mock
}
// Execute provides a mock function with given fields: req
func (_m *RequestResolver) Execute(req *http.Request) {
_m.Called(req)
}

View File

@ -0,0 +1,37 @@
// Code generated by mockery v2.1.0. DO NOT EDIT.
package v1
import (
http "net/http"
mock "github.com/stretchr/testify/mock"
)
// responseHandler is an autogenerated mock type for the responseHandler type
type responseHandler struct {
mock.Mock
}
// Execute provides a mock function with given fields: code, resp
func (_m *responseHandler) Execute(code int, resp *http.Response) ([]byte, error) {
ret := _m.Called(code, resp)
var r0 []byte
if rf, ok := ret.Get(0).(func(int, *http.Response) []byte); ok {
r0 = rf(code, resp)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]byte)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(int, *http.Response) error); ok {
r1 = rf(code, resp)
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scanner

View File

@ -1,17 +0,0 @@
// 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 scheduler
//go:generate mockery -dir ../../../pkg/scheduler -name Scheduler -output . -outpkg scheduler

View File

@ -1,4 +1,4 @@
// Code generated by mockery v1.1.2. DO NOT EDIT.
// Code generated by mockery v2.1.0. DO NOT EDIT.
package scheduler

13
tools/mockery/Dockerfile Normal file
View File

@ -0,0 +1,13 @@
ARG GOLANG
FROM ${GOLANG}
ARG MOCKERY_VERSION
# https://github.com/docker-library/golang/issues/225
ENV XDG_CACHE_HOME /tmp
RUN mkdir -p /tmp/mockery-${MOCKERY_VERSION} && \
curl -fsSL https://github.com/vektra/mockery/releases/download/${MOCKERY_VERSION}/mockery_${MOCKERY_VERSION#v}_Linux_x86_64.tar.gz | tar -xz -C /tmp/mockery-${MOCKERY_VERSION} && \
mv /tmp/mockery-${MOCKERY_VERSION}/mockery /usr/local/bin && \
chmod +x /usr/local/bin/mockery && \
rm -rf /tmp/mockery-${MOCKERY_VERSION}