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