Browse code

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 <vincent@sbr.pm>

Vincent Demeester authored on 2016/02/09 17:06:29
Showing 2 changed files
... ...
@@ -58,6 +58,13 @@ type listContext struct {
58 58
 	filters filters.Args
59 59
 	// exitAllowed is a list of exit codes allowed to filter with
60 60
 	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
+
61 68
 	// beforeFilter is a filter to ignore containers that appear before the one given
62 69
 	// this is used for --filter=before= and --before=, the latter is deprecated.
63 70
 	beforeFilter *container.Container
... ...
@@ -146,6 +153,9 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
146 146
 	}
147 147
 
148 148
 	var beforeContFilter, sinceContFilter *container.Container
149
+	// FIXME remove this for 1.12 as --since and --before are deprecated
150
+	var beforeContainer, sinceContainer *container.Container
151
+
149 152
 	err = psFilters.WalkValues("before", func(value string) error {
150 153
 		beforeContFilter, err = daemon.GetContainer(value)
151 154
 		return err
... ...
@@ -182,15 +192,17 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
182 182
 		})
183 183
 	}
184 184
 
185
-	if config.Before != "" && beforeContFilter == nil {
186
-		beforeContFilter, err = daemon.GetContainer(config.Before)
185
+	// FIXME remove this for 1.12 as --since and --before are deprecated
186
+	if config.Before != "" {
187
+		beforeContainer, err = daemon.GetContainer(config.Before)
187 188
 		if err != nil {
188 189
 			return nil, err
189 190
 		}
190 191
 	}
191 192
 
192
-	if config.Since != "" && sinceContFilter == nil {
193
-		sinceContFilter, err = daemon.GetContainer(config.Since)
193
+	// FIXME remove this for 1.12 as --since and --before are deprecated
194
+	if config.Since != "" {
195
+		sinceContainer, err = daemon.GetContainer(config.Since)
194 196
 		if err != nil {
195 197
 			return nil, err
196 198
 		}
... ...
@@ -201,6 +213,8 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
201 201
 		ancestorFilter:       ancestorFilter,
202 202
 		images:               imagesFilter,
203 203
 		exitAllowed:          filtExited,
204
+		beforeContainer:      beforeContainer,
205
+		sinceContainer:       sinceContainer,
204 206
 		beforeFilter:         beforeContFilter,
205 207
 		sinceFilter:          sinceContFilter,
206 208
 		ContainerListOptions: config,
... ...
@@ -212,7 +226,8 @@ func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listConte
212 212
 // It also decides if the iteration should be stopped or not.
213 213
 func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
214 214
 	// 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 {
215
+	// FIXME remove the ctx.beforContainer part of the condition for 1.12 as --since and --before are deprecated
216
+	if !container.Running && !ctx.All && ctx.Limit <= 0 && ctx.beforeContainer == nil && ctx.sinceContainer == nil {
216 217
 		return excludeContainer
217 218
 	}
218 219
 
... ...
@@ -236,6 +251,21 @@ func includeContainerInList(container *container.Container, ctx *listContext) it
236 236
 		return excludeContainer
237 237
 	}
238 238
 
239
+	// FIXME remove this for 1.12 as --since and --before are deprecated
240
+	if ctx.beforeContainer != nil {
241
+		if container.ID == ctx.beforeContainer.ID {
242
+			ctx.beforeContainer = nil
243
+		}
244
+		return excludeContainer
245
+	}
246
+
247
+	// FIXME remove this for 1.12 as --since and --before are deprecated
248
+	if ctx.sinceContainer != nil {
249
+		if container.ID == ctx.sinceContainer.ID {
250
+			return stopIteration
251
+		}
252
+	}
253
+
239 254
 	// Do not include container if it's in the list before the filter container.
240 255
 	// Set the filter container to nil to include the rest of containers after this one.
241 256
 	if ctx.beforeFilter != nil {
... ...
@@ -47,8 +47,6 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
47 47
 	out, _ = dockerCmd(c, "ps")
48 48
 	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))
49 49
 
50
-	// from here all flag '-a' is ignored
51
-
52 50
 	// limit
53 51
 	out, _ = dockerCmd(c, "ps", "-n=2", "-a")
54 52
 	expected := []string{fourthID, thirdID}
... ...
@@ -60,56 +58,138 @@ func (s *DockerSuite) TestPsListContainersBase(c *check.C) {
60 60
 	// filter since
61 61
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-a")
62 62
 	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))
63
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter & ALL: Container list is not in the correct order: \n%s", out))
64 64
 
65 65
 	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))
66
+	expected = []string{fourthID, secondID}
67
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter: Container list is not in the correct order: \n%s", out))
67 68
 
68 69
 	// 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))
