Browse code

Enhancement of docker ps before and since filters

This enhancement is to fix the wrong list results on
`docker ps` before and since filters specifying the non-running container.

Fixes issue #20431

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

Wen Cheng Ma authored on 2016/02/19 15:07:16
Showing 2 changed files
... ...
@@ -226,8 +226,24 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
226 226
 // includeContainerInList decides whether a containers should be include in the output or not based in the filter.
227 227
 // It also decides if the iteration should be stopped or not.
228 228
 func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
229
+	// Do not include container if it's in the list before the filter container.
230
+	// Set the filter container to nil to include the rest of containers after this one.
231
+	if ctx.beforeFilter != nil {
232
+		if container.ID == ctx.beforeFilter.ID {
233
+			ctx.beforeFilter = nil
234
+		}
235
+		return excludeContainer
236
+	}
237
+
238
+	// Stop iteration when the container arrives to the filter container
239
+	if ctx.sinceFilter != nil {
240
+		if container.ID == ctx.sinceFilter.ID {
241
+			return stopIteration
242
+		}
243
+	}
244
+
229 245
 	// Do not include container if it's stopped and we're not filters
230
-	// FIXME remove the ctx.beforContainer part of the condition for 1.12 as --since and --before are deprecated
246
+	// FIXME remove the ctx.beforContainer and ctx.sinceContainer part of the condition for 1.12 as --since and --before are deprecated
231 247
 	if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
232 248
 		return excludeContainer
233 249
 	}
... ...
@@ -267,22 +283,6 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
267 267
 		}
268 268
 	}
269 269
 
270
-	// Do not include container if it's in the list before the filter container.
271
-	// Set the filter container to nil to include the rest of containers after this one.
272
-	if ctx.beforeFilter != nil {
273
-		if container.ID == ctx.beforeFilter.ID {
274
-			ctx.beforeFilter = nil
275
-		}
276
-		return excludeContainer
277
-	}
278
-
279
-	// Stop iteration when the container arrives to the filter container
280
-	if ctx.sinceFilter != nil {
281
-		if container.ID == ctx.sinceFilter.ID {
282
-			return stopIteration
283
-		}
284
-	}
285
-
286 270
 	// Stop iteration when the index is over the limit
287 271
 	if ctx.Limit > 0 && ctx.idx == ctx.Limit {
288 272
 		return stopIteration
... ...
@@ -64,6 +64,10 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
64 64
 	expected = []string{fourthID, secondID}
65 65
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
66 66
 
67
+	out, _ = dockerCmd(c, "ps", "-f", "since="+thirdID)
68
+	expected = []string{fourthID}
69
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
70
+
67 71
 	// filter before
68 72
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
69 73
 	expected = []string{thirdID, secondID, firstID}
... ...
@@ -73,6 +77,10 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
73 73
 	expected = []string{secondID, firstID}
74 74
 	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
75 75
 
76
+	out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
77
+	expected = []string{secondID, firstID}
78
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
79
+
76 80
 	// filter since & before
77 81
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
78 82
 	expected = []string{thirdID, secondID}