]>
Commit | Line | Data |
---|---|---|
0305db28 JB |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import 'mocha' | |
4 | import * as chai from 'chai' | |
5 | import { merge } from 'lodash' | |
6 | import { | |
7 | areObjectStorageTestsDisabled, | |
8 | checkTmpIsEmpty, | |
9 | cleanupTests, | |
10 | createMultipleServers, | |
11 | createSingleServer, | |
12 | doubleFollow, | |
1f6125be | 13 | expectLogDoesNotContain, |
0305db28 JB |
14 | expectStartWith, |
15 | killallServers, | |
16 | makeRawRequest, | |
17 | MockObjectStorage, | |
18 | ObjectStorageCommand, | |
19 | PeerTubeServer, | |
20 | setAccessTokensToServers, | |
21 | waitJobs, | |
22 | webtorrentAdd | |
23 | } from '@shared/extra-utils' | |
24 | import { HttpStatusCode, VideoDetails } from '@shared/models' | |
25 | ||
26 | const expect = chai.expect | |
27 | ||
28 | async function checkFiles (options: { | |
29 | video: VideoDetails | |
30 | ||
31 | baseMockUrl?: string | |
32 | ||
33 | playlistBucket: string | |
34 | playlistPrefix?: string | |
35 | ||
36 | webtorrentBucket: string | |
37 | webtorrentPrefix?: string | |
38 | }) { | |
39 | const { | |
40 | video, | |
41 | playlistBucket, | |
42 | webtorrentBucket, | |
43 | baseMockUrl, | |
44 | playlistPrefix, | |
45 | webtorrentPrefix | |
46 | } = options | |
47 | ||
48 | let allFiles = video.files | |
49 | ||
50 | for (const file of video.files) { | |
51 | const baseUrl = baseMockUrl | |
52 | ? `${baseMockUrl}/${webtorrentBucket}/` | |
53 | : `http://${webtorrentBucket}.${ObjectStorageCommand.getEndpointHost()}/` | |
54 | ||
55 | const prefix = webtorrentPrefix || '' | |
56 | const start = baseUrl + prefix | |
57 | ||
58 | expectStartWith(file.fileUrl, start) | |
59 | ||
60 | const res = await makeRawRequest(file.fileDownloadUrl, HttpStatusCode.FOUND_302) | |
61 | const location = res.headers['location'] | |
62 | expectStartWith(location, start) | |
63 | ||
64 | await makeRawRequest(location, HttpStatusCode.OK_200) | |
65 | } | |
66 | ||
67 | const hls = video.streamingPlaylists[0] | |
68 | ||
69 | if (hls) { | |
70 | allFiles = allFiles.concat(hls.files) | |
71 | ||
72 | const baseUrl = baseMockUrl | |
73 | ? `${baseMockUrl}/${playlistBucket}/` | |
74 | : `http://${playlistBucket}.${ObjectStorageCommand.getEndpointHost()}/` | |
75 | ||
76 | const prefix = playlistPrefix || '' | |
77 | const start = baseUrl + prefix | |
78 | ||
79 | expectStartWith(hls.playlistUrl, start) | |
80 | expectStartWith(hls.segmentsSha256Url, start) | |
81 | ||
82 | await makeRawRequest(hls.playlistUrl, HttpStatusCode.OK_200) | |
83 | ||
84 | const resSha = await makeRawRequest(hls.segmentsSha256Url, HttpStatusCode.OK_200) | |
85 | expect(JSON.stringify(resSha.body)).to.not.throw | |
86 | ||
87 | for (const file of hls.files) { | |
88 | expectStartWith(file.fileUrl, start) | |
89 | ||
90 | const res = await makeRawRequest(file.fileDownloadUrl, HttpStatusCode.FOUND_302) | |
91 | const location = res.headers['location'] | |
92 | expectStartWith(location, start) | |
93 | ||
94 | await makeRawRequest(location, HttpStatusCode.OK_200) | |
95 | } | |
96 | } | |
97 | ||
98 | for (const file of allFiles) { | |
99 | const torrent = await webtorrentAdd(file.magnetUri, true) | |
100 | ||
101 | expect(torrent.files).to.be.an('array') | |
102 | expect(torrent.files.length).to.equal(1) | |
103 | expect(torrent.files[0].path).to.exist.and.to.not.equal('') | |
104 | ||
105 | const res = await makeRawRequest(file.fileUrl, HttpStatusCode.OK_200) | |
106 | expect(res.body).to.have.length.above(100) | |
107 | } | |
108 | ||
109 | return allFiles.map(f => f.fileUrl) | |
110 | } | |
111 | ||
112 | function runTestSuite (options: { | |
113 | playlistBucket: string | |
114 | playlistPrefix?: string | |
115 | ||
116 | webtorrentBucket: string | |
117 | webtorrentPrefix?: string | |
118 | ||
119 | useMockBaseUrl?: boolean | |
120 | ||
121 | maxUploadPart?: string | |
122 | }) { | |
123 | const mockObjectStorage = new MockObjectStorage() | |
124 | let baseMockUrl: string | |
125 | ||
126 | let servers: PeerTubeServer[] | |
127 | ||
128 | let keptUrls: string[] = [] | |
129 | ||
130 | const uuidsToDelete: string[] = [] | |
131 | let deletedUrls: string[] = [] | |
132 | ||
133 | before(async function () { | |
134 | this.timeout(120000) | |
135 | ||
136 | const port = await mockObjectStorage.initialize() | |
137 | baseMockUrl = options.useMockBaseUrl ? `http://localhost:${port}` : undefined | |
138 | ||
139 | await ObjectStorageCommand.createBucket(options.playlistBucket) | |
140 | await ObjectStorageCommand.createBucket(options.webtorrentBucket) | |
141 | ||
142 | const config = { | |
143 | object_storage: { | |
144 | enabled: true, | |
145 | endpoint: 'http://' + ObjectStorageCommand.getEndpointHost(), | |
146 | region: ObjectStorageCommand.getRegion(), | |
147 | ||
148 | credentials: ObjectStorageCommand.getCredentialsConfig(), | |
149 | ||
150 | max_upload_part: options.maxUploadPart || '2MB', | |
151 | ||
152 | streaming_playlists: { | |
153 | bucket_name: options.playlistBucket, | |
154 | prefix: options.playlistPrefix, | |
155 | base_url: baseMockUrl | |
156 | ? `${baseMockUrl}/${options.playlistBucket}` | |
157 | : undefined | |
158 | }, | |
159 | ||
160 | videos: { | |
161 | bucket_name: options.webtorrentBucket, | |
162 | prefix: options.webtorrentPrefix, | |
163 | base_url: baseMockUrl | |
164 | ? `${baseMockUrl}/${options.webtorrentBucket}` | |
165 | : undefined | |
166 | } | |
167 | } | |
168 | } | |
169 | ||
170 | servers = await createMultipleServers(2, config) | |
171 | ||
172 | await setAccessTokensToServers(servers) | |
173 | await doubleFollow(servers[0], servers[1]) | |
174 | ||
175 | for (const server of servers) { | |
176 | const { uuid } = await server.videos.quickUpload({ name: 'video to keep' }) | |
177 | await waitJobs(servers) | |
178 | ||
179 | const files = await server.videos.listFiles({ id: uuid }) | |
180 | keptUrls = keptUrls.concat(files.map(f => f.fileUrl)) | |
181 | } | |
182 | }) | |
183 | ||
184 | it('Should upload a video and move it to the object storage without transcoding', async function () { | |
185 | this.timeout(20000) | |
186 | ||
187 | const { uuid } = await servers[0].videos.quickUpload({ name: 'video 1' }) | |
188 | uuidsToDelete.push(uuid) | |
189 | ||
190 | await waitJobs(servers) | |
191 | ||
192 | for (const server of servers) { | |
193 | const video = await server.videos.get({ id: uuid }) | |
194 | const files = await checkFiles({ ...options, video, baseMockUrl }) | |
195 | ||
196 | deletedUrls = deletedUrls.concat(files) | |
197 | } | |
198 | }) | |
199 | ||
200 | it('Should upload a video and move it to the object storage with transcoding', async function () { | |
201 | this.timeout(40000) | |
202 | ||
203 | const { uuid } = await servers[1].videos.quickUpload({ name: 'video 2' }) | |
204 | uuidsToDelete.push(uuid) | |
205 | ||
206 | await waitJobs(servers) | |
207 | ||
208 | for (const server of servers) { | |
209 | const video = await server.videos.get({ id: uuid }) | |
210 | const files = await checkFiles({ ...options, video, baseMockUrl }) | |
211 | ||
212 | deletedUrls = deletedUrls.concat(files) | |
213 | } | |
214 | }) | |
215 | ||
216 | it('Should correctly delete the files', async function () { | |
217 | await servers[0].videos.remove({ id: uuidsToDelete[0] }) | |
218 | await servers[1].videos.remove({ id: uuidsToDelete[1] }) | |
219 | ||
220 | await waitJobs(servers) | |
221 | ||
222 | for (const url of deletedUrls) { | |
223 | await makeRawRequest(url, HttpStatusCode.NOT_FOUND_404) | |
224 | } | |
225 | }) | |
226 | ||
227 | it('Should have kept other files', async function () { | |
228 | for (const url of keptUrls) { | |
229 | await makeRawRequest(url, HttpStatusCode.OK_200) | |
230 | } | |
231 | }) | |
232 | ||
233 | it('Should have an empty tmp directory', async function () { | |
234 | for (const server of servers) { | |
235 | await checkTmpIsEmpty(server) | |
236 | } | |
237 | }) | |
238 | ||
1f6125be C |
239 | it('Should not have downloaded files from object storage', async function () { |
240 | for (const server of servers) { | |
241 | await expectLogDoesNotContain(server, 'from object storage') | |
242 | } | |
243 | }) | |
244 | ||
0305db28 JB |
245 | after(async function () { |
246 | mockObjectStorage.terminate() | |
247 | ||
248 | await cleanupTests(servers) | |
249 | }) | |
250 | } | |
251 | ||
252 | describe('Object storage for videos', function () { | |
253 | if (areObjectStorageTestsDisabled()) return | |
254 | ||
255 | describe('Test config', function () { | |
256 | let server: PeerTubeServer | |
257 | ||
258 | const baseConfig = { | |
259 | object_storage: { | |
260 | enabled: true, | |
261 | endpoint: 'http://' + ObjectStorageCommand.getEndpointHost(), | |
262 | region: ObjectStorageCommand.getRegion(), | |
263 | ||
264 | credentials: ObjectStorageCommand.getCredentialsConfig(), | |
265 | ||
266 | streaming_playlists: { | |
267 | bucket_name: ObjectStorageCommand.DEFAULT_PLAYLIST_BUCKET | |
268 | }, | |
269 | ||
270 | videos: { | |
271 | bucket_name: ObjectStorageCommand.DEFAULT_WEBTORRENT_BUCKET | |
272 | } | |
273 | } | |
274 | } | |
275 | ||
276 | const badCredentials = { | |
277 | access_key_id: 'AKIAIOSFODNN7EXAMPLE', | |
278 | secret_access_key: 'aJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY' | |
279 | } | |
280 | ||
281 | it('Should fail with same bucket names without prefix', function (done) { | |
282 | const config = merge({}, baseConfig, { | |
283 | object_storage: { | |
284 | streaming_playlists: { | |
285 | bucket_name: 'aaa' | |
286 | }, | |
287 | ||
288 | videos: { | |
289 | bucket_name: 'aaa' | |
290 | } | |
291 | } | |
292 | }) | |
293 | ||
294 | createSingleServer(1, config) | |
295 | .then(() => done(new Error('Did not throw'))) | |
296 | .catch(() => done()) | |
297 | }) | |
298 | ||
299 | it('Should fail with bad credentials', async function () { | |
300 | this.timeout(60000) | |
301 | ||
302 | await ObjectStorageCommand.prepareDefaultBuckets() | |
303 | ||
304 | const config = merge({}, baseConfig, { | |
305 | object_storage: { | |
306 | credentials: badCredentials | |
307 | } | |
308 | }) | |
309 | ||
310 | server = await createSingleServer(1, config) | |
311 | await setAccessTokensToServers([ server ]) | |
312 | ||
313 | const { uuid } = await server.videos.quickUpload({ name: 'video' }) | |
314 | ||
315 | await waitJobs([ server ], true) | |
316 | const video = await server.videos.get({ id: uuid }) | |
317 | ||
318 | expectStartWith(video.files[0].fileUrl, server.url) | |
319 | ||
320 | await killallServers([ server ]) | |
321 | }) | |
322 | ||
323 | it('Should succeed with credentials from env', async function () { | |
324 | this.timeout(60000) | |
325 | ||
326 | await ObjectStorageCommand.prepareDefaultBuckets() | |
327 | ||
328 | const config = merge({}, baseConfig, { | |
329 | object_storage: { | |
330 | credentials: { | |
331 | access_key_id: '', | |
332 | secret_access_key: '' | |
333 | } | |
334 | } | |
335 | }) | |
336 | ||
337 | const goodCredentials = ObjectStorageCommand.getCredentialsConfig() | |
338 | ||
339 | server = await createSingleServer(1, config, { | |
340 | env: { | |
341 | AWS_ACCESS_KEY_ID: goodCredentials.access_key_id, | |
342 | AWS_SECRET_ACCESS_KEY: goodCredentials.secret_access_key | |
343 | } | |
344 | }) | |
345 | ||
346 | await setAccessTokensToServers([ server ]) | |
347 | ||
348 | const { uuid } = await server.videos.quickUpload({ name: 'video' }) | |
349 | ||
350 | await waitJobs([ server ], true) | |
351 | const video = await server.videos.get({ id: uuid }) | |
352 | ||
353 | expectStartWith(video.files[0].fileUrl, ObjectStorageCommand.getWebTorrentBaseUrl()) | |
354 | }) | |
355 | ||
356 | after(async function () { | |
357 | await killallServers([ server ]) | |
358 | }) | |
359 | }) | |
360 | ||
361 | describe('Test simple object storage', function () { | |
362 | runTestSuite({ | |
363 | playlistBucket: 'streaming-playlists', | |
364 | webtorrentBucket: 'videos' | |
365 | }) | |
366 | }) | |
367 | ||
368 | describe('Test object storage with prefix', function () { | |
369 | runTestSuite({ | |
370 | playlistBucket: 'mybucket', | |
371 | webtorrentBucket: 'mybucket', | |
372 | ||
373 | playlistPrefix: 'streaming-playlists_', | |
374 | webtorrentPrefix: 'webtorrent_' | |
375 | }) | |
376 | }) | |
377 | ||
378 | describe('Test object storage with prefix and base URL', function () { | |
379 | runTestSuite({ | |
380 | playlistBucket: 'mybucket', | |
381 | webtorrentBucket: 'mybucket', | |
382 | ||
383 | playlistPrefix: 'streaming-playlists_', | |
384 | webtorrentPrefix: 'webtorrent_', | |
385 | ||
386 | useMockBaseUrl: true | |
387 | }) | |
388 | }) | |
389 | ||
390 | describe('Test object storage with small upload part', function () { | |
391 | runTestSuite({ | |
392 | playlistBucket: 'streaming-playlists', | |
393 | webtorrentBucket: 'videos', | |
394 | ||
395 | maxUploadPart: '5KB' | |
396 | }) | |
397 | }) | |
398 | }) |