Skip to content

Commit b41dba5

Browse files
committed
Fix the since and before filter behavior
Filters should not include stopped container if `-a` is not specified. Right now, before and since filter are acting as --before and --since deprecated flags. This commit is fixing that. Signed-off-by: Vincent Demeester <[email protected]>
1 parent ac9d1b7 commit b41dba5

File tree

2 files changed

+132
-22
lines changed

2 files changed

+132
-22
lines changed

daemon/list.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ type listContext struct {
5858
filters filters.Args
5959
// exitAllowed is a list of exit codes allowed to filter with
6060
exitAllowed []int
61+
62+
// FIXME Remove this for 1.12 as --since and --before are deprecated
63+
// beforeContainer is a filter to ignore containers that appear before the one given
64+
beforeContainer *container.Container
65+
// sinceContainer is a filter to stop the filtering when the iterator arrive to the given container
66+
sinceContainer *container.Container
67+
6168
// beforeFilter is a filter to ignore containers that appear before the one given
6269
// this is used for --filter=before= and --before=, the latter is deprecated.
6370
beforeFilter *container.Container
@@ -146,6 +153,9 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
146153
}
147154

148155
var beforeContFilter, sinceContFilter *container.Container
156+
// FIXME remove this for 1.12 as --since and --before are deprecated
157+
var beforeContainer, sinceContainer *container.Container
158+
149159
err = psFilters.WalkValues("before", func(value string) error {
150160
beforeContFilter, err = daemon.GetContainer(value)
151161
return err
@@ -182,15 +192,17 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
182192
})
183193
}
184194

185-
if config.Before != "" && beforeContFilter == nil {
186-
beforeContFilter, err = daemon.GetContainer(config.Before)
195+
// FIXME remove this for 1.12 as --since and --before are deprecated
196+
if config.Before != "" {
197+
beforeContainer, err = daemon.GetContainer(config.Before)
187198
if err != nil {
188199
return nil, err
189200
}
190201
}
191202

192-
if config.Since != "" && sinceContFilter == nil {
193-
sinceContFilter, err = daemon.GetContainer(config.Since)
203+
// FIXME remove this for 1.12 as --since and --before are deprecated
204+
if config.Since != "" {
205+
sinceContainer, err = daemon.GetContainer(config.Since)
194206
if err != nil {
195207
return nil, err
196208
}
@@ -201,6 +213,8 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
201213
ancestorFilter: ancestorFilter,
202214
images: imagesFilter,
203215
exitAllowed: filtExited,
216+
beforeContainer: beforeContainer,
217+
sinceContainer: sinceContainer,
204218
beforeFilter: beforeContFilter,
205219
sinceFilter: sinceContFilter,
206220
ContainerListOptions: config,
@@ -212,7 +226,8 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
212226
// It also decides if the iteration should be stopped or not.
213227
func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
214228
// Do not include container if it's stopped and we're not filters
215-
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeFilter == nil && ctx.sinceFilter == nil {
229+
// FIXME remove the ctx.beforContainer part of the condition for 1.12 as --since and --before are deprecated
230+
if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
216231
return excludeContainer
217232
}
218233

@@ -236,6 +251,21 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
236251
return excludeContainer
237252
}
238253

254+
// FIXME remove this for 1.12 as --since and --before are deprecated
255+
if ctx.beforeContainer != nil {
256+
if container.ID == ctx.beforeContainer.ID {
257+
ctx.beforeContainer = nil
258+
}
259+
return excludeContainer
260+
}
261+
262+
// FIXME remove this for 1.12 as --since and --before are deprecated
263+
if ctx.sinceContainer != nil {
264+
if container.ID == ctx.sinceContainer.ID {
265+
return stopIteration
266+
}
267+
}
268+
239269
// Do not include container if it's in the list before the filter container.
240270
// Set the filter container to nil to include the rest of containers after this one.
241271
if ctx.beforeFilter != nil {

integration-cli/docker_cli_ps_test.go

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
4747
out, _ = dockerCmd(c, "ps")
4848
c.Assert(assertContainerList(out, []string{fourthID, secondID, firstID}), checker.Equals, true, check.Commentf("RUNNING: Container list is not in the correct order: \n%s", out))
4949

50-
// from here all flag '-a' is ignored
51-
5250
// limit
5351
out, _ = dockerCmd(c, "ps", "-n=2", "-a")
5452
expected := []string{fourthID, thirdID}
@@ -60,56 +58,138 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
6058
// filter since
6159
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
6260
expected = []string{fourthID, thirdID, secondID}
63-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: \n%s", out))
61+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter & ALL: Container list is not in the correct order: \n%s", out))
6462

6563
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID)
66-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: \n%s", out))
64+
expected = []string{fourthID, secondID}
65+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
6766

6867
// filter before
69-
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID, "-a")
70-
expected = []string{secondID, firstID}
71-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: \n%s", out))
68+
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
69+
expected = []string{thirdID, secondID, firstID}
70+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
7271

73-
out, _ = dockerCmd(c, "ps", "-f", "before="+thirdID)
74-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: \n%s", out))
72+
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID)
73+
expected = []string{secondID, firstID}
74+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
7575

