Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions build-index/tagtype/docker_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tagtype

import (
"context"
"fmt"
"io"
"testing"
"time"
Expand All @@ -27,6 +28,7 @@ import (
mockblobclient "github.com/uber/kraken/mocks/origin/blobclient"
"github.com/uber/kraken/origin/blobclient"
"github.com/uber/kraken/utils/dockerutil"
"github.com/uber/kraken/utils/errutil"
"github.com/uber/kraken/utils/httputil"
"github.com/uber/kraken/utils/mockutil"
)
Expand Down Expand Up @@ -302,3 +304,25 @@ func TestDockerResolver_Resolve_WithRetries(t *testing.T) {
require.NotNil(deps)
require.Equal(core.DigestList(append(layers, manifest)), deps)
}

func TestDockerResolver_DownloadManifest_RetryOnWrappedNetworkError(t *testing.T) {
require, _, resolver, mockOrigin := setupDockerResolverTest(t)

tag := "repo/image:v1.0"
manifest := core.DigestFixture()

// The origin cluster client wraps the per-origin errors, so a network
// error reaches the resolver inside "all origins unavailable: ...".
networkErr := fmt.Errorf("all origins unavailable: %w",
errutil.Join([]error{fmt.Errorf("origin origin-1: %w", httputil.NetworkError{})}))

mockOrigin.EXPECT().
DownloadBlob(gomock.Any(), tag, manifest, gomock.Any()).
Return(networkErr).
Times(testMaxRetries + 1)

result, err := resolver.downloadManifest(tag, manifest)
require.Error(err)
require.Nil(result)
require.True(httputil.IsNetworkError(err))
}
4 changes: 2 additions & 2 deletions origin/blobclient/cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ ORIGINS:
return err
}
}
errs = append(errs, fmt.Errorf("origin %s: %s", client.Addr(), err))
errs = append(errs, fmt.Errorf("origin %s: %w", client.Addr(), err))
continue ORIGINS
}
return nil // Success!
}
errs = append(errs,
fmt.Errorf("origin %s: backoff timed out on 202 responses", client.Addr()))
}
return fmt.Errorf("all origins unavailable: %s", errutil.Join(errs))
return fmt.Errorf("all origins unavailable: %w", errutil.Join(errs))
}
34 changes: 34 additions & 0 deletions origin/blobclient/cluster_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/uber/kraken/origin/blobclient"
"github.com/uber/kraken/utils/httputil"
"github.com/uber/kraken/utils/stringset"

"github.com/cenkalti/backoff"
)

// =============================================================================
Expand Down Expand Up @@ -1107,3 +1109,35 @@ func TestPoll(t *testing.T) {
})
}
}

func TestPollWrappedErrorExposesOriginErrors(t *testing.T) {
require := require.New(t)

ctrl := gomock.NewController(t)
defer ctrl.Finish()

blob := core.NewBlobFixture()
namespace := core.TagFixture()

mockResolver := mockblobclient.NewMockClientResolver(ctrl)

mockClient1 := mockblobclient.NewMockClient(ctrl)
mockClient2 := mockblobclient.NewMockClient(ctrl)

mockResolver.EXPECT().Resolve(blob.Digest).Return([]blobclient.Client{mockClient1, mockClient2}, nil)
mockClient1.EXPECT().DownloadBlob(gomock.Any(), namespace, blob.Digest, nil).Return(httputil.NetworkError{})
mockClient1.EXPECT().Addr().Return("client1")
mockClient2.EXPECT().DownloadBlob(gomock.Any(), namespace, blob.Digest, nil).Return(httputil.NetworkError{})
mockClient2.EXPECT().Addr().Return("client2")

b := backoff.WithMaxRetries(backoff.NewConstantBackOff(100*time.Millisecond), 5)

err := blobclient.Poll(mockResolver, b, blob.Digest, func(c blobclient.Client) error {
return c.DownloadBlob(context.Background(), namespace, blob.Digest, nil)
})
require.Error(err)

// Callers have to be able to classify the failure even though Poll adds
// the origin address to each error.
require.True(httputil.IsNetworkError(err))
}
6 changes: 6 additions & 0 deletions utils/errutil/errutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func (e MultiError) Error() string {
return b.String()
}

// Unwrap returns the underlying errors, so that errors.Is and errors.As can
// inspect each of them.
func (e MultiError) Unwrap() []error {
return e
}

// Join converts errs into an error interface.
func Join(errs []error) error {
if errs == nil {
Expand Down
11 changes: 11 additions & 0 deletions utils/errutil/errutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package errutil

import (
"errors"
"fmt"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -57,3 +58,13 @@ func TestJoinNonNil(t *testing.T) {
}
require.Error(t, f())
}

func TestMultiErrorUnwrap(t *testing.T) {
sentinel := errors.New("sentinel")
err := fmt.Errorf("outer: %w", Join([]error{
errors.New("other"),
fmt.Errorf("origin: %w", sentinel),
}))

require.ErrorIs(t, err, sentinel)
}
6 changes: 3 additions & 3 deletions utils/httputil/httputil.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ func (e NetworkError) Error() string {
return fmt.Sprintf("network error: %s", e.err)
}

// IsNetworkError returns true if err is a NetworkError.
// IsNetworkError returns true if err is or wraps a NetworkError.
func IsNetworkError(err error) bool {
_, ok := err.(NetworkError)
return ok
var networkErr NetworkError
return errors.As(err, &networkErr)
}

type sendOptions struct {
Expand Down
20 changes: 20 additions & 0 deletions utils/httputil/httputil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,23 @@ func TestSendDoesNotResetCallerBackoff(t *testing.T) {
require.Error(err)
require.Less(time.Since(start), 80*time.Millisecond)
}

func TestIsNetworkError(t *testing.T) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider using a table test with giveErr err and wantNetworkErr bool.

tests := []struct {
name string
giveErr error
wantNetworkErr bool
}{
{"nil", nil, false},
{"bare network error", NetworkError{errors.New("boom")}, true},
// Errors are wrapped on their way up from the origin cluster, so the
// wrapped form has to be recognized too.
{"wrapped network error", fmt.Errorf("origin %s: %w", "addr", NetworkError{errors.New("boom")}), true},
{"wrapped status error", fmt.Errorf("origin %s: %w", "addr", StatusError{Status: 500}), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.wantNetworkErr, IsNetworkError(tt.giveErr))
})
}
}
Loading