Browse code

Test invalid filter and move validation on top

Signed-off-by: Vincent Demeester <vincent@sbr.pm>

Vincent Demeester authored on 2018/02/16 00:24:26
Showing 3 changed files
... ...
@@ -182,6 +182,10 @@ func (daemon *Daemon) filterByNameIDMatches(view container.View, ctx *listContex
182 182
 
183 183
 // reduceContainers parses the user's filtering options and generates the list of containers to return based on a reducer.
184 184
 func (daemon *Daemon) reduceContainers(config *types.ContainerListOptions, reducer containerReducer) ([]*types.Container, error) {
185
+	if err := config.Filters.Validate(acceptedPsFilterTags); err != nil {
186
+		return nil, err
187
+	}
188
+
185 189
 	var (
186 190
 		view       = daemon.containersReplica.Snapshot()
187 191
 		containers = []*types.Container{}
... ...
@@ -246,10 +250,6 @@ func (daemon *Daemon) reducePsContainer(container *container.Snapshot, ctx *list
246 246
 func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerListOptions) (*listContext, error) {
247 247
 	psFilters := config.Filters
248 248
 
249
-	if err := psFilters.Validate(acceptedPsFilterTags); err != nil {
250
-		return nil, err
251
-	}
252
-
253 249
 	var filtExited []int
254 250
 
255 251
 	err := psFilters.WalkValues("exited", func(value string) error {
256 252
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+package daemon
1
+
2
+import(
3
+	"testing"
4
+
5
+	"github.com/docker/docker/container"
6
+	"github.com/docker/docker/api/types"
7
+	"github.com/docker/docker/api/types/filters"
8
+	"github.com/gotestyourself/gotestyourself/assert"
9
+	is "github.com/gotestyourself/gotestyourself/assert/cmp"
10
+)
11
+
12
+func TestListInvalidFilter(t *testing.T) {
13
+	db, err := container.NewViewDB()
14
+	assert.Assert(t, err == nil)
15
+	d := &Daemon{
16
+		containersReplica: db,
17
+	}
18
+
19
+	f := filters.NewArgs(filters.Arg("invalid", "foo"))
20
+
21
+	_, err = d.Containers(&types.ContainerListOptions{
22
+		Filters: f,
23
+	})
24
+	assert.Assert(t, is.Error(err, "Invalid filter 'invalid'"))
25
+}
0 26
\ No newline at end of file
... ...
@@ -139,13 +139,6 @@ func assertContainerList(out string, expected []string) bool {
139 139
 	return true
140 140
 }
141 141
 
142
-// FIXME(vdemeester) Move this into a unit test in daemon package
143
-func (s *DockerSuite) TestPsListContainersInvalidFilterName(c *check.C) {
144
-	out, _, err := dockerCmdWithError("ps", "-f", "invalidFilter=test")
145
-	c.Assert(err, checker.NotNil)
146
-	c.Assert(out, checker.Contains, "Invalid filter")
147
-}
148
-
149 142
 func (s *DockerSuite) TestPsListContainersSize(c *check.C) {
150 143
 	// Problematic on Windows as it doesn't report the size correctly @swernli
151 144
 	testRequires(c, DaemonIsLinux)