2017-03-20 22:22:29 +00:00
|
|
|
package remotecontext
|
2015-04-04 14:54:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-05-01 10:58:39 +00:00
|
|
|
"io"
|
2015-04-04 14:54:43 +00:00
|
|
|
"io/ioutil"
|
2016-05-01 10:58:39 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2015-04-04 14:54:43 +00:00
|
|
|
"testing"
|
2016-05-01 10:58:39 +00:00
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
"github.com/docker/docker/builder"
|
2016-05-01 10:58:39 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/docker/docker/pkg/httputils"
|
2015-04-04 14:54:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
|
|
|
|
|
2015-09-06 17:26:40 +00:00
|
|
|
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 {
|
2015-11-25 09:30:13 +00:00
|
|
|
t.Fatalf("Should not have accepted %q", m)
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range validMimeStrings {
|
|
|
|
if str := selectAcceptableMIME(m); str == "" {
|
2015-11-25 09:30:13 +00:00
|
|
|
t.Fatalf("Should have accepted %q", m)
|
2015-09-06 17:26:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 14:54:43 +00:00
|
|
|
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 {
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("Should have generated an error for an empty response")
|
2015-04-04 14:54:43 +00:00
|
|
|
}
|
|
|
|
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))
|
2015-07-23 21:19:58 +00:00
|
|
|
contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext)))
|
2015-04-04 14:54:43 +00:00
|
|
|
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) {
|
2016-05-03 09:10:51 +00:00
|
|
|
content := []byte(dockerfileContents)
|
2015-04-04 14:54:43 +00:00
|
|
|
ct := "application/json"
|
|
|
|
br := ioutil.NopCloser(bytes.NewReader(content))
|
2016-05-03 09:10:51 +00:00
|
|
|
contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))
|
2015-04-04 14:54:43 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2016-05-03 09:10:51 +00:00
|
|
|
if string(body) != dockerfileContents {
|
2015-04-04 14:54:43 +00:00
|
|
|
t.Fatalf("Corrupted response body %s", body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectResponseTextSimple(t *testing.T) {
|
2016-05-03 09:10:51 +00:00
|
|
|
content := []byte(dockerfileContents)
|
2015-04-04 14:54:43 +00:00
|
|
|
ct := "text/plain"
|
|
|
|
br := ioutil.NopCloser(bytes.NewReader(content))
|
2015-07-23 21:19:58 +00:00
|
|
|
contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
|
2015-04-04 14:54:43 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-05-03 09:10:51 +00:00
|
|
|
if string(body) != dockerfileContents {
|
2015-04-04 14:54:43 +00:00
|
|
|
t.Fatalf("Corrupted response body %s", body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInspectResponseEmptyContentType(t *testing.T) {
|
2016-05-03 09:10:51 +00:00
|
|
|
content := []byte(dockerfileContents)
|
2015-04-04 14:54:43 +00:00
|
|
|
br := ioutil.NopCloser(bytes.NewReader(content))
|
2015-07-23 21:19:58 +00:00
|
|
|
contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
|
2015-04-04 14:54:43 +00:00
|
|
|
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)
|
|
|
|
}
|
2016-05-03 09:10:51 +00:00
|
|
|
if string(body) != dockerfileContents {
|
2015-04-04 14:54:43 +00:00
|
|
|
t.Fatalf("Corrupted response body %s", body)
|
|
|
|
}
|
|
|
|
}
|
2016-05-01 10:58:39 +00:00
|
|
|
|
2017-01-30 08:02:47 +00:00
|
|
|
func TestUnknownContentLength(t *testing.T) {
|
|
|
|
content := []byte(dockerfileContents)
|
|
|
|
ct := "text/plain"
|
|
|
|
br := ioutil.NopCloser(bytes.NewReader(content))
|
|
|
|
contentType, bReader, err := inspectResponse(ct, br, -1)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
if string(body) != dockerfileContents {
|
|
|
|
t.Fatalf("Corrupted response body %s", body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 10:58:39 +00:00
|
|
|
func TestMakeRemoteContext(t *testing.T) {
|
2016-05-03 09:10:51 +00:00
|
|
|
contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
|
|
|
|
defer cleanup()
|
2016-05-01 10:58:39 +00:00
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
|
2016-05-01 10:58:39 +00:00
|
|
|
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
server := httptest.NewServer(mux)
|
|
|
|
serverURL, _ := url.Parse(server.URL)
|
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
serverURL.Path = "/" + builder.DefaultDockerfileName
|
2016-05-01 10:58:39 +00:00
|
|
|
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
|
|
|
|
}
|
2016-10-20 23:40:59 +00:00
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
r, err := archive.Generate(builder.DefaultDockerfileName, string(dockerfile))
|
2016-10-20 23:40:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ioutil.NopCloser(r), nil
|
2016-05-01 10:58:39 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error when executing DetectContextFromRemoteURL: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if remoteContext == nil {
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("Remote context should not be nil")
|
2016-05-01 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tarSumCtx, ok := remoteContext.(*tarSumContext)
|
|
|
|
|
|
|
|
if !ok {
|
2017-02-21 08:53:29 +00:00
|
|
|
t.Fatal("Cast error, remote context should be casted to tarSumContext")
|
2016-05-01 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fileInfoSums := tarSumCtx.sums
|
|
|
|
|
|
|
|
if fileInfoSums.Len() != 1 {
|
|
|
|
t.Fatalf("Size of file info sums should be 1, got: %d", fileInfoSums.Len())
|
|
|
|
}
|
|
|
|
|
2017-03-20 22:22:29 +00:00
|
|
|
fileInfo := fileInfoSums.GetFile(builder.DefaultDockerfileName)
|
2016-05-01 10:58:39 +00:00
|
|
|
|
|
|
|
if fileInfo == nil {
|
2017-03-20 22:22:29 +00:00
|
|
|
t.Fatalf("There should be file named %s in fileInfoSums", builder.DefaultDockerfileName)
|
2016-05-01 10:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.Pos() != 0 {
|
2017-03-20 22:22:29 +00:00
|
|
|
t.Fatalf("File %s should have position 0, got %d", builder.DefaultDockerfileName, fileInfo.Pos())
|
2016-05-01 10:58:39 +00:00
|
|
|
}
|
|
|
|
}
|