diff --git a/.gitignore b/.gitignore index 3ff849040..6e6b5a506 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ *.cov *service_account.json +.tools/ diff --git a/.gometalinter.json b/.gometalinter.json deleted file mode 100644 index 5c27fe988..000000000 --- a/.gometalinter.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "Disable": ["maligned","gas","gosec"], - "Exclude": [ - ".+_test\\.go" - ], - "Deadline": "120s" -} \ No newline at end of file diff --git a/Makefile b/Makefile index a0982eeb8..d06fd3717 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +LINTER_TOOL=.tools/bin/golangci-lint +GOVERALLS_TOOL=.tools/bin/goveralls + all: test lint travis: build-all test upload-coverage @@ -10,24 +13,40 @@ setup: travis-setup: GO111MODULE=off go get github.com/mattn/goveralls -lint: - gometalinter.v2 ./... +lint: $(LINTER_TOOL) + $(LINTER_TOOL) run -build-all: - # this downloads all dependencies for all OS/architectures and updates go.mod - # TODO(jkowalski): parallelize this once we're on 1.12 +$(LINTER_TOOL): + mkdir -p .tools + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b .tools/bin/ v1.16.0 + +$(GOVERALLS_TOOL): + mkdir -p .tools + GO111MODULE=off GOPATH=$(CURDIR)/.tools go get github.com/mattn/goveralls + +build-linux-amd64: CGO_ENABLED=0 GO111MODULE=on GOARCH=amd64 GOOS=linux go build ./... + +build-windows-amd64: CGO_ENABLED=0 GO111MODULE=on GOARCH=amd64 GOOS=windows go build ./... + +build-darwin-amd64: CGO_ENABLED=0 GO111MODULE=on GOARCH=amd64 GOOS=darwin go build ./... + +build-linux-arm: CGO_ENABLED=0 GO111MODULE=on GOARCH=arm GOOS=linux go build ./... + +build-linux-arm64: CGO_ENABLED=0 GO111MODULE=on GOARCH=arm64 GOOS=linux go build ./... +build-all: build-linux-amd64 build-windows-amd64 build-darwin-amd64 build-linux-arm build-linux-arm64 + test: GO111MODULE=on go test -tags test -count=1 -coverprofile=raw.cov --coverpkg ./... -timeout 90s ./... grep -v testing/ raw.cov > tmp.cov upload-coverage: - goveralls -service=travis-ci -coverprofile=tmp.cov + $(GOVERALLS_TOOL) -service=travis-ci -coverprofile=tmp.cov coverage-html: go tool cover -html=tmp.cov diff --git a/block/block_cache_test.go b/block/block_cache_test.go index ce9a54677..485b24e4c 100644 --- a/block/block_cache_test.go +++ b/block/block_cache_test.go @@ -17,12 +17,12 @@ "github.com/kopia/repo/storage" ) -func newUnderlyingStorageForBlockCacheTesting() storage.Storage { +func newUnderlyingStorageForBlockCacheTesting(t *testing.T) storage.Storage { ctx := context.Background() data := map[string][]byte{} st := storagetesting.NewMapStorage(data, nil, nil) - st.PutBlock(ctx, "block-1", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) - st.PutBlock(ctx, "block-4k", bytes.Repeat([]byte{1, 2, 3, 4}, 1000)) // 4000 bytes + assertNoError(t, st.PutBlock(ctx, "block-1", []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})) + assertNoError(t, st.PutBlock(ctx, "block-4k", bytes.Repeat([]byte{1, 2, 3, 4}, 1000))) // 4000 bytes return st } @@ -30,7 +30,7 @@ func TestCacheExpiration(t *testing.T) { cacheData := map[string][]byte{} cacheStorage := storagetesting.NewMapStorage(cacheData, nil, nil) - underlyingStorage := newUnderlyingStorageForBlockCacheTesting() + underlyingStorage := newUnderlyingStorageForBlockCacheTesting(t) cache, err := newBlockCacheWithCacheStorage(context.Background(), underlyingStorage, cacheStorage, CachingOptions{ MaxCacheSizeBytes: 10000, @@ -41,10 +41,14 @@ func TestCacheExpiration(t *testing.T) { defer cache.close() ctx := context.Background() - cache.getContentBlock(ctx, "00000a", "block-4k", 0, -1) // 4k - cache.getContentBlock(ctx, "00000b", "block-4k", 0, -1) // 4k - cache.getContentBlock(ctx, "00000c", "block-4k", 0, -1) // 4k - cache.getContentBlock(ctx, "00000d", "block-4k", 0, -1) // 4k + _, err = cache.getContentBlock(ctx, "00000a", "block-4k", 0, -1) // 4k + assertNoError(t, err) + _, err = cache.getContentBlock(ctx, "00000b", "block-4k", 0, -1) // 4k + assertNoError(t, err) + _, err = cache.getContentBlock(ctx, "00000c", "block-4k", 0, -1) // 4k + assertNoError(t, err) + _, err = cache.getContentBlock(ctx, "00000d", "block-4k", 0, -1) // 4k + assertNoError(t, err) // wait for a sweep time.Sleep(2 * time.Second) @@ -52,7 +56,7 @@ func TestCacheExpiration(t *testing.T) { // 00000a and 00000b will be removed from cache because it's the oldest. // to verify, let's remove block-4k from the underlying storage and make sure we can still read // 00000c and 00000d from the cache but not 00000a nor 00000b - underlyingStorage.DeleteBlock(ctx, "block-4k") + assertNoError(t, underlyingStorage.DeleteBlock(ctx, "block-4k")) cases := []struct { block string @@ -83,7 +87,7 @@ func TestDiskBlockCache(t *testing.T) { } defer os.RemoveAll(tmpDir) - cache, err := newBlockCache(ctx, newUnderlyingStorageForBlockCacheTesting(), CachingOptions{ + cache, err := newBlockCache(ctx, newUnderlyingStorageForBlockCacheTesting(t), CachingOptions{ MaxCacheSizeBytes: 10000, CacheDirectory: tmpDir, }) @@ -161,7 +165,7 @@ func TestCacheFailureToOpen(t *testing.T) { cacheData := map[string][]byte{} cacheStorage := storagetesting.NewMapStorage(cacheData, nil, nil) - underlyingStorage := newUnderlyingStorageForBlockCacheTesting() + underlyingStorage := newUnderlyingStorageForBlockCacheTesting(t) faultyCache := &storagetesting.FaultyStorage{ Base: cacheStorage, Faults: map[string][]*storagetesting.Fault{ @@ -172,7 +176,7 @@ func TestCacheFailureToOpen(t *testing.T) { } // Will fail because of ListBlocks failure. - cache, err := newBlockCacheWithCacheStorage(context.Background(), underlyingStorage, faultyCache, CachingOptions{ + _, err := newBlockCacheWithCacheStorage(context.Background(), underlyingStorage, faultyCache, CachingOptions{ MaxCacheSizeBytes: 10000, }, 0, 5*time.Hour) if err == nil || !strings.Contains(err.Error(), someError.Error()) { @@ -180,7 +184,7 @@ func TestCacheFailureToOpen(t *testing.T) { } // ListBlocks fails only once, next time it succeeds. - cache, err = newBlockCacheWithCacheStorage(context.Background(), underlyingStorage, faultyCache, CachingOptions{ + cache, err := newBlockCacheWithCacheStorage(context.Background(), underlyingStorage, faultyCache, CachingOptions{ MaxCacheSizeBytes: 10000, }, 0, 100*time.Millisecond) if err != nil { @@ -195,7 +199,7 @@ func TestCacheFailureToWrite(t *testing.T) { cacheData := map[string][]byte{} cacheStorage := storagetesting.NewMapStorage(cacheData, nil, nil) - underlyingStorage := newUnderlyingStorageForBlockCacheTesting() + underlyingStorage := newUnderlyingStorageForBlockCacheTesting(t) faultyCache := &storagetesting.FaultyStorage{ Base: cacheStorage, } @@ -239,7 +243,7 @@ func TestCacheFailureToRead(t *testing.T) { cacheData := map[string][]byte{} cacheStorage := storagetesting.NewMapStorage(cacheData, nil, nil) - underlyingStorage := newUnderlyingStorageForBlockCacheTesting() + underlyingStorage := newUnderlyingStorageForBlockCacheTesting(t) faultyCache := &storagetesting.FaultyStorage{ Base: cacheStorage, } @@ -275,13 +279,20 @@ func TestCacheFailureToRead(t *testing.T) { func verifyStorageBlockList(t *testing.T, st storage.Storage, expectedBlocks ...string) { t.Helper() var foundBlocks []string - st.ListBlocks(context.Background(), "", func(bm storage.BlockMetadata) error { + assertNoError(t, st.ListBlocks(context.Background(), "", func(bm storage.BlockMetadata) error { foundBlocks = append(foundBlocks, bm.BlockID) return nil - }) + })) sort.Strings(foundBlocks) if !reflect.DeepEqual(foundBlocks, expectedBlocks) { t.Errorf("unexpected block list: %v, wanted %v", foundBlocks, expectedBlocks) } } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +} diff --git a/block/block_index_recovery_test.go b/block/block_index_recovery_test.go index d42cdc672..287d81af1 100644 --- a/block/block_index_recovery_test.go +++ b/block/block_index_recovery_test.go @@ -22,10 +22,10 @@ func TestBlockIndexRecovery(t *testing.T) { } // delete all index blocks - bm.st.ListBlocks(ctx, newIndexBlockPrefix, func(bi storage.BlockMetadata) error { + assertNoError(t, bm.st.ListBlocks(ctx, newIndexBlockPrefix, func(bi storage.BlockMetadata) error { log.Debugf("deleting %v", bi.BlockID) return bm.st.DeleteBlock(ctx, bi.BlockID) - }) + })) // now with index blocks gone, all blocks appear to not be found bm = newTestBlockManager(data, keyTime, nil) diff --git a/block/block_manager.go b/block/block_manager.go index bc417fb9e..1290959ae 100644 --- a/block/block_manager.go +++ b/block/block_manager.go @@ -5,7 +5,6 @@ "bytes" "context" "crypto/aes" - "crypto/cipher" cryptorand "crypto/rand" "encoding/hex" "fmt" @@ -731,17 +730,14 @@ func (bm *Manager) getBlockInfo(blockID string) (Info, error) { func (bm *Manager) BlockInfo(ctx context.Context, blockID string) (Info, error) { bi, err := bm.getBlockInfo(blockID) if err != nil { + log.Debugf("BlockInfo(%q) - error %v", err) return Info{}, err } - if err == nil { - if bi.Deleted { - log.Debugf("BlockInfo(%q) - deleted", blockID) - } else { - log.Debugf("BlockInfo(%q) - exists in %v", blockID, bi.PackFile) - } + if bi.Deleted { + log.Debugf("BlockInfo(%q) - deleted", blockID) } else { - log.Debugf("BlockInfo(%q) - error %v", err) + log.Debugf("BlockInfo(%q) - exists in %v", blockID, bi.PackFile) } return bi, err @@ -1041,9 +1037,3 @@ func createEncryptor(f FormattingOptions) (Encryptor, error) { return e(f) } - -func curryEncryptionKey(n func(k []byte) (cipher.Block, error), key []byte) func() (cipher.Block, error) { - return func() (cipher.Block, error) { - return n(key) - } -} diff --git a/block/block_manager_test.go b/block/block_manager_test.go index e7dc1d825..ecf71de6d 100644 --- a/block/block_manager_test.go +++ b/block/block_manager_test.go @@ -438,7 +438,7 @@ func TestRewriteNonDeleted(t *testing.T) { block1 := writeBlockAndVerify(ctx, t, bm, seededRandomData(10, 100)) applyStep(action1) - bm.RewriteBlock(ctx, block1) + assertNoError(t, bm.RewriteBlock(ctx, block1)) applyStep(action2) verifyBlock(ctx, t, bm, block1, seededRandomData(10, 100)) dumpBlockManagerData(t, data) @@ -500,9 +500,9 @@ func TestRewriteDeleted(t *testing.T) { block1 := writeBlockAndVerify(ctx, t, bm, seededRandomData(10, 100)) applyStep(action1) - bm.DeleteBlock(block1) + assertNoError(t, bm.DeleteBlock(block1)) applyStep(action2) - bm.RewriteBlock(ctx, block1) + assertNoError(t, bm.RewriteBlock(ctx, block1)) applyStep(action3) verifyBlockNotFound(ctx, t, bm, block1) dumpBlockManagerData(t, data) @@ -537,12 +537,12 @@ func TestDeleteAndRecreate(t *testing.T) { // delete but at given timestamp but don't commit yet. bm0 := newTestBlockManager(data, keyTime, fakeTimeNowWithAutoAdvance(tc.deletionTime, 1*time.Second)) - bm0.DeleteBlock(block1) + assertNoError(t, bm0.DeleteBlock(block1)) // delete it at t0+10 bm1 := newTestBlockManager(data, keyTime, fakeTimeNowWithAutoAdvance(fakeTime.Add(10*time.Second), 1*time.Second)) verifyBlock(ctx, t, bm1, block1, seededRandomData(10, 100)) - bm1.DeleteBlock(block1) + assertNoError(t, bm1.DeleteBlock(block1)) bm1.Flush(ctx) // recreate at t0+20 @@ -591,12 +591,12 @@ func TestFindUnreferencedStorageFiles(t *testing.T) { // block still present in first pack verifyUnreferencedStorageFilesCount(ctx, t, bm, 0) - bm.RewriteBlock(ctx, blockID) + assertNoError(t, bm.RewriteBlock(ctx, blockID)) if err := bm.Flush(ctx); err != nil { t.Errorf("flush error: %v", err) } verifyUnreferencedStorageFilesCount(ctx, t, bm, 1) - bm.RewriteBlock(ctx, blockID) + assertNoError(t, bm.RewriteBlock(ctx, blockID)) if err := bm.Flush(ctx); err != nil { t.Errorf("flush error: %v", err) } @@ -732,7 +732,7 @@ func verifyVersionCompat(t *testing.T, writeVersion int) { cnt := 0 for blockID := range dataSet { t.Logf("deleting %v", blockID) - mgr.DeleteBlock(blockID) + assertNoError(t, mgr.DeleteBlock(blockID)) delete(dataSet, blockID) cnt++ if cnt >= 3 { @@ -883,7 +883,7 @@ func seededRandomData(seed int, length int) []byte { func hashValue(b []byte) string { h := hmac.New(sha256.New, hmacSecret) - h.Write(b) + h.Write(b) //nolint:errcheck return hex.EncodeToString(h.Sum(nil)) } @@ -894,10 +894,10 @@ func dumpBlockManagerData(t *testing.T, data map[string][]byte) { ndx, err := openPackIndex(bytes.NewReader(v)) if err == nil { t.Logf("index %v (%v bytes)", k, len(v)) - ndx.Iterate("", func(i Info) error { + assertNoError(t, ndx.Iterate("", func(i Info) error { t.Logf(" %+v\n", i) return nil - }) + })) } } else { diff --git a/block/merged_test.go b/block/merged_test.go index 0c7127ce3..58da5a2c0 100644 --- a/block/merged_test.go +++ b/block/merged_test.go @@ -45,7 +45,7 @@ func TestMerged(t *testing.T) { } var inOrder []string - m.Iterate("", func(i Info) error { + assertNoError(t, m.Iterate("", func(i Info) error { inOrder = append(inOrder, i.BlockID) if i.BlockID == "de1e1e" { if i.Deleted { @@ -53,7 +53,7 @@ func TestMerged(t *testing.T) { } } return nil - }) + })) if i, err := m.GetInfo("de1e1e"); err != nil { t.Errorf("error getting deleted block info: %v", err) diff --git a/block/packindex_test.go b/block/packindex_test.go index 88fd5e3fe..3a4d5c658 100644 --- a/block/packindex_test.go +++ b/block/packindex_test.go @@ -139,14 +139,14 @@ func TestPackIndex(t *testing.T) { } cnt := 0 - ndx.Iterate("", func(info2 Info) error { + assertNoError(t, ndx.Iterate("", func(info2 Info) error { info := infoMap[info2.BlockID] if !reflect.DeepEqual(info, info2) { t.Errorf("invalid value retrieved: %+v, wanted %+v", info2, info) } cnt++ return nil - }) + })) if cnt != len(infoMap) { t.Errorf("invalid number of iterations: %v, wanted %v", cnt, len(infoMap)) } @@ -166,13 +166,13 @@ func TestPackIndex(t *testing.T) { for _, prefix := range prefixes { cnt2 := 0 - ndx.Iterate(string(prefix), func(info2 Info) error { + assertNoError(t, ndx.Iterate(string(prefix), func(info2 Info) error { cnt2++ if !strings.HasPrefix(string(info2.BlockID), string(prefix)) { t.Errorf("unexpected item %v when iterating prefix %v", info2.BlockID, prefix) } return nil - }) + })) t.Logf("found %v elements with prefix %q", cnt2, prefix) } } @@ -188,13 +188,14 @@ func fuzzTestIndexOpen(t *testing.T, originalData []byte) { } defer ndx.Close() cnt := 0 - ndx.Iterate("", func(cb Info) error { + assertNoError(t, ndx.Iterate("", func(cb Info) error { if cnt < 10 { - ndx.GetInfo(cb.BlockID) + _, err := ndx.GetInfo(cb.BlockID) + assertNoError(t, err) } cnt++ return nil - }) + })) }) } diff --git a/connect.go b/connect.go index 16859e181..002319ade 100644 --- a/connect.go +++ b/connect.go @@ -73,8 +73,8 @@ func setupCaching(configPath string, lc *LocalConfig, opt block.CachingOptions, } h := sha256.New() - h.Write(uniqueID) - h.Write([]byte(configPath)) + h.Write(uniqueID) //nolint:errcheck + h.Write([]byte(configPath)) //nolint:errcheck lc.Caching.CacheDirectory = filepath.Join(cacheDir, "kopia", hex.EncodeToString(h.Sum(nil))[0:16]) } else { absCacheDir, err := filepath.Abs(opt.CacheDirectory) diff --git a/format_block_test.go b/format_block_test.go index e6b09711d..e1bd302aa 100644 --- a/format_block_test.go +++ b/format_block_test.go @@ -24,19 +24,19 @@ func TestFormatBlockRecovery(t *testing.T) { t.Errorf("unexpected checksummed length: %v, want %v", got, want) } - st.PutBlock(ctx, "some-block-by-itself", checksummed) - st.PutBlock(ctx, "some-block-suffix", append(append([]byte(nil), 1, 2, 3), checksummed...)) - st.PutBlock(ctx, "some-block-prefix", append(append([]byte(nil), checksummed...), 1, 2, 3)) + assertNoError(t, st.PutBlock(ctx, "some-block-by-itself", checksummed)) + assertNoError(t, st.PutBlock(ctx, "some-block-suffix", append(append([]byte(nil), 1, 2, 3), checksummed...))) + assertNoError(t, st.PutBlock(ctx, "some-block-prefix", append(append([]byte(nil), checksummed...), 1, 2, 3))) // mess up checksum checksummed[len(checksummed)-3] ^= 1 - st.PutBlock(ctx, "bad-checksum", checksummed) - st.PutBlock(ctx, "zero-len", []byte{}) - st.PutBlock(ctx, "one-len", []byte{1}) - st.PutBlock(ctx, "two-len", []byte{1, 2}) - st.PutBlock(ctx, "three-len", []byte{1, 2, 3}) - st.PutBlock(ctx, "four-len", []byte{1, 2, 3, 4}) - st.PutBlock(ctx, "five-len", []byte{1, 2, 3, 4, 5}) + assertNoError(t, st.PutBlock(ctx, "bad-checksum", checksummed)) + assertNoError(t, st.PutBlock(ctx, "zero-len", []byte{})) + assertNoError(t, st.PutBlock(ctx, "one-len", []byte{1})) + assertNoError(t, st.PutBlock(ctx, "two-len", []byte{1, 2})) + assertNoError(t, st.PutBlock(ctx, "three-len", []byte{1, 2, 3})) + assertNoError(t, st.PutBlock(ctx, "four-len", []byte{1, 2, 3, 4})) + assertNoError(t, st.PutBlock(ctx, "five-len", []byte{1, 2, 3, 4, 5})) cases := []struct { block string @@ -70,3 +70,10 @@ func TestFormatBlockRecovery(t *testing.T) { }) } } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +} diff --git a/internal/retry/retry_test.go b/internal/retry/retry_test.go index ae7af310a..18b2e7e5d 100644 --- a/internal/retry/retry_test.go +++ b/internal/retry/retry_test.go @@ -9,8 +9,7 @@ ) var ( - errRetriable = errors.New("retriable") - errNonRetriable = errors.New("non-retriable") + errRetriable = errors.New("retriable") ) func isRetriable(e error) bool { diff --git a/manifest/manifest_manager_test.go b/manifest/manifest_manager_test.go index 685ec0c85..c261d8795 100644 --- a/manifest/manifest_manager_test.go +++ b/manifest/manifest_manager_test.go @@ -145,7 +145,7 @@ func TestManifestInitCorruptedBlock(t *testing.T) { t.Fatalf("err: %v", err) } - mgr.Put(ctx, map[string]string{"type": "foo"}, map[string]string{"some": "value"}) + mgr.Put(ctx, map[string]string{"type": "foo"}, map[string]string{"some": "value"}) //nolint:errcheck mgr.Flush(ctx) bm.Flush(ctx) diff --git a/object/indirect.go b/object/indirect.go index 2f217e536..7fdadfd4e 100644 --- a/object/indirect.go +++ b/object/indirect.go @@ -1,7 +1,5 @@ package object -var indirectStreamType = "kopia:indirect" - // indirectObjectEntry represents an entry in indirect object stream. type indirectObjectEntry struct { Start int64 `json:"s,omitempty"` diff --git a/object/object_manager_test.go b/object/object_manager_test.go index 1bc65fa86..4fb177811 100644 --- a/object/object_manager_test.go +++ b/object/object_manager_test.go @@ -37,7 +37,7 @@ func (f *fakeBlockManager) GetBlock(ctx context.Context, blockID string) ([]byte func (f *fakeBlockManager) WriteBlock(ctx context.Context, data []byte, prefix string) (string, error) { h := sha256.New() - h.Write(data) + h.Write(data) //nolint:errcheck blockID := prefix + string(hex.EncodeToString(h.Sum(nil))) f.mu.Lock() @@ -96,7 +96,9 @@ func TestWriters(t *testing.T) { writer := om.NewWriter(ctx, WriterOptions{}) - writer.Write(c.data) + if _, err := writer.Write(c.data); err != nil { + t.Errorf("write error: %v", err) + } result, err := writer.Result() if err != nil { @@ -131,8 +133,8 @@ func TestWriterCompleteChunkInTwoWrites(t *testing.T) { bytes := make([]byte, 100) writer := om.NewWriter(ctx, WriterOptions{}) - writer.Write(bytes[0:50]) - writer.Write(bytes[0:50]) + writer.Write(bytes[0:50]) //nolint:errcheck + writer.Write(bytes[0:50]) //nolint:errcheck result, err := writer.Result() if !objectIDsEqual(result, "cd00e292c5970d3c5e2f0ffa5171e555bc46bfc4faddfb4a418b6840b86e79a3") { t.Errorf("unexpected result: %v err: %v", result, err) @@ -176,7 +178,9 @@ func TestIndirection(t *testing.T) { contentBytes := make([]byte, c.dataLength) writer := om.NewWriter(ctx, WriterOptions{}) - writer.Write(contentBytes) + if _, err := writer.Write(contentBytes); err != nil { + t.Errorf("write error: %v", err) + } result, err := writer.Result() if err != nil { t.Errorf("error getting writer results: %v", err) @@ -223,7 +227,7 @@ func TestHMAC(t *testing.T) { _, om := setupTest(t) w := om.NewWriter(ctx, WriterOptions{}) - w.Write(content) + w.Write(content) //nolint:errcheck result, err := w.Result() if result.String() != "cad29ff89951a3c085c86cb7ed22b82b51f7bdfda24f932c7f9601f51d5975ba" { t.Errorf("unexpected result: %v err: %v", result.String(), err) @@ -290,10 +294,12 @@ func TestEndToEndReadAndSeek(t *testing.T) { for _, size := range []int{1, 199, 200, 201, 9999, 512434} { // Create some random data sample of the specified size. randomData := make([]byte, size) - cryptorand.Read(randomData) + cryptorand.Read(randomData) //nolint:errcheck writer := om.NewWriter(ctx, WriterOptions{}) - writer.Write(randomData) + if _, err := writer.Write(randomData); err != nil { + t.Errorf("write error: %v", err) + } objectID, err := writer.Result() writer.Close() if err != nil { diff --git a/object/object_writer.go b/object/object_writer.go index a0e7e8f5b..cf79754f8 100644 --- a/object/object_writer.go +++ b/object/object_writer.go @@ -56,8 +56,7 @@ type objectWriter struct { description string - splitter objectSplitter - pendingBlocksWG sync.WaitGroup + splitter objectSplitter } func (w *objectWriter) Close() error { diff --git a/object/objectid_test.go b/object/objectid_test.go index 1aa29484b..022bebf4a 100644 --- a/object/objectid_test.go +++ b/object/objectid_test.go @@ -4,8 +4,6 @@ "testing" ) -type rawObjectID ID - func TestParseObjectID(t *testing.T) { cases := []struct { text string diff --git a/repository_test.go b/repository_test.go index 33e957c40..822ffe6e0 100644 --- a/repository_test.go +++ b/repository_test.go @@ -37,7 +37,9 @@ func TestWriters(t *testing.T) { defer env.Setup(t).Close(t) writer := env.Repository.Objects.NewWriter(ctx, object.WriterOptions{}) - writer.Write(c.data) + if _, err := writer.Write(c.data); err != nil { + t.Fatalf("write error: %v", err) + } result, err := writer.Result() if err != nil { @@ -64,8 +66,8 @@ func TestWriterCompleteChunkInTwoWrites(t *testing.T) { bytes := make([]byte, 100) writer := env.Repository.Objects.NewWriter(ctx, object.WriterOptions{}) - writer.Write(bytes[0:50]) - writer.Write(bytes[0:50]) + writer.Write(bytes[0:50]) //nolint:errcheck + writer.Write(bytes[0:50]) //nolint:errcheck result, err := writer.Result() if result != "1d804f1f69df08f3f59070bf962de69433e3d61ac18522a805a84d8c92741340" { t.Errorf("unexpected result: %v err: %v", result, err) @@ -149,7 +151,7 @@ func TestHMAC(t *testing.T) { content := bytes.Repeat([]byte{0xcd}, 50) w := env.Repository.Objects.NewWriter(ctx, object.WriterOptions{}) - w.Write(content) + w.Write(content) //nolint:errcheck result, err := w.Result() if result.String() != "367352007ee6ca9fa755ce8352347d092c17a24077fd33c62f655574a8cf906d" { t.Errorf("unexpected result: %v err: %v", result.String(), err) @@ -193,10 +195,10 @@ func TestEndToEndReadAndSeek(t *testing.T) { for _, size := range []int{1, 199, 200, 201, 9999, 512434} { // Create some random data sample of the specified size. randomData := make([]byte, size) - cryptorand.Read(randomData) + cryptorand.Read(randomData) //nolint:errcheck writer := env.Repository.Objects.NewWriter(ctx, object.WriterOptions{}) - writer.Write(randomData) + writer.Write(randomData) //nolint:errcheck objectID, err := writer.Result() writer.Close() if err != nil { @@ -300,7 +302,7 @@ func TestFormats(t *testing.T) { for k, v := range c.oids { bytesToWrite := []byte(k) w := env.Repository.Objects.NewWriter(ctx, object.WriterOptions{}) - w.Write(bytesToWrite) + w.Write(bytesToWrite) //nolint:errcheck oid, err := w.Result() if err != nil { t.Errorf("error: %v", err) diff --git a/storage/filesystem/filesystem_storage_test.go b/storage/filesystem/filesystem_storage_test.go index e0a9e7765..1812f51ef 100644 --- a/storage/filesystem/filesystem_storage_test.go +++ b/storage/filesystem/filesystem_storage_test.go @@ -68,26 +68,26 @@ func TestFileStorageTouch(t *testing.T) { } fs := r.(*fsStorage) - fs.PutBlock(ctx, t1, []byte{1}) + assertNoError(t, fs.PutBlock(ctx, t1, []byte{1})) time.Sleep(1 * time.Second) // sleep a bit to accommodate Apple filesystems with low timestamp resolution - fs.PutBlock(ctx, t2, []byte{1}) + assertNoError(t, fs.PutBlock(ctx, t2, []byte{1})) time.Sleep(1 * time.Second) - fs.PutBlock(ctx, t3, []byte{1}) + assertNoError(t, fs.PutBlock(ctx, t3, []byte{1})) verifyBlockTimestampOrder(t, fs, t1, t2, t3) - fs.TouchBlock(ctx, t2, 1*time.Hour) // has no effect, all timestamps are very new + assertNoError(t, fs.TouchBlock(ctx, t2, 1*time.Hour)) // has no effect, all timestamps are very new verifyBlockTimestampOrder(t, fs, t1, t2, t3) - fs.TouchBlock(ctx, t1, 0) // moves t1 to the top of the pile + assertNoError(t, fs.TouchBlock(ctx, t1, 0)) // moves t1 to the top of the pile verifyBlockTimestampOrder(t, fs, t2, t3, t1) time.Sleep(1 * time.Second) - fs.TouchBlock(ctx, t2, 0) // moves t2 to the top of the pile + assertNoError(t, fs.TouchBlock(ctx, t2, 0)) // moves t2 to the top of the pile verifyBlockTimestampOrder(t, fs, t3, t1, t2) time.Sleep(1 * time.Second) - fs.TouchBlock(ctx, t1, 0) // moves t1 to the top of the pile + assertNoError(t, fs.TouchBlock(ctx, t1, 0)) // moves t1 to the top of the pile verifyBlockTimestampOrder(t, fs, t3, t2, t1) } @@ -111,3 +111,10 @@ func verifyBlockTimestampOrder(t *testing.T, st storage.Storage, want ...string) t.Errorf("incorrect block order: %v, wanted %v", blocks, want) } } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +} diff --git a/storage/s3/s3_storage_test.go b/storage/s3/s3_storage_test.go index 8658c780d..1c2c794ef 100644 --- a/storage/s3/s3_storage_test.go +++ b/storage/s3/s3_storage_test.go @@ -61,7 +61,7 @@ func TestS3Storage(t *testing.T) { cleanupOldData(ctx, t) data := make([]byte, 8) - rand.Read(data) + rand.Read(data) //nolint:errcheck st, err := New(context.Background(), &Options{ AccessKeyID: accessKeyID, @@ -86,7 +86,8 @@ func createBucket(t *testing.T) { if err != nil { t.Fatalf("can't initialize minio client: %v", err) } - minioClient.MakeBucket(bucketName, "us-east-1") + // ignore error + _ = minioClient.MakeBucket(bucketName, "us-east-1") } func cleanupOldData(ctx context.Context, t *testing.T) { @@ -101,7 +102,7 @@ func cleanupOldData(ctx context.Context, t *testing.T) { t.Fatalf("err: %v", err) } - st.ListBlocks(ctx, "", func(it storage.BlockMetadata) error { + _ = st.ListBlocks(ctx, "", func(it storage.BlockMetadata) error { age := time.Since(it.Timestamp) if age > cleanupAge { if err := st.DeleteBlock(ctx, it.BlockID); err != nil { diff --git a/storage/storage_test.go b/storage/storage_test.go index a94639694..8f41e4796 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -13,9 +13,9 @@ func TestListAllBlocksConsistent(t *testing.T) { ctx := context.Background() data := map[string][]byte{} st := storagetesting.NewMapStorage(data, nil, time.Now) - st.PutBlock(ctx, "foo1", []byte{1, 2, 3}) - st.PutBlock(ctx, "foo2", []byte{1, 2, 3}) - st.PutBlock(ctx, "foo3", []byte{1, 2, 3}) + st.PutBlock(ctx, "foo1", []byte{1, 2, 3}) //nolint:errcheck + st.PutBlock(ctx, "foo2", []byte{1, 2, 3}) //nolint:errcheck + st.PutBlock(ctx, "foo3", []byte{1, 2, 3}) //nolint:errcheck // set up faulty storage that will add a block while a scan is in progress. f := &storagetesting.FaultyStorage{ @@ -23,7 +23,7 @@ func TestListAllBlocksConsistent(t *testing.T) { Faults: map[string][]*storagetesting.Fault{ "ListBlocksItem": { {ErrCallback: func() error { - st.PutBlock(ctx, "foo0", []byte{1, 2, 3}) + st.PutBlock(ctx, "foo0", []byte{1, 2, 3}) //nolint:errcheck return nil }}, }, diff --git a/storage/webdav/webdav_storage_test.go b/storage/webdav/webdav_storage_test.go index 1315d8bfa..ddfb24a22 100644 --- a/storage/webdav/webdav_storage_test.go +++ b/storage/webdav/webdav_storage_test.go @@ -44,7 +44,7 @@ func TestWebDAVStorage(t *testing.T) { if err := os.RemoveAll(tmpDir); err != nil { t.Errorf("can't remove all: %q", tmpDir) } - os.MkdirAll(tmpDir, 0700) + os.MkdirAll(tmpDir, 0700) //nolint:errcheck r, err := New(context.Background(), &Options{ URL: server.URL, diff --git a/tests/repository_stress_test/repository_stress_test.go b/tests/repository_stress_test/repository_stress_test.go index 91c73d88d..785686ae8 100644 --- a/tests/repository_stress_test/repository_stress_test.go +++ b/tests/repository_stress_test/repository_stress_test.go @@ -22,10 +22,6 @@ const masterPassword = "foo-bar-baz-1234" -type testContext struct { - r *repo.Repository -} - var ( knownBlocks []string knownBlocksMutex sync.Mutex @@ -54,7 +50,7 @@ func TestStressRepository(t *testing.T) { configFile1 := filepath.Join(tmpPath, "kopia1.config") configFile2 := filepath.Join(tmpPath, "kopia2.config") - os.MkdirAll(storagePath, 0700) + assertNoError(t, os.MkdirAll(storagePath, 0700)) st, err := filesystem.New(ctx, &filesystem.Options{ Path: storagePath, }) @@ -314,3 +310,10 @@ func writeRandomManifest(ctx context.Context, t *testing.T, r *repo.Repository) }) return err } + +func assertNoError(t *testing.T, err error) { + t.Helper() + if err != nil { + t.Errorf("err: %v", err) + } +}