Merge pull request #17692 from vdemeester/images-format

Add --format support to images command
Upstream-commit: 42460b6772323552bfb58ae703124145ffd8a56b
Component: engine
This commit is contained in:
Alexander Morozov
2015-12-21 09:57:29 -08:00
17 changed files with 1126 additions and 479 deletions
@@ -2,6 +2,9 @@ package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
@@ -48,17 +51,17 @@ func (s *DockerSuite) TestImagesOrderedByCreationDate(c *check.C) {
testRequires(c, DaemonIsLinux)
id1, err := buildImage("order:test_a",
`FROM scratch
MAINTAINER dockerio1`, true)
MAINTAINER dockerio1`, true)
c.Assert(err, checker.IsNil)
time.Sleep(1 * time.Second)
id2, err := buildImage("order:test_c",
`FROM scratch
MAINTAINER dockerio2`, true)
MAINTAINER dockerio2`, true)
c.Assert(err, checker.IsNil)
time.Sleep(1 * time.Second)
id3, err := buildImage("order:test_b",
`FROM scratch
MAINTAINER dockerio3`, true)
MAINTAINER dockerio3`, true)
c.Assert(err, checker.IsNil)
out, _ := dockerCmd(c, "images", "-q", "--no-trunc")
@@ -81,17 +84,17 @@ func (s *DockerSuite) TestImagesFilterLabelMatch(c *check.C) {
imageName3 := "images_filter_test3"
image1ID, err := buildImage(imageName1,
`FROM scratch
LABEL match me`, true)
LABEL match me`, true)
c.Assert(err, check.IsNil)
image2ID, err := buildImage(imageName2,
`FROM scratch
LABEL match="me too"`, true)
LABEL match="me too"`, true)
c.Assert(err, check.IsNil)
image3ID, err := buildImage(imageName3,
`FROM scratch
LABEL nomatch me`, true)
LABEL nomatch me`, true)
c.Assert(err, check.IsNil)
out, _ := dockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match")
@@ -123,9 +126,9 @@ func (s *DockerSuite) TestImagesFilterSpaceTrimCase(c *check.C) {
imageName := "images_filter_test"
buildImage(imageName,
`FROM scratch
RUN touch /test/foo
RUN touch /test/bar
RUN touch /test/baz`, true)
RUN touch /test/foo
RUN touch /test/bar
RUN touch /test/baz`, true)
filters := []string{
"dangling=true",
@@ -233,3 +236,46 @@ func (s *DockerSuite) TestImagesFilterNameWithPort(c *check.C) {
out, _ = dockerCmd(c, "images", tag+":no-such-tag")
c.Assert(out, checker.Not(checker.Contains), tag)
}
func (s *DockerSuite) TestImagesFormat(c *check.C) {
// testRequires(c, DaemonIsLinux)
tag := "myimage"
dockerCmd(c, "tag", "busybox", tag+":v1")
dockerCmd(c, "tag", "busybox", tag+":v2")
out, _ := dockerCmd(c, "images", "--format", "{{.Repository}}", tag)
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
expected := []string{"myimage", "myimage"}
var names []string
for _, l := range lines {
names = append(names, l)
}
c.Assert(expected, checker.DeepEquals, names, check.Commentf("Expected array with truncated names: %v, got: %v", expected, names))
}
// ImagesDefaultFormatAndQuiet
func (s *DockerSuite) TestImagesFormatDefaultFormat(c *check.C) {
testRequires(c, DaemonIsLinux)
// create container 1
out, _ := dockerCmd(c, "run", "-d", "busybox", "true")
containerID1 := strings.TrimSpace(out)
// tag as foobox
out, _ = dockerCmd(c, "commit", containerID1, "myimage")
imageID := stringid.TruncateID(strings.TrimSpace(out))
config := `{
"imagesFormat": "{{ .ID }} default"
}`
d, err := ioutil.TempDir("", "integration-cli-")
c.Assert(err, checker.IsNil)
defer os.RemoveAll(d)
err = ioutil.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0644)
c.Assert(err, checker.IsNil)
out, _ = dockerCmd(c, "--config", d, "images", "-q", "myimage")
c.Assert(out, checker.Equals, imageID+"\n", check.Commentf("Expected to print only the image id, got %v\n", out))
}
@@ -568,7 +568,7 @@ func (s *DockerSuite) TestPsFormatHeaders(c *check.C) {
func (s *DockerSuite) TestPsDefaultFormatAndQuiet(c *check.C) {
testRequires(c, DaemonIsLinux)
config := `{
"psFormat": "{{ .ID }} default"
"psFormat": "default {{ .ID }}"
}`
d, err := ioutil.TempDir("", "integration-cli-")
c.Assert(err, checker.IsNil)