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