Merge pull request #366 from vmarmol/client

Check HTTP error code in the cAdvisor client.
This commit is contained in:
Rohit Jnagal 2014-12-16 11:40:16 -08:00
commit 5ad9cb5bb5
2 changed files with 27 additions and 0 deletions

View File

@ -153,6 +153,9 @@ func (self *Client) httpGetJsonData(data, postData interface{}, url, infoName st
err = fmt.Errorf("unable to read all %q: %v", infoName, err)
return err
}
if resp.StatusCode != 200 {
return fmt.Errorf("request failed with error: %q", strings.TrimSpace(string(body)))
}
if err = json.Unmarshal(body, data); err != nil {
err = fmt.Errorf("unable to unmarshal %q (Body: %q) with error: %v", infoName, string(body), err)
return err

View File

@ -116,6 +116,30 @@ func TestGetContainerInfo(t *testing.T) {
}
}
// Test a requesy failing
func TestRequestFails(t *testing.T) {
errorText := "there was an error"
// Setup a server that simply fails.
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, errorText, 500)
}))
client, err := NewClient(ts.URL)
if err != nil {
ts.Close()
t.Fatal(err)
}
defer ts.Close()
_, err = client.ContainerInfo("/", &info.ContainerInfoRequest{NumStats: 3})
if err == nil {
t.Fatalf("Expected non-nil error")
}
expectedError := fmt.Sprintf("request failed with error: %q", errorText)
if err.Error() != expectedError {
t.Fatalf("Expected error %q but received %q", expectedError, err)
}
}
func TestGetSubcontainersInfo(t *testing.T) {
query := &info.ContainerInfoRequest{
NumStats: 3,