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