Skip to content

Commit 46ebcec

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
Move TestS3 into a submodule
1 parent 29896c0 commit 46ebcec

File tree

3 files changed

+85
-84
lines changed

3 files changed

+85
-84
lines changed

src/storage/s3.rs

Lines changed: 7 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ use std::io::Read;
1010
use time::Timespec;
1111
use tokio::runtime::Runtime;
1212

13+
#[cfg(test)]
14+
mod test_s3;
15+
#[cfg(test)]
16+
pub(crate) use test_s3::TestS3;
17+
1318
pub(crate) static S3_BUCKET_NAME: &str = "rust-docs-rs";
1419

1520
pub(crate) struct S3Backend<'a> {
@@ -95,10 +100,8 @@ impl<'a> S3Backend<'a> {
95100
}
96101
}
97102

98-
// public for testing
99-
pub(crate) const TIME_FMT: &str = "%a, %d %b %Y %H:%M:%S %Z";
100-
101103
fn parse_timespec(raw: &str) -> Result<Timespec, Error> {
104+
const TIME_FMT: &str = "%a, %d %b %Y %H:%M:%S %Z";
102105
Ok(time::strptime(raw, TIME_FMT)?.to_timespec())
103106
}
104107

@@ -129,90 +132,11 @@ pub(crate) fn s3_client() -> Option<S3Client> {
129132
}
130133

