Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
update to golangci-lint
Browse files Browse the repository at this point in the history
Signed-off-by: yeya24 <[email protected]>
  • Loading branch information
yeya24 committed Aug 15, 2019
1 parent fb377a9 commit fb1dbae
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
22 changes: 10 additions & 12 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
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.2
working_directory: /go/src/github.com/dragonflyoss/Dragonfly
steps:
- checkout
Expand All @@ -32,6 +32,7 @@ jobs:
- run:
name: use opensource tool client9/misspell to correct commonly misspelled English words
command: |
go get -u github.com/client9/misspell/cmd/misspell
find ./* -name "*" | xargs misspell -error
- run:
name: use ShellCheck (https://github.com/koalaman/shellcheck) to check the validateness of shell scripts in pouch repo
Expand All @@ -42,9 +43,12 @@ jobs:
command: |
make check-go-mod
gocode-check-via-gometalinter-swagger:
gocode-check-via-golangci-lint-swagger:
docker:
- image: pouchcontainer/pouchlinter:v0.2.1
- image: pouchcontainer/pouchlinter:v0.2.2
environment:
GO111MODULE: "on"
GOPROXY: "https://goproxy.io"
working_directory: /go/src/github.com/dragonflyoss/Dragonfly
steps:
- checkout
Expand All @@ -53,15 +57,9 @@ jobs:
command: |
swagger validate "/go/src/github.com/dragonflyoss/Dragonfly/apis/swagger.yml"
- run:
name: use gometalinter to check gocode of this project.
name: use golangci-lint 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
command: |
make go-mod-vendor
gometalinter --disable-all --skip vendor --skip test -E deadcode ./...
golangci-lint run
unit-test-golang:
docker:
Expand Down Expand Up @@ -103,5 +101,5 @@ workflows:
jobs:
- markdownlint-misspell-shellcheck
- unit-test-golang
- gocode-check-via-gometalinter-swagger
- gocode-check-via-golangci-lint-swagger
- api-integration-test
28 changes: 28 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
run:
deadline: 3m
modules-download-mode: readonly

linters-settings:
gocyclo:
min-complexity: 20
goconst:
min-len: 3
min-occurrences: 5

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
1 change: 0 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
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
6 changes: 4 additions & 2 deletions dfget/core/downloader/p2p_downloader/p2p_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/dragonflyoss/Dragonfly/dfget/core/regist"
"github.com/dragonflyoss/Dragonfly/dfget/types"
"github.com/dragonflyoss/Dragonfly/pkg/constants"
"github.com/dragonflyoss/Dragonfly/pkg/errortypes"
"github.com/dragonflyoss/Dragonfly/pkg/fileutils"
"github.com/dragonflyoss/Dragonfly/pkg/httputils"
"github.com/dragonflyoss/Dragonfly/pkg/printer"
Expand Down Expand Up @@ -257,8 +258,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
var registerErr *errortypes.DfError
if registerRes, registerErr = p2p.Register.Register(p2p.cfg.RV.PeerPort); registerErr != nil {
return nil, registerErr
}
p2p.pieceSizeHistory[1] = registerRes.PieceSize
item.Status = constants.TaskStatusStart
Expand Down

0 comments on commit fb1dbae

Please sign in to comment.