]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b 3const chai = require('chai')
1a42c9e2 4const each = require('async/each')
f0f5567b 5const expect = chai.expect
9e167724 6const parallel = require('async/parallel')
1a42c9e2 7const series = require('async/series')
7f4e7c36
C
8const WebTorrent = require('webtorrent')
9const webtorrent = new WebTorrent()
9f10b292 10
8d309058
C
11const loginUtils = require('../utils/login')
12const miscsUtils = require('../utils/miscs')
13const podsUtils = require('../utils/pods')
14const serversUtils = require('../utils/servers')
15const videosUtils = require('../utils/videos')
9f10b292
C
16
17describe('Test multiple pods', function () {
0c1cbbfe 18 let servers = []
bc503c2a 19 const toRemove = []
9f10b292
C
20
21 before(function (done) {
22 this.timeout(30000)
23
1a42c9e2 24 series([
9f10b292
C
25 // Run servers
26 function (next) {
8d309058 27 serversUtils.flushAndRunMultipleServers(3, function (serversRun) {
bc503c2a 28 servers = serversRun
9f10b292
C
29 next()
30 })
31 },
0c1cbbfe
C
32 // Get the access tokens
33 function (next) {
1a42c9e2 34 each(servers, function (server, callbackEach) {
8d309058 35 loginUtils.loginAndGetAccessToken(server, function (err, accessToken) {
bc503c2a 36 if (err) return callbackEach(err)
0c1cbbfe 37
bc503c2a
C
38 server.accessToken = accessToken
39 callbackEach()
0c1cbbfe
C
40 })
41 }, next)
42 },
9f10b292
C
43 // The second pod make friend with the third
44 function (next) {
b3b92647 45 const server = servers[1]
8d309058 46 podsUtils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
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) {
b3b92647 54 const server = servers[0]
8d309058 55 podsUtils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
56 }
57 ], done)
58 })
8c308c2b 59
9f10b292 60 it('Should not have videos for all pods', function (done) {
1a42c9e2 61 each(servers, function (server, callback) {
8d309058 62 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 63 if (err) throw err
8c308c2b 64
68ce3ae0
C
65 const videos = res.body.data
66 expect(videos).to.be.an('array')
67 expect(videos.length).to.equal(0)
8c308c2b 68
9f10b292
C
69 callback()
70 })
71 }, done)
72 })
8c308c2b 73
9f10b292
C
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)
8c308c2b 77
1a42c9e2 78 series([
ee66c593 79 function (next) {
be587647
C
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'
8d309058 84 videosUtils.uploadVideo(servers[0].url, servers[0].accessToken, name, description, tags, file, next)
ee66c593 85 },
ee66c593 86 function (next) {
9f10b292
C
87 setTimeout(next, 11000)
88 }],
89 // All pods should have this video
90 function (err) {
91 if (err) throw err
92
1a42c9e2 93 each(servers, function (server, callback) {
bc503c2a 94 let baseMagnet = null
9f10b292 95
8d309058 96 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
97 if (err) throw err
98
68ce3ae0 99 const videos = res.body.data
9f10b292
C
100 expect(videos).to.be.an('array')
101 expect(videos.length).to.equal(1)
f0f5567b 102 const video = videos[0]
9f10b292
C
103 expect(video.name).to.equal('my super name for pod 1')
104 expect(video.description).to.equal('my super description for pod 1')
a4254ea1 105 expect(video.podHost).to.equal('localhost:9001')
9f10b292 106 expect(video.magnetUri).to.exist
3a8a8b51 107 expect(video.duration).to.equal(10)
be587647 108 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
feb4bdfd 109 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
79066fdf 110 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
528a9efa 111 expect(video.author).to.equal('root')
9f10b292 112
6d8ada5f
C
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
9f10b292 119 // All pods should have the same magnet Uri
bc503c2a
C
120 if (baseMagnet === null) {
121 baseMagnet = video.magnetUri
9f10b292
C
122 } else {
123 expect(video.magnetUri).to.equal.magnetUri
124 }
125
8d309058 126 videosUtils.testVideoImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
127 if (err) throw err
128 expect(test).to.equal(true)
129
130 callback()
131 })
9f10b292
C
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
1a42c9e2 141 series([
ee66c593 142 function (next) {
be587647
C
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'
8d309058 147 videosUtils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
ee66c593
C
148 },
149 function (next) {
9f10b292
C
150 setTimeout(next, 11000)
151 }],
152 // All pods should have this video
153 function (err) {
154 if (err) throw err
155
1a42c9e2 156 each(servers, function (server, callback) {
bc503c2a 157 let baseMagnet = null
9f10b292 158
8d309058 159 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
160 if (err) throw err
161
68ce3ae0 162 const videos = res.body.data
9f10b292
C
163 expect(videos).to.be.an('array')
164 expect(videos.length).to.equal(2)
f0f5567b 165 const video = videos[1]
9f10b292
C
166 expect(video.name).to.equal('my super name for pod 2')
167 expect(video.description).to.equal('my super description for pod 2')
a4254ea1 168 expect(video.podHost).to.equal('localhost:9002')
9f10b292 169 expect(video.magnetUri).to.exist
3a8a8b51 170 expect(video.duration).to.equal(5)
be587647 171 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
feb4bdfd 172 expect(miscsUtils.dateIsValid(video.createdAt)).to.be.true
79066fdf 173 expect(miscsUtils.dateIsValid(video.updatedAt)).to.be.true
528a9efa 174 expect(video.author).to.equal('root')
9f10b292 175
6d8ada5f
C
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
9f10b292 182 // All pods should have the same magnet Uri
bc503c2a
C
183 if (baseMagnet === null) {
184 baseMagnet = video.magnetUri
9f10b292
C
185 } else {
186 expect(video.magnetUri).to.equal.magnetUri
187 }
188
8d309058 189 videosUtils.testVideoImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
190 if (err) throw err
191 expect(test).to.equal(true)
192
193 callback()
194 })
9f10b292
C
195 })
196 }, done)
ee66c593 197 }
9f10b292 198 )
8c308c2b
C
199 })
200
9f10b292
C
201 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
202 this.timeout(30000)
8c308c2b 203
1a42c9e2 204 series([
9f10b292 205 function (next) {
be587647
C
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'
8d309058 210 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
211 },
212 function (next) {
be587647
C
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'
8d309058 217 videosUtils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
9f10b292
C
218 },
219 function (next) {
220 setTimeout(next, 22000)
221 }],
222 function (err) {
223 if (err) throw err
8c308c2b 224
bc503c2a 225 let baseMagnet = null
9f10b292 226 // All pods should have this video
1a42c9e2 227 each(servers, function (server, callback) {
8d309058 228 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292
C
229 if (err) throw err
230
68ce3ae0 231 const videos = res.body.data
9f10b292
C
232 expect(videos).to.be.an('array')
233 expect(videos.length).to.equal(4)
3a8a8b51 234
cbe2f7c3
C
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
6d8ada5f
C
246 expect(video1.name).to.equal('my super name for pod 3')
247 expect(video1.description).to.equal('my super description for pod 3')
a4254ea1 248 expect(video1.podHost).to.equal('localhost:9003')
6d8ada5f 249 expect(video1.magnetUri).to.exist
3a8a8b51 250 expect(video1.duration).to.equal(5)
be587647 251 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
528a9efa 252 expect(video1.author).to.equal('root')
feb4bdfd 253 expect(miscsUtils.dateIsValid(video1.createdAt)).to.be.true
79066fdf 254 expect(miscsUtils.dateIsValid(video1.updatedAt)).to.be.true
6d8ada5f 255
6d8ada5f
C
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')
a4254ea1 258 expect(video2.podHost).to.equal('localhost:9003')
6d8ada5f 259 expect(video2.magnetUri).to.exist
3a8a8b51 260 expect(video2.duration).to.equal(5)
be587647 261 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
528a9efa 262 expect(video2.author).to.equal('root')
feb4bdfd 263 expect(miscsUtils.dateIsValid(video2.createdAt)).to.be.true
79066fdf 264 expect(miscsUtils.dateIsValid(video2.updatedAt)).to.be.true
6d8ada5f
C
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 }
9f10b292
C
273
274 // All pods should have the same magnet Uri
bc503c2a
C
275 if (baseMagnet === null) {
276 baseMagnet = video2.magnetUri
9f10b292 277 } else {
6d8ada5f 278 expect(video2.magnetUri).to.equal.magnetUri
9f10b292
C
279 }
280
8d309058 281 videosUtils.testVideoImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
9e5f3740
C
282 if (err) throw err
283 expect(test).to.equal(true)
284
8d309058 285 videosUtils.testVideoImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
9e5f3740
C
286 if (err) throw err
287 expect(test).to.equal(true)
288
289 callback()
290 })
291 })
9f10b292
C
292 })
293 }, done)
294 }
295 )
8c308c2b 296 })
9f10b292 297 })
8c308c2b 298
9f10b292
C
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)
8c308c2b 303
8d309058 304 videosUtils.getVideosList(servers[2].url, function (err, res) {
9f10b292 305 if (err) throw err
8c308c2b 306
68ce3ae0 307 const video = res.body.data[0]
3d118fb5
C
308 toRemove.push(res.body.data[2])
309 toRemove.push(res.body.data[3])
8c308c2b 310
9f10b292
C
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('')
8c308c2b 315
790e65fc 316 done()
9f10b292 317 })
8c308c2b
C
318 })
319 })
320
9f10b292
C
321 it('Should add the file 2 by asking pod 1', function (done) {
322 // Yes, this could be long
323 this.timeout(200000)
8c308c2b 324
8d309058 325 videosUtils.getVideosList(servers[0].url, function (err, res) {
9f10b292 326 if (err) throw err
8c308c2b 327
68ce3ae0 328 const video = res.body.data[1]
0b697522 329
9f10b292
C
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('')
8c308c2b 334
790e65fc 335 done()
8c308c2b
C
336 })
337 })
9f10b292 338 })
8c308c2b 339
9f10b292
C
340 it('Should add the file 3 by asking pod 2', function (done) {
341 // Yes, this could be long
342 this.timeout(200000)
8c308c2b 343
8d309058 344 videosUtils.getVideosList(servers[1].url, function (err, res) {
9f10b292 345 if (err) throw err
8c308c2b 346
68ce3ae0 347 const video = res.body.data[2]
8c308c2b 348
9f10b292
C
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('')
8c308c2b 353
790e65fc 354 done()
8c308c2b
C
355 })
356 })
9f10b292 357 })
8c308c2b 358
9f10b292
C
359 it('Should add the file 3-2 by asking pod 1', function (done) {
360 // Yes, this could be long
361 this.timeout(200000)
8c308c2b 362
8d309058 363 videosUtils.getVideosList(servers[0].url, function (err, res) {
9f10b292 364 if (err) throw err
8c308c2b 365
68ce3ae0 366 const video = res.body.data[3]
8c308c2b 367
9f10b292
C
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('')
8c308c2b 372
790e65fc 373 done()
8c308c2b
C
374 })
375 })
9f10b292 376 })
3d118fb5
C
377 })
378
9e167724
C
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/*
3d118fb5
C
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) {
7f4e7c36
C
452 this.timeout(200000)
453
3d118fb5 454 each(servers, function (server, callback) {
7f4e7c36
C
455 // Avoid "duplicate torrent" errors
456 const webtorrent = new WebTorrent()
457
3d118fb5
C
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' ])
79066fdf 469 expect(miscsUtils.dateIsValid(videoUpdated.updatedAt, 20000)).to.be.true
3d118fb5 470
7f4e7c36
C
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
790e65fc 480 callback()
7f4e7c36
C
481 })
482 })
3d118fb5
C
483 })
484 }, done)
485 })
8c308c2b 486
3d118fb5 487 it('Should remove the videos 3 and 3-2 by asking pod 3', function (done) {
9f10b292 488 this.timeout(15000)
0b697522 489
1a42c9e2 490 series([
9f10b292 491 function (next) {
3d118fb5 492 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0].id, next)
9f10b292
C
493 },
494 function (next) {
3d118fb5 495 videosUtils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1].id, next)
9f10b292
C
496 }],
497 function (err) {
0b697522 498 if (err) throw err
9f10b292
C
499 setTimeout(done, 11000)
500 }
501 )
502 })
0b697522 503
9f10b292 504 it('Should have videos 1 and 3 on each pod', function (done) {
1a42c9e2 505 each(servers, function (server, callback) {
8d309058 506 videosUtils.getVideosList(server.url, function (err, res) {
9f10b292 507 if (err) throw err
0b697522 508
68ce3ae0 509 const videos = res.body.data
9f10b292
C
510 expect(videos).to.be.an('array')
511 expect(videos.length).to.equal(2)
3d118fb5
C
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)
0b697522 517
9f10b292 518 callback()
0b697522 519 })
9f10b292 520 }, done)
8c308c2b 521 })
9f10b292 522 })
9e167724 523*/
9f10b292 524 after(function (done) {
0c1cbbfe
C
525 servers.forEach(function (server) {
526 process.kill(-server.app.pid)
8c308c2b 527 })
9f10b292
C
528
529 // Keep the logs if the test failed
530 if (this.ok) {
8d309058 531 serversUtils.flushTests(done)
9f10b292
C
532 } else {
533 done()
534 }
8c308c2b 535 })
9f10b292 536})