| 1 |
package s3 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/require" |
| 15 |
) |
| 16 |
|
| 17 |
func TestParseURL_Success(t *testing.T) { |
| 18 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket/attachments?region=us-east-1") |
| 19 |
require.Nil(t, err) |
| 20 |
require.Equal(t, "my-bucket", cfg.Bucket) |
| 21 |
require.Equal(t, "attachments", cfg.Prefix) |
| 22 |
require.Equal(t, "us-east-1", cfg.Region) |
| 23 |
require.Equal(t, "AKID", cfg.AccessKey) |
| 24 |
require.Equal(t, "SECRET", cfg.SecretKey) |
| 25 |
require.Equal(t, "https", cfg.Scheme) |
| 26 |
require.Equal(t, "s3.us-east-1.amazonaws.com", cfg.Endpoint) |
| 27 |
require.False(t, cfg.PathStyle) |
| 28 |
} |
| 29 |
|
| 30 |
func TestParseURL_NoPrefix(t *testing.T) { |
| 31 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1") |
| 32 |
require.Nil(t, err) |
| 33 |
require.Equal(t, "my-bucket", cfg.Bucket) |
| 34 |
require.Equal(t, "", cfg.Prefix) |
| 35 |
} |
| 36 |
|
| 37 |
func TestParseURL_WithEndpoint(t *testing.T) { |
| 38 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket/prefix?region=us-east-1&endpoint=https://s3.example.com") |
| 39 |
require.Nil(t, err) |
| 40 |
require.Equal(t, "my-bucket", cfg.Bucket) |
| 41 |
require.Equal(t, "prefix", cfg.Prefix) |
| 42 |
require.Equal(t, "https", cfg.Scheme) |
| 43 |
require.Equal(t, "s3.example.com", cfg.Endpoint) |
| 44 |
require.True(t, cfg.PathStyle) |
| 45 |
} |
| 46 |
|
| 47 |
func TestParseURL_EndpointHTTP(t *testing.T) { |
| 48 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1&endpoint=http://localhost:9000") |
| 49 |
require.Nil(t, err) |
| 50 |
require.Equal(t, "http", cfg.Scheme) |
| 51 |
require.Equal(t, "localhost:9000", cfg.Endpoint) |
| 52 |
require.True(t, cfg.PathStyle) |
| 53 |
} |
| 54 |
|
| 55 |
func TestParseURL_EndpointNoScheme(t *testing.T) { |
| 56 |
// A bare host:port endpoint (no scheme) must default to https for backward compatibility. |
| 57 |
// Without this, url.Parse treats the host as the scheme ("localhost:9000" -> scheme "localhost"). |
| 58 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1&endpoint=localhost:9000") |
| 59 |
require.Nil(t, err) |
| 60 |
require.Equal(t, "https", cfg.Scheme) |
| 61 |
require.Equal(t, "localhost:9000", cfg.Endpoint) |
| 62 |
require.True(t, cfg.PathStyle) |
| 63 |
require.Equal(t, "https://localhost:9000/my-bucket", cfg.BucketURL()) |
| 64 |
} |
| 65 |
|
| 66 |
func TestParseURL_EndpointNoSchemeHostname(t *testing.T) { |
| 67 |
// A dotted hostname with a port and no scheme must also default to https |
| 68 |
// ("minio.example.com:9000" must not become scheme "minio.example.com"). |
| 69 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1&endpoint=minio.example.com:9000") |
| 70 |
require.Nil(t, err) |
| 71 |
require.Equal(t, "https", cfg.Scheme) |
| 72 |
require.Equal(t, "minio.example.com:9000", cfg.Endpoint) |
| 73 |
require.Equal(t, "https://minio.example.com:9000/my-bucket", cfg.BucketURL()) |
| 74 |
} |
| 75 |
|
| 76 |
func TestParseURL_EndpointTrailingSlash(t *testing.T) { |
| 77 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1&endpoint=https://s3.example.com/") |
| 78 |
require.Nil(t, err) |
| 79 |
require.Equal(t, "s3.example.com", cfg.Endpoint) |
| 80 |
} |
| 81 |
|
| 82 |
func TestParseURL_NestedPrefix(t *testing.T) { |
| 83 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket/a/b/c?region=us-east-1") |
| 84 |
require.Nil(t, err) |
| 85 |
require.Equal(t, "my-bucket", cfg.Bucket) |
| 86 |
require.Equal(t, "a/b/c", cfg.Prefix) |
| 87 |
} |
| 88 |
|
| 89 |
func TestParseURL_MissingRegion(t *testing.T) { |
| 90 |
_, err := ParseURL("s3://AKID:SECRET@my-bucket") |
| 91 |
require.Error(t, err) |
| 92 |
require.Contains(t, err.Error(), "region") |
| 93 |
} |
| 94 |
|
| 95 |
func TestParseURL_MissingCredentials(t *testing.T) { |
| 96 |
_, err := ParseURL("s3://my-bucket?region=us-east-1") |
| 97 |
require.Error(t, err) |
| 98 |
require.Contains(t, err.Error(), "access key") |
| 99 |
} |
| 100 |
|
| 101 |
func TestParseURL_MissingSecretKey(t *testing.T) { |
| 102 |
_, err := ParseURL("s3://AKID@my-bucket?region=us-east-1") |
| 103 |
require.Error(t, err) |
| 104 |
require.Contains(t, err.Error(), "secret key") |
| 105 |
} |
| 106 |
|
| 107 |
func TestParseURL_WrongScheme(t *testing.T) { |
| 108 |
_, err := ParseURL("http://AKID:SECRET@my-bucket?region=us-east-1") |
| 109 |
require.Error(t, err) |
| 110 |
require.Contains(t, err.Error(), "scheme") |
| 111 |
} |
| 112 |
|
| 113 |
func TestParseURL_EmptyBucket(t *testing.T) { |
| 114 |
_, err := ParseURL("s3://AKID:SECRET@?region=us-east-1") |
| 115 |
require.Error(t, err) |
| 116 |
require.Contains(t, err.Error(), "bucket") |
| 117 |
} |
| 118 |
|
| 119 |
func TestParseURL_DisableHTTP2(t *testing.T) { |
| 120 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1&disable_http2=true") |
| 121 |
require.Nil(t, err) |
| 122 |
require.True(t, cfg.DisableHTTP2) |
| 123 |
} |
| 124 |
|
| 125 |
func TestParseURL_DisableHTTP2_NotSet(t *testing.T) { |
| 126 |
cfg, err := ParseURL("s3://AKID:SECRET@my-bucket?region=us-east-1") |
| 127 |
require.Nil(t, err) |
| 128 |
require.False(t, cfg.DisableHTTP2) |
| 129 |
} |
| 130 |
|
| 131 |
// --- Unit tests: URL construction --- |
| 132 |
|
| 133 |
func TestConfig_BucketURL_PathStyle(t *testing.T) { |
| 134 |
c := &Config{Endpoint: "s3.example.com", Bucket: "my-bucket", PathStyle: true} |
| 135 |
require.Equal(t, "https://s3.example.com/my-bucket", c.BucketURL()) |
| 136 |
} |
| 137 |
|
| 138 |
func TestConfig_BucketURL_PathStyle_EndpointHTTP(t *testing.T) { |
| 139 |
c := &Config{Scheme: "http", Endpoint: "localhost:9000", Bucket: "b", PathStyle: true} |
| 140 |
require.Equal(t, "http://localhost:9000/b", c.BucketURL()) |
| 141 |
} |
| 142 |
|
| 143 |
func TestConfig_BucketURL_VirtualHosted(t *testing.T) { |
| 144 |
c := &Config{Endpoint: "s3.us-east-1.amazonaws.com", Bucket: "my-bucket", PathStyle: false} |
| 145 |
require.Equal(t, "https://my-bucket.s3.us-east-1.amazonaws.com", c.BucketURL()) |
| 146 |
} |
| 147 |
|
| 148 |
func TestConfig_ObjectURL_PathStyle(t *testing.T) { |
| 149 |
c := &Config{Endpoint: "s3.example.com", Bucket: "my-bucket", Prefix: "prefix", PathStyle: true} |
| 150 |
require.Equal(t, "https://s3.example.com/my-bucket/prefix/obj", c.ObjectURL("obj")) |
| 151 |
} |
| 152 |
|
| 153 |
func TestConfig_ObjectURL_VirtualHosted(t *testing.T) { |
| 154 |
c := &Config{Endpoint: "s3.us-east-1.amazonaws.com", Bucket: "my-bucket", Prefix: "prefix", PathStyle: false} |
| 155 |
require.Equal(t, "https://my-bucket.s3.us-east-1.amazonaws.com/prefix/obj", c.ObjectURL("obj")) |
| 156 |
} |
| 157 |
|
| 158 |
func TestConfig_HostHeader_PathStyle(t *testing.T) { |
| 159 |
c := &Config{Endpoint: "s3.example.com", Bucket: "my-bucket", PathStyle: true} |
| 160 |
require.Equal(t, "s3.example.com", c.HostHeader()) |
| 161 |
} |
| 162 |
|
| 163 |
func TestConfig_HostHeader_VirtualHosted(t *testing.T) { |
| 164 |
c := &Config{Endpoint: "s3.us-east-1.amazonaws.com", Bucket: "my-bucket", PathStyle: false} |
| 165 |
require.Equal(t, "my-bucket.s3.us-east-1.amazonaws.com", c.HostHeader()) |
| 166 |
} |
| 167 |
|
| 168 |
func TestConfig_ObjectKey(t *testing.T) { |
| 169 |
c := &Config{Prefix: "attachments"} |
| 170 |
require.Equal(t, "attachments/file123", c.ObjectKey("file123")) |
| 171 |
|
| 172 |
c2 := &Config{Prefix: ""} |
| 173 |
require.Equal(t, "file123", c2.ObjectKey("file123")) |
| 174 |
} |
| 175 |
|
| 176 |
func TestConfig_ListPrefix(t *testing.T) { |
| 177 |
c := &Config{Prefix: "attachments"} |
| 178 |
require.Equal(t, "attachments/", c.ListPrefix()) |
| 179 |
|
| 180 |
c2 := &Config{Prefix: ""} |
| 181 |
require.Equal(t, "", c2.ListPrefix()) |
| 182 |
} |
| 183 |
|
| 184 |
// --- Integration tests using real S3 --- |
| 185 |
|
| 186 |
func TestClient_PutGetObject(t *testing.T) { |
| 187 |
client := newTestClient(t) |
| 188 |
ctx := context.Background() |
| 189 |
|
| 190 |
// Put |
| 191 |
err := client.PutObject(ctx, "test-key", strings.NewReader("hello world"), 0) |
| 192 |
require.Nil(t, err) |
| 193 |
|
| 194 |
// Get |
| 195 |
reader, size, err := client.GetObject(ctx, "test-key") |
| 196 |
require.Nil(t, err) |
| 197 |
require.Equal(t, int64(11), size) |
| 198 |
data, err := io.ReadAll(reader) |
| 199 |
reader.Close() |
| 200 |
require.Nil(t, err) |
| 201 |
require.Equal(t, "hello world", string(data)) |
| 202 |
} |
| 203 |
|
| 204 |
func TestClient_GetObject_NotFound(t *testing.T) { |
| 205 |
client := newTestClient(t) |
| 206 |
|
| 207 |
_, _, err := client.GetObject(context.Background(), "nonexistent") |
| 208 |
require.Error(t, err) |
| 209 |
} |
| 210 |
|
| 211 |
func TestClient_DeleteObjects(t *testing.T) { |
| 212 |
client := newTestClient(t) |
| 213 |
ctx := context.Background() |
| 214 |
|
| 215 |
// Put several objects |
| 216 |
for i := 0; i < 5; i++ { |
| 217 |
err := client.PutObject(ctx, fmt.Sprintf("del-%d", i), bytes.NewReader([]byte("data")), 0) |
| 218 |
require.Nil(t, err) |
| 219 |
} |
| 220 |
waitForCount(t, client, 5) |
| 221 |
|
| 222 |
// Delete some |
| 223 |
err := client.DeleteObjects(ctx, []string{"del-1", "del-3"}) |
| 224 |
require.Nil(t, err) |
| 225 |
waitForCount(t, client, 3) |
| 226 |
|
| 227 |
// Verify deleted ones are gone |
| 228 |
_, _, err = client.GetObject(ctx, "del-1") |
| 229 |
require.Error(t, err) |
| 230 |
_, _, err = client.GetObject(ctx, "del-3") |
| 231 |
require.Error(t, err) |
| 232 |
|
| 233 |
// Verify remaining ones are still there |
| 234 |
for _, key := range []string{"del-0", "del-2", "del-4"} { |
| 235 |
reader, _, err := client.GetObject(ctx, key) |
| 236 |
require.Nil(t, err) |
| 237 |
reader.Close() |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func TestClient_ListObjects(t *testing.T) { |
| 242 |
client := newTestClient(t) |
| 243 |
ctx := context.Background() |
| 244 |
|
| 245 |
for i := 0; i < 3; i++ { |
| 246 |
err := client.PutObject(ctx, fmt.Sprintf("list-%d", i), bytes.NewReader([]byte("x")), 0) |
| 247 |
require.Nil(t, err) |
| 248 |
} |
| 249 |
waitForCount(t, client, 3) |
| 250 |
} |
| 251 |
|
| 252 |
func TestClient_ListObjects_Pagination(t *testing.T) { |
| 253 |
client := newTestClient(t) |
| 254 |
ctx := context.Background() |
| 255 |
|
| 256 |
// Create 1010 objects in parallel (5 goroutines) |
| 257 |
const total = 1010 |
| 258 |
const workers = 5 |
| 259 |
var wg sync.WaitGroup |
| 260 |
errs := make(chan error, total) |
| 261 |
for w := 0; w < workers; w++ { |
| 262 |
wg.Add(1) |
| 263 |
go func(start int) { |
| 264 |
defer wg.Done() |
| 265 |
for i := start; i < total; i += workers { |
| 266 |
if err := client.PutObject(ctx, fmt.Sprintf("pg-%04d", i), bytes.NewReader([]byte("x")), 0); err != nil { |
| 267 |
errs <- err |
| 268 |
return |
| 269 |
} |
| 270 |
} |
| 271 |
}(w) |
| 272 |
} |
| 273 |
wg.Wait() |
| 274 |
close(errs) |
| 275 |
for err := range errs { |
| 276 |
require.Nil(t, err) |
| 277 |
} |
| 278 |
waitForCount(t, client, total) |
| 279 |
} |
| 280 |
|
| 281 |
func TestClient_PutObject_LargeBody(t *testing.T) { |
| 282 |
client := newTestClient(t) |
| 283 |
ctx := context.Background() |
| 284 |
|
| 285 |
// 1 MB object |
| 286 |
data := make([]byte, 1024*1024) |
| 287 |
for i := range data { |
| 288 |
data[i] = byte(i % 256) |
| 289 |
} |
| 290 |
err := client.PutObject(ctx, "large", bytes.NewReader(data), 0) |
| 291 |
require.Nil(t, err) |
| 292 |
|
| 293 |
reader, size, err := client.GetObject(ctx, "large") |
| 294 |
require.Nil(t, err) |
| 295 |
require.Equal(t, int64(1024*1024), size) |
| 296 |
got, err := io.ReadAll(reader) |
| 297 |
reader.Close() |
| 298 |
require.Nil(t, err) |
| 299 |
require.Equal(t, data, got) |
| 300 |
} |
| 301 |
|
| 302 |
func TestClient_PutObject_ChunkedUpload(t *testing.T) { |
| 303 |
client := newTestClient(t) |
| 304 |
ctx := context.Background() |
| 305 |
|
| 306 |
// 12 MB object, exceeds 5 MB partSize, triggers multipart upload path |
| 307 |
data := make([]byte, 12*1024*1024) |
| 308 |
for i := range data { |
| 309 |
data[i] = byte(i % 256) |
| 310 |
} |
| 311 |
err := client.PutObject(ctx, "multipart", bytes.NewReader(data), 0) |
| 312 |
require.Nil(t, err) |
| 313 |
|
| 314 |
reader, size, err := client.GetObject(ctx, "multipart") |
| 315 |
require.Nil(t, err) |
| 316 |
require.Equal(t, int64(12*1024*1024), size) |
| 317 |
got, err := io.ReadAll(reader) |
| 318 |
reader.Close() |
| 319 |
require.Nil(t, err) |
| 320 |
require.Equal(t, data, got) |
| 321 |
} |
| 322 |
|
| 323 |
func TestClient_PutObject_ExactPartSize(t *testing.T) { |
| 324 |
client := newTestClient(t) |
| 325 |
ctx := context.Background() |
| 326 |
|
| 327 |
// Exactly 5 MB (partSize), should use the simple put path (ReadFull succeeds fully) |
| 328 |
data := make([]byte, 5*1024*1024) |
| 329 |
for i := range data { |
| 330 |
data[i] = byte(i % 256) |
| 331 |
} |
| 332 |
err := client.PutObject(ctx, "exact", bytes.NewReader(data), 0) |
| 333 |
require.Nil(t, err) |
| 334 |
|
| 335 |
reader, size, err := client.GetObject(ctx, "exact") |
| 336 |
require.Nil(t, err) |
| 337 |
require.Equal(t, int64(5*1024*1024), size) |
| 338 |
got, err := io.ReadAll(reader) |
| 339 |
reader.Close() |
| 340 |
require.Nil(t, err) |
| 341 |
require.Equal(t, data, got) |
| 342 |
} |
| 343 |
|
| 344 |
func TestClient_PutObject_StreamingExactLength(t *testing.T) { |
| 345 |
client := newTestClient(t) |
| 346 |
ctx := context.Background() |
| 347 |
|
| 348 |
// untrustedLength matches body exactly — streams directly via putObject |
| 349 |
err := client.PutObject(ctx, "stream-exact", strings.NewReader("hello world"), 11) |
| 350 |
require.Nil(t, err) |
| 351 |
|
| 352 |
reader, size, err := client.GetObject(ctx, "stream-exact") |
| 353 |
require.Nil(t, err) |
| 354 |
require.Equal(t, int64(11), size) |
| 355 |
got, err := io.ReadAll(reader) |
| 356 |
reader.Close() |
| 357 |
require.Nil(t, err) |
| 358 |
require.Equal(t, "hello world", string(got)) |
| 359 |
} |
| 360 |
|
| 361 |
func TestClient_PutObject_StreamingBodyLongerThanClaimed(t *testing.T) { |
| 362 |
client := newTestClient(t) |
| 363 |
ctx := context.Background() |
| 364 |
|
| 365 |
// Body has 11 bytes, but we claim 5 — only first 5 bytes should be stored |
| 366 |
err := client.PutObject(ctx, "stream-long", strings.NewReader("hello world"), 5) |
| 367 |
require.Nil(t, err) |
| 368 |
|
| 369 |
reader, size, err := client.GetObject(ctx, "stream-long") |
| 370 |
require.Nil(t, err) |
| 371 |
require.Equal(t, int64(5), size) |
| 372 |
got, err := io.ReadAll(reader) |
| 373 |
reader.Close() |
| 374 |
require.Nil(t, err) |
| 375 |
require.Equal(t, "hello", string(got)) |
| 376 |
} |
| 377 |
|
| 378 |
func TestClient_PutObject_StreamingBodyShorterThanClaimed(t *testing.T) { |
| 379 |
client := newTestClient(t) |
| 380 |
ctx := context.Background() |
| 381 |
|
| 382 |
// Body has 5 bytes, but we claim 100 — should fail |
| 383 |
err := client.PutObject(ctx, "stream-short", strings.NewReader("hello"), 100) |
| 384 |
require.Error(t, err) |
| 385 |
|
| 386 |
// Object should not exist |
| 387 |
_, _, err = client.GetObject(ctx, "stream-short") |
| 388 |
require.Error(t, err) |
| 389 |
} |
| 390 |
|
| 391 |
func TestClient_PutObject_NestedKey(t *testing.T) { |
| 392 |
client := newTestClient(t) |
| 393 |
ctx := context.Background() |
| 394 |
|
| 395 |
err := client.PutObject(ctx, "deep/nested/prefix/file.txt", strings.NewReader("nested"), 0) |
| 396 |
require.Nil(t, err) |
| 397 |
|
| 398 |
reader, _, err := client.GetObject(ctx, "deep/nested/prefix/file.txt") |
| 399 |
require.Nil(t, err) |
| 400 |
data, _ := io.ReadAll(reader) |
| 401 |
reader.Close() |
| 402 |
require.Equal(t, "nested", string(data)) |
| 403 |
} |
| 404 |
|
| 405 |
func newTestClient(t *testing.T) *Client { |
| 406 |
t.Helper() |
| 407 |
s3URL := os.Getenv("NTFY_TEST_S3_URL") |
| 408 |
if s3URL == "" { |
| 409 |
t.Skip("NTFY_TEST_S3_URL not set") |
| 410 |
} |
| 411 |
cfg, err := ParseURL(s3URL) |
| 412 |
require.Nil(t, err) |
| 413 |
// Use per-test prefix to isolate objects between tests |
| 414 |
if cfg.Prefix != "" { |
| 415 |
cfg.Prefix = cfg.Prefix + "/testpkg-s3/" + t.Name() |
| 416 |
} else { |
| 417 |
cfg.Prefix = "testpkg-s3/" + t.Name() |
| 418 |
} |
| 419 |
client := New(cfg) |
| 420 |
deleteAllObjects(t, client) |
| 421 |
t.Cleanup(func() { deleteAllObjects(t, client) }) |
| 422 |
return client |
| 423 |
} |
| 424 |
|
| 425 |
func deleteAllObjects(t *testing.T, client *Client) { |
| 426 |
t.Helper() |
| 427 |
for i := 0; i < 60; i++ { |
| 428 |
objects, err := client.ListObjectsV2(context.Background()) |
| 429 |
require.Nil(t, err) |
| 430 |
if len(objects) == 0 { |
| 431 |
return |
| 432 |
} |
| 433 |
keys := make([]string, len(objects)) |
| 434 |
for j, obj := range objects { |
| 435 |
keys[j] = obj.Key |
| 436 |
} |
| 437 |
require.Nil(t, client.DeleteObjects(context.Background(), keys)) |
| 438 |
time.Sleep(500 * time.Millisecond) |
| 439 |
} |
| 440 |
t.Fatal("timed out waiting for bucket to be empty") |
| 441 |
} |
| 442 |
|
| 443 |
func waitForCount(t *testing.T, client *Client, expected int) { |
| 444 |
t.Helper() |
| 445 |
for i := 0; i < 60; i++ { |
| 446 |
objects, err := client.ListObjectsV2(context.Background()) |
| 447 |
require.Nil(t, err) |
| 448 |
if len(objects) == expected { |
| 449 |
return |
| 450 |
} |
| 451 |
time.Sleep(500 * time.Millisecond) |
| 452 |
} |
| 453 |
objects, _ := client.ListObjectsV2(context.Background()) |
| 454 |
t.Fatalf("timed out waiting for %d objects, got %d", expected, len(objects)) |
| 455 |
} |