2014-02-25 16:17:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-07-14 18:02:38 +00:00
|
|
|
"context"
|
2014-11-03 11:11:29 +00:00
|
|
|
"fmt"
|
2015-12-18 13:03:41 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-11-03 11:11:29 +00:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2014-02-25 16:17:48 +00:00
|
|
|
"strings"
|
2019-09-09 21:06:12 +00:00
|
|
|
"testing"
|
2014-06-23 18:19:52 +00:00
|
|
|
"time"
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli"
|
2017-03-23 17:35:22 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build"
|
2015-03-24 11:25:26 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/icmd"
|
2014-02-25 16:17:48 +00:00
|
|
|
)
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
type DockerCLIImagesSuite struct {
|
|
|
|
ds *DockerSuite
|
|
|
|
}
|
|
|
|
|
2023-07-14 18:02:38 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TearDownTest(ctx context.Context, c *testing.T) {
|
|
|
|
s.ds.TearDownTest(ctx, c)
|
2022-06-16 21:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIImagesSuite) OnTimeout(c *testing.T) {
|
|
|
|
s.ds.OnTimeout(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureImageIsListed(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
imagesOut := cli.DockerCmd(c, "images").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(imagesOut, "busybox"))
|
2014-02-25 16:17:48 +00:00
|
|
|
}
|
2014-04-04 00:57:41 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureImageWithTagIsListed(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const name = "imagewithtag"
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", name+":v1")
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", name+":v1v1")
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", name+":v2")
|
2015-08-08 13:58:48 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
imagesOut := cli.DockerCmd(c, "images", name+":v1").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(imagesOut, name))
|
|
|
|
assert.Assert(c, strings.Contains(imagesOut, "v1"))
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(imagesOut, "v2"))
|
|
|
|
assert.Assert(c, !strings.Contains(imagesOut, "v1v1"))
|
2023-07-27 20:26:13 +00:00
|
|
|
imagesOut = cli.DockerCmd(c, "images", name).Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(imagesOut, name))
|
|
|
|
assert.Assert(c, strings.Contains(imagesOut, "v1"))
|
|
|
|
assert.Assert(c, strings.Contains(imagesOut, "v1v1"))
|
|
|
|
assert.Assert(c, strings.Contains(imagesOut, "v2"))
|
2015-08-08 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureImageWithBadTagIsNotListed(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
imagesOut := cli.DockerCmd(c, "images", "busybox:nonexistent").Stdout()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(imagesOut, "busybox"))
|
2015-08-08 13:58:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesOrderedByCreationDate(c *testing.T) {
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "order:test_a", build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
MAINTAINER dockerio1`))
|
|
|
|
id1 := getIDByName(c, "order:test_a")
|
2015-08-11 07:41:11 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "order:test_c", build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
MAINTAINER dockerio2`))
|
|
|
|
id2 := getIDByName(c, "order:test_c")
|
2015-08-11 07:41:11 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "order:test_b", build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
MAINTAINER dockerio3`))
|
|
|
|
id3 := getIDByName(c, "order:test_b")
|
2014-06-23 18:19:52 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "-q", "--no-trunc").Stdout()
|
2014-06-23 18:19:52 +00:00
|
|
|
imgs := strings.Split(out, "\n")
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, imgs[0], id3, fmt.Sprintf("First image must be %s, got %s", id3, imgs[0]))
|
|
|
|
assert.Equal(c, imgs[1], id2, fmt.Sprintf("First image must be %s, got %s", id2, imgs[1]))
|
|
|
|
assert.Equal(c, imgs[2], id1, fmt.Sprintf("First image must be %s, got %s", id1, imgs[2]))
|
2014-06-23 18:19:52 +00:00
|
|
|
}
|
2014-11-03 11:11:29 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesErrorWithInvalidFilterNameTest(c *testing.T) {
|
2015-07-27 18:13:25 +00:00
|
|
|
out, _, err := dockerCmdWithError("images", "-f", "FOO=123")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "")
|
2022-03-02 17:56:06 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, "invalid filter"))
|
2014-11-03 11:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFilterLabelMatch(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const imageName1 = "images_filter_test1"
|
|
|
|
const imageName2 = "images_filter_test2"
|
|
|
|
const imageName3 = "images_filter_test3"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, imageName1, build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL match me`))
|
|
|
|
image1ID := getIDByName(c, imageName1)
|
2015-01-07 00:04:10 +00:00
|
|
|
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, imageName2, build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL match="me too"`))
|
|
|
|
image2ID := getIDByName(c, imageName2)
|
2015-01-07 00:04:10 +00:00
|
|
|
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, imageName3, build.WithDockerfile(`FROM busybox
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL nomatch me`))
|
|
|
|
image3ID := getIDByName(c, imageName3)
|
2015-01-07 00:04:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match").Stdout()
|
2015-01-07 00:04:10 +00:00
|
|
|
out = strings.TrimSpace(out)
|
2019-09-09 21:35:23 +00:00
|
|
|
assert.Assert(c, is.Regexp(fmt.Sprintf("^[\\s\\w:]*%s[\\s\\w:]*$", image1ID), out))
|
rm-gocheck: Matches -> cmp.Regexp
sed -E -i '0,/^import "github\.com/ s/^(import "github\.com.*)/\1\nimport "gotest.tools\/assert\/cmp")/' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
sed -E -i '0,/^\t+"github\.com/ s/(^\t+"github\.com.*)/\1\n"gotest.tools\/assert\/cmp"/' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
sed -E -i 's#\bassert\.Assert\(c, (.*), checker\.Matches, (.*)\)$#assert.Assert(c, eg_matches(is.Regexp, \1, \2))#g' \
-- "integration-cli/docker_cli_images_test.go" "integration-cli/docker_api_containers_test.go" \
&& \
sed -E -i 's#\bassert\.Assert\(c, (.*), checker\.Matches, (.*)\)$#assert.Assert(c, eg_matches(cmp.Regexp, \1, \2))#g' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
go get -d golang.org/x/tools/cmd/eg && dir=$(go env GOPATH)/src/golang.org/x/tools && git -C "$dir" fetch https://github.com/tiborvass/tools handle-variadic && git -C "$dir" checkout 61a94b82347c29b3289e83190aa3dda74d47abbb && go install golang.org/x/tools/cmd/eg \
&& \
/bin/echo -e 'package main\nvar eg_matches func(func(cmp.RegexOrPattern, string) cmp.Comparison, interface{}, string, ...interface{}) bool' > ./integration-cli/eg_helper.go \
&& \
goimports -w ./integration-cli \
&& \
eg -w -t template.matches.go -- ./integration-cli \
&& \
rm -f ./integration-cli/eg_helper.go \
&& \
go run rm-gocheck.go redress '\bassert\.Assert\b.*(\(|,)\s*$' \
"integration-cli/docker_api_containers_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_links_test.go"
Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:07:08 +00:00
|
|
|
|
2019-09-09 21:35:23 +00:00
|
|
|
assert.Assert(c, is.Regexp(fmt.Sprintf("^[\\s\\w:]*%s[\\s\\w:]*$", image2ID), out))
|
rm-gocheck: Matches -> cmp.Regexp
sed -E -i '0,/^import "github\.com/ s/^(import "github\.com.*)/\1\nimport "gotest.tools\/assert\/cmp")/' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
sed -E -i '0,/^\t+"github\.com/ s/(^\t+"github\.com.*)/\1\n"gotest.tools\/assert\/cmp"/' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
sed -E -i 's#\bassert\.Assert\(c, (.*), checker\.Matches, (.*)\)$#assert.Assert(c, eg_matches(is.Regexp, \1, \2))#g' \
-- "integration-cli/docker_cli_images_test.go" "integration-cli/docker_api_containers_test.go" \
&& \
sed -E -i 's#\bassert\.Assert\(c, (.*), checker\.Matches, (.*)\)$#assert.Assert(c, eg_matches(cmp.Regexp, \1, \2))#g' \
-- "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_links_test.go" \
&& \
go get -d golang.org/x/tools/cmd/eg && dir=$(go env GOPATH)/src/golang.org/x/tools && git -C "$dir" fetch https://github.com/tiborvass/tools handle-variadic && git -C "$dir" checkout 61a94b82347c29b3289e83190aa3dda74d47abbb && go install golang.org/x/tools/cmd/eg \
&& \
/bin/echo -e 'package main\nvar eg_matches func(func(cmp.RegexOrPattern, string) cmp.Comparison, interface{}, string, ...interface{}) bool' > ./integration-cli/eg_helper.go \
&& \
goimports -w ./integration-cli \
&& \
eg -w -t template.matches.go -- ./integration-cli \
&& \
rm -f ./integration-cli/eg_helper.go \
&& \
go run rm-gocheck.go redress '\bassert\.Assert\b.*(\(|,)\s*$' \
"integration-cli/docker_api_containers_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_links_test.go"
Signed-off-by: Tibor Vass <tibor@docker.com>
2019-09-09 21:07:08 +00:00
|
|
|
|
2019-09-09 21:35:23 +00:00
|
|
|
assert.Assert(c, !is.Regexp(fmt.Sprintf("^[\\s\\w:]*%s[\\s\\w:]*$", image3ID), out)().Success())
|
2015-01-07 00:04:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=match=me too").Stdout()
|
2015-01-07 00:04:10 +00:00
|
|
|
out = strings.TrimSpace(out)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, image2ID)
|
2015-08-22 12:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Regression : #15659
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestCommitWithFilterLabel(c *testing.T) {
|
2015-08-22 12:06:48 +00:00
|
|
|
// Create a container
|
2023-07-27 20:26:13 +00:00
|
|
|
cli.DockerCmd(c, "run", "--name", "bar", "busybox", "/bin/sh")
|
2015-08-22 12:06:48 +00:00
|
|
|
// Commit with labels "using changes"
|
2023-07-27 20:26:13 +00:00
|
|
|
imageID := cli.DockerCmd(c, "commit", "-c", "LABEL foo.version=1.0.0-1", "-c", "LABEL foo.name=bar", "-c", "LABEL foo.author=starlord", "bar", "bar:1.0.0-1").Stdout()
|
|
|
|
imageID = strings.TrimSpace(imageID)
|
2015-08-22 12:06:48 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "--no-trunc", "-q", "-f", "label=foo.version=1.0.0-1").Stdout()
|
2015-08-22 12:06:48 +00:00
|
|
|
out = strings.TrimSpace(out)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, imageID)
|
2015-01-07 00:04:10 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFilterSinceAndBefore(c *testing.T) {
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "image:1", build.WithDockerfile(`FROM `+minimalBaseImage()+`
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL number=1`))
|
|
|
|
imageID1 := getIDByName(c, "image:1")
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "image:2", build.WithDockerfile(`FROM `+minimalBaseImage()+`
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL number=2`))
|
|
|
|
imageID2 := getIDByName(c, "image:2")
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, "image:3", build.WithDockerfile(`FROM `+minimalBaseImage()+`
|
2017-01-16 10:30:14 +00:00
|
|
|
LABEL number=3`))
|
|
|
|
imageID3 := getIDByName(c, "image:3")
|
2016-05-25 11:49:10 +00:00
|
|
|
|
|
|
|
expected := []string{imageID3, imageID2}
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "-f", "since=image:1", "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "since="+imageID1, "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
|
|
|
expected = []string{imageID3}
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "since=image:2", "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "since="+imageID2, "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("SINCE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
|
|
|
expected = []string{imageID2, imageID1}
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "before=image:3", "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "before="+imageID3, "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
|
|
|
expected = []string{imageID1}
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "before=image:2", "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-f", "before="+imageID2, "image").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Equal(c, assertImageList(out, expected), true, fmt.Sprintf("BEFORE filter: Image list is not in the correct order: %v\n%s", expected, out))
|
2016-05-25 11:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertImageList(out string, expected []string) bool {
|
|
|
|
lines := strings.Split(strings.Trim(out, "\n "), "\n")
|
|
|
|
|
|
|
|
if len(lines)-1 != len(expected) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
imageIDIndex := strings.Index(lines[0], "IMAGE ID")
|
|
|
|
for i := 0; i < len(expected); i++ {
|
|
|
|
imageID := lines[i+1][imageIDIndex : imageIDIndex+12]
|
|
|
|
found := false
|
|
|
|
for _, e := range expected {
|
|
|
|
if imageID == e[7:19] {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:30:14 +00:00
|
|
|
// FIXME(vdemeester) should be a unit test on `docker image ls`
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFilterSpaceTrimCase(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const imageName = "images_filter_test"
|
2017-01-16 10:30:14 +00:00
|
|
|
// Build a image and fail to build so that we have dangling images ?
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImage(imageName, build.WithDockerfile(`FROM busybox
|
2015-12-18 13:03:41 +00:00
|
|
|
RUN touch /test/foo
|
|
|
|
RUN touch /test/bar
|
2017-01-16 10:30:14 +00:00
|
|
|
RUN touch /test/baz`)).Assert(c, icmd.Expected{
|
|
|
|
ExitCode: 1,
|
|
|
|
})
|
2014-11-03 11:11:29 +00:00
|
|
|
|
|
|
|
filters := []string{
|
|
|
|
"dangling=true",
|
|
|
|
"Dangling=true",
|
|
|
|
" dangling=true",
|
|
|
|
"dangling=true ",
|
|
|
|
"dangling = true",
|
|
|
|
}
|
|
|
|
|
2019-08-05 15:07:43 +00:00
|
|
|
imageListings := make([][]string, 5)
|
2014-11-03 11:11:29 +00:00
|
|
|
for idx, filter := range filters {
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "-q", "-f", filter).Stdout()
|
2014-11-03 11:11:29 +00:00
|
|
|
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 {
|
2016-08-23 00:59:45 +00:00
|
|
|
fmt.Printf("out %d\n", idx)
|
2014-11-03 11:11:29 +00:00
|
|
|
for _, image := range errListing {
|
|
|
|
fmt.Print(image)
|
|
|
|
}
|
|
|
|
fmt.Print("")
|
|
|
|
}
|
2015-04-18 16:46:47 +00:00
|
|
|
c.Fatalf("All output must be the same")
|
2014-11-03 11:11:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureDanglingImageOnlyListedOnce(c *testing.T) {
|
2015-08-28 17:36:42 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-03-18 14:11:01 +00:00
|
|
|
// create container 1
|
2023-07-27 20:26:13 +00:00
|
|
|
containerID1 := cli.DockerCmd(c, "run", "-d", "busybox", "true").Stdout()
|
|
|
|
containerID1 = strings.TrimSpace(containerID1)
|
2015-03-18 14:11:01 +00:00
|
|
|
|
|
|
|
// tag as foobox
|
2023-07-27 20:26:13 +00:00
|
|
|
imageID := cli.DockerCmd(c, "commit", containerID1, "foobox").Stdout()
|
|
|
|
imageID = stringid.TruncateID(strings.TrimSpace(imageID))
|
2015-03-18 14:11:01 +00:00
|
|
|
|
|
|
|
// overwrite the tag, making the previous image dangling
|
2023-07-27 20:26:13 +00:00
|
|
|
cli.DockerCmd(c, "tag", "busybox", "foobox")
|
2015-03-18 14:11:01 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "-q", "-f", "dangling=true").Stdout()
|
2015-12-13 16:00:39 +00:00
|
|
|
// Expect one dangling image
|
2019-09-09 21:05:56 +00:00
|
|
|
assert.Equal(c, strings.Count(out, imageID), 1)
|
2016-01-14 06:58:31 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", "-q", "-f", "dangling=false").Stdout()
|
2019-11-27 14:36:45 +00:00
|
|
|
// dangling=false would not include dangling images
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(out, imageID))
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images").Stdout()
|
2019-11-27 14:36:45 +00:00
|
|
|
// docker images still include dangling images
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, imageID))
|
2015-03-18 14:11:01 +00:00
|
|
|
}
|
2015-10-16 14:09:32 +00:00
|
|
|
|
2017-01-16 10:30:14 +00:00
|
|
|
// FIXME(vdemeester) should be a unit test for `docker image ls`
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesWithIncorrectFilter(c *testing.T) {
|
2015-10-16 14:09:32 +00:00
|
|
|
out, _, err := dockerCmdWithError("images", "-f", "dangling=invalid")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.ErrorContains(c, err, "")
|
2022-03-02 17:56:06 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, "invalid filter"))
|
2015-10-16 14:09:32 +00:00
|
|
|
}
|
2015-10-09 17:13:45 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureOnlyHeadsImagesShown(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const dockerfile = `
|
2016-08-30 22:20:55 +00:00
|
|
|
FROM busybox
|
2015-10-09 17:13:45 +00:00
|
|
|
MAINTAINER docker
|
|
|
|
ENV foo bar`
|
2023-07-27 20:26:13 +00:00
|
|
|
const name = "scratch-image"
|
2017-03-23 17:35:22 +00:00
|
|
|
result := buildImage(name, build.WithDockerfile(dockerfile))
|
2017-01-16 10:30:14 +00:00
|
|
|
result.Assert(c, icmd.Success)
|
|
|
|
id := getIDByName(c, name)
|
2015-10-09 17:13:45 +00:00
|
|
|
|
2015-10-11 07:32:52 +00:00
|
|
|
// this is just the output of docker build
|
|
|
|
// we're interested in getting the image id of the MAINTAINER instruction
|
|
|
|
// and that's located at output, line 5, from 7 to end
|
2017-01-16 10:30:14 +00:00
|
|
|
split := strings.Split(result.Combined(), "\n")
|
2015-10-09 17:13:45 +00:00
|
|
|
intermediate := strings.TrimSpace(split[5][7:])
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images").Stdout()
|
2015-10-26 18:47:21 +00:00
|
|
|
// images shouldn't show non-heads images
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(out, intermediate))
|
2015-10-26 18:47:21 +00:00
|
|
|
// images should contain final built images
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, stringid.TruncateID(id)))
|
2015-10-09 17:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureImagesFromScratchShown(c *testing.T) {
|
2016-08-30 22:20:55 +00:00
|
|
|
testRequires(c, DaemonIsLinux) // Windows does not support FROM scratch
|
2023-07-27 20:26:13 +00:00
|
|
|
const dockerfile = `
|
2015-10-09 17:13:45 +00:00
|
|
|
FROM scratch
|
|
|
|
MAINTAINER docker`
|
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
const name = "scratch-image"
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(dockerfile))
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, name)
|
2015-10-09 17:13:45 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images").Stdout()
|
2015-10-26 18:47:21 +00:00
|
|
|
// images should contain images built from scratch
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, stringid.TruncateID(id)))
|
2015-10-09 17:13:45 +00:00
|
|
|
}
|
2015-11-25 02:43:14 +00:00
|
|
|
|
2016-08-30 22:20:55 +00:00
|
|
|
// For W2W - equivalent to TestImagesEnsureImagesFromScratchShown but Windows
|
|
|
|
// doesn't support from scratch
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesEnsureImagesFromBusyboxShown(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const dockerfile = `
|
2016-08-30 22:20:55 +00:00
|
|
|
FROM busybox
|
|
|
|
MAINTAINER docker`
|
2023-07-27 20:26:13 +00:00
|
|
|
const name = "busybox-image"
|
2016-08-30 22:20:55 +00:00
|
|
|
|
2017-03-23 17:35:22 +00:00
|
|
|
buildImageSuccessfully(c, name, build.WithDockerfile(dockerfile))
|
2017-01-16 10:30:14 +00:00
|
|
|
id := getIDByName(c, name)
|
2016-08-30 22:20:55 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images").Stdout()
|
2016-08-30 22:20:55 +00:00
|
|
|
// images should contain images built from busybox
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, stringid.TruncateID(id)))
|
2016-08-30 22:20:55 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 02:43:14 +00:00
|
|
|
// #18181
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFilterNameWithPort(c *testing.T) {
|
2023-07-27 20:26:13 +00:00
|
|
|
const tag = "a.b.c.d:5000/hello"
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", tag)
|
|
|
|
out := cli.DockerCmd(c, "images", tag).Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, tag))
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", tag+":latest").Stdout()
|
2019-09-09 21:08:22 +00:00
|
|
|
assert.Assert(c, strings.Contains(out, tag))
|
2023-07-27 20:26:13 +00:00
|
|
|
out = cli.DockerCmd(c, "images", tag+":no-such-tag").Stdout()
|
2019-09-09 21:07:46 +00:00
|
|
|
assert.Assert(c, !strings.Contains(out, tag))
|
2015-11-25 02:43:14 +00:00
|
|
|
}
|
2015-12-18 13:03:41 +00:00
|
|
|
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFormat(c *testing.T) {
|
2015-12-18 13:03:41 +00:00
|
|
|
// testRequires(c, DaemonIsLinux)
|
2023-07-27 20:26:13 +00:00
|
|
|
const imageName = "myimage"
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", imageName+":v1")
|
|
|
|
cli.DockerCmd(c, "tag", "busybox", imageName+":v2")
|
2015-12-18 13:03:41 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "images", "--format", "{{.Repository}}", imageName).Stdout()
|
2019-08-05 15:54:15 +00:00
|
|
|
lines := strings.Split(strings.TrimSpace(out), "\n")
|
2015-12-18 13:03:41 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
expected := []string{imageName, imageName}
|
2015-12-18 13:03:41 +00:00
|
|
|
var names []string
|
2016-10-13 16:34:19 +00:00
|
|
|
names = append(names, lines...)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Assert(c, is.DeepEqual(names, expected), "Expected array with truncated names: %v, got: %v", expected, names)
|
2015-12-18 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ImagesDefaultFormatAndQuiet
|
2022-06-16 21:32:10 +00:00
|
|
|
func (s *DockerCLIImagesSuite) TestImagesFormatDefaultFormat(c *testing.T) {
|
2015-12-18 13:03:41 +00:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
|
|
|
|
// create container 1
|
2023-07-27 20:26:13 +00:00
|
|
|
containerID1 := cli.DockerCmd(c, "run", "-d", "busybox", "true").Stdout()
|
|
|
|
containerID1 = strings.TrimSpace(containerID1)
|
2015-12-18 13:03:41 +00:00
|
|
|
|
|
|
|
// tag as foobox
|
2023-07-27 20:26:13 +00:00
|
|
|
imageID := cli.DockerCmd(c, "commit", containerID1, "myimage").Stdout()
|
|
|
|
imageID = stringid.TruncateID(strings.TrimSpace(imageID))
|
2015-12-18 13:03:41 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
const config = `{
|
2015-12-18 13:03:41 +00:00
|
|
|
"imagesFormat": "{{ .ID }} default"
|
|
|
|
}`
|
2021-08-24 10:10:50 +00:00
|
|
|
d, err := os.MkdirTemp("", "integration-cli-")
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-12-18 13:03:41 +00:00
|
|
|
defer os.RemoveAll(d)
|
|
|
|
|
2022-01-20 12:43:42 +00:00
|
|
|
err = os.WriteFile(filepath.Join(d, "config.json"), []byte(config), 0o644)
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.NilError(c, err)
|
2015-12-18 13:03:41 +00:00
|
|
|
|
2023-07-27 20:26:13 +00:00
|
|
|
out := cli.DockerCmd(c, "--config", d, "images", "-q", "myimage").Stdout()
|
2019-04-04 13:23:19 +00:00
|
|
|
assert.Equal(c, out, imageID+"\n", "Expected to print only the image id, got %v\n", out)
|
2015-12-18 13:03:41 +00:00
|
|
|
}
|