131134
#[cfg(test)]
132-
pub(crate) mod tests {
135+
pub(crate) mod test {
133136
use super::*;
134-
use crate::storage::test::*;
135137
use crate::test::*;
136-
137-
use crate::storage::s3::S3Backend;
138-
use rusoto_s3::{
139-
CreateBucketRequest, DeleteBucketRequest, DeleteObjectRequest, ListObjectsRequest, S3,
140-
};
141-
142-
use std::cell::RefCell;
143138
use std::slice;
144139

145-
pub(crate) struct TestS3(RefCell<S3Backend<'static>>);
146-
147-
impl TestS3 {
148-
pub(crate) fn new() -> Self {
149-
// A random bucket name is generated and used for the current connection.
150-
// This allows each test to create a fresh bucket to test with.
151-
let bucket = format!("docs-rs-test-bucket-{}", rand::random::<u64>());
152-
let client = s3_client().unwrap();
153-
client
154-
.create_bucket(CreateBucketRequest {
155-
bucket: bucket.clone(),
156-
..Default::default()
157-
})
158-
.sync()
159-
.expect("failed to create test bucket");
160-
let bucket = Box::leak(bucket.into_boxed_str());
161-
TestS3(RefCell::new(S3Backend::new(client, bucket)))
162-
}
163-
pub(crate) fn upload(&self, blobs: &[Blob]) -> Result<(), Error> {
164-
self.0.borrow_mut().store_batch(blobs)
165-
}
166-
fn assert_404(&self, path: &'static str) {
167-
use rusoto_core::RusotoError;
168-
use rusoto_s3::GetObjectError;
169-
170-
let err = self.0.borrow().get(path).unwrap_err();
171-
match err
172-
.downcast_ref::<RusotoError<GetObjectError>>()
173-
.expect("wanted GetObject")
174-
{
175-
RusotoError::Unknown(http) => assert_eq!(http.status, 404),
176-
RusotoError::Service(GetObjectError::NoSuchKey(_)) => {}
177-
x => panic!("wrong error: {:?}", x),
178-
};
179-
}
180-
pub(crate) fn assert_blob(&self, blob: &Blob, path: &str) {
181-
let actual = self.0.borrow().get(path).unwrap();
182-
assert_blob_eq(blob, &actual);
183-
}
184-
}
185-
186-
impl Drop for TestS3 {
187-
fn drop(&mut self) {
188-
// delete the bucket when the test ends
189-
// this has to delete all the objects in the bucket first or min.io will give an error
190-
let inner = self.0.borrow();
191-
let list_req = ListObjectsRequest {
192-
bucket: inner.bucket.to_owned(),
193-
..Default::default()
194-
};
195-
let objects = inner.client.list_objects(list_req).sync().unwrap();
196-
assert!(!objects.is_truncated.unwrap_or(false));
197-
for path in objects.contents.unwrap() {
198-
let delete_req = DeleteObjectRequest {
199-
bucket: inner.bucket.to_owned(),
200-
key: path.key.unwrap(),
201-
..Default::default()
202-
};
203-
inner.client.delete_object(delete_req).sync().unwrap();
204-
}
205-
let delete_req = DeleteBucketRequest {
206-
bucket: inner.bucket.to_owned(),
207-
};
208-
inner
209-
.client
210-
.delete_bucket(delete_req)
211-
.sync()
212-
.expect("failed to delete test bucket");
213-
}
214-
}
215-
216140
#[test]
217141
fn test_parse_timespec() {
218142
// Test valid conversions

src/storage/s3/test_s3.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
use super::*;
2+
use crate::storage::test::assert_blob_eq;
3+
use rusoto_s3::{
4+
CreateBucketRequest, DeleteBucketRequest, DeleteObjectRequest, ListObjectsRequest, S3,
5+
};
6+
use std::cell::RefCell;
7+
8+
pub(crate) struct TestS3(RefCell<S3Backend<'static>>);
9+
10+
impl TestS3 {
11+
pub(crate) fn new() -> Self {
12+
// A random bucket name is generated and used for the current connection.
13+
// This allows each test to create a fresh bucket to test with.
14+
let bucket = format!("docs-rs-test-bucket-{}", rand::random::<u64>());
15+
let client = s3_client().unwrap();
16+
client
17+
.create_bucket(CreateBucketRequest {
18+
bucket: bucket.clone(),
19+
..Default::default()
20+
})
21+
.sync()
22+
.expect("failed to create test bucket");
23+
let bucket = Box::leak(bucket.into_boxed_str());
24+
TestS3(RefCell::new(S3Backend::new(client, bucket)))
25+
}
26+
pub(crate) fn upload(&self, blobs: &[Blob]) -> Result<(), Error> {
27+
self.0.borrow_mut().store_batch(blobs)
28+
}
29+
pub(crate) fn assert_404(&self, path: &'static str) {
30+
use rusoto_core::RusotoError;
31+
use rusoto_s3::GetObjectError;
32+
33+
let err = self.0.borrow().get(path).unwrap_err();
34+
match err
35+
.downcast_ref::<RusotoError<GetObjectError>>()
36+
.expect("wanted GetObject")
37+
{
38+
RusotoError::Unknown(http) => assert_eq!(http.status, 404),
39+
RusotoError::Service(GetObjectError::NoSuchKey(_)) => {}
40+
x => panic!("wrong error: {:?}", x),
41+
};
42+
}
43+
pub(crate) fn assert_blob(&self, blob: &Blob, path: &str) {
44+
let actual = self.0.borrow().get(path).unwrap();
45+
assert_blob_eq(blob, &actual);
46+
}
47+
}
48+
49+
impl Drop for TestS3 {
50+
fn drop(&mut self) {
51+
// delete the bucket when the test ends
52+
// this has to delete all the objects in the bucket first or min.io will give an error
53+
let inner = self.0.borrow();
54+
let list_req = ListObjectsRequest {
55+
bucket: inner.bucket.to_owned(),
56+
..Default::default()
57+
};
58+
let objects = inner.client.list_objects(list_req).sync().unwrap();
59+
assert!(!objects.is_truncated.unwrap_or(false));
60+
for path in objects.contents.unwrap() {
61+
let delete_req = DeleteObjectRequest {
62+
bucket: inner.bucket.to_owned(),
63+
key: path.key.unwrap(),
64+
..Default::default()
65+
};
66+
inner.client.delete_object(delete_req).sync().unwrap();
67+
}
68+
let delete_req = DeleteBucketRequest {
69+
bucket: inner.bucket.to_owned(),
70+
};
71+
inner
72+
.client
73+
.delete_bucket(delete_req)
74+
.sync()
75+
.expect("failed to delete test bucket");
76+
}
77+
}

src/test/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod fakes;
22

3-
use crate::storage::s3::tests::TestS3;
3+
use crate::storage::s3::TestS3;
44
use crate::web::Server;
55
use failure::Error;
66
use log::error;

0 commit comments

Comments
 (0)