Skip to content

Commit c792a0d

Browse files
authored
fix(compute/metadata): return an error when all retries have failed (#5063)
Minor change to ensure an error is returned to the caller when all retries have failed. Fixes #5062
1 parent 54cbf4c commit c792a0d

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

compute/metadata/metadata.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
323323
break
324324
}
325325
if reqErr != nil {
326-
return "", "", nil
326+
return "", "", reqErr
327327
}
328328
defer res.Body.Close()
329329
if res.StatusCode == http.StatusNotFound {

compute/metadata/metadata_go113_test.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestRetry(t *testing.T) {
3232
failCode int
3333
failErr error
3434
response string
35+
expectError bool
3536
}{
3637
{
3738
name: "no retries",
@@ -49,6 +50,12 @@ func TestRetry(t *testing.T) {
4950
failErr: io.ErrUnexpectedEOF,
5051
timesToFail: 1,
5152
},
53+
{
54+
name: "retry io.ErrUnexpectedEOF permanent",
55+
failErr: io.ErrUnexpectedEOF,
56+
timesToFail: maxRetryAttempts + 1,
57+
expectError: true,
58+
},
5259
}
5360
for _, tt := range tests {
5461
t.Run(tt.name, func(t *testing.T) {
@@ -60,15 +67,23 @@ func TestRetry(t *testing.T) {
6067
}
6168
c := NewClient(&http.Client{Transport: ft})
6269
s, err := c.Get("")
63-
if err != nil {
70+
if tt.expectError && err == nil {
71+
t.Fatalf("did not receive expected error")
72+
} else if !tt.expectError && err != nil {
6473
t.Fatalf("unexpected error: %v", err)
6574
}
66-
if ft.called != ft.failedAttempts+1 {
67-
t.Fatalf("failed %d times, want %d", ft.called, ft.failedAttempts+1)
68-
}
69-
if s != tt.response {
75+
76+
expectedCount := ft.failedAttempts + 1
77+
if tt.expectError {
78+
expectedCount = ft.failedAttempts
79+
} else if s != tt.response {
80+
// Responses are only meaningful if err == nil
7081
t.Fatalf("c.Get() = %q, want %q", s, tt.response)
7182
}
83+
84+
if ft.called != expectedCount {
85+
t.Fatalf("failed %d times, want %d", ft.called, expectedCount)
86+
}
7287
})
7388
}
7489
}

0 commit comments

Comments
 (0)