-
Notifications
You must be signed in to change notification settings - Fork 10
/
Tests.hs
384 lines (348 loc) · 15.6 KB
/
Tests.hs
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
-----------------------------------------------------------------------------
-- |
-- Module : AWS S3 Tests
-- Copyright : (c) Greg Heartsfield 2007
-- License : BSD3
--
-- Test hS3 library against Amazon S3. This requires the following
-- environment variables to be set with your Amazon keys:
-- AWS_ACCESS_KEY_ID
-- AWS_ACCESS_KEY_SECRET
-----------------------------------------------------------------------------
module Main(main) where
import Network.AWS.AWSConnection
import Network.AWS.AWSResult
import Network.AWS.S3Object
import Network.AWS.S3Bucket
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as L
import Control.Exception(finally,bracket)
import Control.Concurrent(threadDelay)
import Data.List.Utils(hasKeyAL)
import Test.HUnit
-- | Run the tests
main = runTestTT tests
tests =
TestList
[
TestLabel "S3 Operations Test" s3OperationsTest,
TestLabel "S3 Message Integrity Check" s3MessageIntegrityCheckTest,
TestLabel "S3 Copy Test" s3CopyTest,
TestLabel "S3 Copy/Replace Test" s3CopyReplaceTest,
TestLabel "S3 Location Test" s3LocationTest,
TestLabel "Bucket Naming Test" bucketNamingTest,
TestLabel "Reduced Redundancy Creation Test" reducedRedundancyCreateTest,
TestLabel "Reduced Redundancy Existing Test" reducedRedundancyExistingTest,
TestLabel "Versioning Test" versioningTest
]
testBucket = "hs3-test"
testObjectTemplate = S3Object testBucket "hS3-object-test" "text/plain"
[("x-amz-meta-foo", "bar"),
("x-amz-meta-french", "Bonjour, ça va?"),
("x-amz-meta-smiley", "☺")
] (L.pack "Hello S3!")
testSourceTemplate = S3Object testBucket "hS3-object-source" "text/plain"
[] (L.pack "testing")
testDestinationTemplate = testSourceTemplate {obj_name = "hS3-object-destination"}
-- | A sequence of several operations.
s3OperationsTest =
TestCase (
do c <- getConn
-- Bucket Creation
bucket <- testCreateBucket c
testGetBucketLocation c bucket "US"
let testObj = testObjectTemplate {obj_bucket = bucket}
-- Object send
testSendObject c testObj
-- Object get
testGetObject c testObj
-- Object get info
testGetObjectInfo c testObj
-- Object list (should have 1 object in bucket)
testListAllObjects c bucket 1
-- Object delete
testDeleteObject c testObj
-- Object send, and then bucket empty
testSendObject c testObj
testEmptyBucket c bucket
-- Bucket should now be empty
testListAllObjects c bucket 0
-- Delete bucket
testDeleteBucket c bucket
-- Bucket should be gone
threadDelay 3000000 -- sleep 3 sec, since bucket isn't always unavailable immediately
testBucketGone c bucket
)
s3MessageIntegrityCheckTest =
TestCase (
do c <- getConn
bucket <- testCreateBucket c
testGetBucketLocation c bucket "US"
let testObj = testObjectTemplate {obj_bucket = bucket}
-- Object send
testSendObjectMIC c testObj
-- Object get
testGetObject c testObj
)
s3LocationTest =
TestCase (
do c <- getConn
-- European buckets
bracket (testCreateBucketIn c "EU")
(\b ->
do testEmptyBucket c b
testDeleteBucket c b
)
(\b ->
do testGetBucketLocation c b "EU"
let euTestObj = testObjectTemplate {obj_bucket = b}
testSendObject c euTestObj
testGetObject c euTestObj
testDeleteObject c euTestObj
)
-- US buckets
bracket (testCreateBucketIn c "US")
(\b -> testDeleteBucket c b)
(\b -> testGetBucketLocation c b "US")
)
bucketNamingTest =
TestList
[
(nameNotValidTC "At least 3 chars" "ab"),
(nameValidTC "At least 3 chars" "abc"),
(nameNotValidTC "63 chars or fewer" (replicate 64 'a')),
(nameNotValidTC "Starts with alphanum char" "."),
(nameNotValidTC "Starts with alphanum char" "_"),
(nameNotValidTC "Starts with alphanum char" "-"),
(nameNotValidTC "No underscores" "ab_cd"),
(nameNotValidTC "Do not end with a dash" "foo-"),
(nameNotValidTC "Dashes should not be next to periods" "ab.-cd")
]
nameValidTC :: String -> String -> Test
nameValidTC msg name = TestCase (assertBool msg (isBucketNameValid name))
nameNotValidTC :: String -> String -> Test
nameNotValidTC msg name = TestCase (assertBool msg (not (isBucketNameValid name)))
s3CopyTest =
TestCase (
do c <- getConn
-- Bucket Creation
b <- testCreateBucket c
d <- testCreateBucket c
let srcHeader = "x-amz-meta-src"
let srcValue = "foo"
finally (
do let srcObj = testSourceTemplate {obj_bucket = d, obj_headers = [(srcHeader,srcValue)]}
let destObj = testDestinationTemplate {obj_bucket = b}
-- Object send
testSendObject c srcObj
-- Verify headers were set on original object
sr <- getObject c srcObj
failOnError sr ()
(\x -> assertBool "Original sent object has custom headers"
(hasKeyAL srcHeader (obj_headers x))
)
-- Object copy
testCopyObject c srcObj destObj
-- Verify destination object contains same added header as source object
testGetObjectInfo c (destObj {obj_headers = [(srcHeader,srcValue)]})
) (
-- Empty buckets
do testEmptyBucket c b
testEmptyBucket c d
-- Destroy buckets
testDeleteBucket c b
testDeleteBucket c d
)
)
s3CopyReplaceTest =
TestCase (
do c <- getConn
-- Bucket Creation
b <- testCreateBucket c
d <- testCreateBucket c
let srcHeader = "x-amz-meta-src"
let srcValue = "foo"
finally (
do let srcObj = testSourceTemplate {obj_bucket = d, obj_headers = [(srcHeader,srcValue)]}
let destObj = testDestinationTemplate {obj_bucket = b}
-- Object send
testSendObject c srcObj
sr <- getObject c srcObj
failOnError sr ()
(\x -> assertBool "Original sent object has custom headers"
(hasKeyAL srcHeader (obj_headers x))
)
-- Object copy
testCopyObjectWithReplace c srcObj destObj
-- Object get info from copied object
testGetObjectInfo c destObj
dr <- getObject c destObj
failOnError dr ()
(\x -> assertBool "Copied object w/ replace does not have source headers"
(not (hasKeyAL srcHeader (obj_headers x)))
)
) (
-- Empty buckets
do testEmptyBucket c b
testEmptyBucket c d
-- Destroy buckets
testDeleteBucket c b
testDeleteBucket c d
)
)
failOnError :: (Show a) =>
Either a b -- ^ AWS Result to inspect
-> t -- ^ Value to return on failure
-> (b -> IO t) -- ^ Assertions to run on success
-> IO t
failOnError r f d = either
(\x ->
do assertFailure (show x)
return f)
(\x -> d x) r
testCreateNamedBucket :: AWSConnection -> String -> IO ()
testCreateNamedBucket c bucket =
do r <- createBucket c bucket
failOnError r ()
(const $ assertBool "bucket creation" True)
testCreateBucket :: AWSConnection -> IO String
testCreateBucket c =
do r <- createBucketWithPrefix c testBucket
failOnError r "" (\x -> do assertBool "bucket creation" True
return x
)
testCreateBucketIn :: AWSConnection -> String -> IO String
testCreateBucketIn c location =
do r <- createBucketWithPrefixIn c testBucket location
failOnError r "" (\x -> do assertBool ("bucket creation in " ++ location) True
return x
)
testGetBucketLocation :: AWSConnection -> String -> String -> IO ()
testGetBucketLocation c bucket expectedLocation =
do r <- getBucketLocation c bucket
failOnError r () (\x ->
assertEqual ("Bucket in the " ++ expectedLocation)
expectedLocation x)
testSendObject :: AWSConnection -> S3Object -> IO ()
testSendObject c o =
do r <- sendObject c o
failOnError r ()
(const $ assertBool "object send" True)
testSendObjectMIC :: AWSConnection -> S3Object -> IO ()
testSendObjectMIC c o =
do r <- sendObjectMIC c o
failOnError r ()
(const $ assertBool "object send" True)
testCopyObject :: AWSConnection -> S3Object -> S3Object -> IO ()
testCopyObject c srco desto =
do r <- copyObject c srco desto
failOnError r ()
(const $ assertBool "object copied" True)
testCopyObjectWithReplace :: AWSConnection -> S3Object -> S3Object -> IO ()
testCopyObjectWithReplace c srco desto =
do r <- copyObjectWithReplace c srco desto
failOnError r ()
(const $ assertBool "object copied" True)
testGetObject :: AWSConnection -> S3Object -> IO ()
testGetObject c o =
do r <- getObject c o
failOnError r ()
(\x -> do assertEqual "object get body"
(obj_data o) (obj_data x)
assertEqual "object get metadata"
(obj_headers o)
(realMetadata (obj_headers x))
)
-- Test to ensure an object on S3 matches the headers passed to this function.
testGetObjectInfo :: AWSConnection -> S3Object -> IO ()
testGetObjectInfo c o =
do r <- getObject c o
failOnError r ()
(\x -> assertEqual "object info get metadata"
(obj_headers o) (realMetadata (obj_headers x))
)
-- test that a bucket has a given number of objects
testListAllObjects :: AWSConnection -> String -> Int -> IO ()
testListAllObjects c bucket count =
do r <- listAllObjects c bucket (ListRequest "" "" "" 100)
failOnError r ()
(\x -> assertEqual "object list" count (length x))
testEmptyBucket :: AWSConnection -> String -> IO ()
testEmptyBucket c b =
do r <- emptyBucket c b
failOnError r ()
(const $ assertBool "bucket empty" True)
testDeleteObject :: AWSConnection -> S3Object -> IO ()
testDeleteObject c o =
do r <- deleteObject c o
failOnError r ()
(const $ assertBool "object delete" True)
testDeleteBucket :: AWSConnection -> String -> IO ()
testDeleteBucket c bucket =
do r <- deleteBucket c bucket
failOnError r ()
(const $ assertBool "bucket deletion" True)
-- test if a bucket is not present
-- It sometimes takes a second or two for a bucket to disappear after a delete,
-- so failing this is not fatal.
testBucketGone :: AWSConnection -> String -> IO ()
testBucketGone c bucket =
getBucketLocation c bucket >>=
either (\(AWSError code msg) -> assertEqual "Bucket is gone" "NotFound" code)
(\x -> do assertFailure "Bucket still there, should be gone (sometimes slow, not fatal)"
return ())
reducedRedundancyCreateTest =
TestCase (
do c <- getConn
b <- testCreateBucket c
let rr = "reduced-redundancy"
let testObj = testObjectTemplate {obj_bucket = b, obj_name = rr}
let rrTestObj = setStorageClass REDUCED_REDUNDANCY testObj
testSendObject c rrTestObj
r <- getObjectStorageClass c testObj
failOnError r ()
(\sc -> assertEqual "storage class is reduced-redundancy"
REDUCED_REDUNDANCY sc)
)
reducedRedundancyExistingTest =
TestCase (
do c <- getConn
b <- testCreateBucket c
let rr = "reduced-redundancy"
let testObj = testObjectTemplate {obj_bucket = b, obj_name = rr}
testSendObject c testObj
rewriteStorageClass c REDUCED_REDUNDANCY testObj
r <- getObjectStorageClass c testObj
failOnError r ()
(\sc -> assertEqual "storage class is reduced-redundancy"
REDUCED_REDUNDANCY sc)
-- Set storage class back to STANDARD
rewriteStorageClass c STANDARD testObj
s <- getObjectStorageClass c testObj
failOnError s ()
(\sc -> assertEqual "storage class switched back to standard"
STANDARD sc)
)
versioningTest =
TestCase (
do c <- getConn
b <- testCreateBucket c
r <- getVersioningConfiguration c b
failOnError r ()
(\vc -> assertEqual "versioning is disabled by default"
(VersioningConfiguration VersioningDisabled False) vc)
sr <- setVersioningConfiguration c b (VersioningConfiguration VersioningEnabled False)
failOnError sr ()
(\const -> assertBool "versioning set without error" True)
ur <- getVersioningConfiguration c b
failOnError ur ()
(\vc -> assertEqual "versioning is now enabled"
(VersioningConfiguration VersioningEnabled False) vc)
)
getConn = do mConn <- amazonS3ConnectionFromEnv
return (fromJust mConn)
-- These headers get added by amazon, but ignore them for
-- testing metadata storage.
headersToIgnore = ["x-amz-id-2", "x-amz-request-id"]
realMetadata :: [(String, b)] -> [(String, b)]
realMetadata = filter (\x -> fst x `notElem` headersToIgnore)