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