builder/remote_test.go
e0ef11a4
 package builder
d48bface
 
 import (
 	"bytes"
e00ad722
 	"io"
d48bface
 	"io/ioutil"
e00ad722
 	"net/http"
 	"net/http/httptest"
 	"net/url"
d48bface
 	"testing"
e00ad722
 
 	"github.com/docker/docker/pkg/archive"
 	"github.com/docker/docker/pkg/httputils"
d48bface
 )
 
 var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
 
e0ef11a4
 func TestSelectAcceptableMIME(t *testing.T) {
 	validMimeStrings := []string{
 		"application/x-bzip2",
 		"application/bzip2",
 		"application/gzip",
 		"application/x-gzip",
 		"application/x-xz",
 		"application/xz",
 		"application/tar",
 		"application/x-tar",
 		"application/octet-stream",
 		"text/plain",
 	}
 
 	invalidMimeStrings := []string{
 		"",
 		"application/octet",
 		"application/json",
 	}
 
 	for _, m := range invalidMimeStrings {
 		if len(selectAcceptableMIME(m)) > 0 {
478b773f
 			t.Fatalf("Should not have accepted %q", m)
e0ef11a4
 		}
 	}
 
 	for _, m := range validMimeStrings {
 		if str := selectAcceptableMIME(m); str == "" {
478b773f
 			t.Fatalf("Should have accepted %q", m)
e0ef11a4
 		}
 	}
 }
 
d48bface
 func TestInspectEmptyResponse(t *testing.T) {
 	ct := "application/octet-stream"
 	br := ioutil.NopCloser(bytes.NewReader([]byte("")))
 	contentType, bReader, err := inspectResponse(ct, br, 0)
 	if err == nil {
 		t.Fatalf("Should have generated an error for an empty response")
 	}
 	if contentType != "application/octet-stream" {
 		t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
 	}
 	body, err := ioutil.ReadAll(bReader)
 	if err != nil {
 		t.Fatal(err)
 	}
 	if len(body) != 0 {
 		t.Fatal("response body should remain empty")
 	}
 }
 
 func TestInspectResponseBinary(t *testing.T) {
 	ct := "application/octet-stream"
 	br := ioutil.NopCloser(bytes.NewReader(binaryContext))
1f61084d
 	contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext)))
d48bface
 	if err != nil {
 		t.Fatal(err)
 	}
 	if contentType != "application/octet-stream" {
 		t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
 	}
 	body, err := ioutil.ReadAll(bReader)
 	if err != nil {
 		t.Fatal(err)
 	}
 	if len(body) != len(binaryContext) {
 		t.Fatalf("Wrong response size %d, should be == len(binaryContext)", len(body))
 	}
 	for i := range body {
 		if body[i] != binaryContext[i] {
 			t.Fatalf("Corrupted response body at byte index %d", i)
 		}
 	}
 }
 
 func TestResponseUnsupportedContentType(t *testing.T) {
ab2baf08
 	content := []byte(dockerfileContents)
d48bface
 	ct := "application/json"
 	br := ioutil.NopCloser(bytes.NewReader(content))
ab2baf08
 	contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))
d48bface
 
 	if err == nil {
 		t.Fatal("Should have returned an error on content-type 'application/json'")
 	}
 	if contentType != ct {
 		t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType)
 	}
 	body, err := ioutil.ReadAll(bReader)
 	if err != nil {
 		t.Fatal(err)
 	}
ab2baf08
 	if string(body) != dockerfileContents {
d48bface
 		t.Fatalf("Corrupted response body %s", body)
 	}
 }
 
 func TestInspectResponseTextSimple(t *testing.T) {
ab2baf08
 	content := []byte(dockerfileContents)
d48bface
 	ct := "text/plain"
 	br := ioutil.NopCloser(bytes.NewReader(content))
1f61084d
 	contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
d48bface
 	if err != nil {
 		t.Fatal(err)
 	}
 	if contentType != "text/plain" {
 		t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
 	}
 	body, err := ioutil.ReadAll(bReader)
 	if err != nil {
 		t.Fatal(err)
 	}
ab2baf08
 	if string(body) != dockerfileContents {
d48bface
 		t.Fatalf("Corrupted response body %s", body)
 	}
 }
 
 func TestInspectResponseEmptyContentType(t *testing.T) {
ab2baf08
 	content := []byte(dockerfileContents)
d48bface
 	br := ioutil.NopCloser(bytes.NewReader(content))
1f61084d
 	contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
d48bface
 	if err != nil {
 		t.Fatal(err)
 	}
 	if contentType != "text/plain" {
 		t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
 	}
 	body, err := ioutil.ReadAll(bodyReader)
 	if err != nil {
 		t.Fatal(err)
 	}
ab2baf08
 	if string(body) != dockerfileContents {
d48bface
 		t.Fatalf("Corrupted response body %s", body)
 	}
 }
e00ad722
 
 func TestMakeRemoteContext(t *testing.T) {
ab2baf08
 	contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
 	defer cleanup()
e00ad722
 
ab2baf08
 	createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
e00ad722
 
 	mux := http.NewServeMux()
 	server := httptest.NewServer(mux)
 	serverURL, _ := url.Parse(server.URL)
 
 	serverURL.Path = "/" + DefaultDockerfileName
 	remoteURL := serverURL.String()
 
 	mux.Handle("/", http.FileServer(http.Dir(contextDir)))
 
 	remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
 		httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
 			dockerfile, err := ioutil.ReadAll(rc)
 			if err != nil {
 				return nil, err
 			}
aa2cc187
 
 			r, err := archive.Generate(DefaultDockerfileName, string(dockerfile))
 			if err != nil {
 				return nil, err
 			}
 			return ioutil.NopCloser(r), nil
e00ad722
 		},
 	})
 
 	if err != nil {
 		t.Fatalf("Error when executing DetectContextFromRemoteURL: %s", err)
 	}
 
 	if remoteContext == nil {
 		t.Fatalf("Remote context should not be nil")
 	}
 
 	tarSumCtx, ok := remoteContext.(*tarSumContext)
 
 	if !ok {
 		t.Fatalf("Cast error, remote context should be casted to tarSumContext")
 	}
 
 	fileInfoSums := tarSumCtx.sums
 
 	if fileInfoSums.Len() != 1 {
 		t.Fatalf("Size of file info sums should be 1, got: %d", fileInfoSums.Len())
 	}
 
 	fileInfo := fileInfoSums.GetFile(DefaultDockerfileName)
 
 	if fileInfo == nil {
 		t.Fatalf("There should be file named %s in fileInfoSums", DefaultDockerfileName)
 	}
 
 	if fileInfo.Pos() != 0 {
 		t.Fatalf("File %s should have position 0, got %d", DefaultDockerfileName, fileInfo.Pos())
 	}
 }