unit test for GetContainerStats()

pull/6/head
Nan Deng 2014-06-19 20:43:35 +00:00
parent 1b9cb5d501
commit 2cc3a88411
1 changed files with 40 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package kubelet
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
@ -134,3 +135,42 @@ func TestContainerInfo(t *testing.T) {
t.Errorf("Expected: '%v', got: '%v'", expected, got)
}
}
func TestContainerStats(t *testing.T) {
fw := makeServerTest()
expectedStats := &api.ContainerStats{
MaxMemoryUsage: 1024001,
CpuUsagePercentiles: []api.Percentile{
api.Percentile{50, 150},
api.Percentile{80, 180},
api.Percentile{90, 190},
},
MemoryUsagePercentiles: []api.Percentile{
api.Percentile{50, 150},
api.Percentile{80, 180},
api.Percentile{90, 190},
},
}
expectedContainerName := "goodcontainer"
fw.fakeKubelet.statsFunc = func(name string) (*api.ContainerStats, error) {
if name != expectedContainerName {
return nil, fmt.Errorf("bad container name: %v", name)
}
return expectedStats, nil
}
resp, err := http.Get(fw.testHttpServer.URL + fmt.Sprintf("/containerStats?container=%v", expectedContainerName))
if err != nil {
t.Fatalf("Got error GETing: %v", err)
}
defer resp.Body.Close()
var receivedStats api.ContainerStats
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&receivedStats)
if err != nil {
t.Fatalf("received invalid json data: %v", err)
}
if !reflect.DeepEqual(&receivedStats, expectedStats) {
t.Errorf("received wrong data: %#v", receivedStats)
}
}