|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2017-2020 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package minio |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "net/http" |
| 24 | + "net/http/httptest" |
| 25 | + "net/url" |
| 26 | + "os" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 31 | +) |
| 32 | + |
| 33 | +func Test200MultipartUploadWithSpaces(t *testing.T) { |
| 34 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 35 | + w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>`)) |
| 36 | + w.(http.Flusher).Flush() |
| 37 | + for i := 0; i < 10; i++ { |
| 38 | + time.Sleep(time.Second) |
| 39 | + w.Write([]byte(" ")) |
| 40 | + w.(http.Flusher).Flush() |
| 41 | + } |
| 42 | + |
| 43 | + w.Write([]byte(`<CompleteMultipartUploadResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Location>http://random/bucket/object</Location><Bucket>bucket</Bucket><Key>object</Key><ETag>"2b3ffa539769372e2df9553358fe26b2-2"</ETag></CompleteMultipartUploadResult>`)) |
| 44 | + })) |
| 45 | + |
| 46 | + srv, err := url.Parse(ts.URL) |
| 47 | + if err != nil { |
| 48 | + t.Fatal(err) |
| 49 | + } |
| 50 | + |
| 51 | + // Instantiate new minio client object. |
| 52 | + core, err := NewCore( |
| 53 | + srv.Host, |
| 54 | + &Options{ |
| 55 | + Creds: credentials.NewStaticV4("foo", "foo12345", ""), |
| 56 | + Secure: srv.Scheme == "https", |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + t.Fatal("Error:", err) |
| 60 | + } |
| 61 | + |
| 62 | + parts := []CompletePart{ |
| 63 | + {PartNumber: 1, ETag: "b386a859d8a22ff986c0b1252be34658"}, |
| 64 | + {PartNumber: 2, ETag: "78c577a580bbbba92845789cda1fa932"}, |
| 65 | + } |
| 66 | + |
| 67 | + foundUploadInfo, err := core.CompleteMultipartUpload(context.Background(), |
| 68 | + "bucket", |
| 69 | + "object", |
| 70 | + "jY1M2U5NWMtZGY2OC00ZjYyLTljZGYtYmZlOWEzODM3MDMwLjlmZWY5OGNlLWQ1Y2EtNDgwMC04N2Y4LWZkNTNkMDM4ZDdiMXgxNzQ4NjA0NzI0NzE4NjU3MTY3", |
| 71 | + parts, |
| 72 | + PutObjectOptions{}, |
| 73 | + ) |
| 74 | + if err != nil { |
| 75 | + t.Fatal("Error:", err) |
| 76 | + } |
| 77 | + |
| 78 | + expectedUploadInfo := UploadInfo{ |
| 79 | + Bucket: "bucket", |
| 80 | + Key: "object", |
| 81 | + ETag: "2b3ffa539769372e2df9553358fe26b2-2", |
| 82 | + Location: "http://random/bucket/object", |
| 83 | + } |
| 84 | + |
| 85 | + if foundUploadInfo != expectedUploadInfo { |
| 86 | + t.Fatalf("Unexpected upload info, expected: `%v`, found: `%v`", expectedUploadInfo, foundUploadInfo) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func Test200MultipartUploadWithError(t *testing.T) { |
| 91 | + const maxRetries = 3 |
| 92 | + retries := maxRetries |
| 93 | + |
| 94 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 95 | + retries-- |
| 96 | + w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>`)) |
| 97 | + w.(http.Flusher).Flush() |
| 98 | + for i := 0; i < 5; i++ { |
| 99 | + time.Sleep(time.Second) |
| 100 | + w.Write([]byte(" ")) |
| 101 | + w.(http.Flusher).Flush() |
| 102 | + } |
| 103 | + |
| 104 | + w.Write([]byte(`<Error><Code>SlowDownWrite</Code><Message>Resource requested is unwritable, please reduce your request rate</Message><Key>object</Key><BucketName>bucket</BucketName><Resource>/bucket/object</Resource><RequestId>18413E84F6C30613</RequestId><HostId>49371f38c0d7ec74eae2befc695360a3dfece04732914e58a4281759cd2eba4f</HostId></Error>`)) |
| 105 | + })) |
| 106 | + |
| 107 | + srv, err := url.Parse(ts.URL) |
| 108 | + if err != nil { |
| 109 | + t.Fatal(err) |
| 110 | + } |
| 111 | + |
| 112 | + // Instantiate new minio client object. |
| 113 | + core, err := NewCore( |
| 114 | + srv.Host, |
| 115 | + &Options{ |
| 116 | + Creds: credentials.NewStaticV4("foo", "foo12345", ""), |
| 117 | + Secure: srv.Scheme == "https", |
| 118 | + Region: "us-east-1", |
| 119 | + MaxRetries: retries, |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + t.Fatal("Error:", err) |
| 123 | + } |
| 124 | + |
| 125 | + parts := []CompletePart{ |
| 126 | + {PartNumber: 1, ETag: "b386a859d8a22ff986c0b1252be34658"}, |
| 127 | + {PartNumber: 2, ETag: "78c577a580bbbba92845789cda1fa932"}, |
| 128 | + } |
| 129 | + |
| 130 | + _, err = core.CompleteMultipartUpload(context.Background(), |
| 131 | + "bucket", |
| 132 | + "object", |
| 133 | + "jY1M2U5NWMtZGY2OC00ZjYyLTljZGYtYmZlOWEzODM3MDMwLjlmZWY5OGNlLWQ1Y2EtNDgwMC04N2Y4LWZkNTNkMDM4ZDdiMXgxNzQ4NjA0NzI0NzE4NjU3MTY3", |
| 134 | + parts, |
| 135 | + PutObjectOptions{}, |
| 136 | + ) |
| 137 | + if err == nil { |
| 138 | + t.Fatal("CompleteMultipartUpload() returned <nil>, which is unexpected") |
| 139 | + } |
| 140 | + |
| 141 | + expectedErrorMsg := "Resource requested is unwritable, please reduce your request rate" |
| 142 | + if err.Error() != expectedErrorMsg { |
| 143 | + t.Fatalf("Unexpected returned error, expected: `%v`, found: `%v`", expectedErrorMsg, err.Error()) |
| 144 | + } |
| 145 | + |
| 146 | + if retries != 0 { |
| 147 | + t.Fatalf("CompleteMultipart request was not retried enough times, expected: %d, found: %d", maxRetries, retries) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func Test200DeleteObjectsWithError(t *testing.T) { |
| 152 | + const maxRetries = 3 |
| 153 | + retries := maxRetries |
| 154 | + |
| 155 | + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 156 | + retries-- |
| 157 | + w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>`)) |
| 158 | + w.(http.Flusher).Flush() |
| 159 | + for i := 0; i < 5; i++ { |
| 160 | + time.Sleep(time.Second) |
| 161 | + w.Write([]byte(" ")) |
| 162 | + w.(http.Flusher).Flush() |
| 163 | + } |
| 164 | + |
| 165 | + w.Write([]byte(`<Error><Code>SlowDownWrite</Code><Message>Resource requested is unwritable, please reduce your request rate</Message><Key>object</Key><BucketName>bucket</BucketName><Resource>/bucket/object</Resource><RequestId>18413E84F6C30613</RequestId><HostId>49371f38c0d7ec74eae2befc695360a3dfece04732914e58a4281759cd2eba4f</HostId></Error>`)) |
| 166 | + })) |
| 167 | + |
| 168 | + srv, err := url.Parse(ts.URL) |
| 169 | + if err != nil { |
| 170 | + t.Fatal(err) |
| 171 | + } |
| 172 | + |
| 173 | + // Instantiate new minio client object. |
| 174 | + core, err := NewCore( |
| 175 | + srv.Host, |
| 176 | + &Options{ |
| 177 | + Creds: credentials.NewStaticV4("foo", "foo12345", ""), |
| 178 | + Secure: srv.Scheme == "https", |
| 179 | + Region: "us-east-1", |
| 180 | + MaxRetries: retries, |
| 181 | + }) |
| 182 | + if err != nil { |
| 183 | + t.Fatal("Error:", err) |
| 184 | + } |
| 185 | + |
| 186 | + core.TraceOn(os.Stderr) |
| 187 | + |
| 188 | + objs := make(chan ObjectInfo, 1000) |
| 189 | + for i := range 1000 { |
| 190 | + objs <- ObjectInfo{Key: fmt.Sprintf("obj-%d", i)} |
| 191 | + } |
| 192 | + close(objs) |
| 193 | + |
| 194 | + delErrCh := core.RemoveObjects(context.Background(), "bucket", objs, RemoveObjectsOptions{}) |
| 195 | + delErr := <-delErrCh |
| 196 | + err = delErr.Err |
| 197 | + if err == nil { |
| 198 | + t.Fatal("RemoveObjects() returned <nil>, which is unexpected") |
| 199 | + } |
| 200 | + |
| 201 | + expectedErrorMsg := "Resource requested is unwritable, please reduce your request rate" |
| 202 | + if err.Error() != expectedErrorMsg { |
| 203 | + t.Fatalf("Unexpected returned error, expected: `%v`, found: `%v`", expectedErrorMsg, err.Error()) |
| 204 | + } |
| 205 | + |
| 206 | + if retries != 0 { |
| 207 | + t.Fatalf("RemoveObjects() request was not retried enough times, expected: %d, found: %d", maxRetries, retries) |
| 208 | + } |
| 209 | +} |
0 commit comments