Browse code

Merge branch 'tests/insert' of https://github.com/mrallen1/docker into mrallen1-tests/insert

Conflicts:
server.go

Victor Vieux authored on 2013/11/13 03:19:29
Showing 4 changed files
... ...
@@ -479,15 +479,16 @@ func postImagesInsert(srv *Server, version float64, w http.ResponseWriter, r *ht
479 479
 		w.Header().Set("Content-Type", "application/json")
480 480
 	}
481 481
 	sf := utils.NewStreamFormatter(version > 1.0)
482
-	imgID, err := srv.ImageInsert(name, url, path, w, sf)
482
+	err := srv.ImageInsert(name, url, path, w, sf)
483 483
 	if err != nil {
484 484
 		if sf.Used() {
485 485
 			w.Write(sf.FormatError(err))
486 486
 			return nil
487 487
 		}
488
+		return err
488 489
 	}
489 490
 
490
-	return writeJSON(w, http.StatusOK, &APIID{ID: imgID})
491
+	return nil
491 492
 }
492 493
 
493 494
 func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
... ...
@@ -130,10 +130,7 @@ func (cli *DockerCli) CmdInsert(args ...string) error {
130 130
 	v.Set("url", cmd.Arg(1))
131 131
 	v.Set("path", cmd.Arg(2))
132 132
 
133
-	if err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, cli.out, nil); err != nil {
134
-		return err
135
-	}
136
-	return nil
133
+	return cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, cli.out, nil)
137 134
 }
138 135
 
139 136
 // mkBuildContext returns an archive of an empty context with the contents
... ...
@@ -198,39 +198,39 @@ func (srv *Server) ImagesSearch(term string) ([]registry.SearchResult, error) {
198 198
 	return results.Results, nil
199 199
 }
200 200
 
201
-func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) (string, error) {
201
+func (srv *Server) ImageInsert(name, url, path string, out io.Writer, sf *utils.StreamFormatter) error {
202 202
 	out = utils.NewWriteFlusher(out)
203 203
 	img, err := srv.runtime.repositories.LookupImage(name)
204 204
 	if err != nil {
205
-		return "", err
205
+		return err
206 206
 	}
207 207
 
208 208
 	file, err := utils.Download(url, out)
209 209
 	if err != nil {
210
-		return "", err
210
+		return err
211 211
 	}
212 212
 	defer file.Body.Close()
213 213
 
214 214
 	config, _, _, err := ParseRun([]string{img.ID, "echo", "insert", url, path}, srv.runtime.capabilities)
215 215
 	if err != nil {
216
-		return "", err
216
+		return err
217 217
 	}
218 218
 
219 219
 	c, _, err := srv.runtime.Create(config, "")
220 220
 	if err != nil {
221
-		return "", err
221
+		return err
222 222
 	}
223 223
 
224
-	if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("", "Downloading", "%8v/%v (%v)"), sf, true), path); err != nil {
225
-		return "", err
224
+	if err := c.Inject(utils.ProgressReader(file.Body, int(file.ContentLength), out, sf.FormatProgress("", "Downloading", "%8v/%v (%v)"), sf, false), path); err != nil {
225
+		return err
226 226
 	}
227 227
 	// FIXME: Handle custom repo, tag comment, author
228 228
 	img, err = srv.runtime.Commit(c, "", "", img.Comment, img.Author, nil)
229 229
 	if err != nil {
230
-		return "", err
230
+		return err
231 231
 	}
232
-	out.Write(sf.FormatStatus("", img.ID))
233
-	return img.ID, nil
232
+	out.Write(sf.FormatStatus(img.ID, ""))
233
+	return nil
234 234
 }
235 235
 
236 236
 func (srv *Server) ImagesViz(out io.Writer) error {
... ...
@@ -3,6 +3,7 @@ package docker
3 3
 import (
4 4
 	"github.com/dotcloud/docker/utils"
5 5
 	"strings"
6
+	"io/ioutil"
6 7
 	"testing"
7 8
 	"time"
8 9
 )
... ...
@@ -521,3 +522,25 @@ func TestImagesFilter(t *testing.T) {
521 521
 		t.Fatal("incorrect number of matches returned")
522 522
 	}
523 523
 }
524
+
525
+func TestImageInsert(t *testing.T) {
526
+	runtime := mkRuntime(t)
527
+	defer nuke(runtime)
528
+	srv := &Server{runtime: runtime}
529
+	sf := utils.NewStreamFormatter(true)
530
+
531
+	// bad image name fails
532
+	if err := srv.ImageInsert("foo", "https://www.docker.io/static/img/docker-top-logo.png", "/foo", ioutil.Discard, sf); err == nil {
533
+		t.Fatal("expected an error and got none")
534
+	}
535
+
536
+	// bad url fails
537
+	if err := srv.ImageInsert(GetTestImage(runtime).ID, "http://bad_host_name_that_will_totally_fail.com/", "/foo", ioutil.Discard, sf); err == nil {
538
+		t.Fatal("expected an error and got none")
539
+	}
540
+
541
+	// success returns nil
542
+	if err := srv.ImageInsert(GetTestImage(runtime).ID, "https://www.docker.io/static/img/docker-top-logo.png", "/foo", ioutil.Discard, sf); err != nil {
543
+		t.Fatalf("expected no error, but got %v", err)
544
+	}
545
+}