-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathreleases_test.go
More file actions
158 lines (138 loc) · 4.69 KB
/
Copy pathreleases_test.go
File metadata and controls
158 lines (138 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package client
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/http"
"strings"
"testing"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/databricks-sdk-go/apierr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/http2"
)
// stubFiler is a filer.Filer whose Stat and Write return scripted results per call,
// so tests can drive the retry loops in binaryExists/uploadRelease deterministically.
type stubFiler struct {
filer.Filer
statErrs []error
writeErrs []error
statCalls int
writes int
}
func (s *stubFiler) Stat(ctx context.Context, name string) (fs.FileInfo, error) {
err := s.statErrs[s.statCalls]
s.statCalls++
return nil, err
}
func (s *stubFiler) Write(ctx context.Context, path string, reader io.Reader, mode ...filer.WriteMode) error {
// Drain the reader as the real filer would, so a retry must supply a fresh one.
_, _ = io.Copy(io.Discard, reader)
err := s.writeErrs[s.writes]
s.writes++
return err
}
func fakeRelease(ctx context.Context, arch, version, releasesDir string) (io.ReadCloser, error) {
return io.NopCloser(strings.NewReader("archive")), nil
}
func timeoutErr() error {
return errors.New(`Get "https://example.test/api/2.0/workspace/get-status": request timed out after 1m0s of inactivity`)
}
func TestIsStreamResetError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{
name: "typed http2.StreamError wrapped",
err: fmt.Errorf(`Post "https://example/api/2.0/workspace-files/import-file/...": %w`, http2.StreamError{StreamID: 15, Code: http2.ErrCodeNo}),
want: true,
},
{
name: "stringified stream error",
err: errors.New("stream error: stream ID 15; NO_ERROR; received from peer"),
want: true,
},
{
name: "unrelated error",
err: errors.New("connection refused"),
want: false,
},
{
name: "API error message",
err: errors.New("RESOURCE_DOES_NOT_EXIST: path does not exist"),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, isStreamResetError(tt.err))
})
}
}
func TestIsRetriableUploadError(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
err error
want bool
}{
{"inactivity timeout string", timeoutErr(), true},
{"context deadline", context.DeadlineExceeded, true},
{"retriable API 503", &apierr.APIError{StatusCode: http.StatusServiceUnavailable}, true},
{"retriable API 429", &apierr.APIError{StatusCode: http.StatusTooManyRequests}, true},
{"non-retriable API 404", &apierr.APIError{StatusCode: http.StatusNotFound}, false},
{"plain error", errors.New("connection refused"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, isRetriableUploadError(ctx, tt.err))
})
}
}
func TestBinaryExistsRetriesTransientStat(t *testing.T) {
ctx := context.Background()
// A transient timeout on the first stat, then a clean "exists" on the retry.
f := &stubFiler{statErrs: []error{timeoutErr(), nil}}
exists, err := binaryExists(ctx, f, "amd64/databricks")
require.NoError(t, err)
assert.True(t, exists)
assert.Equal(t, 2, f.statCalls)
}
func TestBinaryExistsNotFoundNoRetry(t *testing.T) {
ctx := context.Background()
// A definitive "not found" must resolve immediately (it's the signal to upload), not retry.
f := &stubFiler{statErrs: []error{fs.ErrNotExist}}
exists, err := binaryExists(ctx, f, "amd64/databricks")
require.NoError(t, err)
assert.False(t, exists)
assert.Equal(t, 1, f.statCalls)
}
func TestBinaryExistsHaltsOnNonRetriable(t *testing.T) {
ctx := context.Background()
f := &stubFiler{statErrs: []error{&apierr.APIError{StatusCode: http.StatusForbidden, Message: "denied"}}}
_, err := binaryExists(ctx, f, "amd64/databricks")
require.Error(t, err)
assert.Equal(t, 1, f.statCalls)
}
func TestUploadReleaseRetriesTransientWrite(t *testing.T) {
ctx := context.Background()
// First write fails transiently; the retry must re-fetch a fresh reader and succeed.
f := &stubFiler{writeErrs: []error{&apierr.APIError{StatusCode: http.StatusServiceUnavailable}, nil}}
err := uploadRelease(ctx, f, fakeRelease, "amd64", "1.0.0", "", "amd64/databricks.zip")
require.NoError(t, err)
assert.Equal(t, 2, f.writes)
}
func TestUploadReleaseStreamResetNoRetry(t *testing.T) {
ctx := context.Background()
// A stream reset is a proxy body-size rejection: fail fast with the hint, don't retry.
f := &stubFiler{writeErrs: []error{http2.StreamError{StreamID: 1, Code: http2.ErrCodeNo}}}
err := uploadRelease(ctx, f, fakeRelease, "amd64", "1.0.0", "", "amd64/databricks.zip")
require.Error(t, err)
assert.Contains(t, err.Error(), "request-body size limit")
assert.Equal(t, 1, f.writes)
}