From 6dbceba63095049e538b7869906260c53276adaa Mon Sep 17 00:00:00 2001 From: Shinya Hayashi Date: Wed, 25 Oct 2023 21:47:31 +0900 Subject: [PATCH] enable lint and fix lint errors Signed-off-by: Shinya Hayashi --- .github/workflows/lint.yml | 25 +++++++++++++++++++++++++ Makefile | 11 +++++++++++ argparser/ope_ratio.go | 2 +- cmd/follower.go | 5 ++++- pattern/pattern.go | 9 +++++++-- pattern/pattern_test.go | 7 ++++--- runner/runner.go | 11 ++++++----- 7 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..7ba47af --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,25 @@ +name: Main + +on: + push: + branches: [ "main" ] + paths-ignore: + - '**.md' + pull_request: + branches: [ "main" ] + paths-ignore: + - '**.md' + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: "1.20" + + - name: Lint + run: make lint diff --git a/Makefile b/Makefile index 38081ef..cfcc756 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ MINIO_CERTDIR := $(shell git rev-parse --show-toplevel)/test/certs CERTGEN_VERSION := v1.2.1 CERTGEN := $(BINDIR)/certgen-$(CERTGEN_VERSION) +GOLANGCI_LINT_VERSION := v1.55.1 +GOLANGCI_LINT := $(BINDIR)/golangci-lint-$(GOLANGCI_LINT_VERSION) + S3_ENDPOINT ?= http://localhost:9000 CERT_CONFIG ?= "" @@ -31,6 +34,14 @@ $(MINIO_CERTDIR): image: docker build . --file Dockerfile --tag $(IMAGE_NAME) +$(GOLANGCI_LINT): | $(BINDIR) + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b . $(GOLANGCI_LINT_VERSION) + mv golangci-lint $(GOLANGCI_LINT) + +.PHONY: lint +lint: | $(GOLANGCI_LINT) + $(GOLANGCI_LINT) run + .PHONY: test test: $(OVAL) go test -v ./... diff --git a/argparser/ope_ratio.go b/argparser/ope_ratio.go index 8368d84..803387c 100644 --- a/argparser/ope_ratio.go +++ b/argparser/ope_ratio.go @@ -24,7 +24,7 @@ func ParseOpeRatio(opeRatioStr string) ([]float64, error) { ratio[i] = float64(intV) sum += ratio[i] } - for i, _ := range ratio { + for i := range ratio { ratio[i] /= sum } return ratio, nil diff --git a/cmd/follower.go b/cmd/follower.go index d80ba6a..203bf5f 100644 --- a/cmd/follower.go +++ b/cmd/follower.go @@ -54,5 +54,8 @@ func init() { followerCmd.Flags().IntVar(&followerPort, "follower_port", invalidPortNumber, "TCP port number to which the follower listens.") followerCmd.Flags().StringVar(&caCertFileName, "cacert", "", "File name of CA certificate.") - followerCmd.MarkFlagRequired("follower_port") + err := followerCmd.MarkFlagRequired("follower_port") + if err != nil { + log.Fatal(err) + } } diff --git a/pattern/pattern.go b/pattern/pattern.go index cb4b6f1..c5a0e84 100644 --- a/pattern/pattern.go +++ b/pattern/pattern.go @@ -120,7 +120,13 @@ func generateDataUnit(unitCount, workerID int, bucketName string, obj *object.Ob unixTime := dt.UnixMicro() binary.LittleEndian.PutUint32(numBinBuf[8:], uint32(workerID)) binary.LittleEndian.PutUint64(numBinBuf[12:], uint64(unixTime)) - writer.Write(numBinBuf) + n, err = writer.Write(numBinBuf) + if err != nil { + return err + } + if n != dataUnitHeaderSizeWithoutBucketAndKey { + return fmt.Errorf("the data unit header without bucket name and key was not written correctly. n = %d", n) + } unitBodyStartPos := object.MaxBucketNameLength + object.MaxKeyLength + dataUnitHeaderSizeWithoutBucketAndKey tmpData := make([]byte, 4) @@ -186,7 +192,6 @@ func validDataUnit(unitCount, workerID int, expectedBucketName string, obj *obje } actualWorkerID := int(binary.LittleEndian.Uint32(data[current : current+4])) - current = current + 4 if workerID != actualWorkerID { errMsg += fmt.Sprintf("- WorkerID is wrong. (expected = \"%d\", actual = \"%d\")\n", workerID, actualWorkerID) diff --git a/pattern/pattern_test.go b/pattern/pattern_test.go index b36e2ff..1534daf 100644 --- a/pattern/pattern_test.go +++ b/pattern/pattern_test.go @@ -39,7 +39,8 @@ func (suite *PatternSuite) TestGenerateDataUnitSuccess() { workerID := 100 suite.Equal(nil, generateDataUnit(4, workerID, testBucketName, obj, suite.f)) - suite.f.Seek(0, 0) + _, err := suite.f.Seek(0, 0) + suite.NoError(err) data, err := io.ReadAll(suite.f) suite.NoError(err) suite.Equal(dataUnitSize, len(data)) @@ -129,7 +130,7 @@ func (suite *PatternSuite) TestGenerateLongBucketName() { // 1st data unit // bucketName - suite.Equal(append([]byte(testLongBucketName[:object.MaxBucketNameLength])), + suite.Equal([]byte(testLongBucketName[:object.MaxBucketNameLength]), data[0:object.MaxBucketNameLength]) current := object.MaxBucketNameLength // keyName @@ -147,7 +148,7 @@ func (suite *PatternSuite) TestGenerateLongBucketName() { // 2nd data unit current = dataUnitSize // bucketName - suite.Equal(append([]byte(testLongBucketName[:object.MaxBucketNameLength])), + suite.Equal([]byte(testLongBucketName[:object.MaxBucketNameLength]), data[current:current+object.MaxBucketNameLength]) current += object.MaxBucketNameLength // keyName diff --git a/runner/runner.go b/runner/runner.go index 4408bb8..61e6e9e 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -86,7 +86,10 @@ func loadSavedContext(loadFileName string) *ExecutionContext { log.Fatal(err) } ec := &ExecutionContext{} - json.Unmarshal(savedContext, ec) + err = json.Unmarshal(savedContext, ec) + if err != nil { + log.Fatal(err) + } return ec } @@ -123,7 +126,6 @@ func (r *Runner) init() { if r.loadFileName == "" { r.execContext.Workers = make([]Worker, r.execContext.NumWorker) - rand.Seed(time.Now().UnixNano()) startID := rand.Intn(maxWorkerID) r.execContext.StartWorkerID = startID for i := range r.execContext.Workers { @@ -144,13 +146,13 @@ func (r *Runner) init() { r.execContext.Workers[i].ShowInfo() } } else { - for i, _ := range r.execContext.Workers { + for i := range r.execContext.Workers { r.execContext.Workers[i].id = (r.execContext.StartWorkerID + i) % maxWorkerID r.execContext.Workers[i].minSize = r.execContext.MinSize r.execContext.Workers[i].maxSize = r.execContext.MaxSize r.execContext.Workers[i].client = r.client r.execContext.Workers[i].st = &r.st - for j, _ := range r.execContext.Workers[i].BucketsWithObject { + for j := range r.execContext.Workers[i].BucketsWithObject { r.execContext.Workers[i].BucketsWithObject[j].ObjectMeta.TidyUp() } r.execContext.Workers[i].ShowInfo() @@ -227,7 +229,6 @@ const ( ) func (r *Runner) selectOperation() Operation { - rand.Seed(time.Now().UnixNano()) randVal := rand.Float64() if randVal < r.opeRatio[0] { return Put