Browse code

Optimize TestApiStatsNetworkStats and TestApiStatsNetworkStatsVersioning

Signed-off-by: Wen Cheng Ma <wenchma@cn.ibm.com>

Wen Cheng Ma authored on 2016/01/28 16:36:31
Showing 1 changed files
... ...
@@ -82,14 +82,14 @@ func (s *DockerSuite) TestApiStatsStoppedContainerInGoroutines(c *check.C) {
82 82
 func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
83 83
 	testRequires(c, SameHostDaemon)
84 84
 	testRequires(c, DaemonIsLinux)
85
-	// Run container for 30 secs
86
-	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
85
+
86
+	out, _ := runSleepingContainer(c)
87 87
 	id := strings.TrimSpace(out)
88 88
 	c.Assert(waitRun(id), checker.IsNil)
89 89
 
90 90
 	// Retrieve the container address
91 91
 	contIP := findContainerIP(c, id, "bridge")
92
-	numPings := 10
92
+	numPings := 4
93 93
 
94 94
 	var preRxPackets uint64
95 95
 	var preTxPackets uint64
... ...
@@ -128,21 +128,20 @@ func (s *DockerSuite) TestApiStatsNetworkStats(c *check.C) {
128 128
 func (s *DockerSuite) TestApiStatsNetworkStatsVersioning(c *check.C) {
129 129
 	testRequires(c, SameHostDaemon)
130 130
 	testRequires(c, DaemonIsLinux)
131
-	// Run container for 30 secs
132
-	out, _ := dockerCmd(c, "run", "-d", "busybox", "top")
131
+
132
+	out, _ := runSleepingContainer(c)
133 133
 	id := strings.TrimSpace(out)
134 134
 	c.Assert(waitRun(id), checker.IsNil)
135 135
 
136 136
 	for i := 17; i <= 21; i++ {
137 137
 		apiVersion := fmt.Sprintf("v1.%d", i)
138
-		for _, statsJSONBlob := range getVersionedStats(c, id, 3, apiVersion) {
139
-			if version.Version(apiVersion).LessThan("v1.21") {
140
-				c.Assert(jsonBlobHasLTv121NetworkStats(statsJSONBlob), checker.Equals, true,
141
-					check.Commentf("Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob))
142
-			} else {
143
-				c.Assert(jsonBlobHasGTE121NetworkStats(statsJSONBlob), checker.Equals, true,
144
-					check.Commentf("Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob))
145
-			}
138
+		statsJSONBlob := getVersionedStats(c, id, apiVersion)
139
+		if version.Version(apiVersion).LessThan("v1.21") {
140
+			c.Assert(jsonBlobHasLTv121NetworkStats(statsJSONBlob), checker.Equals, true,
141
+				check.Commentf("Stats JSON blob from API %s %#v does not look like a <v1.21 API stats structure", apiVersion, statsJSONBlob))
142
+		} else {
143
+			c.Assert(jsonBlobHasGTE121NetworkStats(statsJSONBlob), checker.Equals, true,
144
+				check.Commentf("Stats JSON blob from API %s %#v does not look like a >=v1.21 API stats structure", apiVersion, statsJSONBlob))
146 145
 		}
147 146
 	}
148 147
 }
... ...
@@ -160,23 +159,19 @@ func getNetworkStats(c *check.C, id string) map[string]types.NetworkStats {
160 160
 	return st.Networks
161 161
 }
162 162
 
163
-// getVersionedNetworkStats returns a slice of numStats stats results for the
164
-// container with id id using an API call with version apiVersion. Since the
163
+// getVersionedStats returns stats result for the
164
+// container with id using an API call with version apiVersion. Since the
165 165
 // stats result type differs between API versions, we simply return
166
-// []map[string]interface{}.
167
-func getVersionedStats(c *check.C, id string, numStats int, apiVersion string) []map[string]interface{} {
168
-	stats := make([]map[string]interface{}, numStats)
166
+// map[string]interface{}.
167
+func getVersionedStats(c *check.C, id string, apiVersion string) map[string]interface{} {
168
+	stats := make(map[string]interface{})
169 169
 
170
-	requestPath := fmt.Sprintf("/%s/containers/%s/stats?stream=true", apiVersion, id)
171
-	_, body, err := sockRequestRaw("GET", requestPath, nil, "")
170
+	_, body, err := sockRequestRaw("GET", fmt.Sprintf("/%s/containers/%s/stats?stream=false", apiVersion, id), nil, "")
172 171
 	c.Assert(err, checker.IsNil)
173 172
 	defer body.Close()
174 173
 
175
-	statsDecoder := json.NewDecoder(body)
176
-	for i := range stats {
177
-		err = statsDecoder.Decode(&stats[i])
178
-		c.Assert(err, checker.IsNil, check.Commentf("failed to decode %dth stat: %s", i, err))
179
-	}
174
+	err = json.NewDecoder(body).Decode(&stats)
175
+	c.Assert(err, checker.IsNil, check.Commentf("failed to decode stat: %s", err))
180 176
 
181 177
 	return stats
182 178
 }