Repository: dragonflyoss/Dragonfly2
Pull request #912 (2021-12-09 12:53:13)

fix: go plugin test build error
Signed-off-by: Jim Ma <majinjing3@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate):

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] Code compiles correctly.
- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [ ] I have read the **CONTRIBUTING** document.
- [ ] I have added tests to cover my changes.
- [ ] All new and existing tests passed.


codecov-commenter: # [Codecov](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) Report
> Merging [#912](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (15fbf2e) into [main](https://codecov.io/gh/dragonflyoss/Dragonfly2/commit/9b27f121700269488f00dd1551190e4aaf76e754?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) (9b27f12) will **increase** coverage by `0.05%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/graphs/tree.svg?width=650&height=150&src=pr&token=PUICJYSAT7&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss)

```diff
@@            Coverage Diff             @@
##             main     #912      +/-   ##
==========================================
+ Coverage   41.52%   41.57%   +0.05%     
==========================================
  Files         106      106              
  Lines        8263     8264       +1     
==========================================
+ Hits         3431     3436       +5     
+ Misses       4391     4389       -2     
+ Partials      441      439       -2     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `41.57% <100.00%> (+0.05%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss) | Coverage Δ | |
|---|---|---|
| [pkg/source/hdfsprotocol/hdfs\_source\_client.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3NvdXJjZS9oZGZzcHJvdG9jb2wvaGRmc19zb3VyY2VfY2xpZW50Lmdv) | `50.44% <100.00%> (ø)` | |
| [pkg/util/rangeutils/range.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-cGtnL3V0aWwvcmFuZ2V1dGlscy9yYW5nZS5nbw==) | `52.94% <100.00%> (+0.70%)` | :arrow_up: |
| [client/daemon/peer/peertask\_stream.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX3N0cmVhbS5nbw==) | `55.31% <0.00%> (-1.25%)` | :arrow_down: |
| [client/daemon/peer/piece\_manager.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BpZWNlX21hbmFnZXIuZ28=) | `63.08% <0.00%> (+0.67%)` | :arrow_up: |
| [client/daemon/peer/peertask\_base.go](https://codecov.io/gh/dragonflyoss/Dragonfly2/pull/912/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=dragonflyoss#diff-Y2xpZW50L2RhZW1vbi9wZWVyL3BlZXJ0YXNrX2Jhc2UuZ28=) | `48.49% <0.00%> (+1.20%)` | :arrow_up: |


Patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index adff4272..3ce15150 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -26,7 +26,12 @@ jobs:
         uses: actions/checkout@v2
 
       - name: Run Unit tests
-        run: sudo make test-coverage
+        run: |-
+          # switch to installed go
+          sudo ln -sf `which go` `sudo which go`
+          go version
+          sudo go version
+          sudo make test-coverage
 
       - name: Upload coverage to Codecov
         uses: codecov/codecov-action@v1
diff --git a/go.mod b/go.mod
index ac61ba55..886953f2 100644
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,7 @@ require (
 	github.com/RichardKnop/machinery v1.10.6
 	github.com/Showmax/go-fqdn v1.0.0
 	github.com/VividCortex/mysqlerr v1.0.0
-	github.com/agiledragon/gomonkey v2.0.2+incompatible
+	github.com/agiledragon/gomonkey/v2 v2.3.0
 	github.com/aliyun/aliyun-oss-go-sdk v2.1.6+incompatible
 	github.com/appleboy/gin-jwt/v2 v2.6.5-0.20210827121450-79689222c755
 	github.com/casbin/casbin/v2 v2.34.1
@@ -93,7 +93,7 @@ require (
 	github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
-	github.com/cespare/xxhash/v2 v2.1.1 // indirect
+	github.com/cespare/xxhash/v2 v2.1.2 // indirect
 	github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
 	github.com/davecgh/go-spew v1.1.1 // indirect
 	github.com/denisenkom/go-mssqldb v0.0.0-20200428022330-06a60b6afbbc // indirect
diff --git a/go.sum b/go.sum
index a09edb28..9afe1e59 100644
--- a/go.sum
+++ b/go.sum
@@ -65,8 +65,8 @@ github.com/Showmax/go-fqdn v1.0.0 h1:0rG5IbmVliNT5O19Mfuvna9LL7zlHyRfsSvBPZmF9tM
 github.com/Showmax/go-fqdn v1.0.0/go.mod h1:SfrFBzmDCtCGrnHhoDjuvFnKsWjEQX/Q9ARZvOrJAko=
 github.com/VividCortex/mysqlerr v1.0.0 h1:5pZ2TZA+YnzPgzBfiUWGqWmKDVNBdrkf9g+DNe1Tiq8=
 github.com/VividCortex/mysqlerr v1.0.0/go.mod h1:xERx8E4tBhLvpjzdUyQiSfUxeMcATEQrflDAfXsqcAE=
-github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw=
-github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
+github.com/agiledragon/gomonkey/v2 v2.3.0 h1:olsJjnSDpvqWl4FHByeMUs/r54/az+gQitU3VeEbC98=
+github.com/agiledragon/gomonkey/v2 v2.3.0/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
 github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
 github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
@@ -112,6 +112,8 @@ github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
+github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=
+github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
 github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
 github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
diff --git a/pkg/source/hdfsprotocol/hdfs_source_client.go b/pkg/source/hdfsprotocol/hdfs_source_client.go
index 71944a9b..9f626f9c 100644
--- a/pkg/source/hdfsprotocol/hdfs_source_client.go
+++ b/pkg/source/hdfsprotocol/hdfs_source_client.go
@@ -139,7 +139,7 @@ func (h *hdfsSourceClient) Download(request *source.Request) (io.ReadCloser, err
 			hdfsFile.Close()
 			return nil, err
 		}
-		limitReadN = int64(requestRange.EndIndex - requestRange.StartIndex)
+		limitReadN = int64(requestRange.Length())
 	}
 
 	return newHdfsFileReaderClose(hdfsFile, limitReadN, hdfsFile), nil
diff --git a/pkg/util/rangeutils/range.go b/pkg/util/rangeutils/range.go
index 57e1b732..6e3af269 100644
--- a/pkg/util/rangeutils/range.go
+++ b/pkg/util/rangeutils/range.go
@@ -37,6 +37,10 @@ func (r Range) String() string {
 	return fmt.Sprintf("%d%s%d", r.StartIndex, separator, r.EndIndex)
 }
 
+func (r Range) Length() uint64 {
+	return r.EndIndex - r.StartIndex + 1
+}
+
 // GetRange parses Range according to range string.
 // rangeStr: "start-end"
 func GetRange(rangeStr string) (r *Range, err error) {


Test patch:
diff --git a/pkg/source/hdfsprotocol/hdfs_source_client_test.go b/pkg/source/hdfsprotocol/hdfs_source_client_test.go
index 02d484f1..70aeaede 100644
--- a/pkg/source/hdfsprotocol/hdfs_source_client_test.go
+++ b/pkg/source/hdfsprotocol/hdfs_source_client_test.go
@@ -1,4 +1,6 @@
-//+build linux
+//go:build linux
+// +build linux
+
 /*
  *     Copyright 2020 The Dragonfly Authors
  *
@@ -25,7 +27,7 @@ import (
 	"testing"
 	"time"
 
-	"github.com/agiledragon/gomonkey"
+	"github.com/agiledragon/gomonkey/v2"
 	"github.com/colinmarc/hdfs/v2"
 	"github.com/pkg/errors"
 	"github.com/stretchr/testify/assert"
@@ -197,7 +199,7 @@ func TestIsExpired_LastModifiedNotExpired(t *testing.T) {
 	assert.Nil(t, err)
 }
 
-func Test_Download_FileExist_ByRang(t *testing.T) {
+func Test_Download_FileExist_ByRange(t *testing.T) {
 	var reader *hdfs.FileReader = &hdfs.FileReader{}
 	patch := gomonkey.ApplyMethod(reflect.TypeOf(fakeHDFSClient), "Open", func(*hdfs.Client, string) (*hdfs.FileReader, error) {
 		return reader, nil
@@ -206,9 +208,8 @@ func Test_Download_FileExist_ByRang(t *testing.T) {
 		return 0 - hdfsExistFileContentLength, nil
 	})
 	patch.ApplyMethod(reflect.TypeOf(reader), "Read", func(_ *hdfs.FileReader, b []byte) (int, error) {
-		byets := []byte(hdfsExistFileContent)
-		copy(b, byets)
-		return int(hdfsExistFileContentLength), io.EOF
+		bytes := []byte(hdfsExistFileContent)
+		return copy(b, bytes), io.EOF
 	})
 	patch.ApplyMethodSeq(reflect.TypeOf(reader), "Stat", []gomonkey.OutputCell{
 		{
@@ -221,7 +222,7 @@ func Test_Download_FileExist_ByRang(t *testing.T) {
 	})
 	defer patch.Reset()
 
-	rang := &rangeutils.Range{StartIndex: 0, EndIndex: uint64(hdfsExistFileContentLength)}
+	rang := &rangeutils.Range{StartIndex: 0, EndIndex: uint64(hdfsExistFileContentLength) - 1}
 	// exist file
 	request, err := source.NewRequestWithHeader(hdfsExistFileURL, map[string]string{
 		source.Range: rang.String(),
@@ -229,11 +230,10 @@ func Test_Download_FileExist_ByRang(t *testing.T) {
 	assert.Nil(t, err)
 
 	download, err := sourceClient.Download(request)
+	assert.Nil(t, err)
 	data, _ := ioutil.ReadAll(download)
 
 	assert.Equal(t, hdfsExistFileContent, string(data))
-	assert.Nil(t, err)
-
 }
 
 func TestDownload_FileNotExist(t *testing.T) {
diff --git a/pkg/util/rangeutils/range_test.go b/pkg/util/rangeutils/range_test.go
index 8b458c8b..4da97fc5 100644
--- a/pkg/util/rangeutils/range_test.go
+++ b/pkg/util/rangeutils/range_test.go
@@ -24,46 +24,52 @@ import (
 
 func TestParseRange(t *testing.T) {
 	var cases = []struct {
-		rangeStr string
-		length   uint64
-		expected *Range
-		wantErr  bool
+		rangeStr     string
+		totalLength  uint64
+		targetLength uint64
+		expected     *Range
+		wantErr      bool
 	}{
 		{
-			rangeStr: "0-65575",
-			length:   65576,
+			rangeStr:     "0-65575",
+			totalLength:  65576,
+			targetLength: 65576,
 			expected: &Range{
 				StartIndex: 0,
 				EndIndex:   65575,
 			},
 			wantErr: false,
 		}, {
-			rangeStr: "2-2",
-			length:   65576,
+			rangeStr:     "2-2",
+			totalLength:  65576,
+			targetLength: 1,
 			expected: &Range{
 				StartIndex: 2,
 				EndIndex:   2,
 			},
 			wantErr: false,
 		}, {
-			rangeStr: "2-",
-			length:   65576,
+			rangeStr:     "2-",
+			totalLength:  65576,
+			targetLength: 65574,
 			expected: &Range{
 				StartIndex: 2,
 				EndIndex:   65575,
 			},
 			wantErr: false,
 		}, {
-			rangeStr: "-100",
-			length:   65576,
+			rangeStr:     "-100",
+			totalLength:  65576,
+			targetLength: 100,
 			expected: &Range{
 				StartIndex: 65476,
 				EndIndex:   65575,
 			},
 			wantErr: false,
 		}, {
-			rangeStr: "0-66575",
-			length:   65576,
+			rangeStr:     "0-66575",
+			totalLength:  65576,
+			targetLength: 65576,
 			expected: &Range{
 				StartIndex: 0,
 				EndIndex:   65575,
@@ -71,34 +77,39 @@ func TestParseRange(t *testing.T) {
 			wantErr: false,
 		},
 		{
-			rangeStr: "0-65-575",
-			length:   65576,
-			expected: nil,
-			wantErr:  true,
+			rangeStr:    "0-65-575",
+			totalLength: 65576,
+			expected:    nil,
+			wantErr:     true,
 		},
 		{
-			rangeStr: "0-hello",
-			length:   65576,
-			expected: nil,
-			wantErr:  true,
+			rangeStr:    "0-hello",
+			totalLength: 65576,
+			expected:    nil,
+			wantErr:     true,
 		},
 		{
-			rangeStr: "65575-0",
-			length:   65576,
-			expected: nil,
-			wantErr:  true,
+			rangeStr:    "65575-0",
+			totalLength: 65576,
+			expected:    nil,
+			wantErr:     true,
 		},
 		{
-			rangeStr: "-1-8",
-			length:   65576,
-			expected: nil,
-			wantErr:  true,
+			rangeStr:    "-1-8",
+			totalLength: 65576,
+			expected:    nil,
+			wantErr:     true,
 		},
 	}
 
 	for _, v := range cases {
-		result, err := ParseRange(v.rangeStr, v.length)
-		assert.Equal(t, v.expected, result)
-		assert.Equal(t, v.wantErr, err != nil)
+		t.Run(v.rangeStr, func(t *testing.T) {
+			result, err := ParseRange(v.rangeStr, v.totalLength)
+			assert.Equal(t, v.expected, result)
+			assert.Equal(t, v.wantErr, err != nil)
+			if !v.wantErr {
+				assert.Equal(t, v.targetLength, result.Length())
+			}
+		})
 	}
 }
diff --git a/scheduler/supervisor/cdn_test.go b/scheduler/supervisor/cdn_test.go
index 8c283be5..bfb6b98a 100644
--- a/scheduler/supervisor/cdn_test.go
+++ b/scheduler/supervisor/cdn_test.go
@@ -26,7 +26,7 @@ import (
 	"reflect"
 	"testing"
 
-	"github.com/agiledragon/gomonkey"
+	"github.com/agiledragon/gomonkey/v2"
 	"github.com/golang/mock/gomock"
 	"github.com/pkg/errors"
 	"github.com/stretchr/testify/assert"