]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/multiple-pods.ts
08fa73aa2df5387729586bea2a277c60b7d3644d
[github/Chocobozzz/PeerTube.git] / server / tests / api / multiple-pods.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4 import * as chai from 'chai'
5
6 import {
7 dateIsValid,
8 flushAndRunMultipleServers,
9 flushTests,
10 getVideo,
11 getVideosList,
12 killallServers,
13 makeFriends,
14 rateVideo,
15 removeVideo,
16 ServerInfo,
17 setAccessTokensToServers,
18 testVideoImage,
19 updateVideo,
20 uploadVideo,
21 wait,
22 webtorrentAdd
23 } from '../utils'
24
25 const expect = chai.expect
26
27 describe('Test multiple pods', function () {
28 let servers: ServerInfo[] = []
29 const toRemove = []
30 let videoUUID = ''
31
32 before(async function () {
33 this.timeout(120000)
34
35 servers = await flushAndRunMultipleServers(3)
36
37 // Get the access tokens
38 await setAccessTokensToServers(servers)
39
40 // The second pod make friend with the third
41 await makeFriends(servers[1].url, servers[1].accessToken)
42
43 // Wait for the request between pods
44 await wait(10000)
45
46 // Pod 1 make friends too
47 await makeFriends(servers[0].url, servers[0].accessToken)
48 })
49
50 it('Should not have videos for all pods', async function () {
51 for (const server of servers) {
52 const res = await getVideosList(server.url)
53 const videos = res.body.data
54 expect(videos).to.be.an('array')
55 expect(videos.length).to.equal(0)
56 }
57 })
58
59 describe('Should upload the video and propagate on each pod', function () {
60 it('Should upload the video on pod 1 and propagate on each pod', async function () {
61 // Pod 1 has video transcoding activated
62 this.timeout(15000)
63
64 const videoAttributes = {
65 name: 'my super name for pod 1',
66 category: 5,
67 licence: 4,
68 language: 9,
69 nsfw: true,
70 description: 'my super description for pod 1',
71 tags: [ 'tag1p1', 'tag2p1' ],
72 fixture: 'video_short1.webm'
73 }
74 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
75
76 await wait(11000)
77
78 // All pods should have this video
79 for (const server of servers) {
80 let baseMagnet = null
81
82 const res = await getVideosList(server.url)
83
84 const videos = res.body.data
85 expect(videos).to.be.an('array')
86 expect(videos.length).to.equal(1)
87 const video = videos[0]
88 expect(video.name).to.equal('my super name for pod 1')
89 expect(video.category).to.equal(5)
90 expect(video.categoryLabel).to.equal('Sports')
91 expect(video.licence).to.equal(4)
92 expect(video.licenceLabel).to.equal('Attribution - Non Commercial')
93 expect(video.language).to.equal(9)
94 expect(video.languageLabel).to.equal('Japanese')
95 expect(video.nsfw).to.be.ok
96 expect(video.description).to.equal('my super description for pod 1')
97 expect(video.podHost).to.equal('localhost:9001')
98 expect(video.duration).to.equal(10)
99 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
100 expect(dateIsValid(video.createdAt)).to.be.true
101 expect(dateIsValid(video.updatedAt)).to.be.true
102 expect(video.author).to.equal('root')
103
104 expect(video.files).to.have.lengthOf(1)
105
106 const file = video.files[0]
107 const magnetUri = file.magnetUri
108 expect(file.magnetUri).to.have.lengthOf.above(2)
109 expect(file.resolution).to.equal(0)
110 expect(file.resolutionLabel).to.equal('original')
111 expect(file.size).to.equal(572456)
112
113 if (server.url !== 'http://localhost:9001') {
114 expect(video.isLocal).to.be.false
115 } else {
116 expect(video.isLocal).to.be.true
117 }
118
119 // All pods should have the same magnet Uri
120 if (baseMagnet === null) {
121 baseMagnet = magnetUri
122 } else {
123 expect(baseMagnet).to.equal(magnetUri)
124 }
125
126 const test = await testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath)
127 expect(test).to.equal(true)
128 }
129 })
130
131 it('Should upload the video on pod 2 and propagate on each pod', async function () {
132 this.timeout(120000)
133
134 const videoAttributes = {
135 name: 'my super name for pod 2',
136 category: 4,
137 licence: 3,
138 language: 11,
139 nsfw: true,
140 description: 'my super description for pod 2',
141 tags: [ 'tag1p2', 'tag2p2', 'tag3p2' ],
142 fixture: 'video_short2.webm'
143 }
144 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
145
146 // Transcoding, so wait more than 22000
147 await wait(60000)
148
149 // All pods should have this video
150 for (const server of servers) {
151 let baseMagnet = {}
152
153 const res = await getVideosList(server.url)
154
155 const videos = res.body.data
156 expect(videos).to.be.an('array')
157 expect(videos.length).to.equal(2)
158 const video = videos[1]
159 expect(video.name).to.equal('my super name for pod 2')
160 expect(video.category).to.equal(4)
161 expect(video.categoryLabel).to.equal('Art')
162 expect(video.licence).to.equal(3)
163 expect(video.licenceLabel).to.equal('Attribution - No Derivatives')
164 expect(video.language).to.equal(11)
165 expect(video.languageLabel).to.equal('German')
166 expect(video.nsfw).to.be.true
167 expect(video.description).to.equal('my super description for pod 2')
168 expect(video.podHost).to.equal('localhost:9002')
169 expect(video.duration).to.equal(5)
170 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
171 expect(dateIsValid(video.createdAt)).to.be.true
172 expect(dateIsValid(video.updatedAt)).to.be.true
173 expect(video.author).to.equal('root')
174
175 expect(video.files).to.have.lengthOf(5)
176
177 // Check common attributes
178 for (const file of video.files) {
179 expect(file.magnetUri).to.have.lengthOf.above(2)
180
181 if (server.url !== 'http://localhost:9002') {
182 expect(video.isLocal).to.be.false
183 } else {
184 expect(video.isLocal).to.be.true
185 }
186
187 // All pods should have the same magnet Uri
188 if (baseMagnet[file.resolution] === undefined) {
189 baseMagnet[file.resolution] = file.magnet
190 } else {
191 expect(baseMagnet[file.resolution]).to.equal(file.magnet)
192 }
193 }
194
195 const originalFile = video.files.find(f => f.resolution === 0)
196 expect(originalFile).not.to.be.undefined
197 expect(originalFile.resolutionLabel).to.equal('original')
198 expect(originalFile.size).to.be.above(700000).and.below(720000)
199
200 const file240p = video.files.find(f => f.resolution === 240)
201 expect(file240p).not.to.be.undefined
202 expect(file240p.resolutionLabel).to.equal('240p')
203 expect(file240p.size).to.be.above(130000).and.below(150000)
204
205 const file360p = video.files.find(f => f.resolution === 360)
206 expect(file360p).not.to.be.undefined
207 expect(file360p.resolutionLabel).to.equal('360p')
208 expect(file360p.size).to.be.above(160000).and.below(180000)
209
210 const file480p = video.files.find(f => f.resolution === 480)
211 expect(file480p).not.to.be.undefined
212 expect(file480p.resolutionLabel).to.equal('480p')
213 expect(file480p.size).to.be.above(200000).and.below(220000)
214
215 const file720p = video.files.find(f => f.resolution === 720)
216 expect(file720p).not.to.be.undefined
217 expect(file720p.resolutionLabel).to.equal('720p')
218 expect(file720p.size).to.be.above(310000).and.below(320000)
219
220 const test = await testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath)
221 expect(test).to.equal(true)
222 }
223 })
224
225 it('Should upload two videos on pod 3 and propagate on each pod', async function () {
226 this.timeout(45000)
227
228 const videoAttributes1 = {
229 name: 'my super name for pod 3',
230 category: 6,
231 licence: 5,
232 language: 11,
233 nsfw: true,
234 description: 'my super description for pod 3',
235 tags: [ 'tag1p3' ],
236 fixture: 'video_short3.webm'
237 }
238 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes1)
239
240 const videoAttributes2 = {
241 name: 'my super name for pod 3-2',
242 category: 7,
243 licence: 6,
244 language: 12,
245 nsfw: false,
246 description: 'my super description for pod 3-2',
247 tags: [ 'tag2p3', 'tag3p3', 'tag4p3' ],
248 fixture: 'video_short.webm'
249 }
250 await uploadVideo(servers[2].url, servers[2].accessToken, videoAttributes2)
251
252 await wait(33000)
253
254 let baseMagnet = null
255 // All pods should have this video
256 for (const server of servers) {
257 const res = await getVideosList(server.url)
258
259 const videos = res.body.data
260 expect(videos).to.be.an('array')
261 expect(videos.length).to.equal(4)
262
263 // We not sure about the order of the two last uploads
264 let video1 = null
265 let video2 = null
266 if (videos[2].name === 'my super name for pod 3') {
267 video1 = videos[2]
268 video2 = videos[3]
269 } else {
270 video1 = videos[3]
271 video2 = videos[2]
272 }
273
274 expect(video1.name).to.equal('my super name for pod 3')
275 expect(video1.category).to.equal(6)
276 expect(video1.categoryLabel).to.equal('Travels')
277 expect(video1.licence).to.equal(5)
278 expect(video1.licenceLabel).to.equal('Attribution - Non Commercial - Share Alike')
279 expect(video1.language).to.equal(11)
280 expect(video1.languageLabel).to.equal('German')
281 expect(video1.nsfw).to.be.ok
282 expect(video1.description).to.equal('my super description for pod 3')
283 expect(video1.podHost).to.equal('localhost:9003')
284 expect(video1.duration).to.equal(5)
285 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
286 expect(video1.author).to.equal('root')
287 expect(dateIsValid(video1.createdAt)).to.be.true
288 expect(dateIsValid(video1.updatedAt)).to.be.true
289
290 expect(video1.files).to.have.lengthOf(1)
291
292 const file1 = video1.files[0]
293 expect(file1.magnetUri).to.have.lengthOf.above(2)
294 expect(file1.resolution).to.equal(0)
295 expect(file1.resolutionLabel).to.equal('original')
296 expect(file1.size).to.equal(292677)
297
298 expect(video2.name).to.equal('my super name for pod 3-2')
299 expect(video2.category).to.equal(7)
300 expect(video2.categoryLabel).to.equal('Gaming')
301 expect(video2.licence).to.equal(6)
302 expect(video2.licenceLabel).to.equal('Attribution - Non Commercial - No Derivatives')
303 expect(video2.language).to.equal(12)
304 expect(video2.languageLabel).to.equal('Korean')
305 expect(video2.nsfw).to.be.false
306 expect(video2.description).to.equal('my super description for pod 3-2')
307 expect(video2.podHost).to.equal('localhost:9003')
308 expect(video2.duration).to.equal(5)
309 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
310 expect(video2.author).to.equal('root')
311 expect(dateIsValid(video2.createdAt)).to.be.true
312 expect(dateIsValid(video2.updatedAt)).to.be.true
313
314 expect(video2.files).to.have.lengthOf(1)
315
316 const file2 = video2.files[0]
317 const magnetUri2 = file2.magnetUri
318 expect(file2.magnetUri).to.have.lengthOf.above(2)
319 expect(file2.resolution).to.equal(0)
320 expect(file2.resolutionLabel).to.equal('original')
321 expect(file2.size).to.equal(218910)
322
323 if (server.url !== 'http://localhost:9003') {
324 expect(video1.isLocal).to.be.false
325 expect(video2.isLocal).to.be.false
326 } else {
327 expect(video1.isLocal).to.be.true
328 expect(video2.isLocal).to.be.true
329 }
330
331 // All pods should have the same magnet Uri
332 if (baseMagnet === null) {
333 baseMagnet = magnetUri2
334 } else {
335 expect(baseMagnet).to.equal(magnetUri2)
336 }
337
338 const test1 = await testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath)
339 expect(test1).to.equal(true)
340
341 const test2 = await testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath)
342 expect(test2).to.equal(true)
343 }
344 })
345 })
346
347 describe('Should seed the uploaded video', function () {
348 it('Should add the file 1 by asking pod 3', async function () {
349 // Yes, this could be long
350 this.timeout(200000)
351
352 const res = await getVideosList(servers[2].url)
353
354 const video = res.body.data[0]
355 toRemove.push(res.body.data[2])
356 toRemove.push(res.body.data[3])
357
358 const torrent = await webtorrentAdd(video.files[0].magnetUri)
359 expect(torrent.files).to.be.an('array')
360 expect(torrent.files.length).to.equal(1)
361 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
362 })
363
364 it('Should add the file 2 by asking pod 1', async function () {
365 // Yes, this could be long
366 this.timeout(200000)
367
368 const res = await getVideosList(servers[0].url)
369
370 const video = res.body.data[1]
371
372 const torrent = await webtorrentAdd(video.files[0].magnetUri)
373 expect(torrent.files).to.be.an('array')
374 expect(torrent.files.length).to.equal(1)
375 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
376 })
377
378 it('Should add the file 3 by asking pod 2', async function () {
379 // Yes, this could be long
380 this.timeout(200000)
381
382 const res = await getVideosList(servers[1].url)
383
384 const video = res.body.data[2]
385
386 const torrent = await webtorrentAdd(video.files[0].magnetUri)
387 expect(torrent.files).to.be.an('array')
388 expect(torrent.files.length).to.equal(1)
389 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
390 })
391
392 it('Should add the file 3-2 by asking pod 1', async function () {
393 // Yes, this could be long
394 this.timeout(200000)
395
396 const res = await getVideosList(servers[0].url)
397
398 const video = res.body.data[3]
399
400 const torrent = await webtorrentAdd(video.files[0].magnetUri)
401 expect(torrent.files).to.be.an('array')
402 expect(torrent.files.length).to.equal(1)
403 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
404 })
405 })
406
407 describe('Should update video views, likes and dislikes', function () {
408 let localVideosPod3 = []
409 let remoteVideosPod1 = []
410 let remoteVideosPod2 = []
411 let remoteVideosPod3 = []
412
413 before(async function () {
414 const res1 = await getVideosList(servers[0].url)
415 remoteVideosPod1 = res1.body.data.filter(video => video.isLocal === false).map(video => video.id)
416
417 const res2 = await getVideosList(servers[1].url)
418 remoteVideosPod2 = res2.body.data.filter(video => video.isLocal === false).map(video => video.id)
419
420 const res3 = await getVideosList(servers[2].url)
421 localVideosPod3 = res3.body.data.filter(video => video.isLocal === true).map(video => video.id)
422 remoteVideosPod3 = res3.body.data.filter(video => video.isLocal === false).map(video => video.id)
423 })
424
425 it('Should view multiple videos on owned servers', async function () {
426 this.timeout(30000)
427
428 const tasks: Promise<any>[] = []
429 tasks.push(getVideo(servers[2].url, localVideosPod3[0]))
430 tasks.push(getVideo(servers[2].url, localVideosPod3[0]))
431 tasks.push(getVideo(servers[2].url, localVideosPod3[0]))
432 tasks.push(getVideo(servers[2].url, localVideosPod3[1]))
433
434 await Promise.all(tasks)
435
436 await wait(22000)
437
438 for (const server of servers) {
439 const res = await getVideosList(server.url)
440
441 const videos = res.body.data
442 expect(videos.find(video => video.views === 3)).to.be.an('object')
443 expect(videos.find(video => video.views === 1)).to.be.an('object')
444 }
445 })
446
447 it('Should view multiple videos on each servers', async function () {
448 this.timeout(30000)
449
450 const tasks: Promise<any>[] = []
451 tasks.push(getVideo(servers[0].url, remoteVideosPod1[0]))
452 tasks.push(getVideo(servers[1].url, remoteVideosPod2[0]))
453 tasks.push(getVideo(servers[1].url, remoteVideosPod2[0]))
454 tasks.push(getVideo(servers[2].url, remoteVideosPod3[0]))
455 tasks.push(getVideo(servers[2].url, remoteVideosPod3[1]))
456 tasks.push(getVideo(servers[2].url, remoteVideosPod3[1]))
457 tasks.push(getVideo(servers[2].url, remoteVideosPod3[1]))
458 tasks.push(getVideo(servers[2].url, localVideosPod3[1]))
459 tasks.push(getVideo(servers[2].url, localVideosPod3[1]))
460 tasks.push(getVideo(servers[2].url, localVideosPod3[1]))
461
462 await Promise.all(tasks)
463
464 await wait(22000)
465
466 let baseVideos = null
467
468 for (const server of servers) {
469 const res = await getVideosList(server.url)
470
471 const videos = res.body.data
472
473 // Initialize base videos for future comparisons
474 if (baseVideos === null) {
475 baseVideos = videos
476 return
477 }
478
479 for (const baseVideo of baseVideos) {
480 const sameVideo = videos.find(video => video.name === baseVideo.name)
481 expect(baseVideo.views).to.equal(sameVideo.views)
482 }
483 }
484 })
485
486 it('Should like and dislikes videos on different services', async function () {
487 this.timeout(30000)
488
489 const tasks: Promise<any>[] = []
490 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'like'))
491 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'dislike'))
492 tasks.push(rateVideo(servers[0].url, servers[0].accessToken, remoteVideosPod1[0], 'like'))
493 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosPod3[1], 'like'))
494 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, localVideosPod3[1], 'dislike'))
495 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosPod3[1], 'dislike'))
496 tasks.push(rateVideo(servers[2].url, servers[2].accessToken, remoteVideosPod3[0], 'like'))
497
498 await Promise.all(tasks)
499
500 await wait(22000)
501
502 let baseVideos = null
503 for (const server of servers) {
504 const res = await getVideosList(server.url)
505
506 const videos = res.body.data
507
508 // Initialize base videos for future comparisons
509 if (baseVideos === null) {
510 baseVideos = videos
511 return
512 }
513
514 baseVideos.forEach(baseVideo => {
515 const sameVideo = videos.find(video => video.name === baseVideo.name)
516 expect(baseVideo.likes).to.equal(sameVideo.likes)
517 expect(baseVideo.dislikes).to.equal(sameVideo.dislikes)
518 })
519 }
520 })
521 })
522
523 describe('Should manipulate these videos', function () {
524 it('Should update the video 3 by asking pod 3', async function () {
525 this.timeout(15000)
526
527 const attributes = {
528 name: 'my super video updated',
529 category: 10,
530 licence: 7,
531 language: 13,
532 nsfw: true,
533 description: 'my super description updated',
534 tags: [ 'tag_up_1', 'tag_up_2' ]
535 }
536
537 await updateVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, attributes)
538
539 await wait(11000)
540 })
541
542 it('Should have the video 3 updated on each pod', async function () {
543 this.timeout(200000)
544
545 for (const server of servers) {
546 const res = await getVideosList(server.url)
547
548 const videos = res.body.data
549 const videoUpdated = videos.find(video => video.name === 'my super video updated')
550
551 expect(!!videoUpdated).to.be.true
552 expect(videoUpdated.category).to.equal(10)
553 expect(videoUpdated.categoryLabel).to.equal('Entertainment')
554 expect(videoUpdated.licence).to.equal(7)
555 expect(videoUpdated.licenceLabel).to.equal('Public Domain Dedication')
556 expect(videoUpdated.language).to.equal(13)
557 expect(videoUpdated.languageLabel).to.equal('French')
558 expect(videoUpdated.nsfw).to.be.ok
559 expect(videoUpdated.description).to.equal('my super description updated')
560 expect(videoUpdated.tags).to.deep.equal([ 'tag_up_1', 'tag_up_2' ])
561 expect(dateIsValid(videoUpdated.updatedAt, 20000)).to.be.true
562
563 const file = videoUpdated.files[0]
564 expect(file.magnetUri).to.have.lengthOf.above(2)
565 expect(file.resolution).to.equal(0)
566 expect(file.resolutionLabel).to.equal('original')
567 expect(file.size).to.equal(292677)
568
569 const test = await testVideoImage(server.url, 'video_short3.webm', videoUpdated.thumbnailPath)
570 expect(test).to.equal(true)
571
572 // Avoid "duplicate torrent" errors
573 const refreshWebTorrent = true
574 const torrent = await webtorrentAdd(videoUpdated.files[0].magnetUri, refreshWebTorrent)
575 expect(torrent.files).to.be.an('array')
576 expect(torrent.files.length).to.equal(1)
577 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
578 }
579 })
580
581 it('Should remove the videos 3 and 3-2 by asking pod 3', async function () {
582 this.timeout(15000)
583
584 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id)
585 await removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id)
586
587 await wait(11000)
588 })
589
590 it('Should have videos 1 and 3 on each pod', async function () {
591 for (const server of servers) {
592 const res = await getVideosList(server.url)
593
594 const videos = res.body.data
595 expect(videos).to.be.an('array')
596 expect(videos.length).to.equal(2)
597 expect(videos[0].name).not.to.equal(videos[1].name)
598 expect(videos[0].name).not.to.equal(toRemove[0].name)
599 expect(videos[1].name).not.to.equal(toRemove[0].name)
600 expect(videos[0].name).not.to.equal(toRemove[1].name)
601 expect(videos[1].name).not.to.equal(toRemove[1].name)
602
603 videoUUID = videos.find(video => video.name === 'my super name for pod 1').uuid
604 }
605 })
606
607 it('Should get the same video by UUID on each pod', async function () {
608 let baseVideo = null
609 for (const server of servers) {
610 const res = await getVideo(server.url, videoUUID)
611
612 const video = res.body
613
614 if (baseVideo === null) {
615 baseVideo = video
616 return
617 }
618
619 expect(baseVideo.name).to.equal(video.name)
620 expect(baseVideo.uuid).to.equal(video.uuid)
621 expect(baseVideo.category).to.equal(video.category)
622 expect(baseVideo.language).to.equal(video.language)
623 expect(baseVideo.licence).to.equal(video.licence)
624 expect(baseVideo.category).to.equal(video.category)
625 expect(baseVideo.nsfw).to.equal(video.nsfw)
626 expect(baseVideo.author).to.equal(video.author)
627 expect(baseVideo.tags).to.deep.equal(video.tags)
628 }
629 })
630
631 it('Should get the preview from each pod', async function () {
632 for (const server of servers) {
633 const res = await getVideo(server.url, videoUUID)
634 const video = res.body
635
636 const test = await testVideoImage(server.url, 'video_short1-preview.webm', video.previewPath)
637 expect(test).to.equal(true)
638 }
639 })
640 })
641
642 after(async function () {
643 killallServers(servers)
644
645 // Keep the logs if the test failed
646 if (this['ok']) {
647 await flushTests()
648 }
649 })
650 })