Merge pull request #6857 from vbatts/vbatts-fix_save_repositories

docker save: fix the 'repositories' file
This commit is contained in:
Tibor Vass 2014-07-07 17:26:52 -04:00
commit 52c58b9ea0
2 changed files with 86 additions and 6 deletions

View File

@ -59,6 +59,63 @@ func TestSaveAndLoadRepoStdout(t *testing.T) {
logDone("load - load a repo using stdout")
}
func TestSaveSingleTag(t *testing.T) {
repoName := "foobar-save-single-tag-test"
tagCmdFinal := fmt.Sprintf("%v tag busybox:latest %v:latest", dockerBinary, repoName)
tagCmd := exec.Command("bash", "-c", tagCmdFinal)
out, _, err := runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
idCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
idCmd := exec.Command("bash", "-c", idCmdFinal)
out, _, err = runCommandWithOutput(idCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
cleanedImageID := stripTrailingCharacters(out)
saveCmdFinal := fmt.Sprintf("%v save %v:latest | tar t | grep -E '(^repositories$|%v)'", dockerBinary, repoName, cleanedImageID)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo with image ID and 'repositories' file: %v %v", out, err))
deleteImages(repoName)
logDone("save - save a specific image:tag")
}
func TestSaveImageId(t *testing.T) {
repoName := "foobar-save-image-id-test"
tagCmdFinal := fmt.Sprintf("%v tag scratch:latest %v:latest", dockerBinary, repoName)
tagCmd := exec.Command("bash", "-c", tagCmdFinal)
out, _, err := runCommandWithOutput(tagCmd)
errorOut(err, t, fmt.Sprintf("failed to tag repo: %v %v", out, err))
idLongCmdFinal := fmt.Sprintf("%v images -q --no-trunc %v", dockerBinary, repoName)
idLongCmd := exec.Command("bash", "-c", idLongCmdFinal)
out, _, err = runCommandWithOutput(idLongCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo ID: %v %v", out, err))
cleanedLongImageID := stripTrailingCharacters(out)
idShortCmdFinal := fmt.Sprintf("%v images -q %v", dockerBinary, repoName)
idShortCmd := exec.Command("bash", "-c", idShortCmdFinal)
out, _, err = runCommandWithOutput(idShortCmd)
errorOut(err, t, fmt.Sprintf("failed to get repo short ID: %v %v", out, err))
cleanedShortImageID := stripTrailingCharacters(out)
saveCmdFinal := fmt.Sprintf("%v save %v | tar t | grep %v", dockerBinary, cleanedShortImageID, cleanedLongImageID)
saveCmd := exec.Command("bash", "-c", saveCmdFinal)
out, _, err = runCommandWithOutput(saveCmd)
errorOut(err, t, fmt.Sprintf("failed to save repo with image ID: %v %v", out, err))
deleteImages(repoName)
logDone("save - save a image by ID")
}
// save a repo and try to load it using flags
func TestSaveAndLoadRepoFlags(t *testing.T) {
runCmd := exec.Command(dockerBinary, "run", "-d", "busybox", "true")

View File

@ -351,29 +351,52 @@ func (srv *Server) ImageExport(job *engine.Job) engine.Status {
utils.Debugf("Serializing %s", name)
rootRepoMap := map[string]graph.Repository{}
rootRepo, err := srv.daemon.Repositories().Get(name)
if err != nil {
return job.Error(err)
}
if rootRepo != nil {
// this is a base repo name, like 'busybox'
for _, id := range rootRepo {
if err := srv.exportImage(job.Eng, id, tempdir); err != nil {
return job.Error(err)
}
}
// write repositories
rootRepoMap := map[string]graph.Repository{}
rootRepoMap[name] = rootRepo
} else {
img, err := srv.daemon.Repositories().LookupImage(name)
if err != nil {
return job.Error(err)
}
if img != nil {
// This is a named image like 'busybox:latest'
repoName, repoTag := utils.ParseRepositoryTag(name)
if err := srv.exportImage(job.Eng, img.ID, tempdir); err != nil {
return job.Error(err)
}
// check this length, because a lookup of a truncated has will not have a tag
// and will not need to be added to this map
if len(repoTag) > 0 {
rootRepoMap[repoName] = graph.Repository{repoTag: img.ID}
}
} else {
// this must be an ID that didn't get looked up just right?
if err := srv.exportImage(job.Eng, name, tempdir); err != nil {
return job.Error(err)
}
}
}
// write repositories, if there is something to write
if len(rootRepoMap) > 0 {
rootRepoJson, _ := json.Marshal(rootRepoMap)
if err := ioutil.WriteFile(path.Join(tempdir, "repositories"), rootRepoJson, os.FileMode(0644)); err != nil {
return job.Error(err)
}
} else {
if err := srv.exportImage(job.Eng, name, tempdir); err != nil {
return job.Error(err)
}
utils.Debugf("There were no repositories to write")
}
fs, err := archive.Tar(tempdir, archive.Uncompressed)