Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #814 from yeya24/feature/support-golangci-lint
Browse files Browse the repository at this point in the history
feature: replace gometalinter with golangci-lint
  • Loading branch information
allencloud authored Aug 20, 2019
2 parents a5c9629 + 3149d9d commit 017c312
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 42 deletions.
24 changes: 5 additions & 19 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
jobs:
markdownlint-misspell-shellcheck:
linters-check:
docker:
# this image is build from Dockerfile
# https://github.com/pouchcontainer/pouchlinter/blob/master/Dockerfile
- image: pouchcontainer/pouchlinter:v0.2.1
- image: pouchcontainer/pouchlinter:v0.2.3
working_directory: /go/src/github.com/dragonflyoss/Dragonfly
steps:
- checkout
Expand Down Expand Up @@ -41,27 +41,14 @@ jobs:
name: validate go mod files
command: |
make check-go-mod
gocode-check-via-gometalinter-swagger:
docker:
- image: pouchcontainer/pouchlinter:v0.2.1
working_directory: /go/src/github.com/dragonflyoss/Dragonfly
steps:
- checkout
- run:
name: validate swagger.yml
command: |
swagger validate "/go/src/github.com/dragonflyoss/Dragonfly/apis/swagger.yml"
- run:
name: use gometalinter to check gocode of this project.
command: |
make go-mod-vendor
gometalinter --disable-all --cyclo-over=20 --skip vendor -E gofmt -E goimports -E golint -E misspell -E vet -E goconst -E gocyclo ./...
- run:
name: detect deadcode without test folder
name: use golangci-lint to check gocode of this project.
command: |
make go-mod-vendor
gometalinter --disable-all --skip vendor --skip test -E deadcode ./...
make golangci-lint
unit-test-golang:
docker:
Expand Down Expand Up @@ -101,7 +88,6 @@ workflows:
version: 2
ci:
jobs:
- markdownlint-misspell-shellcheck
- linters-check
- unit-test-golang
- gocode-check-via-gometalinter-swagger
- api-integration-test
25 changes: 25 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
run:
deadline: 3m
modules-download-mode: readonly

linters-settings:
gocyclo:
min-complexity: 20

linters:
disable-all: true
enable:
- gofmt
- goimports
- golint
- misspell
- govet
- goconst
- deadcode
- gocyclo

# output configuration options
output:
format: colored-line-number
print-issued-lines: true
print-linter-name: true
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ go-mod-tidy:
@go mod tidy
.PHONY: go-mod-tidy

# we need this because currently gometalinter can't work in go mod environment.
go-mod-vendor:
@echo "Begin to vendor go mod dependency"
@go mod vendor
Expand Down Expand Up @@ -154,3 +153,7 @@ df.crt: df.key
openssl req -new -key df.key -out df.csr
openssl x509 -req -sha256 -days 365 -in df.csr -signkey df.key -out df.crt
rm df.csr

