Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compute/metadata): return an error when all retries have failed #5063

Merged
merged 3 commits into from Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion compute/metadata/metadata.go
Expand Up @@ -323,7 +323,7 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
break
}
if reqErr != nil {
return "", "", nil
return "", "", reqErr
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
Expand Down
25 changes: 20 additions & 5 deletions compute/metadata/metadata_go113_test.go
Expand Up @@ -32,6 +32,7 @@ func TestRetry(t *testing.T) {
failCode int
failErr error
response string
expectError bool
}{
{
name: "no retries",
Expand All @@ -49,6 +50,12 @@ func TestRetry(t *testing.T) {
failErr: io.ErrUnexpectedEOF,
timesToFail: 1,
},
{
name: "retry io.ErrUnexpectedEOF permanent",
failErr: io.ErrUnexpectedEOF,
timesToFail: maxRetryAttempts + 1,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -60,15 +67,23 @@ func TestRetry(t *testing.T) {
}
c := NewClient(&http.Client{Transport: ft})
s, err := c.Get("")
if err != nil {
if tt.expectError && err == nil {
t.Fatalf("did not receive expected error")
} else if !tt.expectError && err != nil {
t.Fatalf("unexpected error: %v", err)
}
if ft.called != ft.failedAttempts+1 {
t.Fatalf("failed %d times, want %d", ft.called, ft.failedAttempts+1)
}
if s != tt.response {

expectedCount := ft.failedAttempts + 1
if tt.expectError {
expectedCount = ft.failedAttempts
} else if s != tt.response {
// Responses are only meaningful if err == nil
t.Fatalf("c.Get() = %q, want %q", s, tt.response)
}

if ft.called != expectedCount {
t.Fatalf("failed %d times, want %d", ft.called, expectedCount)
}
})
}
}
Expand Down