]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/multiplePods.js
Server: udpate async to 2.0.0
[github/Chocobozzz/PeerTube.git] / server / tests / api / multiplePods.js
1 'use strict'
2
3 const chai = require('chai')
4 const each = require('async/each')
5 const expect = chai.expect
6 const pathUtils = require('path')
7 const series = require('async/series')
8
9 const utils = require('./utils')
10 const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
11 webtorrent.silent = true
12
13 describe('Test multiple pods', function () {
14 let servers = []
15 const toRemove = []
16
17 before(function (done) {
18 this.timeout(30000)
19
20 series([
21 // Run servers
22 function (next) {
23 utils.flushAndRunMultipleServers(3, function (serversRun) {
24 servers = serversRun
25 next()
26 })
27 },
28 // Get the access tokens
29 function (next) {
30 each(servers, function (server, callbackEach) {
31 utils.loginAndGetAccessToken(server, function (err, accessToken) {
32 if (err) return callbackEach(err)
33
34 server.accessToken = accessToken
35 callbackEach()
36 })
37 }, next)
38 },
39 // The second pod make friend with the third
40 function (next) {
41 const server = servers[1]
42 utils.makeFriends(server.url, server.accessToken, next)
43 },
44 // Wait for the request between pods
45 function (next) {
46 setTimeout(next, 10000)
47 },
48 // Pod 1 make friends too
49 function (next) {
50 const server = servers[0]
51 utils.makeFriends(server.url, server.accessToken, next)
52 },
53 function (next) {
54 webtorrent.create({ host: 'client', port: '1' }, next)
55 }
56 ], done)
57 })
58
59 it('Should not have videos for all pods', function (done) {
60 each(servers, function (server, callback) {
61 utils.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 utils.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 utils.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.podUrl).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(utils.dateIsValid(video.createdDate)).to.be.true
109 expect(video.author).to.equal('root')
110
111 if (server.url !== 'http://localhost:9001') {
112 expect(video.isLocal).to.be.false
113 } else {
114 expect(video.isLocal).to.be.true
115 }
116
117 // All pods should have the same magnet Uri
118 if (baseMagnet === null) {
119 baseMagnet = video.magnetUri
120 } else {
121 expect(video.magnetUri).to.equal.magnetUri
122 }
123
124 utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
125 if (err) throw err
126 expect(test).to.equal(true)
127
128 callback()
129 })
130 })
131 }, done)
132 }
133 )
134 })
135
136 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
137 this.timeout(15000)
138
139 series([
140 function (next) {
141 const name = 'my super name for pod 2'
142 const description = 'my super description for pod 2'
143 const tags = [ 'tag1p2', 'tag2p2', 'tag3p2' ]
144 const file = 'video_short2.webm'
145 utils.uploadVideo(servers[1].url, servers[1].accessToken, name, description, tags, file, next)
146 },
147 function (next) {
148 setTimeout(next, 11000)
149 }],
150 // All pods should have this video
151 function (err) {
152 if (err) throw err
153
154 each(servers, function (server, callback) {
155 let baseMagnet = null
156
157 utils.getVideosList(server.url, function (err, res) {
158 if (err) throw err
159
160 const videos = res.body.data
161 expect(videos).to.be.an('array')
162 expect(videos.length).to.equal(2)
163 const video = videos[1]
164 expect(video.name).to.equal('my super name for pod 2')
165 expect(video.description).to.equal('my super description for pod 2')
166 expect(video.podUrl).to.equal('localhost:9002')
167 expect(video.magnetUri).to.exist
168 expect(video.duration).to.equal(5)
169 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
170 expect(utils.dateIsValid(video.createdDate)).to.be.true
171 expect(video.author).to.equal('root')
172
173 if (server.url !== 'http://localhost:9002') {
174 expect(video.isLocal).to.be.false
175 } else {
176 expect(video.isLocal).to.be.true
177 }
178
179 // All pods should have the same magnet Uri
180 if (baseMagnet === null) {
181 baseMagnet = video.magnetUri
182 } else {
183 expect(video.magnetUri).to.equal.magnetUri
184 }
185
186 utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
187 if (err) throw err
188 expect(test).to.equal(true)
189
190 callback()
191 })
192 })
193 }, done)
194 }
195 )
196 })
197
198 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
199 this.timeout(30000)
200
201 series([
202 function (next) {
203 const name = 'my super name for pod 3'
204 const description = 'my super description for pod 3'
205 const tags = [ 'tag1p3' ]
206 const file = 'video_short3.webm'
207 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
208 },
209 function (next) {
210 const name = 'my super name for pod 3-2'
211 const description = 'my super description for pod 3-2'
212 const tags = [ 'tag2p3', 'tag3p3', 'tag4p3' ]
213 const file = 'video_short.webm'
214 utils.uploadVideo(servers[2].url, servers[2].accessToken, name, description, tags, file, next)
215 },
216 function (next) {
217 setTimeout(next, 22000)
218 }],
219 function (err) {
220 if (err) throw err
221
222 let baseMagnet = null
223 // All pods should have this video
224 each(servers, function (server, callback) {
225 utils.getVideosList(server.url, function (err, res) {
226 if (err) throw err
227
228 const videos = res.body.data
229 expect(videos).to.be.an('array')
230 expect(videos.length).to.equal(4)
231
232 // We not sure about the order of the two last uploads
233 let video1 = null
234 let video2 = null
235 if (videos[2].name === 'my super name for pod 3') {
236 video1 = videos[2]
237 video2 = videos[3]
238 } else {
239 video1 = videos[3]
240 video2 = videos[2]
241 }
242
243 expect(video1.name).to.equal('my super name for pod 3')
244 expect(video1.description).to.equal('my super description for pod 3')
245 expect(video1.podUrl).to.equal('localhost:9003')
246 expect(video1.magnetUri).to.exist
247 expect(video1.duration).to.equal(5)
248 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
249 expect(video1.author).to.equal('root')
250 expect(utils.dateIsValid(video1.createdDate)).to.be.true
251
252 expect(video2.name).to.equal('my super name for pod 3-2')
253 expect(video2.description).to.equal('my super description for pod 3-2')
254 expect(video2.podUrl).to.equal('localhost:9003')
255 expect(video2.magnetUri).to.exist
256 expect(video2.duration).to.equal(5)
257 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
258 expect(video2.author).to.equal('root')
259 expect(utils.dateIsValid(video2.createdDate)).to.be.true
260
261 if (server.url !== 'http://localhost:9003') {
262 expect(video1.isLocal).to.be.false
263 expect(video2.isLocal).to.be.false
264 } else {
265 expect(video1.isLocal).to.be.true
266 expect(video2.isLocal).to.be.true
267 }
268
269 // All pods should have the same magnet Uri
270 if (baseMagnet === null) {
271 baseMagnet = video2.magnetUri
272 } else {
273 expect(video2.magnetUri).to.equal.magnetUri
274 }
275
276 utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
277 if (err) throw err
278 expect(test).to.equal(true)
279
280 utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
281 if (err) throw err
282 expect(test).to.equal(true)
283
284 callback()
285 })
286 })
287 })
288 }, done)
289 }
290 )
291 })
292 })
293
294 describe('Should seed the uploaded video', function () {
295 it('Should add the file 1 by asking pod 3', function (done) {
296 // Yes, this could be long
297 this.timeout(200000)
298
299 utils.getVideosList(servers[2].url, function (err, res) {
300 if (err) throw err
301
302 const video = res.body.data[0]
303 toRemove.push(res.body.data[2].id)
304 toRemove.push(res.body.data[3].id)
305
306 webtorrent.add(video.magnetUri, function (torrent) {
307 expect(torrent.files).to.exist
308 expect(torrent.files.length).to.equal(1)
309 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
310
311 done()
312 })
313 })
314 })
315
316 it('Should add the file 2 by asking pod 1', function (done) {
317 // Yes, this could be long
318 this.timeout(200000)
319
320 utils.getVideosList(servers[0].url, function (err, res) {
321 if (err) throw err
322
323 const video = res.body.data[1]
324
325 webtorrent.add(video.magnetUri, function (torrent) {
326 expect(torrent.files).to.exist
327 expect(torrent.files.length).to.equal(1)
328 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
329
330 done()
331 })
332 })
333 })
334
335 it('Should add the file 3 by asking pod 2', function (done) {
336 // Yes, this could be long
337 this.timeout(200000)
338
339 utils.getVideosList(servers[1].url, function (err, res) {
340 if (err) throw err
341
342 const video = res.body.data[2]
343
344 webtorrent.add(video.magnetUri, function (torrent) {
345 expect(torrent.files).to.exist
346 expect(torrent.files.length).to.equal(1)
347 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
348
349 webtorrent.remove(video.magnetUri, done)
350 })
351 })
352 })
353
354 it('Should add the file 3-2 by asking pod 1', function (done) {
355 // Yes, this could be long
356 this.timeout(200000)
357
358 utils.getVideosList(servers[0].url, function (err, res) {
359 if (err) throw err
360
361 const video = res.body.data[3]
362
363 webtorrent.add(video.magnetUri, function (torrent) {
364 expect(torrent.files).to.exist
365 expect(torrent.files.length).to.equal(1)
366 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
367
368 done()
369 })
370 })
371 })
372
373 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
374 this.timeout(15000)
375
376 series([
377 function (next) {
378 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
379 },
380 function (next) {
381 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
382 }],
383 function (err) {
384 if (err) throw err
385 setTimeout(done, 11000)
386 }
387 )
388 })
389
390 it('Should have videos 1 and 3 on each pod', function (done) {
391 each(servers, function (server, callback) {
392 utils.getVideosList(server.url, function (err, res) {
393 if (err) throw err
394
395 const videos = res.body.data
396 expect(videos).to.be.an('array')
397 expect(videos.length).to.equal(2)
398 expect(videos[0].id).not.to.equal(videos[1].id)
399 expect(videos[0].id).not.to.equal(toRemove[0])
400 expect(videos[1].id).not.to.equal(toRemove[0])
401 expect(videos[0].id).not.to.equal(toRemove[1])
402 expect(videos[1].id).not.to.equal(toRemove[1])
403
404 callback()
405 })
406 }, done)
407 })
408 })
409
410 after(function (done) {
411 servers.forEach(function (server) {
412 process.kill(-server.app.pid)
413 })
414 process.kill(-webtorrent.app.pid)
415
416 // Keep the logs if the test failed
417 if (this.ok) {
418 utils.flushTests(done)
419 } else {
420 done()
421 }
422 })
423 })