forked from goodeggs/knox-copy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknox-copy.spec.coffee
162 lines (143 loc) · 5.18 KB
/
knox-copy.spec.coffee
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
knoxCopy = require '..'
fs = require 'fs'
{parallel} = require 'async'
describe 'knox-copy', ->
{key, bucket, client} = {}
beforeEach ->
try
{key, secret, bucket} = JSON.parse fs.readFileSync('auth', 'ascii')
client = knoxCopy.createClient {key, secret, bucket}
catch err
console.error 'The tests require ./auth to contain a JSON string with'
console.error '`key, secret, and bucket in order to run tests.'
process.exit 1
with4Files = (body) ->
keys = [1..4].map (n) -> "tmp/spec/list/file_#{n}"
beforeEach (done) ->
parallel(
keys.map (key) -> (cb) ->
client.putBuffer 'test file', key, cb
done)
afterEach (done) ->
parallel(
keys.map (key) -> (cb) ->
client.deleteFile key, cb
done)
describe 'with 4 files', ->
body(keys)
describe 'listPageOfKeys()', ->
with4Files (keys) ->
it 'should list a page of S3 Object keys', (done) ->
# Middle 2 of 4 files
client.listPageOfKeys
maxKeys: 2
prefix: '/tmp/spec/list'
marker: '/tmp/spec/list/file_1'
(err, page) ->
expect(err).toBeFalsy()
expect(page.IsTruncated).toBeTruthy()
expect(page.Contents.length).toBe 2
done()
# Last of 4 files
client.listPageOfKeys
maxKeys: 4
prefix: '/tmp/spec/list'
marker: '/tmp/spec/list/file_3'
(err, page) ->
expect(err).toBeFalsy()
expect(page.IsTruncated).toBeFalsy()
expect(page.Contents.length).toBe 1
done()
# Marker at the end
client.listPageOfKeys
maxKeys: 4
prefix: '/tmp/spec/list'
marker: '/tmp/spec/list/file_4'
(err, page) ->
expect(err).toBeFalsy()
expect(page.IsTruncated).toBeFalsy()
expect(page.Contents.length).toBe 0
done()
# Empty prefix
client.listPageOfKeys
maxKeys: 4
prefix: '/does_not_exist'
(err, page) ->
expect(err).toBeFalsy()
expect(page.IsTruncated).toBeFalsy()
expect(page.Contents.length).toBe 0
done()
describe 'streamKeys()', ->
with4Files (keys) ->
describe 'when all keys fit on single a page', ->
it 'should emit a data event for every key and an end event when keys are exhausted', (done) ->
streamedKeys = []
stream = client.streamKeys(prefix: '/tmp/spec/list')
stream.on 'data', (key) -> streamedKeys.push key
stream.on 'end', ->
expect(streamedKeys).toEqual keys
done()
describe 'when the number of keys exceeds page size', ->
maxKeysPerRequest = 2
it 'should emit a data event for every key and an end event when keys are exhausted', (done) ->
streamedKeys = []
stream = client.streamKeys({prefix: '/tmp/spec/list', maxKeysPerRequest})
stream.on 'data', (key) -> streamedKeys.push key
stream.on 'end', ->
expect(streamedKeys).toEqual keys
done()
describe 'copyBucket()', ->
with4Files (keys) ->
afterEach (done) ->
parallel(
[1..4].map (n) -> (cb) ->
client.deleteFile "/tmp/spec/copy_bucket/file_#{n}", cb
done)
it 'should copy a prefixed set of files across buckets', (done) ->
client.copyBucket
fromBucket: bucket # this is the default value. Included here to show where you'd set a different bucket
fromPrefix: '/tmp/spec/list'
toPrefix: '/tmp/spec/copy_bucket'
(err, count) ->
expect(err).toBeFalsy()
# returns the number of copied objects
expect(count).toBe 4
done()
describe 'with funky filenames', ->
filenames = [
'fruit and flour.png'
'MarkBreadMED05%20-%20cropped.jpg'
]
sourceKeys = filenames.map((filename) -> "/tmp/spec/spaces/#{filename}")
destinationKeys = filenames.map((filename) -> "tmp/spec/copy_spaces/#{filename}")
beforeEach (done) ->
parallel(
sourceKeys.map (key) -> (cb) ->
client.putBuffer 'test file', key, cb
done
)
afterEach (done) ->
parallel(
sourceKeys
.concat(destinationKeys)
.map((key) -> (cb) ->
client.deleteFile(key, cb)
), done)
it 'should copy and preserve filenames', (done) ->
client.copyBucket
fromBucket: bucket # this is the default value. Included here to show where you'd set a different bucket
fromPrefix: '/tmp/spec/spaces'
toPrefix: '/tmp/spec/copy_spaces'
(err, count) ->
expect(err).toBeFalsy()
# returns the number of copied objects
expect(count).toBe 2
client.listPageOfKeys
maxKeys: 2
prefix: '/tmp/spec/copy_spaces'
(err, page) ->
expect(err).toBeFalsy()
expect(
(Key for {Key} in page.Contents).sort()
).toEqual destinationKeys.sort()
done()