s7cmd 1.5.0

Reliable, flexible, and fast command-line tool for Amazon S3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! E2E tests for `s7cmd rename`.
//!
//! These tests hit real AWS and are reserved for the user to run. The
//! CI executor only compile-checks them (under `RUSTFLAGS="--cfg e2e_test"`)
//! to keep rename's end-to-end behavior covered without spending money or
//! touching production buckets in CI.
//!
//! Run with:
//! ```sh
//! RUSTFLAGS="--cfg e2e_test" cargo test --test e2e_rename -- --test-threads=1
//! ```
//!
//! Requires the `s7cmd-e2e-test` AWS profile to be configured.
//!
//! Coverage:
//! - Basic rename: source key moves to destination, source no longer exists.
//! - Content preservation: renamed object's bytes match the original.
//! - Source-not-found: exit 1, no crash.
//! - Dry run: no actual rename, exit 0, `[dry-run]` in stderr.
//! - Conditional `--source-if-match`: matching ETag succeeds, wrong ETag fails.
//! - Conditional `--source-if-none-match`: passing `*` means "rename only if
//!   source has no ETag"; object exists so the condition fails with 412
//!   (precondition failure → exit 1).
//! - Conditional `--target-if-none-match`: passing `*` means "rename only if
//!   destination has no ETag"; destination absent → succeeds;
//!   destination present → precondition failure → exit 1.
//! - Conditional `--target-if-match`: matching destination ETag succeeds;
//!   wrong ETag fails.
//! - Special characters in key: spaces, slashes, Unicode percent-encoded correctly.
//! - CLI validation: non-Express-One-Zone bucket name → exit 2.
//! - CLI validation: source and target in different buckets → exit 2.

#![cfg(e2e_test)]

mod common;

use common::{EXPRESS_ONE_ZONE_AZ, PROFILE_NAME, TestHelper};

use std::process::{Command, Output, Stdio};

/// Suffix appended to base bucket names to form Express One Zone directory bucket names.
const EXPRESS_ONE_ZONE_BUCKET_SUFFIX: &str = "--apne1-az4--x-s3";

const EXIT_CODE_SUCCESS: i32 = 0;
const EXIT_CODE_ERROR: i32 = 1;
const EXIT_CODE_CLAP_ARG_ERROR: i32 = 2;

fn run_s7cmd(args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_s7cmd"))
        .args(args)
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output()
        .expect("spawn s7cmd")
}

/// Create a fresh Express One Zone directory bucket, put a small object
/// into it with the SDK, and return `(bucket_name, etag)`.
async fn setup_bucket_with_object(helper: &TestHelper, key: &str, body: &[u8]) -> (String, String) {
    let bucket = format!(
        "s7e2e-{}{}",
        uuid::Uuid::new_v4(),
        EXPRESS_ONE_ZONE_BUCKET_SUFFIX
    );
    helper
        .create_directory_bucket(&bucket, EXPRESS_ONE_ZONE_AZ)
        .await;
    helper.put_object(&bucket, key, body.to_vec()).await;
    let head = helper.head_object(&bucket, key, None).await;
    let etag = head.e_tag().unwrap().to_string();
    (bucket, etag)
}

// ---------------------------------------------------------------
// Basic rename (3 tests)
// ---------------------------------------------------------------

