Skip to content

Commit 48716ed

Browse files
detect download failures when StatusCode > 200 (#820)
1 parent ac28333 commit 48716ed

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

internal/download/fetch.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ func (HTTPFetcher) Get(uri string) (io.ReadCloser, error) {
4141
if err != nil {
4242
return nil, errors.Wrapf(err, "failed to download %q", uri)
4343
}
44+
if resp.StatusCode > 200 {
45+
return nil, errors.Errorf("failed to download %q, status code %d", uri, resp.StatusCode)
46+
}
4447
return resp.Body, nil
4548
}
4649

internal/download/fetch_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Copyright 2019 The Kubernetes Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package download
16+
17+
import (
18+
"bytes"
19+
"io"
20+
"net/http"
21+
"net/http/httptest"
22+
"reflect"
23+
"strconv"
24+
"strings"
25+
"testing"
26+
)
27+
28+
func TestHTTPFetcher_Get(t *testing.T) {
29+
type args struct {
30+
uri string
31+
}
32+
tests := []struct {
33+
name string
34+
h HTTPFetcher
35+
args args
36+
want io.ReadCloser
37+
wantErr bool
38+
}{
39+
{
40+
name: "200",
41+
h: HTTPFetcher{},
42+
args: args{
43+
uri: "content:foo",
44+
},
45+
want: io.NopCloser(bytes.NewBufferString("foo")),
46+
wantErr: false,
47+
},
48+
{
49+
name: "404",
50+
h: HTTPFetcher{},
51+
args: args{
52+
uri: "404",
53+
},
54+
want: nil,
55+
wantErr: true,
56+
},
57+
{
58+
name: "401",
59+
h: HTTPFetcher{},
60+
args: args{
61+
uri: "401",
62+
},
63+
want: nil,
64+
wantErr: true,
65+
},
66+
}
67+
68+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
69+
if r.Method != "GET" {
70+
t.Fatalf("test server download %v must always use GET, used %s instead", r.URL, r.Method)
71+
}
72+
// return everything after colon as content to the client, return 200
73+
if strings.HasPrefix(r.URL.Path, "/content:") {
74+
if len(r.URL.Path) < 10 {
75+
w.WriteHeader(http.StatusBadRequest)
76+
t.Fatalf("test server received content directive with no content")
77+
return
78+
}
79+
content := r.URL.Path[9:]
80+
_, err := w.Write([]byte(content))
81+
if err != nil {
82+
t.Fatalf("test server could not write content response: %s", err)
83+
}
84+
return
85+
}
86+
// assume that any other resource is just asking to return a specific return code
87+
i, err := strconv.Atoi(r.URL.Path[1:])
88+
if err != nil {
89+
t.Fatalf("test server url received unknown directive: %v", r.URL.Path)
90+
}
91+
w.WriteHeader(i)
92+
}))
93+
defer ts.Close()
94+
95+
for _, tt := range tests {
96+
t.Run(tt.name, func(t *testing.T) {
97+
h := HTTPFetcher{}
98+
got, err := h.Get(ts.URL + "/" + tt.args.uri)
99+
if got != nil {
100+
defer got.Close()
101+
}
102+
if (err != nil) != tt.wantErr {
103+
t.Errorf("HTTPFetcher.Get() error = %v, wantErr %v", err, tt.wantErr)
104+
return
105+
}
106+
if (tt.want != nil) && (got == nil) {
107+
t.Error("HTTPFetcher.Get() = nil, want not nil")
108+
return
109+
}
110+
if (tt.want == nil) && (got != nil) {
111+
t.Error("HTTPFetcher.Get() != nil, want nil")
112+
return
113+
}
114+
if got == nil && tt.want == nil {
115+
return
116+
}
117+
gotS, err := io.ReadAll(got)
118+
if err != nil {
119+
t.Errorf("HTTPFetcher.Get() could not read body: %s", err)
120+
return
121+
}
122+
// this is a local buffer, no error will be returned
123+
wantS, _ := io.ReadAll(tt.want)
124+
if !reflect.DeepEqual(gotS, wantS) {
125+
t.Errorf("HTTPFetcher.Get() = %v, want %v", gotS, wantS)
126+
}
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)