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