remote_test.go 6.0 KB

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