7676
// filter since & before
7777
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
7878
expected = []string{thirdID, secondID}
79-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: \n%s", out))
79+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
8080

8181
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID)
82-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: \n%s", out))
82+
expected = []string{secondID}
83+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter: Container list is not in the correct order: \n%s", out))
8384

8485
// filter since & limit
8586
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
8687
expected = []string{fourthID, thirdID}
8788

88-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
89+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
8990

9091
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2")
91-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: \n%s", out))
92+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out))
9293

9394
// filter before & limit
9495
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
9596
expected = []string{thirdID}
96-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
97+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
9798

9899
out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1")
99-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
100+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
100101

101102
// filter since & filter before & limit
102103
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
103104
expected = []string{thirdID}
104-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
105+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT & ALL: Container list is not in the correct order: \n%s", out))
105106

106107
out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1")
107-
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT: Container list is not in the correct order: \n%s", out))
108+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
109+
110+
}
111+
112+
// FIXME remove this for 1.12 as --since and --before are deprecated
113+
func (s *DockerSuite) TestPsListContainersDeprecatedSinceAndBefore(c *check.C) {
114+
out, _ := runSleepingContainer(c, "-d")
115+
firstID := strings.TrimSpace(out)
116+
117+
out, _ = runSleepingContainer(c, "-d")
118+
secondID := strings.TrimSpace(out)
119+
120+
// not long running
121+
out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
122+
thirdID := strings.TrimSpace(out)
123+
124+
out, _ = runSleepingContainer(c, "-d")
125+
fourthID := strings.TrimSpace(out)
126+
127+
// make sure the second is running
128+
c.Assert(waitRun(secondID), checker.IsNil)
129+
130+
// make sure third one is not running
131+
dockerCmd(c, "wait", thirdID)
132+
133+
// make sure the forth is running
134+
c.Assert(waitRun(fourthID), checker.IsNil)
135+
136+
// since
137+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "-a")
138+
expected := []string{fourthID, thirdID, secondID}
139+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE & ALL: Container list is not in the correct order: %v \n%s", expected, out))
140+
141+
out, _ = dockerCmd(c, "ps", "--since="+firstID)
142+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: %v \n%s", expected, out))
143+
144+
// before
145+
out, _ = dockerCmd(c, "ps", "--before="+thirdID, "-a")
146+
expected = []string{secondID, firstID}
147+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE & ALL: Container list is not in the correct order: %v \n%s", expected, out))
148+
149+
out, _ = dockerCmd(c, "ps", "--before="+thirdID)
150+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: %v \n%s", expected, out))
151+
152+
// since & before
153+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID, "-a")
154+
expected = []string{thirdID, secondID}
155+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE & ALL: Container list is not in the correct order: %v \n%s", expected, out))
156+
157+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID)
158+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE: Container list is not in the correct order: %v \n%s", expected, out))
159+
160+
// since & limit
161+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "-n=2", "-a")
162+
expected = []string{fourthID, thirdID}
163+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT & ALL: Container list is not in the correct order: %v \n%s", expected, out))
164+
165+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "-n=2")
166+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, LIMIT: Container list is not in the correct order: %v \n%s", expected, out))
167+
168+
// before & limit
169+
out, _ = dockerCmd(c, "ps", "--before="+fourthID, "-n=1", "-a")
170+
expected = []string{thirdID}
171+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT & ALL: Container list is not in the correct order: %v \n%s", expected, out))
172+
173+
out, _ = dockerCmd(c, "ps", "--before="+fourthID, "-n=1")
174+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE, LIMIT: Container list is not in the correct order: %v \n%s", expected, out))
175+
176+
// since & before & limit
177+
out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID, "-n=1", "-a")
178+
expected = []string{thirdID}
179+
c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE, BEFORE, LIMIT & ALL: Container list is not in the correct order: %v \n%s", expected, out))
108180

109181
}
110182

111183
func assertContainerList(out string, expected []string) bool {
112184
lines := strings.Split(strings.Trim(out, "\n "), "\n")
185+
// FIXME remove this for 1.12 as --since and --before are deprecated
186+
// This is here to remove potential Warning: lines (printed out with deprecated flags)
187+
for i := 0; i < 2; i++ {
188+
if strings.Contains(lines[0], "Warning:") {
189+
lines = lines[1:]
190+
}
191+
}
192+
113193
if len(lines)-1 != len(expected) {
114194
return false
115195
}

0 commit comments

Comments
 (0)