`docker cp` error when container doesn't exist

Fix cp api to return a 404 notfound if container doesn't exist.
Fixes #4119.

Docker-DCO-1.1-Signed-off-by: Fabio Falci <fabiofalci@gmail.com> (github: fabiofalci)
This commit is contained in:
Fabio Falci 2014-02-15 00:43:55 +00:00
parent 72ed2537b3
commit a51441278a
3 changed files with 34 additions and 0 deletions

View File

@ -943,6 +943,9 @@ func postContainersCopy(eng *engine.Engine, version float64, w http.ResponseWrit
streamJSON(job, w, false)
if err := job.Run(); err != nil {
utils.Errorf("%s", err.Error())
if strings.Contains(err.Error(), "No such container") {
w.WriteHeader(http.StatusNotFound)
}
}
return nil
}

View File

@ -1961,6 +1961,9 @@ func (cli *DockerCli) CmdCp(args ...string) error {
if stream != nil {
defer stream.Close()
}
if statusCode == 404 {
return fmt.Errorf("No such container: %v", info[0])
}
if err != nil {
return err
}

View File

@ -1217,6 +1217,34 @@ func TestPostContainersCopy(t *testing.T) {
}
}
func TestPostContainersCopyWhenContainerNotFound(t *testing.T) {
eng := NewTestEngine(t)
defer mkRuntimeFromEngine(eng, t).Nuke()
r := httptest.NewRecorder()
var copyData engine.Env
copyData.Set("Resource", "/test.txt")
copyData.Set("HostPath", ".")
jsonData := bytes.NewBuffer(nil)
if err := copyData.Encode(jsonData); err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("POST", "/containers/id_not_found/copy", jsonData)
if err != nil {
t.Fatal(err)
}
req.Header.Add("Content-Type", "application/json")
if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
t.Fatal(err)
}
if r.Code != http.StatusNotFound {
t.Fatalf("404 expected for id_not_found Container, received %v", r.Code)
}
}
// Mocked types for tests
type NopConn struct {
io.ReadCloser