瀏覽代碼

integ-cli: fix TestImportDisplay & add FileServer

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)
unclejack 10 年之前
父節點
當前提交
9e5592d6a1
共有 3 個文件被更改,包括 39 次插入1 次删除
  1. 3 0
      Dockerfile
  2. 10 1
      integration-cli/docker_cli_import_test.go
  3. 26 0
      integration-cli/utils.go

+ 3 - 0
Dockerfile

@@ -89,6 +89,9 @@ RUN mkdir -p /go/src/github.com/cpuguy83 \
 # Get the "busybox" image source so we can build locally instead of pulling
 # Get the "busybox" image source so we can build locally instead of pulling
 RUN	git clone -b buildroot-2014.02 https://github.com/jpetazzo/docker-busybox.git /docker-busybox
 RUN	git clone -b buildroot-2014.02 https://github.com/jpetazzo/docker-busybox.git /docker-busybox
 
 
+# Get the "cirros" image source so we can import it instead of fetching it during tests
+RUN	curl -sSL -o /cirros.tar.gz https://github.com/ewindisch/docker-cirros/raw/1cded459668e8b9dbf4ef976c94c05add9bbd8e9/cirros-0.3.0-x86_64-lxc.tar.gz
+
 # Setup s3cmd config
 # Setup s3cmd config
 RUN	/bin/echo -e '[default]\naccess_key=$AWS_ACCESS_KEY\nsecret_key=$AWS_SECRET_KEY' > /.s3cfg
 RUN	/bin/echo -e '[default]\naccess_key=$AWS_ACCESS_KEY\nsecret_key=$AWS_SECRET_KEY' > /.s3cfg
 
 

+ 10 - 1
integration-cli/docker_cli_import_test.go

@@ -1,13 +1,22 @@
 package main
 package main
 
 
 import (
 import (
+	"fmt"
 	"os/exec"
 	"os/exec"
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 )
 )
 
 
 func TestImportDisplay(t *testing.T) {
 func TestImportDisplay(t *testing.T) {
-	importCmd := exec.Command(dockerBinary, "import", "https://github.com/ewindisch/docker-cirros/raw/master/cirros-0.3.0-x86_64-lxc.tar.gz")
+	server, err := fileServer(map[string]string{
+		"/cirros.tar.gz": "/cirros.tar.gz",
+	})
+	if err != nil {
+		t.Fatal(err)
+	}
+	defer server.Close()
+	fileUrl := fmt.Sprintf("%s/cirros.tar.gz", server.URL)
+	importCmd := exec.Command(dockerBinary, "import", fileUrl)
 	out, _, err := runCommandWithOutput(importCmd)
 	out, _, err := runCommandWithOutput(importCmd)
 	if err != nil {
 	if err != nil {
 		t.Errorf("import failed with errors: %v, output: %q", err, out)
 		t.Errorf("import failed with errors: %v, output: %q", err, out)

+ 26 - 0
integration-cli/utils.go

@@ -5,6 +5,8 @@ import (
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
+	"net/http"
+	"net/http/httptest"
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"reflect"
 	"reflect"
@@ -212,3 +214,27 @@ func ListTar(f io.Reader) ([]string, error) {
 		entries = append(entries, th.Name)
 		entries = append(entries, th.Name)
 	}
 	}
 }
 }
+
+type FileServer struct {
+	*httptest.Server
+}
+
+func fileServer(files map[string]string) (*FileServer, error) {
+	var handler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
+		if filePath, found := files[r.URL.Path]; found {
+			http.ServeFile(w, r, filePath)
+		} else {
+			http.Error(w, http.StatusText(404), 404)
+		}
+	}
+
+	for _, file := range files {
+		if _, err := os.Stat(file); err != nil {
+			return nil, err
+		}
+	}
+	server := httptest.NewServer(handler)
+	return &FileServer{
+		Server: server,
+	}, nil
+}