remote_test.go 5.4 KB

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