/// Happy path: rename an object within the same Express One Zone bucket.
/// After rename: destination exists, source is gone.
#[tokio::test]
async fn rename_basic_source_gone_destination_present() {
    let helper = TestHelper::new().await;
    let src_key = "src/basic.txt";
    let dst_key = "dst/basic.txt";
    let body = b"rename basic test body";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&["rename", "--source-profile", PROFILE_NAME, &src, &dst]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(
        !helper.is_object_exist(&bucket, src_key, None).await,
        "source must be gone after rename"
    );
    assert!(
        helper.is_object_exist(&bucket, dst_key, None).await,
        "destination must exist after rename"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

/// Rename preserves object content: bytes at destination match original.
#[tokio::test]
async fn rename_preserves_content() {
    let helper = TestHelper::new().await;
    let src_key = "preserve_src.txt";
    let dst_key = "preserve_dst.txt";
    let body = b"content to be preserved across rename";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&["rename", "--source-profile", PROFILE_NAME, &src, &dst]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );

    let dst_bytes = helper.get_object_bytes(&bucket, dst_key, None).await;
    assert_eq!(
        dst_bytes, body,
        "renamed object content must match original"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

/// Rename when the source key does not exist must exit 1 without panicking.
#[tokio::test]
async fn rename_missing_source_exits_error() {
    let helper = TestHelper::new().await;
    let bucket = format!(
        "s7e2e-{}{}",
        uuid::Uuid::new_v4(),
        EXPRESS_ONE_ZONE_BUCKET_SUFFIX
    );
    helper
        .create_directory_bucket(&bucket, EXPRESS_ONE_ZONE_AZ)
        .await;

    let src = format!("s3://{}/nonexistent.txt", bucket);
    let dst = format!("s3://{}/dest.txt", bucket);

    let output = run_s7cmd(&["rename", "--source-profile", PROFILE_NAME, &src, &dst]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_ERROR),
        "rename of nonexistent source must exit 1; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Dry run (1 test)
// ---------------------------------------------------------------

/// `--dry-run` must exit 0, emit `[dry-run]` in stderr, and leave both
/// source present and destination absent (no actual rename performed).
#[tokio::test]
async fn rename_dry_run_no_side_effects() {
    let helper = TestHelper::new().await;
    let src_key = "dryrun_src.txt";
    let dst_key = "dryrun_dst.txt";
    let body = b"dry run body";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--dry-run",
        &src,
        &dst,
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename --dry-run must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("[dry-run]"),
        "stderr must contain [dry-run] prefix; got: {stderr}"
    );
    assert!(
        helper.is_object_exist(&bucket, src_key, None).await,
        "source must still exist after --dry-run"
    );
    assert!(
        !helper.is_object_exist(&bucket, dst_key, None).await,
        "destination must not exist after --dry-run"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Conditional checks — source-if-match (2 tests)
// ---------------------------------------------------------------

/// `--source-if-match <correct ETag>` must succeed and rename the object.
#[tokio::test]
async fn rename_source_if_match_correct_etag_succeeds() {
    let helper = TestHelper::new().await;
    let src_key = "sim_src.txt";
    let dst_key = "sim_dst.txt";
    let body = b"source-if-match test";
    let (bucket, etag) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--source-if-match",
        &etag,
        &src,
        &dst,
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with correct --source-if-match must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(!helper.is_object_exist(&bucket, src_key, None).await);
    assert!(helper.is_object_exist(&bucket, dst_key, None).await);

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

/// `--source-if-match <wrong ETag>` must fail (412 precondition) and
/// leave the source object intact.
#[tokio::test]
async fn rename_source_if_match_wrong_etag_fails() {
    let helper = TestHelper::new().await;
    let src_key = "sim_fail_src.txt";
    let dst_key = "sim_fail_dst.txt";
    let body = b"source-if-match fail test";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--source-if-match",
        "\"aaaabbbbccccdddd0000111122223333\"",
        &src,
        &dst,
    ]);

    assert_ne!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with wrong --source-if-match must not succeed; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(
        helper.is_object_exist(&bucket, src_key, None).await,
        "source must remain after precondition failure"
    );
    assert!(
        !helper.is_object_exist(&bucket, dst_key, None).await,
        "destination must not be created after precondition failure"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Conditional checks — source-if-none-match (1 test)
// ---------------------------------------------------------------

/// `--source-if-none-match *` means "rename only if the source has no ETag".
/// Because the source object exists it always has an ETag, so the condition
/// is false and the API returns a 412 precondition failure.
/// The source must remain intact.
#[tokio::test]
async fn rename_source_if_none_match_existing_object_fails() {
    let helper = TestHelper::new().await;
    let src_key = "sinm_src.txt";
    let dst_key = "sinm_dst.txt";
    let body = b"source-if-none-match test";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--source-if-none-match",
        "*",
        &src,
        &dst,
    ]);

    assert_ne!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with --source-if-none-match on existing object must not succeed; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(
        helper.is_object_exist(&bucket, src_key, None).await,
        "source must remain after precondition failure"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Conditional checks — target-if-none-match (2 tests)
// ---------------------------------------------------------------

/// `--target-if-none-match *` means "rename only if the destination has no
/// ETag". When the destination does not exist, no ETag is present, so the
/// condition is true and the rename succeeds.
#[tokio::test]
async fn rename_target_if_none_match_destination_absent_succeeds() {
    let helper = TestHelper::new().await;
    let src_key = "tinm_src.txt";
    let dst_key = "tinm_dst_absent.txt";
    let body = b"target-if-none-match absent test";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--target-if-none-match",
        "*",
        &src,
        &dst,
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with --target-if-none-match on absent destination must succeed; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(helper.is_object_exist(&bucket, dst_key, None).await);
    assert!(!helper.is_object_exist(&bucket, src_key, None).await);

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

/// `--target-if-none-match *` means "rename only if the destination has no
/// ETag". When the destination already exists it has an ETag, so the
/// condition is false and the API returns 412.
/// Source and destination must remain unchanged.
#[tokio::test]
async fn rename_target_if_none_match_destination_present_fails() {
    let helper = TestHelper::new().await;
    let src_key = "tinm_src2.txt";
    let dst_key = "tinm_dst_present.txt";
    let src_body = b"source body";
    let dst_body = b"pre-existing destination body";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, src_body).await;
    helper.put_object(&bucket, dst_key, dst_body.to_vec()).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--target-if-none-match",
        "*",
        &src,
        &dst,
    ]);

    assert_ne!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with --target-if-none-match on present destination must not succeed; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(
        helper.is_object_exist(&bucket, src_key, None).await,
        "source must remain"
    );
    let dst_bytes = helper.get_object_bytes(&bucket, dst_key, None).await;
    assert_eq!(dst_bytes, dst_body, "destination content must be unchanged");

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Conditional checks — target-if-match (2 tests)
// ---------------------------------------------------------------

/// `--target-if-match <correct ETag>` with a pre-existing destination
/// whose ETag matches must overwrite the destination and succeed.
#[tokio::test]
async fn rename_target_if_match_correct_etag_succeeds() {
    let helper = TestHelper::new().await;
    let src_key = "tim_src.txt";
    let dst_key = "tim_dst.txt";
    let src_body = b"source for target-if-match";
    let dst_body = b"existing destination";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, src_body).await;
    helper.put_object(&bucket, dst_key, dst_body.to_vec()).await;
    let dst_head = helper.head_object(&bucket, dst_key, None).await;
    let dst_etag = dst_head.e_tag().unwrap().to_string();

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--target-if-match",
        &dst_etag,
        &src,
        &dst,
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with correct --target-if-match must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(!helper.is_object_exist(&bucket, src_key, None).await);
    let new_dst_bytes = helper.get_object_bytes(&bucket, dst_key, None).await;
    assert_eq!(
        new_dst_bytes, src_body,
        "destination must now contain source content"
    );

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

/// `--target-if-match <wrong ETag>` must fail (412) and leave both
/// source and destination unchanged.
#[tokio::test]
async fn rename_target_if_match_wrong_etag_fails() {
    let helper = TestHelper::new().await;
    let src_key = "tim_fail_src.txt";
    let dst_key = "tim_fail_dst.txt";
    let src_body = b"source for target-if-match failure";
    let dst_body = b"existing destination unchanged";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, src_body).await;
    helper.put_object(&bucket, dst_key, dst_body.to_vec()).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "--target-if-match",
        "\"aaaabbbbccccdddd0000111122223333\"",
        &src,
        &dst,
    ]);

    assert_ne!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with wrong --target-if-match must not succeed; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(
        helper.is_object_exist(&bucket, src_key, None).await,
        "source must remain"
    );
    let dst_bytes = helper.get_object_bytes(&bucket, dst_key, None).await;
    assert_eq!(dst_bytes, dst_body, "destination content must be unchanged");

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// Special characters in key (1 test)
// ---------------------------------------------------------------

/// Rename with spaces, slashes, and Unicode characters in the key name.
/// The CLI must percent-encode them correctly when building the
/// `rename_source` header.
#[tokio::test]
async fn rename_special_characters_in_key() {
    let helper = TestHelper::new().await;
    let src_key = "dir/file with spaces & unicode \u{00e9}.txt";
    let dst_key = "dir/renamed \u{00e9} file.txt";
    let body = b"special chars rename body";
    let (bucket, _) = setup_bucket_with_object(&helper, src_key, body).await;

    let src = format!("s3://{}/{}", bucket, src_key);
    let dst = format!("s3://{}/{}", bucket, dst_key);

    let output = run_s7cmd(&["rename", "--source-profile", PROFILE_NAME, &src, &dst]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_SUCCESS),
        "rename with special-character keys must exit 0; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
    assert!(!helper.is_object_exist(&bucket, src_key, None).await);
    assert!(helper.is_object_exist(&bucket, dst_key, None).await);

    helper.delete_directory_bucket_with_cascade(&bucket).await;
}

// ---------------------------------------------------------------
// CLI validation — no AWS contact (2 tests)
// ---------------------------------------------------------------

/// Non-Express-One-Zone bucket name (does not end with `--<az>--x-s3`)
/// must be rejected by `validate()` before any AWS call, exiting 2.
#[tokio::test]
async fn rename_non_express_onezone_bucket_exits_2() {
    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "s3://my-regular-bucket/source.txt",
        "s3://my-regular-bucket/destination.txt",
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_CLAP_ARG_ERROR),
        "rename to non-Express-One-Zone bucket must exit 2; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
}

/// Source and destination in different buckets must be rejected by
/// `validate()` before any AWS call, exiting 2.
#[tokio::test]
async fn rename_different_buckets_exits_2() {
    let output = run_s7cmd(&[
        "rename",
        "--source-profile",
        PROFILE_NAME,
        "s3://bucket-a--apne1-az4--x-s3/source.txt",
        "s3://bucket-b--apne1-az4--x-s3/destination.txt",
    ]);

    assert_eq!(
        output.status.code(),
        Some(EXIT_CODE_CLAP_ARG_ERROR),
        "rename across different buckets must exit 2; stdout={}, stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr),
    );
}