Browse code

`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)

Fabio Falci authored on 2014/02/15 09:43:55
Showing 3 changed files
... ...
@@ -943,6 +943,9 @@ func postContainersCopy(eng *engine.Engine, version float64, w http.ResponseWrit
943 943
 	streamJSON(job, w, false)
944 944
 	if err := job.Run(); err != nil {
945 945
 		utils.Errorf("%s", err.Error())
946
+		if strings.Contains(err.Error(), "No such container") {
947
+			w.WriteHeader(http.StatusNotFound)
948
+		}
946 949
 	}
947 950
 	return nil
948 951
 }
... ...
@@ -1961,6 +1961,9 @@ func (cli *DockerCli) CmdCp(args ...string) error {
1961 1961
 	if stream != nil {
1962 1962
 		defer stream.Close()
1963 1963
 	}
1964
+	if statusCode == 404 {
1965
+		return fmt.Errorf("No such container: %v", info[0])
1966
+	}
1964 1967
 	if err != nil {
1965 1968
 		return err
1966 1969
 	}
... ...
@@ -1217,6 +1217,34 @@ func TestPostContainersCopy(t *testing.T) {
1217 1217
 	}
1218 1218
 }
1219 1219
 
1220
+func TestPostContainersCopyWhenContainerNotFound(t *testing.T) {
1221
+	eng := NewTestEngine(t)
1222
+	defer mkRuntimeFromEngine(eng, t).Nuke()
1223
+
1224
+	r := httptest.NewRecorder()
1225
+
1226
+	var copyData engine.Env
1227
+	copyData.Set("Resource", "/test.txt")
1228
+	copyData.Set("HostPath", ".")
1229
+
1230
+	jsonData := bytes.NewBuffer(nil)
1231
+	if err := copyData.Encode(jsonData); err != nil {
1232
+		t.Fatal(err)
1233
+	}
1234
+
1235
+	req, err := http.NewRequest("POST", "/containers/id_not_found/copy", jsonData)
1236
+	if err != nil {
1237
+		t.Fatal(err)
1238
+	}
1239
+	req.Header.Add("Content-Type", "application/json")
1240
+	if err := api.ServeRequest(eng, api.APIVERSION, r, req); err != nil {
1241
+		t.Fatal(err)
1242
+	}
1243
+	if r.Code != http.StatusNotFound {
1244
+		t.Fatalf("404 expected for id_not_found Container, received %v", r.Code)
1245
+	}
1246
+}
1247
+
1220 1248
 // Mocked types for tests
1221 1249
 type NopConn struct {
1222 1250
 	io.ReadCloser