github.com/a4a881d4/docker@v1.9.0-rc2/builder/remote_test.go (about) 1 package builder 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "testing" 8 ) 9 10 var textPlainDockerfile = "FROM busybox" 11 var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic 12 13 func TestSelectAcceptableMIME(t *testing.T) { 14 validMimeStrings := []string{ 15 "application/x-bzip2", 16 "application/bzip2", 17 "application/gzip", 18 "application/x-gzip", 19 "application/x-xz", 20 "application/xz", 21 "application/tar", 22 "application/x-tar", 23 "application/octet-stream", 24 "text/plain", 25 } 26 27 invalidMimeStrings := []string{ 28 "", 29 "application/octet", 30 "application/json", 31 } 32 33 for _, m := range invalidMimeStrings { 34 if len(selectAcceptableMIME(m)) > 0 { 35 err := fmt.Errorf("Should not have accepted %q", m) 36 t.Fatal(err) 37 } 38 } 39 40 for _, m := range validMimeStrings { 41 if str := selectAcceptableMIME(m); str == "" { 42 err := fmt.Errorf("Should have accepted %q", m) 43 t.Fatal(err) 44 } 45 } 46 } 47 48 func TestInspectEmptyResponse(t *testing.T) { 49 ct := "application/octet-stream" 50 br := ioutil.NopCloser(bytes.NewReader([]byte(""))) 51 contentType, bReader, err := inspectResponse(ct, br, 0) 52 if err == nil { 53 t.Fatalf("Should have generated an error for an empty response") 54 } 55 if contentType != "application/octet-stream" { 56 t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType) 57 } 58 body, err := ioutil.ReadAll(bReader) 59 if err != nil { 60 t.Fatal(err) 61 } 62 if len(body) != 0 { 63 t.Fatal("response body should remain empty") 64 } 65 } 66 67 func TestInspectResponseBinary(t *testing.T) { 68 ct := "application/octet-stream" 69 br := ioutil.NopCloser(bytes.NewReader(binaryContext)) 70 contentType, bReader, err := inspectResponse(ct, br, int64(len(binaryContext))) 71 if err != nil { 72 t.Fatal(err) 73 } 74 if contentType != "application/octet-stream" { 75 t.Fatalf("Content type should be 'application/octet-stream' but is %q", contentType) 76 } 77 body, err := ioutil.ReadAll(bReader) 78 if err != nil { 79 t.Fatal(err) 80 } 81 if len(body) != len(binaryContext) { 82 t.Fatalf("Wrong response size %d, should be == len(binaryContext)", len(body)) 83 } 84 for i := range body { 85 if body[i] != binaryContext[i] { 86 t.Fatalf("Corrupted response body at byte index %d", i) 87 } 88 } 89 } 90 91 func TestResponseUnsupportedContentType(t *testing.T) { 92 content := []byte(textPlainDockerfile) 93 ct := "application/json" 94 br := ioutil.NopCloser(bytes.NewReader(content)) 95 contentType, bReader, err := inspectResponse(ct, br, int64(len(textPlainDockerfile))) 96 97 if err == nil { 98 t.Fatal("Should have returned an error on content-type 'application/json'") 99 } 100 if contentType != ct { 101 t.Fatalf("Should not have altered content-type: orig: %s, altered: %s", ct, contentType) 102 } 103 body, err := ioutil.ReadAll(bReader) 104 if err != nil { 105 t.Fatal(err) 106 } 107 if string(body) != textPlainDockerfile { 108 t.Fatalf("Corrupted response body %s", body) 109 } 110 } 111 112 func TestInspectResponseTextSimple(t *testing.T) { 113 content := []byte(textPlainDockerfile) 114 ct := "text/plain" 115 br := ioutil.NopCloser(bytes.NewReader(content)) 116 contentType, bReader, err := inspectResponse(ct, br, int64(len(content))) 117 if err != nil { 118 t.Fatal(err) 119 } 120 if contentType != "text/plain" { 121 t.Fatalf("Content type should be 'text/plain' but is %q", contentType) 122 } 123 body, err := ioutil.ReadAll(bReader) 124 if err != nil { 125 t.Fatal(err) 126 } 127 if string(body) != textPlainDockerfile { 128 t.Fatalf("Corrupted response body %s", body) 129 } 130 } 131 132 func TestInspectResponseEmptyContentType(t *testing.T) { 133 content := []byte(textPlainDockerfile) 134 br := ioutil.NopCloser(bytes.NewReader(content)) 135 contentType, bodyReader, err := inspectResponse("", br, int64(len(content))) 136 if err != nil { 137 t.Fatal(err) 138 } 139 if contentType != "text/plain" { 140 t.Fatalf("Content type should be 'text/plain' but is %q", contentType) 141 } 142 body, err := ioutil.ReadAll(bodyReader) 143 if err != nil { 144 t.Fatal(err) 145 } 146 if string(body) != textPlainDockerfile { 147 t.Fatalf("Corrupted response body %s", body) 148 } 149 }