job_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package builder
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "testing"
  6. )
  7. var textPlainDockerfile = "FROM busybox"
  8. var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic
  9. func TestInspectEmptyResponse(t *testing.T) {
  10. ct := "application/octet-stream"
  11. br := ioutil.NopCloser(bytes.NewReader([]byte("")))
  12. contentType, bReader, err := inspectResponse(ct, br, 0)
  13. if err == nil {
  14. t.Fatalf("Should have generated an error for an empty response")
  15. }
  16. if contentType != "application/octet-stream" {
  17. t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
  18. }
  19. body, err := ioutil.ReadAll(bReader)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. if len(body) != 0 {
  24. t.Fatal("response body should remain empty")
  25. }
  26. }
  27. func TestInspectResponseBinary(t *testing.T) {
  28. ct := "application/octet-stream"
  29. br := ioutil.NopCloser(bytes.NewReader(binaryContext))
  30. contentType, bReader, err := inspectResponse(ct, br, len(binaryContext))
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. if contentType != "application/octet-stream" {
  35. t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType)
  36. }
  37. body, err := ioutil.ReadAll(bReader)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if len(body) != len(binaryContext) {
  42. t.Fatalf("Wrong response size %d, should be == len(binaryContext)", len(body))
  43. }
  44. for i := range body {
  45. if body[i] != binaryContext[i] {
  46. t.Fatalf("Corrupted response body at byte index %d", i)
  47. }
  48. }
  49. }
  50. func TestResponseUnsupportedContentType(t *testing.T) {
  51. content := []byte(textPlainDockerfile)
  52. ct := "application/json"
  53. br := ioutil.NopCloser(bytes.NewReader(content))
  54. contentType, bReader, err := inspectResponse(ct, br, len(textPlainDockerfile))
  55. if err == nil {
  56. t.Fatal("Should have returned an error on content-type 'application/json'")
  57. }
  58. if contentType != ct {
  59. t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType)
  60. }
  61. body, err := ioutil.ReadAll(bReader)
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if string(body) != textPlainDockerfile {
  66. t.Fatalf("Corrupted response body %s", body)
  67. }
  68. }
  69. func TestInspectResponseTextSimple(t *testing.T) {
  70. content := []byte(textPlainDockerfile)
  71. ct := "text/plain"
  72. br := ioutil.NopCloser(bytes.NewReader(content))
  73. contentType, bReader, err := inspectResponse(ct, br, len(content))
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if contentType != "text/plain" {
  78. t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
  79. }
  80. body, err := ioutil.ReadAll(bReader)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if string(body) != textPlainDockerfile {
  85. t.Fatalf("Corrupted response body %s", body)
  86. }
  87. }
  88. func TestInspectResponseEmptyContentType(t *testing.T) {
  89. content := []byte(textPlainDockerfile)
  90. br := ioutil.NopCloser(bytes.NewReader(content))
  91. contentType, bodyReader, err := inspectResponse("", br, len(content))
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. if contentType != "text/plain" {
  96. t.Fatalf("Content type should be 'text/plain' but is %q", contentType)
  97. }
  98. body, err := ioutil.ReadAll(bodyReader)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if string(body) != textPlainDockerfile {
  103. t.Fatalf("Corrupted response body %s", body)
  104. }
  105. }