add support do with image digest

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo 2015-11-11 12:11:08 +08:00
parent fe360978ac
commit bea12ab8ab
7 changed files with 96 additions and 2 deletions

View File

@ -147,6 +147,7 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
// grouping images by Id, and concat their RepoTags
if entry, existed := groupImages[image.Id]; existed {
entry.RepoTags = append(entry.RepoTags, image.RepoTags...)
entry.RepoDigests = append(entry.RepoDigests, image.RepoDigests...)
groupImages[image.Id] = entry
} else {
groupImages[image.Id] = image.Image
@ -166,6 +167,18 @@ func getImagesJSON(c *context, w http.ResponseWriter, r *http.Request) {
}
}
image.RepoTags = result
// de-duplicate RepoDigests
result = []string{}
seen = map[string]bool{}
for _, val := range image.RepoDigests {
if _, ok := seen[val]; !ok {
result = append(result, val)
seen[val] = true
}
}
image.RepoDigests = result
images = append(images, image)
}
w.Header().Set("Content-Type", "application/json")
@ -504,7 +517,11 @@ func postImagesCreate(c *context, w http.ResponseWriter, r *http.Request) {
}
if tag := r.Form.Get("tag"); tag != "" {
image += ":" + tag
if tagHasDigest(tag) {
image += "@" + tag
} else {
image += ":" + tag
}
}
var errorMessage string

View File

@ -219,3 +219,7 @@ func int64ValueOrZero(r *http.Request, k string) int64 {
}
return val
}
func tagHasDigest(tag string) bool {
return strings.Contains(tag, ":")
}

View File

@ -26,6 +26,7 @@ func (image *Image) Match(IDOrName string, matchTag bool) bool {
repoName, tag := parsers.ParseRepositoryTag(IDOrName)
// match repotag
for _, imageRepoTag := range image.RepoTags {
imageRepoName, imageTag := parsers.ParseRepositoryTag(imageRepoTag)
@ -36,6 +37,18 @@ func (image *Image) Match(IDOrName string, matchTag bool) bool {
return true
}
}
// match repodigests
for _, imageDigest := range image.RepoDigests {
imageRepoName, imageDigest := parsers.ParseRepositoryTag(imageDigest)
if matchTag == false && imageRepoName == repoName {
return true
}
if imageRepoName == repoName && (imageDigest == tag || tag == "") {
return true
}
}
return false
}
@ -55,7 +68,9 @@ type Images []*Image
func (images Images) Filter(opts ImageFilterOptions) Images {
includeAll := func(image *Image) bool {
// TODO: this is wrong if RepoTags == []
return opts.All || (len(image.RepoTags) != 0 && image.RepoTags[0] != "<none>:<none>")
return opts.All ||
(len(image.RepoTags) != 0 && image.RepoTags[0] != "<none>:<none>") ||
(len(image.RepoDigests) != 0 && image.RepoDigests[0] != "<none>@<none>")
}
includeFilter := func(image *Image) bool {

View File

@ -13,6 +13,7 @@ func TestMatch(t *testing.T) {
img.Id = "378954456789"
img.RepoTags = []string{"name:latest"}
img.RepoDigests = []string{"name@sha256:a973f1415c489a934bf56dd653079d36b4ec717760215645726439de9705911d"}
assert.True(t, img.Match("378954456789", true))
assert.True(t, img.Match("3789", true))
@ -33,6 +34,9 @@ func TestMatch(t *testing.T) {
assert.True(t, img.Match("name", false))
assert.False(t, img.Match("nam", false))
assert.False(t, img.Match("na", false))
assert.True(t, img.Match("name@sha256:a973f1415c489a934bf56dd653079d36b4ec717760215645726439de9705911d", true))
assert.False(t, img.Match("name@sha256:111111415c489a934bf56dd653079d36b4ec717760215645726439de9705911d", true))
}
func TestMatchPrivateRepo(t *testing.T) {

View File

@ -36,6 +36,22 @@ function teardown() {
done
}
@test "docker pull with image digest" {
start_docker 2
swarm_manage
# make sure no image exists
run docker_swarm images -q
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
docker_swarm pull jimmyxian/busybox@sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d
run docker_swarm images --digests
[ "$status" -eq 0 ]
[[ "${output}" == *"sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d"* ]]
}
@test "docker pull -check error code" {
start_docker 2
swarm_manage

View File

@ -103,6 +103,28 @@ function teardown() {
[[ "${output}" != *"testimage"* ]]
}
@test "docker rmi with image digest" {
start_docker 2
swarm_manage
# make sure no image exists
run docker_swarm images -q
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
docker_swarm pull jimmyxian/busybox@sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d
run docker_swarm images --digests
[ "$status" -eq 0 ]
[[ "${output}" == *"sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d"* ]]
docker_swarm rmi jimmyxian/busybox@sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d
run docker_swarm images --digests
[[ "${output}" != *"busybox"* ]]
[[ "${output}" != *"sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d"* ]]
}
@test "docker rmi --force with image tag" {
start_docker_with_busybox 1
start_docker 1

View File

@ -26,6 +26,22 @@ function teardown() {
[[ "${output}" == *"cannot specify 64-byte hexadecimal strings"* ]]
}
@test "docker run with image digest" {
start_docker 2
swarm_manage
docker_swarm pull jimmyxian/busybox@sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d
# make sure no container exist
run docker_swarm ps -qa
[ "${#lines[@]}" -eq 0 ]
docker_swarm run -d --name test_container jimmyxian/busybox@sha256:649374debd26307573564fcf9748d39db33ef61fbf88ee84c3af10fd7e08765d sleep 100
# verify, container is running
[ -n $(docker_swarm ps -q --filter=name=test_container --filter=status=running) ]
}
@test "docker run with resources" {
start_docker_with_busybox 2
swarm_manage