|
| 1 | +package s3 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/aws/aws-sdk-go/aws" |
| 14 | + "github.com/aws/aws-sdk-go/aws/credentials" |
| 15 | + "github.com/aws/aws-sdk-go/aws/session" |
| 16 | + awss3 "github.com/aws/aws-sdk-go/service/s3" |
| 17 | +) |
| 18 | + |
| 19 | +func TestCopyFileUsesCopyObjectAtLimit(t *testing.T) { |
| 20 | + copyRequests := 0 |
| 21 | + d := newTestS3Driver(t, func(w http.ResponseWriter, r *http.Request) { |
| 22 | + if r.Method != http.MethodPut || r.URL.Query().Get("uploadId") != "" { |
| 23 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.String()) |
| 24 | + w.WriteHeader(http.StatusBadRequest) |
| 25 | + return |
| 26 | + } |
| 27 | + copyRequests++ |
| 28 | + writeTestXML(t, w, `<CopyObjectResult><ETag>"copy"</ETag></CopyObjectResult>`) |
| 29 | + }) |
| 30 | + |
| 31 | + if err := d.copyFile(context.Background(), "source+file", "destination", maxCopyObjectSize); err != nil { |
| 32 | + t.Fatalf("copyFile: %v", err) |
| 33 | + } |
| 34 | + if copyRequests != 1 { |
| 35 | + t.Fatalf("copy requests = %d, want 1", copyRequests) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestCopyFileUsesMultipartCopyAboveLimit(t *testing.T) { |
| 40 | + size := maxCopyObjectSize + 1 |
| 41 | + wantParts := int((size + defaultCopyPartSize - 1) / defaultCopyPartSize) |
| 42 | + ranges := make(map[int]string, wantParts) |
| 43 | + completed := false |
| 44 | + aborted := false |
| 45 | + |
| 46 | + d := newTestS3Driver(t, func(w http.ResponseWriter, r *http.Request) { |
| 47 | + switch { |
| 48 | + case r.Method == http.MethodHead: |
| 49 | + w.Header().Set("Content-Length", strconv.FormatInt(size, 10)) |
| 50 | + w.Header().Set("Content-Type", "application/octet-stream") |
| 51 | + w.Header().Set("Cache-Control", "max-age=60") |
| 52 | + w.Header().Set("Content-Disposition", "attachment") |
| 53 | + w.Header().Set("Expires", "Wed, 21 Oct 2015 07:28:00 GMT") |
| 54 | + w.Header().Set("X-Amz-Meta-Source", "preserved") |
| 55 | + w.Header().Set("X-Amz-Website-Redirect-Location", "/redirect") |
| 56 | + w.WriteHeader(http.StatusOK) |
| 57 | + case r.Method == http.MethodPost && r.URL.Query().Has("uploads"): |
| 58 | + if got := r.Header.Get("Cache-Control"); got != "max-age=60" { |
| 59 | + t.Errorf("Cache-Control = %q, want %q", got, "max-age=60") |
| 60 | + } |
| 61 | + if got := r.Header.Get("Content-Disposition"); got != "attachment" { |
| 62 | + t.Errorf("Content-Disposition = %q, want %q", got, "attachment") |
| 63 | + } |
| 64 | + if got := r.Header.Get("Content-Type"); got != "application/octet-stream" { |
| 65 | + t.Errorf("Content-Type = %q, want %q", got, "application/octet-stream") |
| 66 | + } |
| 67 | + if got := r.Header.Get("Expires"); got != "Wed, 21 Oct 2015 07:28:00 GMT" { |
| 68 | + t.Errorf("Expires = %q, want an unchanged HTTP date", got) |
| 69 | + } |
| 70 | + if got := r.Header.Get("X-Amz-Meta-Source"); got != "preserved" { |
| 71 | + t.Errorf("metadata = %q, want %q", got, "preserved") |
| 72 | + } |
| 73 | + if got := r.Header.Get("X-Amz-Website-Redirect-Location"); got != "/redirect" { |
| 74 | + t.Errorf("website redirect = %q, want %q", got, "/redirect") |
| 75 | + } |
| 76 | + writeTestXML(t, w, `<InitiateMultipartUploadResult><UploadId>upload-id</UploadId></InitiateMultipartUploadResult>`) |
| 77 | + case r.Method == http.MethodPut && r.URL.Query().Get("uploadId") == "upload-id": |
| 78 | + partNumber, err := strconv.Atoi(r.URL.Query().Get("partNumber")) |
| 79 | + if err != nil { |
| 80 | + t.Errorf("invalid part number: %v", err) |
| 81 | + w.WriteHeader(http.StatusBadRequest) |
| 82 | + return |
| 83 | + } |
| 84 | + if got := r.Header.Get("X-Amz-Copy-Source"); !strings.Contains(got, "source%2Bfile") { |
| 85 | + t.Errorf("copy source = %q, want encoded source key", got) |
| 86 | + } |
| 87 | + ranges[partNumber] = r.Header.Get("X-Amz-Copy-Source-Range") |
| 88 | + writeTestXML(t, w, fmt.Sprintf(`<CopyPartResult><ETag>"part-%d"</ETag></CopyPartResult>`, partNumber)) |
| 89 | + case r.Method == http.MethodPost && r.URL.Query().Get("uploadId") == "upload-id": |
| 90 | + body, err := io.ReadAll(r.Body) |
| 91 | + if err != nil { |
| 92 | + t.Errorf("read complete body: %v", err) |
| 93 | + } |
| 94 | + if got := strings.Count(string(body), "<Part>"); got != wantParts { |
| 95 | + t.Errorf("completed parts = %d, want %d", got, wantParts) |
| 96 | + } |
| 97 | + completed = true |
| 98 | + writeTestXML(t, w, `<CompleteMultipartUploadResult><ETag>"complete"</ETag></CompleteMultipartUploadResult>`) |
| 99 | + case r.Method == http.MethodDelete && r.URL.Query().Get("uploadId") == "upload-id": |
| 100 | + aborted = true |
| 101 | + w.WriteHeader(http.StatusNoContent) |
| 102 | + default: |
| 103 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.String()) |
| 104 | + w.WriteHeader(http.StatusBadRequest) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + if err := d.copyFile(context.Background(), "source+file", "destination", size); err != nil { |
| 109 | + t.Fatalf("copyFile: %v", err) |
| 110 | + } |
| 111 | + if !completed { |
| 112 | + t.Fatal("multipart upload was not completed") |
| 113 | + } |
| 114 | + if aborted { |
| 115 | + t.Fatal("successful multipart upload was aborted") |
| 116 | + } |
| 117 | + if len(ranges) != wantParts { |
| 118 | + t.Fatalf("copied parts = %d, want %d", len(ranges), wantParts) |
| 119 | + } |
| 120 | + if got := ranges[1]; got != fmt.Sprintf("bytes=0-%d", defaultCopyPartSize-1) { |
| 121 | + t.Errorf("first range = %q", got) |
| 122 | + } |
| 123 | + lastStart := int64(wantParts-1) * defaultCopyPartSize |
| 124 | + if got := ranges[wantParts]; got != fmt.Sprintf("bytes=%d-%d", lastStart, size-1) { |
| 125 | + t.Errorf("last range = %q", got) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestCopyFileMultipartAbortsOnPartFailure(t *testing.T) { |
| 130 | + size := maxCopyObjectSize + 1 |
| 131 | + aborted := false |
| 132 | + completed := false |
| 133 | + |
| 134 | + d := newTestS3Driver(t, func(w http.ResponseWriter, r *http.Request) { |
| 135 | + switch { |
| 136 | + case r.Method == http.MethodHead: |
| 137 | + w.Header().Set("Content-Length", strconv.FormatInt(size, 10)) |
| 138 | + w.WriteHeader(http.StatusOK) |
| 139 | + case r.Method == http.MethodPost && r.URL.Query().Has("uploads"): |
| 140 | + writeTestXML(t, w, `<InitiateMultipartUploadResult><UploadId>upload-id</UploadId></InitiateMultipartUploadResult>`) |
| 141 | + case r.Method == http.MethodPut && r.URL.Query().Get("uploadId") == "upload-id": |
| 142 | + w.WriteHeader(http.StatusInternalServerError) |
| 143 | + writeTestXML(t, w, `<Error><Code>InternalError</Code><Message>copy failed</Message></Error>`) |
| 144 | + case r.Method == http.MethodDelete && r.URL.Query().Get("uploadId") == "upload-id": |
| 145 | + aborted = true |
| 146 | + w.WriteHeader(http.StatusNoContent) |
| 147 | + case r.Method == http.MethodPost && r.URL.Query().Get("uploadId") == "upload-id": |
| 148 | + completed = true |
| 149 | + w.WriteHeader(http.StatusOK) |
| 150 | + default: |
| 151 | + t.Errorf("unexpected request: %s %s", r.Method, r.URL.String()) |
| 152 | + w.WriteHeader(http.StatusBadRequest) |
| 153 | + } |
| 154 | + }) |
| 155 | + |
| 156 | + if err := d.copyFile(context.Background(), "source", "destination", size); err == nil { |
| 157 | + t.Fatal("copyFile returned nil error") |
| 158 | + } |
| 159 | + if !aborted { |
| 160 | + t.Fatal("failed multipart upload was not aborted") |
| 161 | + } |
| 162 | + if completed { |
| 163 | + t.Fatal("failed multipart upload was completed") |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestGetCopyPartSize(t *testing.T) { |
| 168 | + partSize, err := getCopyPartSize(defaultCopyPartSize * maxCopyParts) |
| 169 | + if err != nil { |
| 170 | + t.Fatalf("getCopyPartSize: %v", err) |
| 171 | + } |
| 172 | + if partSize != defaultCopyPartSize { |
| 173 | + t.Fatalf("part size = %d, want %d", partSize, defaultCopyPartSize) |
| 174 | + } |
| 175 | + |
| 176 | + partSize, err = getCopyPartSize(defaultCopyPartSize*maxCopyParts + 1) |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("getCopyPartSize: %v", err) |
| 179 | + } |
| 180 | + if partSize != defaultCopyPartSize+1 { |
| 181 | + t.Fatalf("grown part size = %d, want %d", partSize, defaultCopyPartSize+1) |
| 182 | + } |
| 183 | + |
| 184 | + if _, err := getCopyPartSize(maxCopyPartSize*maxCopyParts + 1); err == nil { |
| 185 | + t.Fatal("getCopyPartSize returned nil error for an oversized object") |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func newTestS3Driver(t *testing.T, handler http.HandlerFunc) *S3 { |
| 190 | + t.Helper() |
| 191 | + server := httptest.NewServer(handler) |
| 192 | + t.Cleanup(server.Close) |
| 193 | + sess, err := session.NewSession(&aws.Config{ |
| 194 | + Credentials: credentials.NewStaticCredentials("access-key", "secret-key", ""), |
| 195 | + Endpoint: aws.String(server.URL), |
| 196 | + Region: aws.String("us-east-1"), |
| 197 | + S3ForcePathStyle: aws.Bool(true), |
| 198 | + MaxRetries: aws.Int(0), |
| 199 | + }) |
| 200 | + if err != nil { |
| 201 | + t.Fatalf("create AWS session: %v", err) |
| 202 | + } |
| 203 | + return &S3{ |
| 204 | + Addition: Addition{Bucket: "bucket"}, |
| 205 | + client: awss3.New(sess), |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func writeTestXML(t *testing.T, w http.ResponseWriter, body string) { |
| 210 | + t.Helper() |
| 211 | + w.Header().Set("Content-Type", "application/xml") |
| 212 | + if _, err := io.WriteString(w, body); err != nil { |
| 213 | + t.Errorf("write response: %v", err) |
| 214 | + } |
| 215 | +} |
0 commit comments