70
+	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-a")
71
+	expected = []string{thirdID, secondID, firstID}
72
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter & ALL: Container list is not in the correct order: \n%s", out))
72 73
 
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))
74
+	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID)
75
+	expected = []string{secondID, firstID}
76
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter: Container list is not in the correct order: \n%s", out))
75 77
 
76 78
 	// filter since & before
77 79
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-a")
78 80
 	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))
81
+	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))
80 82
 
81 83
 	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))
84
+	expected = []string{secondID}
85
+	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))
83 86
 
84 87
 	// filter since & limit
85 88
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-n=2", "-a")
86 89
 	expected = []string{fourthID, thirdID}
87 90
 
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))
91
+	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))
89 92
 
90 93
 	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))
94
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE filter, LIMIT: Container list is not in the correct order: \n%s", out))
92 95
 
93 96
 	// filter before & limit
94 97
 	out, _ = dockerCmd(c, "ps", "-f", "before="+fourthID, "-n=1", "-a")
95 98
 	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))
99
+	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))
97 100
 
98 101
 	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))
102
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE filter, LIMIT: Container list is not in the correct order: \n%s", out))
100 103
 
101 104
 	// filter since & filter before & limit
102 105
 	out, _ = dockerCmd(c, "ps", "-f", "since="+firstID, "-f", "before="+fourthID, "-n=1", "-a")
103 106
 	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))
107
+	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))
105 108
 
106 109
 	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))
110
+	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))
111
+
112
+}
113
+
114
+// FIXME remove this for 1.12 as --since and --before are deprecated
115
+func (s *DockerSuite) TestPsListContainersDeprecatedSinceAndBefore(c *check.C) {
116
+	out, _ := runSleepingContainer(c, "-d")
117
+	firstID := strings.TrimSpace(out)
118
+
119
+	out, _ = runSleepingContainer(c, "-d")
120
+	secondID := strings.TrimSpace(out)
121
+
122
+	// not long running
123
+	out, _ = dockerCmd(c, "run", "-d", "busybox", "true")
124
+	thirdID := strings.TrimSpace(out)
125
+
126
+	out, _ = runSleepingContainer(c, "-d")
127
+	fourthID := strings.TrimSpace(out)
128
+
129
+	// make sure the second is running
130
+	c.Assert(waitRun(secondID), checker.IsNil)
131
+
132
+	// make sure third one is not running
133
+	dockerCmd(c, "wait", thirdID)
134
+
135
+	// make sure the forth is running
136
+	c.Assert(waitRun(fourthID), checker.IsNil)
137
+
138
+	// since
139
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "-a")
140
+	expected := []string{fourthID, thirdID, secondID}
141
+	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))
142
+
143
+	out, _ = dockerCmd(c, "ps", "--since="+firstID)
144
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("SINCE: Container list is not in the correct order: %v \n%s", expected, out))
145
+
146
+	// before
147
+	out, _ = dockerCmd(c, "ps", "--before="+thirdID, "-a")
148
+	expected = []string{secondID, firstID}
149
+	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))
150
+
151
+	out, _ = dockerCmd(c, "ps", "--before="+thirdID)
152
+	c.Assert(assertContainerList(out, expected), checker.Equals, true, check.Commentf("BEFORE: Container list is not in the correct order: %v \n%s", expected, out))
153
+
154
+	// since & before
155
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID, "-a")
156
+	expected = []string{thirdID, secondID}
157
+	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))
158
+
159
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID)
160
+	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))
161
+
162
+	// since & limit
163
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "-n=2", "-a")
164
+	expected = []string{fourthID, thirdID}
165
+	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))
166
+
167
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "-n=2")
168
+	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))
169
+
170
+	// before & limit
171
+	out, _ = dockerCmd(c, "ps", "--before="+fourthID, "-n=1", "-a")
172
+	expected = []string{thirdID}
173
+	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))
174
+
175
+	out, _ = dockerCmd(c, "ps", "--before="+fourthID, "-n=1")
176
+	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))
177
+
178
+	// since & before & limit
179
+	out, _ = dockerCmd(c, "ps", "--since="+firstID, "--before="+fourthID, "-n=1", "-a")
180
+	expected = []string{thirdID}
181
+	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))
108 182
 
109 183
 }
110 184
 
111 185
 func assertContainerList(out string, expected []string) bool {
112 186
 	lines := strings.Split(strings.Trim(out, "\n "), "\n")
187
+	// FIXME remove this for 1.12 as --since and --before are deprecated
188
+	// This is here to remove potential Warning: lines (printed out with deprecated flags)
189
+	for i := 0; i < 2; i++ {
190
+		if strings.Contains(lines[0], "Warning:") {
191
+			lines = lines[1:]
192
+		}
193
+	}
194
+
113 195
 	if len(lines)-1 != len(expected) {
114 196
 		return false
115 197
 	}