Skip to content

Commit

Permalink
Merge pull request moby#9188 from somaopensource/8777-fix
Browse files Browse the repository at this point in the history
Fix for moby#8777 (continuing PR moby#9061)
  • Loading branch information
Jessie Frazelle committed Nov 22, 2014
2 parents fb15401 + 4deac03 commit d7626e9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
10 changes: 10 additions & 0 deletions api/client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const (
tarHeaderSize = 512
)

var (
acceptedImageFilterTags = map[string]struct{}{"dangling": {}}
)

func (cli *DockerCli) CmdHelp(args ...string) error {
if len(args) > 1 {
method, exists := cli.getMethod(args[:2]...)
Expand Down Expand Up @@ -1349,6 +1353,12 @@ func (cli *DockerCli) CmdImages(args ...string) error {
}
}

for name := range imageFilterArgs {
if _, ok := acceptedImageFilterTags[name]; !ok {
return fmt.Errorf("Invalid filter '%s'", name)
}
}

matchName := cmd.Arg(0)
// FIXME: --viz and --tree are deprecated. Remove them in a future version.
if *flViz || *flTree {
Expand Down
59 changes: 59 additions & 0 deletions integration-cli/docker_cli_images_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"fmt"
"os/exec"
"reflect"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -63,3 +66,59 @@ func TestImagesOrderedByCreationDate(t *testing.T) {

logDone("images - ordering by creation date")
}

func TestImagesErrorWithInvalidFilterNameTest(t *testing.T) {
imagesCmd := exec.Command(dockerBinary, "images", "-f", "FOO=123")
out, _, err := runCommandWithOutput(imagesCmd)
if !strings.Contains(out, "Invalid filter") {
t.Fatalf("error should occur when listing images with invalid filter name FOO, %s, %v", out, err)
}

logDone("images - invalid filter name check working")
}

func TestImagesFilterWhiteSpaceTrimmingAndLowerCasingWorking(t *testing.T) {
imageName := "images_filter_test"
defer deleteAllContainers()
defer deleteImages(imageName)
buildImage(imageName,
`FROM scratch
RUN touch /test/foo
RUN touch /test/bar
RUN touch /test/baz`, true)

filters := []string{
"dangling=true",
"Dangling=true",
" dangling=true",
"dangling=true ",
"dangling = true",
}

imageListings := make([][]string, 5, 5)
for idx, filter := range filters {
cmd := exec.Command(dockerBinary, "images", "-f", filter)
out, _, err := runCommandWithOutput(cmd)
if err != nil {
t.Fatal(err)
}
listing := strings.Split(out, "\n")
sort.Strings(listing)
imageListings[idx] = listing
}

for idx, listing := range imageListings {
if idx < 4 && !reflect.DeepEqual(listing, imageListings[idx+1]) {
for idx, errListing := range imageListings {
fmt.Printf("out %d", idx)
for _, image := range errListing {
fmt.Print(image)
}
fmt.Print("")
}
t.Fatalf("All output must be the same")
}
}

logDone("images - white space trimming and lower casing")
}
4 changes: 3 additions & 1 deletion pkg/parsers/filters/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func ParseFlag(arg string, prev Args) (Args, error) {
}

f := strings.SplitN(arg, "=", 2)
filters[f[0]] = append(filters[f[0]], f[1])
name := strings.ToLower(strings.TrimSpace(f[0]))
value := strings.TrimSpace(f[1])
filters[name] = append(filters[name], value)

return filters, nil
}
Expand Down

0 comments on commit d7626e9

Please sign in to comment.