1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Optimize TestApiStatsNetworkStats and TestApiStatsNetworkStatsVersioning

Signed-off-by: Wen Cheng Ma <wenchma@cn.ibm.com>
This commit is contained in:
Wen Cheng Ma 2016-01-28 15:36:31 +08:00
parent 40ce2574b4
commit 695b7e8d11

View file

@ -82,14 +82,14 @@ func (s *DockerSuite) TestApiStatsStoppedContainerInGoroutines(c *check.C) {
func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) { func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
testRequires(c, SameHostDaemon) testRequires(c, SameHostDaemon)
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
// Run container for 30 secs
out, _ := dockerCmd(c, "run", "-d", "busybox", "top") out, _ := runSleepingContainer(c)
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
c.Assert(waitRun(id), checker.IsNil) c.Assert(waitRun(id), checker.IsNil)
// Retrieve the container address // Retrieve the container address
contIP := findContainerIP(c, id, "bridge") contIP := findContainerIP(c, id, "bridge")
numPings := 10 numPings := 4
var preRxPackets uint64 var preRxPackets uint64
var preTxPackets uint64 var preTxPackets uint64
@ -128,21 +128,20 @@ func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
func (s *DockerSuite) TestApiStatsNetworkStatsVersioning(c *check.C) { func (s *DockerSuite) TestApiStatsNetworkStatsVersioning(c *check.C) {
testRequires(c, SameHostDaemon) testRequires(c, SameHostDaemon)
testRequires(c, DaemonIsLinux) testRequires(c, DaemonIsLinux)
// Run container for 30 secs
out, _ := dockerCmd(c, "run", "-d", "busybox", "top") out, _ := runSleepingContainer(c)
id := strings.TrimSpace(out) id := strings.TrimSpace(out)
c.Assert(waitRun(id), checker.IsNil) c.Assert(waitRun(id), checker.IsNil)
for i := 17; i <= 21; i++ { for i := 17; i <= 21; i++ {
apiVersion := fmt.Sprintf("v1.%d", i) apiVersion := fmt.Sprintf("v1.%d", i)
for _, statsJSONBlob := range getVersionedStats(c, id, 3, apiVersion) { statsJSONBlob := getVersionedStats(c, id, apiVersion)
if version.Version(apiVersion).LessThan("v1.21") { if version.Version(apiVersion).LessThan("v1.21") {
c.Assert(jsonBlobHasLTv121NetworkStats(statsJSONBlob), checker.Equals, true, c.Assert(jsonBlobHasLTv121NetworkStats(statsJSONBlob), checker.Equals, true,
check.Commentf("Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob)) check.Commentf("Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob))
} else { } else {
c.Assert(jsonBlobHasGTE121NetworkStats(statsJSONBlob), checker.Equals, true, c.Assert(jsonBlobHasGTE121NetworkStats(statsJSONBlob), checker.Equals, true,
check.Commentf("Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob)) check.Commentf("Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob))
}
} }
} }
} }
@ -160,23 +159,19 @@ func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
return st.Networks return st.Networks
} }
// getVersionedNetworkStats returns a slice of numStats stats results for the // getVersionedStats returns stats result for the
// container with id id using an API call with version apiVersion. Since the // container with id using an API call with version apiVersion. Since the
// stats result type differs between API versions, we simply return // stats result type differs between API versions, we simply return
// []map[string]interface{}. // map[string]interface{}.
func getVersionedStats(c *check.C, id string, numStats int, apiVersion string) []map[string]interface{} { func getVersionedStats(c *check.C, id string, apiVersion string) map[string]interface{} {
stats := make([]map[string]interface{}, numStats) stats := make(map[string]interface{})
requestPath := fmt.Sprintf("/%s/containers/%s/stats?stream=true", apiVersion, id) _, body, err := sockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "")
_, body, err := sockRequestRaw("GET", requestPath, nil, "")
c.Assert(err, checker.IsNil) c.Assert(err, checker.IsNil)
defer body.Close() defer body.Close()
statsDecoder := json.NewDecoder(body) err = json.NewDecoder(body).Decode(&stats)
for i := range stats { c.Assert(err, checker.IsNil, check.Commentf("failed to decode stat: %s", err))
err = statsDecoder.Decode(&stats[i])
c.Assert(err, checker.IsNil, check.Commentf("failed to decode %dth stat: %s", i, err))
}
return stats return stats
} }