mirror of
https://github.com/distribution/distribution.git
synced 2025-09-12 13:23:32 +00:00
feat(linter): enable errcheck linter in golangci-lint
Also, bump the linter version to the latest available version. Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
This commit is contained in:
@@ -319,7 +319,9 @@ func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) e
|
||||
copyStatus := *resp.CopyStatus
|
||||
|
||||
if d.copyStatusPollMaxRetry == -1 && copyStatus == blob.CopyStatusTypePending {
|
||||
destBlobRef.AbortCopyFromURL(ctx, *resp.CopyID, nil)
|
||||
if _, err := destBlobRef.AbortCopyFromURL(ctx, *resp.CopyID, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -331,7 +333,9 @@ func (d *driver) Move(ctx context.Context, sourcePath string, destPath string) e
|
||||
}
|
||||
|
||||
if retryCount >= d.copyStatusPollMaxRetry {
|
||||
destBlobRef.AbortCopyFromURL(ctx, *props.CopyID, nil)
|
||||
if _, err := destBlobRef.AbortCopyFromURL(ctx, *props.CopyID, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("max retries for copy polling reached, aborting copy")
|
||||
}
|
||||
|
||||
|
@@ -107,7 +107,9 @@ func TestCommitAfterMove(t *testing.T) {
|
||||
destPath := "/dest/file"
|
||||
ctx := context.Background()
|
||||
|
||||
// nolint:errcheck
|
||||
defer driver.Delete(ctx, sourcePath)
|
||||
// nolint:errcheck
|
||||
defer driver.Delete(ctx, destPath)
|
||||
|
||||
writer, err := driver.Writer(ctx, sourcePath, false)
|
||||
|
@@ -4,6 +4,7 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -140,7 +141,9 @@ func (d *driver) PutContent(ctx context.Context, subPath string, contents []byte
|
||||
defer writer.Close()
|
||||
_, err = io.Copy(writer, bytes.NewReader(contents))
|
||||
if err != nil {
|
||||
writer.Cancel(ctx)
|
||||
if cErr := writer.Cancel(ctx); cErr != nil {
|
||||
return errors.Join(err, cErr)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return writer.Commit(ctx)
|
||||
|
@@ -119,6 +119,7 @@ func TestCommitEmpty(t *testing.T) {
|
||||
ctx := dcontext.Background()
|
||||
|
||||
writer, err := driver.Writer(ctx, filename, false)
|
||||
// nolint:errcheck
|
||||
defer driver.Delete(ctx, filename)
|
||||
if err != nil {
|
||||
t.Fatalf("driver.Writer: unexpected error: %v", err)
|
||||
@@ -162,6 +163,7 @@ func TestCommit(t *testing.T) {
|
||||
|
||||
contents := make([]byte, defaultChunkSize)
|
||||
writer, err := driver.Writer(ctx, filename, false)
|
||||
// nolint:errcheck
|
||||
defer driver.Delete(ctx, filename)
|
||||
if err != nil {
|
||||
t.Fatalf("driver.Writer: unexpected error: %v", err)
|
||||
|
@@ -97,7 +97,9 @@ func (d *driver) PutContent(ctx context.Context, p string, contents []byte) erro
|
||||
}
|
||||
|
||||
f.truncate()
|
||||
f.WriteAt(contents, 0)
|
||||
if _, err := f.WriteAt(contents, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -301,7 +303,10 @@ func (w *writer) Close() error {
|
||||
return fmt.Errorf("already closed")
|
||||
}
|
||||
w.closed = true
|
||||
w.flush()
|
||||
|
||||
if err := w.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -329,16 +334,23 @@ func (w *writer) Commit(ctx context.Context) error {
|
||||
return fmt.Errorf("already cancelled")
|
||||
}
|
||||
w.committed = true
|
||||
w.flush()
|
||||
|
||||
if err := w.flush(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *writer) flush() {
|
||||
func (w *writer) flush() error {
|
||||
w.d.mutex.Lock()
|
||||
defer w.d.mutex.Unlock()
|
||||
|
||||
w.f.WriteAt(w.buffer, int64(len(w.f.data)))
|
||||
if _, err := w.f.WriteAt(w.buffer, int64(len(w.f.data))); err != nil {
|
||||
return err
|
||||
}
|
||||
w.buffer = []byte{}
|
||||
w.buffSize = 0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/distribution/distribution/v3/internal/dcontext"
|
||||
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
||||
storagemiddleware "github.com/distribution/distribution/v3/registry/storage/driver/middleware"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// cloudFrontStorageMiddleware provides a simple implementation of layerHandler that
|
||||
@@ -226,5 +227,7 @@ func (lh *cloudFrontStorageMiddleware) RedirectURL(r *http.Request, path string)
|
||||
|
||||
// init registers the cloudfront layerHandler backend.
|
||||
func init() {
|
||||
storagemiddleware.Register("cloudfront", newCloudFrontStorageMiddleware)
|
||||
if err := storagemiddleware.Register("cloudfront", newCloudFrontStorageMiddleware); err != nil {
|
||||
logrus.Errorf("failed to register cloudfront middleware: %v", err)
|
||||
}
|
||||
}
|
||||
|
@@ -45,7 +45,9 @@ pZeMRablbPQdp8/1NyIwimq1VlG0ohQ4P6qhW7E09ZMC
|
||||
if err != nil {
|
||||
t.Fatal("File cannot be created")
|
||||
}
|
||||
file.WriteString(privk)
|
||||
if _, err := file.WriteString(privk); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
options["privatekey"] = file.Name()
|
||||
options["keypairid"] = "test"
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect" // used as a replacement for testify
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -31,7 +32,10 @@ func (m mockIPRangeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
if _, err := w.Write(bytes); err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func newTestHandler(data awsIPResponse) *httptest.Server {
|
||||
@@ -77,7 +81,8 @@ func TestMatchIPV6(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, nil)
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("ff00::")))
|
||||
assertEqual(t, 1, len(ips.ipv6))
|
||||
assertEqual(t, 0, len(ips.ipv4))
|
||||
@@ -94,7 +99,8 @@ func TestMatchIPV4(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, nil)
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.0")))
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.1")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.169.0.0")))
|
||||
@@ -114,7 +120,8 @@ func TestMatchIPV4_2(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, nil)
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.0")))
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.1")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.169.0.0")))
|
||||
@@ -134,7 +141,8 @@ func TestMatchIPV4WithRegionMatched(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, []string{"us-east-1"})
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.0")))
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.1")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.169.0.0")))
|
||||
@@ -154,7 +162,8 @@ func TestMatchIPV4WithRegionMatch_2(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, []string{"us-west-2", "us-east-1"})
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.0")))
|
||||
assertEqual(t, true, ips.contains(net.ParseIP("192.168.0.1")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.169.0.0")))
|
||||
@@ -174,7 +183,8 @@ func TestMatchIPV4WithRegionNotMatched(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, []string{"us-west-2"})
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.168.0.0")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.168.0.1")))
|
||||
assertEqual(t, false, ips.contains(net.ParseIP("192.169.0.0")))
|
||||
@@ -193,7 +203,8 @@ func TestInvalidData(t *testing.T) {
|
||||
|
||||
ctx := context.Background()
|
||||
ips, _ := newAWSIPs(ctx, serverIPRanges(server), time.Hour, nil)
|
||||
ips.tryUpdate(ctx)
|
||||
err := ips.tryUpdate(ctx)
|
||||
assertEqual(t, err, nil)
|
||||
assertEqual(t, 1, len(ips.ipv4))
|
||||
}
|
||||
|
||||
@@ -227,7 +238,12 @@ func TestParsing(t *testing.T) {
|
||||
"region": "anotherregion",
|
||||
"service": "ec2"}]
|
||||
}`
|
||||
rawMockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(data)) })
|
||||
rawMockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, err := w.Write([]byte(data)); err != nil {
|
||||
w.WriteHeader(500)
|
||||
return
|
||||
}
|
||||
})
|
||||
t.Parallel()
|
||||
server := httptest.NewServer(rawMockHandler)
|
||||
defer server.Close()
|
||||
@@ -251,15 +267,25 @@ func TestParsing(t *testing.T) {
|
||||
func TestUpdateCalledRegularly(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mu := &sync.RWMutex{}
|
||||
updateCount := 0
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(
|
||||
func(rw http.ResponseWriter, req *http.Request) {
|
||||
mu.Lock()
|
||||
updateCount++
|
||||
rw.Write([]byte("ok"))
|
||||
mu.Unlock()
|
||||
if _, err := rw.Write([]byte("ok")); err != nil {
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
newAWSIPs(context.Background(), fmt.Sprintf("%s/", server.URL), time.Second, nil)
|
||||
if _, err := newAWSIPs(context.Background(), fmt.Sprintf("%s/", server.URL), time.Second, nil); err != nil {
|
||||
t.Fatalf("failed creating AWS IP filter: %v", err)
|
||||
}
|
||||
time.Sleep(time.Second*4 + time.Millisecond*500)
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
if updateCount < 4 {
|
||||
t.Errorf("Update should have been called at least 4 times, actual=%d", updateCount)
|
||||
}
|
||||
@@ -364,8 +390,14 @@ func BenchmarkContainsRandom(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
ipv4[i] = make([]byte, 4)
|
||||
ipv6[i] = make([]byte, 16)
|
||||
rand.Read(ipv4[i])
|
||||
rand.Read(ipv6[i])
|
||||
_, err := rand.Read(ipv4[i])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
_, err = rand.Read(ipv6[i])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
@@ -381,8 +413,14 @@ func BenchmarkContainsProd(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
ipv4[i] = make([]byte, 4)
|
||||
ipv6[i] = make([]byte, 16)
|
||||
rand.Read(ipv4[i])
|
||||
rand.Read(ipv6[i])
|
||||
_, err := rand.Read(ipv4[i])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
_, err = rand.Read(ipv6[i])
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
|
||||
storagemiddleware "github.com/distribution/distribution/v3/registry/storage/driver/middleware"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type redirectStorageMiddleware struct {
|
||||
@@ -52,5 +53,7 @@ func (r *redirectStorageMiddleware) RedirectURL(_ *http.Request, urlPath string)
|
||||
}
|
||||
|
||||
func init() {
|
||||
storagemiddleware.Register("redirect", newRedirectStorageMiddleware)
|
||||
if err := storagemiddleware.Register("redirect", newRedirectStorageMiddleware); err != nil {
|
||||
logrus.Errorf("tailed to register redirect storage middleware: %v", err)
|
||||
}
|
||||
}
|
||||
|
@@ -186,6 +186,7 @@ func TestEmptyRootList(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating content: %v", err)
|
||||
}
|
||||
// nolint:errcheck
|
||||
defer rootedDriver.Delete(ctx, filename)
|
||||
|
||||
keys, _ := emptyRootDriver.List(ctx, "/")
|
||||
@@ -230,6 +231,7 @@ func TestStorageClass(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating content with storage class %v: %v", storageClass, err)
|
||||
}
|
||||
// nolint:errcheck
|
||||
defer s3Driver.Delete(ctx, filename)
|
||||
|
||||
driverUnwrapped := s3Driver.Base.StorageDriver.(*driver)
|
||||
@@ -751,17 +753,21 @@ func TestMoveWithMultipartCopy(t *testing.T) {
|
||||
sourcePath := "/source"
|
||||
destPath := "/dest"
|
||||
|
||||
// nolint:errcheck
|
||||
defer d.Delete(ctx, sourcePath)
|
||||
// nolint:errcheck
|
||||
defer d.Delete(ctx, destPath)
|
||||
|
||||
// An object larger than d's MultipartCopyThresholdSize will cause d.Move() to perform a multipart copy.
|
||||
multipartCopyThresholdSize := d.baseEmbed.Base.StorageDriver.(*driver).MultipartCopyThresholdSize
|
||||
contents := make([]byte, 2*multipartCopyThresholdSize)
|
||||
rand.Read(contents)
|
||||
if _, err := rand.Read(contents); err != nil {
|
||||
t.Fatalf("unexpected error creating content: %v", err)
|
||||
}
|
||||
|
||||
err = d.PutContent(ctx, sourcePath, contents)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error creating content: %v", err)
|
||||
t.Fatalf("unexpected error writing content: %v", err)
|
||||
}
|
||||
|
||||
err = d.Move(ctx, sourcePath, destPath)
|
||||
|
@@ -105,6 +105,9 @@ func Sign(req *request.Request) {
|
||||
Time: req.Time,
|
||||
Credentials: req.Config.Credentials,
|
||||
}
|
||||
// TODO(milosgajdos): figure this out; if Sign returns error which we should check,
|
||||
// we should modify the codepath related to svc.Handlers.Sign.PushBack etc.
|
||||
// nolint:errcheck
|
||||
v2.Sign()
|
||||
}
|
||||
|
||||
|
@@ -302,7 +302,9 @@ func (suite *DriverSuite) TestWriteReadLargeStreams(c *check.C) {
|
||||
defer reader.Close()
|
||||
|
||||
writtenChecksum := sha256.New()
|
||||
io.Copy(writtenChecksum, reader)
|
||||
if _, err := io.Copy(writtenChecksum, reader); err != nil {
|
||||
c.Assert(err, check.IsNil)
|
||||
}
|
||||
|
||||
c.Assert(writtenChecksum.Sum(nil), check.DeepEquals, checksum.Sum(nil))
|
||||
}
|
||||
@@ -1108,6 +1110,7 @@ func (suite *DriverSuite) benchmarkPutGetFiles(c *check.C, size int64) {
|
||||
parentDir := randomPath(8)
|
||||
defer func() {
|
||||
c.StopTimer()
|
||||
// nolint:errcheck
|
||||
suite.StorageDriver.Delete(suite.ctx, firstPart(parentDir))
|
||||
}()
|
||||
|
||||
@@ -1146,6 +1149,7 @@ func (suite *DriverSuite) benchmarkStreamFiles(c *check.C, size int64) {
|
||||
parentDir := randomPath(8)
|
||||
defer func() {
|
||||
c.StopTimer()
|
||||
// nolint:errcheck
|
||||
suite.StorageDriver.Delete(suite.ctx, firstPart(parentDir))
|
||||
}()
|
||||
|
||||
@@ -1182,6 +1186,7 @@ func (suite *DriverSuite) benchmarkListFiles(c *check.C, numFiles int64) {
|
||||
parentDir := randomPath(8)
|
||||
defer func() {
|
||||
c.StopTimer()
|
||||
// nolint:errcheck
|
||||
suite.StorageDriver.Delete(suite.ctx, firstPart(parentDir))
|
||||
}()
|
||||
|
||||
@@ -1240,8 +1245,10 @@ func (suite *DriverSuite) testFileStreams(c *check.C, size int64) {
|
||||
_, err = tf.Write(contents)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
tf.Sync()
|
||||
tf.Seek(0, io.SeekStart)
|
||||
err = tf.Sync()
|
||||
c.Assert(err, check.IsNil)
|
||||
_, err = tf.Seek(0, io.SeekStart)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
||||
writer, err := suite.StorageDriver.Writer(suite.ctx, filename, false)
|
||||
c.Assert(err, check.IsNil)
|
||||
|
Reference in New Issue
Block a user