-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathbasic.rs
224 lines (175 loc) · 5.7 KB
/
basic.rs
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
use crate::case;
use crate::utils::*;
use std::sync::Arc;
use s3s_test::Result;
use s3s_test::TestFixture;
use s3s_test::TestSuite;
use s3s_test::tcx::TestContext;
use aws_sdk_s3::primitives::ByteStream;
pub fn register(tcx: &mut TestContext) {
case!(tcx, Basic, Essential, test_list_buckets);
case!(tcx, Basic, Essential, test_list_objects);
case!(tcx, Basic, Essential, test_get_object);
case!(tcx, Basic, Put, test_put_object_tiny);
}
struct Basic {
s3: aws_sdk_s3::Client,
}
impl TestSuite for Basic {
async fn setup() -> Result<Self> {
let sdk_conf = aws_config::from_env().load().await;
let s3 = aws_sdk_s3::Client::from_conf(
aws_sdk_s3::config::Builder::from(&sdk_conf)
.force_path_style(true) // FIXME: remove force_path_style
.build(),
);
Ok(Self { s3 })
}
}
struct Essential {
s3: aws_sdk_s3::Client,
}
impl TestFixture<Basic> for Essential {
async fn setup(suite: Arc<Basic>) -> Result<Self> {
Ok(Self { s3: suite.s3.clone() })
}
}
impl Essential {
async fn test_list_buckets(self: Arc<Self>) -> Result {
let s3 = &self.s3;
let buckets = ["test-list-buckets-1", "test-list-buckets-2"];
{
for &bucket in &buckets {
delete_bucket_loose(s3, bucket).await?;
}
}
{
for &bucket in &buckets {
create_bucket(s3, bucket).await?;
}
let resp = s3.list_buckets().send().await?;
let bucket_list: Vec<_> = resp.buckets.as_deref().unwrap().iter().filter_map(|b| b.name()).collect();
for &bucket in &buckets {
assert!(bucket_list.contains(&bucket));
s3.head_bucket().bucket(bucket).send().await?;
}
}
{
for &bucket in &buckets {
delete_bucket_strict(s3, bucket).await?;
}
}
Ok(())
}
async fn test_list_objects(self: Arc<Self>) -> Result {
let s3 = &self.s3;
let bucket = "test-list-objects";
let keys = ["file-1", "file-2", "file-3"];
let content = "hello world 你好世界 123456 !@#$%😂^&*()";
{
for key in &keys {
delete_object_loose(s3, bucket, key).await?;
}
delete_bucket_loose(s3, bucket).await?;
}
{
create_bucket(s3, bucket).await?;
for &key in &keys {
s3.put_object()
.bucket(bucket)
.key(key)
.body(ByteStream::from_static(content.as_bytes()))
.send()
.await?;
}
let resp = s3.list_objects_v2().bucket(bucket).send().await?;
let object_list: Vec<_> = resp.contents.as_deref().unwrap().iter().filter_map(|o| o.key()).collect();
for &key in &keys {
assert!(object_list.contains(&key));
s3.head_object().bucket(bucket).key(key).send().await?;
}
}
{
for &key in &keys {
delete_object_strict(s3, bucket, key).await?;
}
delete_bucket_strict(s3, bucket).await?;
}
Ok(())
}
async fn test_get_object(self: Arc<Self>) -> Result {
let s3 = &self.s3;
let bucket = "test-get-object";
let key = "file-1";
let content = "hello world 你好世界 123456 !@#$%😂^&*()";
{
delete_object_loose(s3, bucket, key).await?;
delete_bucket_loose(s3, bucket).await?;
}
{
create_bucket(s3, bucket).await?;
s3.put_object()
.bucket(bucket)
.key(key)
.body(ByteStream::from_static(content.as_bytes()))
.send()
.await?;
let resp = s3.get_object().bucket(bucket).key(key).send().await?;
let body = resp.body.collect().await?;
let body = String::from_utf8(body.to_vec())?;
assert_eq!(body, content);
}
{
delete_object_strict(s3, bucket, key).await?;
delete_bucket_strict(s3, bucket).await?;
}
Ok(())
}
}
struct Put {
s3: aws_sdk_s3::Client,
bucket: String,
key: String,
}
impl TestFixture<Basic> for Put {
async fn setup(suite: Arc<Basic>) -> Result<Self> {
let s3 = &suite.s3;
let bucket = "test-put";
let key = "file";
delete_object_loose(s3, bucket, key).await?;
delete_bucket_loose(s3, bucket).await?;
create_bucket(s3, bucket).await?;
Ok(Self {
s3: suite.s3.clone(),
bucket: bucket.to_owned(),
key: key.to_owned(),
})
}
async fn teardown(self) -> Result {
let Self { s3, bucket, key } = &self;
delete_object_loose(s3, bucket, key).await?;
delete_bucket_loose(s3, bucket).await?;
Ok(())
}
}
impl Put {
async fn test_put_object_tiny(self: Arc<Self>) -> Result {
let s3 = &self.s3;
let bucket = self.bucket.as_str();
let key = self.key.as_str();
let contents = ["", "1", "22", "333"];
for content in contents {
s3.put_object()
.bucket(bucket)
.key(key)
.body(ByteStream::from_static(content.as_bytes()))
.send()
.await?;
let resp = s3.get_object().bucket(bucket).key(key).send().await?;
let body = resp.body.collect().await?;
let body = String::from_utf8(body.to_vec())?;
assert_eq!(body, content);
}
Ok(())
}
}