1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #11464 from ncdc/fix-duplicate-dangling-images

Fix duplicate display of dangling images
This commit is contained in:
Jessie Frazelle 2015-03-18 13:53:33 -07:00
commit 29f809233e
2 changed files with 63 additions and 17 deletions

View file

@ -1518,29 +1518,32 @@ func (cli *DockerCli) CmdImages(args ...string) error {
outID = common.TruncateID(outID) outID = common.TruncateID(outID)
} }
// Tags referring to this image ID. repoTags := out.GetList("RepoTags")
for _, repotag := range out.GetList("RepoTags") { repoDigests := out.GetList("RepoDigests")
repo, tag := parsers.ParseRepositoryTag(repotag)
if !*quiet { if len(repoTags) == 1 && repoTags[0] == "<none>:<none>" && len(repoDigests) == 1 && repoDigests[0] == "<none>@<none>" {
if *showDigests { // dangling image - clear out either repoTags or repoDigsts so we only show it once below
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, "<none>", outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize")))) repoDigests = []string{}
} else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize"))))
}
} else {
fmt.Fprintln(w, outID)
}
} }
// Digests referring to this image ID. // combine the tags and digests lists
for _, repoDigest := range out.GetList("RepoDigests") { tagsAndDigests := append(repoTags, repoDigests...)
repo, digest := parsers.ParseRepositoryTag(repoDigest) for _, repoAndRef := range tagsAndDigests {
repo, ref := parsers.ParseRepositoryTag(repoAndRef)
// default tag and digest to none - if there's a value, it'll be set below
tag := "<none>"
digest := "<none>"
if utils.DigestReference(ref) {
digest = ref
} else {
tag = ref
}
if !*quiet { if !*quiet {
if *showDigests { if *showDigests {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, "<none>", digest, outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize")))) fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s ago\t%s\n", repo, tag, digest, outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize"))))
} else { } else {
fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, "<none>", outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize")))) fmt.Fprintf(w, "%s\t%s\t%s\t%s ago\t%s\n", repo, tag, outID, units.HumanDuration(time.Now().UTC().Sub(time.Unix(out.GetInt64("Created"), 0))), units.HumanSize(float64(out.GetInt64("VirtualSize"))))
} }
} else { } else {
fmt.Fprintln(w, outID) fmt.Fprintln(w, outID)

View file

@ -8,6 +8,8 @@ import (
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/docker/docker/pkg/common"
) )
func TestImagesEnsureImageIsListed(t *testing.T) { func TestImagesEnsureImageIsListed(t *testing.T) {
@ -176,3 +178,44 @@ func TestImagesFilterWhiteSpaceTrimmingAndLowerCasingWorking(t *testing.T) {
logDone("images - white space trimming and lower casing") logDone("images - white space trimming and lower casing")
} }
func TestImagesEnsureDanglingImageOnlyListedOnce(t *testing.T) {
defer deleteAllContainers()
// create container 1
c := exec.Command(dockerBinary, "run", "-d", "busybox", "true")
out, _, err := runCommandWithOutput(c)
if err != nil {
t.Fatalf("error running busybox: %s, %v", out, err)
}
containerId1 := strings.TrimSpace(out)
// tag as foobox
c = exec.Command(dockerBinary, "commit", containerId1, "foobox")
out, _, err = runCommandWithOutput(c)
if err != nil {
t.Fatalf("error tagging foobox: %s", err)
}
imageId := common.TruncateID(strings.TrimSpace(out))
defer deleteImages(imageId)
// overwrite the tag, making the previous image dangling
c = exec.Command(dockerBinary, "tag", "-f", "busybox", "foobox")
out, _, err = runCommandWithOutput(c)
if err != nil {
t.Fatalf("error tagging foobox: %s", err)
}
defer deleteImages("foobox")
c = exec.Command(dockerBinary, "images", "-q", "-f", "dangling=true")
out, _, err = runCommandWithOutput(c)
if err != nil {
t.Fatalf("listing images failed with errors: %s, %v", out, err)
}
if e, a := 1, strings.Count(out, imageId); e != a {
t.Fatalf("expected 1 dangling image, got %d: %s", a, out)
}
logDone("images - dangling image only listed once")
}