mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
unit test for GetContainerStats()
This commit is contained in:
parent
1b9cb5d501
commit
2cc3a88411
@ -2,6 +2,7 @@ package kubelet
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -134,3 +135,42 @@ func TestContainerInfo(t *testing.T) {
|
|||||||
t.Errorf("Expected: '%v', got: '%v'", expected, got)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user