github.com/gravitational/moby@v1.13.1/builder/remote_test.go (about)

     1  package builder
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"io/ioutil"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/docker/docker/pkg/archive"
    13  	"github.com/docker/docker/pkg/httputils"
    14  )
    15  
    16  var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
    17  
    18  func TestSelectAcceptableMIME(t *testing.T) {
    19  	validMimeStrings := []string{
    20  		"application/x-bzip2",
    21  		"application/bzip2",
    22  		"application/gzip",
    23  		"application/x-gzip",
    24  		"application/x-xz",
    25  		"application/xz",
    26  		"application/tar",
    27  		"application/x-tar",
    28  		"application/octet-stream",
    29  		"text/plain",
    30  	}
    31  
    32  	invalidMimeStrings := []string{
    33  		"",
    34  		"application/octet",
    35  		"application/json",
    36  	}
    37  
    38  	for _, m := range invalidMimeStrings {
    39  		if len(selectAcceptableMIME(m)) > 0 {
    40  			t.Fatalf("Should not have accepted %q", m)
    41  		}
    42  	}
    43  
    44  	for _, m := range validMimeStrings {
    45  		if str := selectAcceptableMIME(m); str == "" {
    46  			t.Fatalf("Should have accepted %q", m)
    47  		}
    48  	}
    49  }
    50  
    51  func TestInspectEmptyResponse(t *testing.T) {
    52  	ct := "application/octet-stream"
    53  	br := ioutil.NopCloser(bytes.NewReader([]byte("")))
    54  	contentType, bReader, err := inspectResponse(ct, br, 0)
    55  	if err == nil {
    56  		t.Fatalf("Should have generated an error for an empty response")
    57  	}
    58  	if contentType != "application/octet-stream" {
    59  		t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
    60  	}
    61  	body, err := ioutil.ReadAll(bReader)
    62  	if err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if len(body) != 0 {
    66  		t.Fatal("response body should remain empty")
    67  	}
    68  }
    69  
    70  func TestInspectResponseBinary(t *testing.T) {
    71  	ct := "application/octet-stream"
    72  	br := ioutil.NopCloser(bytes.NewReader(binaryContext))
    73  	contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext)))
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	if contentType != "application/octet-stream" {
    78  		t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
    79  	}
    80  	body, err := ioutil.ReadAll(bReader)
    81  	if err != nil {
    82  		t.Fatal(err)
    83  	}
    84  	if len(body) != len(binaryContext) {
    85  		t.Fatalf("Wrong response size %d, should be == len(binaryContext)", len(body))
    86  	}
    87  	for i := range body {
    88  		if body[i] != binaryContext[i] {
    89  			t.Fatalf("Corrupted response body at byte index %d", i)
    90  		}
    91  	}
    92  }
    93  
    94  func TestResponseUnsupportedContentType(t *testing.T) {
    95  	content := []byte(dockerfileContents)
    96  	ct := "application/json"
    97  	br := ioutil.NopCloser(bytes.NewReader(content))
    98  	contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents)))
    99  
   100  	if err == nil {
   101  		t.Fatal("Should have returned an error on content-type 'application/json'")
   102  	}
   103  	if contentType != ct {
   104  		t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType)
   105  	}
   106  	body, err := ioutil.ReadAll(bReader)
   107  	if err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	if string(body) != dockerfileContents {
   111  		t.Fatalf("Corrupted response body %s", body)
   112  	}
   113  }
   114  
   115  func TestInspectResponseTextSimple(t *testing.T) {
   116  	content := []byte(dockerfileContents)
   117  	ct := "text/plain"
   118  	br := ioutil.NopCloser(bytes.NewReader(content))
   119  	contentType, bReader, err := inspectResponse(ct, br, int64(len(content)))
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	if contentType != "text/plain" {
   124  		t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
   125  	}
   126  	body, err := ioutil.ReadAll(bReader)
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  	if string(body) != dockerfileContents {
   131  		t.Fatalf("Corrupted response body %s", body)
   132  	}
   133  }
   134  
   135  func TestInspectResponseEmptyContentType(t *testing.T) {
   136  	content := []byte(dockerfileContents)
   137  	br := ioutil.NopCloser(bytes.NewReader(content))
   138  	contentType, bodyReader, err := inspectResponse("", br, int64(len(content)))
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  	if contentType != "text/plain" {
   143  		t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
   144  	}
   145  	body, err := ioutil.ReadAll(bodyReader)
   146  	if err != nil {
   147  		t.Fatal(err)
   148  	}
   149  	if string(body) != dockerfileContents {
   150  		t.Fatalf("Corrupted response body %s", body)
   151  	}
   152  }
   153  
   154  func TestMakeRemoteContext(t *testing.T) {
   155  	contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test")
   156  	defer cleanup()
   157  
   158  	createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777)
   159  
   160  	mux := http.NewServeMux()
   161  	server := httptest.NewServer(mux)
   162  	serverURL, _ := url.Parse(server.URL)
   163  
   164  	serverURL.Path = "/" + DefaultDockerfileName
   165  	remoteURL := serverURL.String()
   166  
   167  	mux.Handle("/", http.FileServer(http.Dir(contextDir)))
   168  
   169  	remoteContext, err := MakeRemoteContext(remoteURL, map[string]func(io.ReadCloser) (io.ReadCloser, error){
   170  		httputils.MimeTypes.TextPlain: func(rc io.ReadCloser) (io.ReadCloser, error) {
   171  			dockerfile, err := ioutil.ReadAll(rc)
   172  			if err != nil {
   173  				return nil, err
   174  			}
   175  
   176  			r, err := archive.Generate(DefaultDockerfileName, string(dockerfile))
   177  			if err != nil {
   178  				return nil, err
   179  			}
   180  			return ioutil.NopCloser(r), nil
   181  		},
   182  	})
   183  
   184  	if err != nil {
   185  		t.Fatalf("Error when executing DetectContextFromRemoteURL: %s", err)
   186  	}
   187  
   188  	if remoteContext == nil {
   189  		t.Fatalf("Remote context should not be nil")
   190  	}
   191  
   192  	tarSumCtx, ok := remoteContext.(*tarSumContext)
   193  
   194  	if !ok {
   195  		t.Fatalf("Cast error, remote context should be casted to tarSumContext")
   196  	}
   197  
   198  	fileInfoSums := tarSumCtx.sums
   199  
   200  	if fileInfoSums.Len() != 1 {
   201  		t.Fatalf("Size of file info sums should be 1, got: %d", fileInfoSums.Len())
   202  	}
   203  
   204  	fileInfo := fileInfoSums.GetFile(DefaultDockerfileName)
   205  
   206  	if fileInfo == nil {
   207  		t.Fatalf("There should be file named %s in fileInfoSums", DefaultDockerfileName)
   208  	}
   209  
   210  	if fileInfo.Pos() != 0 {
   211  		t.Fatalf("File %s should have position 0, got %d", DefaultDockerfileName, fileInfo.Pos())
   212  	}
   213  }