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