Browse code

Fix for #8777

Now filter name is trimmed and lowercased before evaluation for case
insensitive and whitespace trimemd check.

Signed-off-by: Oh Jinkyun <tintypemolly@gmail.com>

Oh Jinkyun authored on 2014/11/03 20:11:29
Showing 3 changed files
... ...
@@ -47,6 +47,10 @@ const (
47 47
 	tarHeaderSize = 512
48 48
 )
49 49
 
50
+var (
51
+	acceptedImageFilterTags = map[string]struct{}{"dangling": {}}
52
+)
53
+
50 54
 func (cli *DockerCli) CmdHelp(args ...string) error {
51 55
 	if len(args) > 1 {
52 56
 		method, exists := cli.getMethod(args[:2]...)
... ...
@@ -1336,6 +1340,12 @@ func (cli *DockerCli) CmdImages(args ...string) error {
1336 1336
 		}
1337 1337
 	}
1338 1338
 
1339
+	for name := range imageFilterArgs {
1340
+		if _, ok := acceptedImageFilterTags[name]; !ok {
1341
+			return fmt.Errorf("Invalid filter '%s'", name)
1342
+		}
1343
+	}
1344
+
1339 1345
 	matchName := cmd.Arg(0)
1340 1346
 	// FIXME: --viz and --tree are deprecated. Remove them in a future version.
1341 1347
 	if *flViz || *flTree {
... ...
@@ -1,7 +1,10 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"fmt"
4 5
 	"os/exec"
6
+	"reflect"
7
+	"sort"
5 8
 	"strings"
6 9
 	"testing"
7 10
 	"time"
... ...
@@ -63,3 +66,59 @@ func TestImagesOrderedByCreationDate(t *testing.T) {
63 63
 
64 64
 	logDone("images - ordering by creation date")
65 65
 }
66
+
67
+func TestImagesErrorWithInvalidFilterNameTest(t *testing.T) {
68
+	imagesCmd := exec.Command(dockerBinary, "images", "-f", "FOO=123")
69
+	out, _, err := runCommandWithOutput(imagesCmd)
70
+	if !strings.Contains(out, "Invalid filter") {
71
+		t.Fatalf("error should occur when listing images with invalid filter name FOO, %s, %v", out, err)
72
+	}
73
+
74
+	logDone("images - invalid filter name check working")
75
+}
76
+
77
+func TestImagesFilterWhiteSpaceTrimmingAndLowerCasingWorking(t *testing.T) {
78
+	imageName := "images_filter_test"
79
+	defer deleteAllContainers()
80
+	defer deleteImages(imageName)
81
+	buildImage(imageName,
82
+		`FROM scratch
83
+		 RUN touch /test/foo
84
+		 RUN touch /test/bar
85
+		 RUN touch /test/baz`, true)
86
+
87
+	filters := []string{
88
+		"dangling=true",
89
+		"Dangling=true",
90
+		" dangling=true",
91
+		"dangling=true ",
92
+		"dangling = true",
93
+	}
94
+
95
+	imageListings := make([][]string, 5, 5)
96
+	for idx, filter := range filters {
97
+		cmd := exec.Command(dockerBinary, "images", "-f", filter)
98
+		out, _, err := runCommandWithOutput(cmd)
99
+		if err != nil {
100
+			t.Fatal(err)
101
+		}
102
+		listing := strings.Split(out, "\n")
103
+		sort.Strings(listing)
104
+		imageListings[idx] = listing
105
+	}
106
+
107
+	for idx, listing := range imageListings {
108
+		if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
109
+			for idx, errListing := range imageListings {
110
+				fmt.Printf("out %d", idx)
111
+				for _, image := range errListing {
112
+					fmt.Print(image)
113
+				}
114
+				fmt.Print("")
115
+			}
116
+			t.Fatalf("All output must be the same")
117
+		}
118
+	}
119
+
120
+	logDone("images - white space trimming and lower casing")
121
+}
... ...
@@ -29,7 +29,9 @@ func ParseFlag(arg string, prev Args) (Args, error) {
29 29
 	}
30 30
 
31 31
 	f := strings.SplitN(arg, "=", 2)
32
-	filters[f[0]] = append(filters[f[0]], f[1])
32
+	name := strings.ToLower(strings.TrimSpace(f[0]))
33
+	value := strings.TrimSpace(f[1])
34
+	filters[name] = append(filters[name], value)
33 35
 
34 36
 	return filters, nil
35 37
 }