golangci-lint:
./hack/golangci-lint.sh
.PHONY: golangci-lint
4 changes: 2 additions & 2 deletions dfget/core/api/supernode_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (api *supernodeAPI) ReportPiece(node string, req *types.ReportPieceRequest)
logrus.Errorf("failed to report piece{taskid:%s,range:%s},err: %v", req.TaskID, req.PieceRange, e)
return nil, e
}
if resp != nil && resp.Code != constants.CodeGetPieceReport {
if resp.Code != constants.CodeGetPieceReport {
logrus.Errorf("failed to report piece{taskid:%s,range:%s} to supernode: api response code is %d not equal to %d", req.TaskID, req.PieceRange, resp.Code, constants.CodeGetPieceReport)
}
return
Expand All @@ -131,7 +131,7 @@ func (api *supernodeAPI) ServiceDown(node string, taskID string, cid string) (
logrus.Errorf("failed to send service down,err: %v", e)
return nil, e
}
if resp != nil && resp.Code != constants.CodeGetPeerDown {
if resp.Code != constants.CodeGetPeerDown {
logrus.Errorf("failed to send service down to supernode: api response code is %d not equal to %d", resp.Code, constants.CodeGetPeerDown)
}
return
Expand Down
27 changes: 10 additions & 17 deletions dfget/core/api/supernode_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
"github.com/go-check/check"
)

const localhost = "127.0.0.1"

func Test(t *testing.T) {
check.TestingT(t)
}
Expand Down Expand Up @@ -55,75 +57,66 @@ func init() {
// unit tests for SupernodeAPI

func (s *SupernodeAPITestSuite) TestSupernodeAPI_Register(c *check.C) {
ip := "127.0.0.1"

s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(0, nil, nil)
r, e := s.api.Register(ip, createRegisterRequest())
r, e := s.api.Register(localhost, createRegisterRequest())
c.Assert(r, check.IsNil)
c.Assert(e.Error(), check.Equals, "0:")

s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(0, nil,
fmt.Errorf("test"))
r, e = s.api.Register(ip, createRegisterRequest())
r, e = s.api.Register(localhost, createRegisterRequest())
c.Assert(r, check.IsNil)
c.Assert(e.Error(), check.Equals, "test")

res := types.RegisterResponse{BaseResponse: &types.BaseResponse{}}
s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(200, []byte(res.String()), nil)
r, e = s.api.Register(ip, createRegisterRequest())
r, e = s.api.Register(localhost, createRegisterRequest())
c.Assert(r, check.NotNil)
c.Assert(r.Code, check.Equals, 0)

res.Code = constants.Success
res.Data = &types.RegisterResponseData{FileLength: int64(32)}
s.mock.PostJSONFunc = s.mock.CreatePostJSONFunc(200, []byte(res.String()), nil)
r, e = s.api.Register(ip, createRegisterRequest())
r, e = s.api.Register(localhost, createRegisterRequest())
c.Assert(r, check.NotNil)
c.Assert(r.Code, check.Equals, constants.Success)
c.Assert(r.Data.FileLength, check.Equals, res.Data.FileLength)
}

func (s *SupernodeAPITestSuite) TestSupernodeAPI_PullPieceTask(c *check.C) {
ip := "127.0.0.1"

res := &types.PullPieceTaskResponse{BaseResponse: &types.BaseResponse{}}
res.Code = constants.CodePeerFinish
res.Data = []byte(`{"fileLength":2}`)
s.mock.GetFunc = s.mock.CreateGetFunc(200, []byte(res.String()), nil)

r, e := s.api.PullPieceTask(ip, nil)
r, e := s.api.PullPieceTask(localhost, nil)

c.Assert(e, check.IsNil)
c.Assert(r.Code, check.Equals, res.Code)
c.Assert(r.FinishData().FileLength, check.Equals, int64(2))
}

func (s *SupernodeAPITestSuite) TestSupernodeAPI_ReportPiece(c *check.C) {
ip := "127.0.0.1"
req := &types.ReportPieceRequest{
TaskID: "sssss",
PieceRange: "0-11",
}
s.mock.GetFunc = s.mock.CreateGetFunc(200, []byte(`{"Code":700}`), nil)
r, e := s.api.ReportPiece(ip, req)
r, e := s.api.ReportPiece(localhost, req)
c.Check(e, check.IsNil)
c.Check(r.Code, check.Equals, 700)
}

func (s *SupernodeAPITestSuite) TestSupernodeAPI_ServiceDown(c *check.C) {
ip := "127.0.0.1"

s.mock.GetFunc = s.mock.CreateGetFunc(200, []byte(`{"Code":200}`), nil)
r, e := s.api.ServiceDown(ip, "", "")
r, e := s.api.ServiceDown(localhost, "", "")
c.Check(e, check.IsNil)
c.Check(r.Code, check.Equals, 200)
}

func (s *SupernodeAPITestSuite) TestSupernodeAPI_ReportClientError(c *check.C) {
ip := "127.0.0.1"

s.mock.GetFunc = s.mock.CreateGetFunc(200, []byte(`{"Code":700}`), nil)
r, e := s.api.ReportClientError(ip, nil)
r, e := s.api.ReportClientError(localhost, nil)
c.Check(e, check.IsNil)
c.Check(r.Code, check.Equals, 700)
}
Expand Down
5 changes: 3 additions & 2 deletions dfget/core/downloader/p2p_downloader/p2p_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,9 @@ func (p2p *P2PDownloader) pullPieceTask(item *Piece) (

logrus.Errorf("pull piece task fail:%v and will migrate", res)
var registerRes *regist.RegisterResult
if registerRes, err = p2p.Register.Register(p2p.cfg.RV.PeerPort); err != nil {
return nil, err
registerRes, registerErr := p2p.Register.Register(p2p.cfg.RV.PeerPort)
if registerErr != nil {
return nil, registerErr
}
p2p.pieceSizeHistory[1] = registerRes.PieceSize
item.Status = constants.TaskStatusStart
Expand Down
2 changes: 1 addition & 1 deletion hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ build-docker() {
-e GOARCH="${GOARCH}" \
-e CGO_ENABLED=0 \
-e GO111MODULE=on \
-e GOPROXY=https://goproxy.io \
-e GOPROXY="${GOPROXY}" \
-w /go/src/${PKG} \
${BUILD_IMAGE} \
go build -o "/go/bin/$1" -ldflags "${LDFLAGS}" ./cmd/"$2"
Expand Down
67 changes: 67 additions & 0 deletions hack/golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail

# golangci-lint binary version v1.17.1

PKG=github.com/dragonflyoss/Dragonfly
LINT_IMAGE=pouchcontainer/pouchlinter:v0.2.3
USE_DOCKER=${USE_DOCKER:-"0"}

curDir=$(cd "$(dirname "$0")" && pwd)
cd "${curDir}" || return
LINT_SOURCE_HOME=$(cd ".." && pwd)

. ./env.sh

golangci-lint-docker() {
cd "${LINT_SOURCE_HOME}" || return
docker run \
--rm \
-ti \
-v "$(pwd)"/.go/pkg:/go/pkg \
-v "$(pwd)":/go/src/${PKG} \
-e GO111MODULE=on \
-e GOPROXY="${GOPROXY}" \
-w /go/src/${PKG} \
${LINT_IMAGE} \
golangci-lint run
}

check-golangci-lint() {
has_installed="$(command -v golangci-lint || echo false)"
if [[ "${has_installed}" = "false" ]]; then
echo false
return
fi
echo true
}

golangci-lint-local() {
has_installed="$(check-golangci-lint)"
if [[ "${has_installed}" = "true" ]]; then
echo "Detected that golangci-lint has already been installed. Start linting..."
cd "${LINT_SOURCE_HOME}" || return
golangci-lint run
return
else
echo "Detected that golangci-lint has not been installed. You have to install golangci-lint first."
return 1
fi
}

main() {
if [[ "1" == "${USE_DOCKER}" ]]
then
echo "Begin to check gocode with golangci-lint in docker."
golangci-lint-docker
else
echo "Begin to check gocode with golangci-lint locally"
golangci-lint-local
fi
echo "Check gocode with golangci-lint successfully "
}

main "$@"

0 comments on commit 017c312

Please sign in to comment.