remote_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package remotecontext // import "github.com/docker/docker/builder/remotecontext"
  2. import (
  3. "bytes"
  4. "io"
  5. "net/http"
  6. "net/http/httptest"
  7. "net/url"
  8. "testing"
  9. "github.com/docker/docker/builder"
  10. "gotest.tools/v3/assert"
  11. is "gotest.tools/v3/assert/cmp"
  12. "gotest.tools/v3/fs"
  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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 := io.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 TestDownloadRemote(t *testing.T) {
  161. contextDir := fs.NewDir(t, "test-builder-download-remote",
  162. fs.WithFile(builder.DefaultDockerfileName, dockerfileContents))
  163. defer contextDir.Remove()
  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.Path())))
  170. contentType, content, err := downloadRemote(remoteURL)
  171. assert.NilError(t, err)
  172. assert.Check(t, is.Equal(mimeTypeTextPlain, contentType))
  173. raw, err := io.ReadAll(content)
  174. assert.NilError(t, err)
  175. assert.Check(t, is.Equal(dockerfileContents, string(raw)))
  176. }
  177. func TestGetWithStatusError(t *testing.T) {
  178. testcases := []struct {
  179. err error
  180. statusCode int
  181. expectedErr string
  182. expectedBody string
  183. }{
  184. {
  185. statusCode: 200,
  186. expectedBody: "THE BODY",
  187. },
  188. {
  189. statusCode: 400,
  190. expectedErr: "with status 400 Bad Request: broke",
  191. expectedBody: "broke",
  192. },
  193. }
  194. for _, testcase := range testcases {
  195. ts := httptest.NewServer(
  196. http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  197. buffer := bytes.NewBufferString(testcase.expectedBody)
  198. w.WriteHeader(testcase.statusCode)
  199. w.Write(buffer.Bytes())
  200. }),
  201. )
  202. defer ts.Close()
  203. response, err := GetWithStatusError(ts.URL)
  204. if testcase.expectedErr == "" {
  205. assert.NilError(t, err)
  206. body, err := readBody(response.Body)
  207. assert.NilError(t, err)
  208. assert.Check(t, is.Contains(string(body), testcase.expectedBody))
  209. } else {
  210. assert.Check(t, is.ErrorContains(err, testcase.expectedErr))
  211. }
  212. }
  213. }
  214. func readBody(b io.ReadCloser) ([]byte, error) {
  215. defer b.Close()
  216. return io.ReadAll(b)
  217. }