Skip to content

Commit 666f702

Browse files
authored
Rust S3 unit tests (#4299)
* Rust S3 unit tests * Finish delete test.
1 parent 97a177a commit 666f702

File tree

8 files changed

+364
-45
lines changed

8 files changed

+364
-45
lines changed

rust_dev_preview/s3/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ aws-config = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main"
2626
aws-endpoint = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main" }
2727
# snippet-end:[s3.rust.s3-object-lambda-cargo.toml]
2828
aws-sdk-s3 = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main" }
29+
aws-smithy-client = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main", features = [
30+
"test-util",
31+
] }
2932
aws-smithy-http = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main", features = ["rt-tokio"] }
3033
aws-smithy-types = { git = "https://github.com/awslabs/aws-sdk-rust", branch = "main" }
34+
sdk-examples-test-utils = { path = "../test-utils" }
3135
bytes = "0.4.12"
3236
futures-util = { version = "0.3.21", features = ["alloc"] }
3337
http = "0.2.8"

rust_dev_preview/s3/src/bin/copy-object.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55

66
use aws_config::meta::region::RegionProviderChain;
7-
use aws_sdk_s3::{Client, Error, Region, PKG_VERSION};
7+
use aws_sdk_s3::{
8+
error::CopyObjectError, output::CopyObjectOutput, types::SdkError, Client, Error, Region,
9+
PKG_VERSION,
10+
};
811
use structopt::StructOpt;
912

1013
#[derive(Debug, StructOpt)]
@@ -42,7 +45,7 @@ async fn cp_object(
4245
destination_bucket: &str,
4346
source_object: &str,
4447
destination_object: &str,
45-
) -> Result<(), Error> {
48+
) -> Result<CopyObjectOutput, SdkError<CopyObjectError>> {
4649
// Create source of object:
4750
// source-bucket-name/source-object-name
4851
let mut source_bucket_and_object: String = "".to_owned();
@@ -56,14 +59,37 @@ async fn cp_object(
5659
.bucket(destination_bucket)
5760
.key(destination_object)
5861
.send()
59-
.await?;
60-
61-
println!("Object copied.");
62-
63-
Ok(())
62+
.await
6463
}
6564
// snippet-end:[bin.rust.copy-object]
6665

66+
#[cfg(test)]
67+
mod test_cp_object {
68+
use sdk_examples_test_utils::single_shot_client;
69+
70+
use crate::cp_object;
71+
72+
#[tokio::test]
73+
async fn test_cp_object() {
74+
let client = single_shot_client! {
75+
sdk: aws_sdk_s3,
76+
status: 200,
77+
response: r#""#
78+
};
79+
80+
let response = cp_object(
81+
&client,
82+
"source_bucket",
83+
"destination_bucket",
84+
"source_object",
85+
"destination_object",
86+
)
87+
.await;
88+
89+
assert!(response.is_ok(), "{response:?}");
90+
}
91+
}
92+
6793
/// Copies an object from one Amazon S3 bucket to another.
6894
/// # Arguments
6995
///
@@ -113,5 +139,9 @@ async fn main() -> Result<(), Error> {
113139
let shared_config = aws_config::from_env().region(region_provider).load().await;
114140
let client = Client::new(&shared_config);
115141

116-
cp_object(&client, &source, &destination, &key, &new_name).await
142+
cp_object(&client, &source, &destination, &key, &new_name).await?;
143+
144+
println!("Object copied.");
145+
146+
Ok(())
117147
}

rust_dev_preview/s3/src/bin/create-bucket.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55

66
use aws_config::meta::region::RegionProviderChain;
7+
use aws_sdk_s3::error::CreateBucketError;
78
use aws_sdk_s3::model::{BucketLocationConstraint, CreateBucketConfiguration};
9+
use aws_sdk_s3::output::CreateBucketOutput;
10+
use aws_sdk_s3::types::SdkError;
811
use aws_sdk_s3::{Client, Error, Region, PKG_VERSION};
912
use structopt::StructOpt;
1013

@@ -25,7 +28,11 @@ struct Opt {
2528

2629
// Creates a bucket.
2730
// snippet-start:[s3.rust.create-bucket]
28-
async fn make_bucket(client: &Client, bucket: &str, region: &str) -> Result<(), Error> {
31+
async fn make_bucket(
32+
client: &Client,
33+
bucket: &str,
34+
region: &str,
35+
) -> Result<CreateBucketOutput, SdkError<CreateBucketError>> {
2936
let constraint = BucketLocationConstraint::from(region);
3037
let cfg = CreateBucketConfiguration::builder()
3138
.location_constraint(constraint)
@@ -36,13 +43,33 @@ async fn make_bucket(client: &Client, bucket: &str, region: &str) -> Result<(),
3643
.create_bucket_configuration(cfg)
3744
.bucket(bucket)
3845
.send()
39-
.await?;
40-
println!("Created bucket.");
41-
42-
Ok(())
46+
.await
4347
}
4448
// snippet-end:[s3.rust.create-bucket]
4549

50+
#[cfg(test)]
51+
mod test_make_bucket {
52+
use sdk_examples_test_utils::single_shot_client;
53+
54+
use crate::make_bucket;
55+
56+
#[tokio::test]
57+
async fn test_make_bucket() {
58+
let client = single_shot_client! {
59+
sdk: aws_sdk_s3,
60+
status: 200,
61+
headers: vec![("Location", "/test_bucket")],
62+
response: r#""#
63+
};
64+
65+
let res = make_bucket(&client, "bucket", "region").await;
66+
67+
assert!(res.is_ok(), "{res:?}");
68+
69+
assert_eq!(res.unwrap().location(), Some("/test_bucket"))
70+
}
71+
}
72+
4673
/// Creates an Amazon S3 bucket in the Region.
4774
/// # Arguments
4875
///
@@ -83,5 +110,8 @@ async fn main() -> Result<(), Error> {
83110
let shared_config = aws_config::from_env().region(region_provider).load().await;
84111
let client = Client::new(&shared_config);
85112

86-
make_bucket(&client, &bucket, r_str).await
113+
make_bucket(&client, &bucket, r_str).await?;
114+
println!("Created bucket.");
115+
116+
Ok(())
87117
}

rust_dev_preview/s3/src/bin/s3-multipart-upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ async fn run_example() -> Result<(), Error> {
144144
.unwrap();
145145
// snippet-end:[rust.example_code.s3.complete_multipart_upload]
146146

147-
let data: GetObjectOutput = s3_service::download_object(&client, &bucket_name, &key).await;
147+
let data: GetObjectOutput = s3_service::download_object(&client, &bucket_name, &key).await?;
148148
let data_length: u64 = data.content_length().try_into().unwrap();
149149
if file.metadata().unwrap().len() == data_length {
150150
println!("Data lengths match.");

0 commit comments

Comments
 (0)