fix image match and fix docker save

Signed-off-by: Xian Chaobo <xianchaobo@huawei.com>
This commit is contained in:
Xian Chaobo
2015-05-19 05:21:14 -04:00
parent 1f5d66d1f1
commit 064e91cd23
4 changed files with 59 additions and 6 deletions

View File

@@ -20,13 +20,34 @@ func (image *Image) Match(IDOrName string) bool {
if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) {
return true
}
if len(strings.SplitN(IDOrName, ":", 2)) == 1 {
IDOrName = IDOrName + ":latest"
}
for _, repoTag := range image.RepoTags {
if len(strings.SplitN(repoTag, ":", 2)) == 1 {
repoTag = repoTag + ":latest"
}
if repoTag == IDOrName {
return true
}
}
return false
}
// MatchWithoutTag is exported
func (image *Image) MatchWithoutTag(IDOrName string) bool {
size := len(IDOrName)
if image.Id == IDOrName || (size > 2 && strings.HasPrefix(image.Id, IDOrName)) {
return true
}
name := strings.SplitN(IDOrName, ":", 2)[0]
for _, repoTag := range image.RepoTags {
repoName := strings.SplitN(repoTag, ":", 2)[0]
if repoName == name {
return true
}
}
return false
}

View File

@@ -18,7 +18,24 @@ func TestMatch(t *testing.T) {
assert.False(t, img.Match("37"))
assert.True(t, img.Match("name:latest"))
assert.False(t, img.Match("name"))
assert.True(t, img.Match("name"))
assert.False(t, img.Match("nam"))
assert.False(t, img.Match("na"))
}
func TestMatchWithoutTag(t *testing.T) {
img := Image{}
img.Id = "378954456789"
img.RepoTags = []string{"name:latest"}
assert.True(t, img.MatchWithoutTag("378954456789"))
assert.True(t, img.MatchWithoutTag("3789"))
assert.True(t, img.MatchWithoutTag("378"))
assert.False(t, img.MatchWithoutTag("37"))
assert.True(t, img.MatchWithoutTag("name:latest"))
assert.True(t, img.MatchWithoutTag("name"))
assert.False(t, img.MatchWithoutTag("nam"))
assert.False(t, img.MatchWithoutTag("na"))
}