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