@@ -10,6 +10,11 @@ use std::io::Read;
10
10
use time:: Timespec ;
11
11
use tokio:: runtime:: Runtime ;
12
12
13
+ #[ cfg( test) ]
14
+ mod test_s3;
15
+ #[ cfg( test) ]
16
+ pub ( crate ) use test_s3:: TestS3 ;
17
+
13
18
pub ( crate ) static S3_BUCKET_NAME : & str = "rust-docs-rs" ;
14
19
15
20
pub ( crate ) struct S3Backend < ' a > {
@@ -95,10 +100,8 @@ impl<'a> S3Backend<'a> {
95
100
}
96
101
}
97
102
98
- // public for testing
99
- pub ( crate ) const TIME_FMT : & str = "%a, %d %b %Y %H:%M:%S %Z" ;
100
-
101
103
fn parse_timespec ( raw : & str ) -> Result < Timespec , Error > {
104
+ const TIME_FMT : & str = "%a, %d %b %Y %H:%M:%S %Z" ;
102
105
Ok ( time:: strptime ( raw, TIME_FMT ) ?. to_timespec ( ) )
103
106
}
104
107
@@ -129,90 +132,11 @@ pub(crate) fn s3_client() -> Option<S3Client> {
129
132
}
130
133
131
134
#[ cfg( test) ]
132
- pub ( crate ) mod tests {
135
+ pub ( crate ) mod test {
133
136
use super :: * ;
134
- use crate :: storage:: test:: * ;
135
137
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 ;
143
138
use std:: slice;
144
139
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
-
216
140
#[ test]
217
141
fn test_parse_timespec ( ) {
218
142
// Test valid conversions
0 commit comments