Skip to content

Commit

Permalink
Merge pull request kubernetes#2090 from ddysher/stop-httptest-server
Browse files Browse the repository at this point in the history
Stop httptest server.
  • Loading branch information
brendandburns committed Oct 31, 2014
2 parents c6df93d + acf9d23 commit 5633110
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/apiserver/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func TestNotFound(t *testing.T) {
"foo": &SimpleRESTStorage{},
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}
for k, v := range cases {
request, err := http.NewRequest(v.Method, server.URL+v.Path, nil)
Expand All @@ -227,6 +228,7 @@ func TestNotFound(t *testing.T) {
func TestVersion(t *testing.T) {
handler := Handle(map[string]RESTStorage{}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

request, err := http.NewRequest("GET", server.URL+"/version", nil)
Expand Down Expand Up @@ -260,6 +262,7 @@ func TestSimpleList(t *testing.T) {
}
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + "/prefix/version/simple")
if err != nil {
Expand All @@ -282,6 +285,7 @@ func TestErrorList(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + "/prefix/version/simple")
if err != nil {
Expand All @@ -306,6 +310,7 @@ func TestNonEmptyList(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + "/prefix/version/simple")
if err != nil {
Expand Down Expand Up @@ -347,6 +352,7 @@ func TestGet(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + "/prefix/version/simple/id")
var itemOut Simple
Expand All @@ -371,6 +377,7 @@ func TestGetMissing(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

resp, err := http.Get(server.URL + "/prefix/version/simple/id")
if err != nil {
Expand All @@ -389,6 +396,7 @@ func TestDelete(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

client := http.Client{}
request, err := http.NewRequest("DELETE", server.URL+"/prefix/version/simple/"+ID, nil)
Expand All @@ -411,6 +419,7 @@ func TestDeleteMissing(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

client := http.Client{}
request, err := http.NewRequest("DELETE", server.URL+"/prefix/version/simple/"+ID, nil)
Expand All @@ -435,6 +444,7 @@ func TestUpdate(t *testing.T) {
}
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

item := &Simple{
Other: "bar",
Expand Down Expand Up @@ -468,6 +478,7 @@ func TestUpdateMissing(t *testing.T) {
storage["simple"] = &simpleStorage
handler := Handle(storage, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

item := &Simple{
Other: "bar",
Expand Down Expand Up @@ -496,6 +507,7 @@ func TestCreate(t *testing.T) {
}, codec, "/prefix/version", selfLinker)
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

simple := &Simple{
Expand Down Expand Up @@ -536,6 +548,7 @@ func TestCreateNotFound(t *testing.T) {
},
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

simple := &Simple{Other: "foo"}
Expand Down Expand Up @@ -600,6 +613,7 @@ func TestSyncCreate(t *testing.T) {
"foo": &storage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

simple := &Simple{
Expand Down Expand Up @@ -673,6 +687,7 @@ func TestAsyncDelayReturnsError(t *testing.T) {
handler := Handle(map[string]RESTStorage{"foo": &storage}, codec, "/prefix/version", selfLinker)
handler.(*defaultAPIServer).group.handler.asyncOpWait = time.Millisecond / 2
server := httptest.NewServer(handler)
defer server.Close()

status := expectApiStatus(t, "DELETE", fmt.Sprintf("%s/prefix/version/foo/bar", server.URL), nil, http.StatusConflict)
if status.Status != api.StatusFailure || status.Message == "" || status.Details == nil || status.Reason != api.StatusReasonAlreadyExists {
Expand All @@ -696,6 +711,7 @@ func TestAsyncCreateError(t *testing.T) {
handler := Handle(map[string]RESTStorage{"foo": &storage}, codec, "/prefix/version", selfLinker)
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)
defer server.Close()

simple := &Simple{Other: "bar"}
data, _ := codec.Encode(simple)
Expand Down Expand Up @@ -746,6 +762,7 @@ func TestWriteJSONDecodeError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
writeJSON(http.StatusOK, latest.Codec, &UnregisteredAPIObject{"Undecodable"}, w)
}))
defer server.Close()
status := expectApiStatus(t, "GET", server.URL, nil, http.StatusInternalServerError)
if status.Reason != api.StatusReasonUnknown {
t.Errorf("unexpected reason %#v", status)
Expand All @@ -767,6 +784,7 @@ func TestWriteRAWJSONMarshalError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
writeRawJSON(http.StatusOK, &marshalError{errors.New("Undecodable")}, w)
}))
defer server.Close()
client := http.Client{}
resp, err := client.Get(server.URL)
if err != nil {
Expand All @@ -792,6 +810,7 @@ func TestSyncCreateTimeout(t *testing.T) {
"foo": &storage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

simple := &Simple{Other: "foo"}
data, _ := codec.Encode(simple)
Expand Down Expand Up @@ -825,6 +844,7 @@ func TestCORSAllowedOrigins(t *testing.T) {
allowedOriginRegexps, nil, nil, "true",
)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

request, err := http.NewRequest("GET", server.URL+"/version", nil)
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestRateLimit(t *testing.T) {
}
},
)))
defer server.Close()
http.Get(server.URL)
}
}
Expand All @@ -49,6 +50,7 @@ func TestReadOnly(t *testing.T) {
}
},
)))
defer server.Close()
for _, verb := range []string{"GET", "POST", "PUT", "DELETE", "CREATE"} {
req, err := http.NewRequest(verb, server.URL, nil)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiserver/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func TestOperationsList(t *testing.T) {
}, codec, "/prefix/version", selfLinker)
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

simple := &Simple{
Expand Down Expand Up @@ -172,6 +173,7 @@ func TestOpGet(t *testing.T) {
}, codec, "/prefix/version", selfLinker)
handler.(*defaultAPIServer).group.handler.asyncOpWait = 0
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

simple := &Simple{
Expand Down
2 changes: 2 additions & 0 deletions pkg/apiserver/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func TestProxy(t *testing.T) {
}
fmt.Fprint(w, item.respBody)
}))
defer proxyServer.Close()

simpleStorage := &SimpleRESTStorage{
errors: map[string]error{},
Expand All @@ -166,6 +167,7 @@ func TestProxy(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

req, err := http.NewRequest(
item.method,
Expand Down
1 change: 1 addition & 0 deletions pkg/apiserver/redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestRedirect(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

dontFollow := errors.New("don't follow")
client := http.Client{
Expand Down
5 changes: 5 additions & 0 deletions pkg/apiserver/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestWatchWebsocket(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

dest, _ := url.Parse(server.URL)
dest.Scheme = "ws" // Required by websocket, though the server never sees it.
Expand Down Expand Up @@ -98,6 +99,7 @@ func TestWatchHTTP(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
client := http.Client{}

dest, _ := url.Parse(server.URL)
Expand Down Expand Up @@ -151,6 +153,7 @@ func TestWatchParamParsing(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()

dest, _ := url.Parse(server.URL)
dest.Path = "/prefix/version/watch/foo"
Expand Down Expand Up @@ -222,6 +225,8 @@ func TestWatchProtocolSelection(t *testing.T) {
"foo": simpleStorage,
}, codec, "/prefix/version", selfLinker)
server := httptest.NewServer(handler)
defer server.Close()
defer server.CloseClientConnections()
client := http.Client{}

dest, _ := url.Parse(server.URL)
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/containerinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func testHTTPContainerInfoGetter(
t.Fatal(err)
}
}))
defer ts.Close()
hostURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -170,6 +171,7 @@ func TestHTTPGetMachineInfo(t *testing.T) {
t.Fatal(err)
}
}))
defer ts.Close()
hostURL, err := url.Parse(ts.URL)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/kubelet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestHTTPKubeletClient(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()

hostURL, err := url.Parse(testServer.URL)
if err != nil {
Expand Down Expand Up @@ -85,6 +86,7 @@ func TestHTTPKubeletClientNotFound(t *testing.T) {
ResponseBody: "Pod not found",
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()

hostURL, err := url.Parse(testServer.URL)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/restclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestDoRequestBearer(t *testing.T) {
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
request, _ := http.NewRequest("GET", testServer.URL, nil)
c, err := RESTClientFor(&Config{Host: testServer.URL, BearerToken: "test"})
if err != nil {
Expand All @@ -131,6 +132,7 @@ func TestDoRequestAccepted(t *testing.T) {
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "test", Version: testapi.Version()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -164,6 +166,7 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass", Version: testapi.Version()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -194,6 +197,7 @@ func TestDoRequestFailed(t *testing.T) {
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -221,6 +225,7 @@ func TestDoRequestCreated(t *testing.T) {
T: t,
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
c, err := RESTClientFor(&Config{Host: testServer.URL, Username: "user", Password: "pass", Version: testapi.Version()})
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/controller/replication_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func TestSyncReplicationControllerDoesNothing(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})

fakePodControl := FakePodControl{}
Expand All @@ -133,6 +134,7 @@ func TestSyncReplicationControllerDeletes(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})

fakePodControl := FakePodControl{}
Expand All @@ -153,6 +155,7 @@ func TestSyncReplicationControllerCreates(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})

fakePodControl := FakePodControl{}
Expand All @@ -174,6 +177,7 @@ func TestCreateReplica(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewServer(&fakeHandler)
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})

podControl := RealPodControl{
Expand Down Expand Up @@ -306,6 +310,7 @@ func TestSynchonize(t *testing.T) {
t.Errorf("Unexpected request for %v", req.RequestURI)
})
testServer := httptest.NewServer(mux)
defer testServer.Close()
client := client.NewOrDie(&client.Config{Host: testServer.URL, Version: testapi.Version()})
manager := NewReplicationManager(client)
fakePodControl := FakePodControl{}
Expand Down
2 changes: 2 additions & 0 deletions pkg/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestHealthChecker(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tt.status)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down Expand Up @@ -114,6 +115,7 @@ func TestMuxHealthChecker(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
u, err := url.Parse(ts.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down
1 change: 1 addition & 0 deletions pkg/health/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestTcpHealthChecker(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
u, err := url.Parse(server.URL)
if err != nil {
t.Errorf("Unexpected error: %v", err)
Expand Down
Loading

0 comments on commit 5633110

Please sign in to comment.