]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/multiplePods.js
Fix adding twice a torrent in webtorrent
[github/Chocobozzz/PeerTube.git] / server / tests / api / multiplePods.js
CommitLineData
9f10b292
C
1'use strict'
2
f0f5567b
C
3const async = require('async')
4const chai = require('chai')
5const expect = chai.expect
6const pathUtils = require('path')
9f10b292 7
f0f5567b
C
8const utils = require('./utils')
9const webtorrent = require(pathUtils.join(__dirname, '../../lib/webtorrent'))
9f10b292
C
10webtorrent.silent = true
11
12describe('Test multiple pods', function () {
0c1cbbfe 13 let servers = []
bc503c2a 14 const toRemove = []
9f10b292
C
15
16 before(function (done) {
17 this.timeout(30000)
18
19 async.series([
20 // Run servers
21 function (next) {
bc503c2a
C
22 utils.flushAndRunMultipleServers(3, function (serversRun) {
23 servers = serversRun
9f10b292
C
24 next()
25 })
26 },
0c1cbbfe
C
27 // Get the access tokens
28 function (next) {
bc503c2a
C
29 async.each(servers, function (server, callbackEach) {
30 utils.loginAndGetAccessToken(server, function (err, accessToken) {
31 if (err) return callbackEach(err)
0c1cbbfe 32
bc503c2a
C
33 server.accessToken = accessToken
34 callbackEach()
0c1cbbfe
C
35 })
36 }, next)
37 },
9f10b292
C
38 // The second pod make friend with the third
39 function (next) {
b3b92647
C
40 const server = servers[1]
41 utils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
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) {
b3b92647
C
49 const server = servers[0]
50 utils.makeFriends(server.url, server.accessToken, next)
9f10b292
C
51 },
52 function (next) {
53 webtorrent.create({ host: 'client', port: '1' }, next)
54 }
55 ], done)
56 })
8c308c2b 57
9f10b292 58 it('Should not have videos for all pods', function (done) {
0c1cbbfe
C
59 async.each(servers, function (server, callback) {
60 utils.getVideosList(server.url, function (err, res) {
9f10b292 61 if (err) throw err
8c308c2b 62
9f10b292
C
63 expect(res.body).to.be.an('array')
64 expect(res.body.length).to.equal(0)
8c308c2b 65
9f10b292
C
66 callback()
67 })
68 }, done)
69 })
8c308c2b 70
9f10b292
C
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)
8c308c2b 74
ee66c593 75 async.series([
ee66c593 76 function (next) {
b6c6f935 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)
ee66c593 78 },
ee66c593 79 function (next) {
9f10b292
C
80 setTimeout(next, 11000)
81 }],
82 // All pods should have this video
83 function (err) {
84 if (err) throw err
85
0c1cbbfe 86 async.each(servers, function (server, callback) {
bc503c2a 87 let baseMagnet = null
9f10b292 88
0c1cbbfe 89 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
90 if (err) throw err
91
f0f5567b 92 const videos = res.body
9f10b292
C
93 expect(videos).to.be.an('array')
94 expect(videos.length).to.equal(1)
f0f5567b 95 const video = videos[0]
9f10b292
C
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
3a8a8b51 100 expect(video.duration).to.equal(10)
9f10b292 101
6d8ada5f
C
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
9f10b292 108 // All pods should have the same magnet Uri
bc503c2a
C
109 if (baseMagnet === null) {
110 baseMagnet = video.magnetUri
9f10b292
C
111 } else {
112 expect(video.magnetUri).to.equal.magnetUri
113 }
114
36d56024 115 utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
116 if (err) throw err
117 expect(test).to.equal(true)
118
119 callback()
120 })
9f10b292
C
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([
ee66c593 131 function (next) {
b6c6f935 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)
ee66c593
C
133 },
134 function (next) {
9f10b292
C
135 setTimeout(next, 11000)
136 }],
137 // All pods should have this video
138 function (err) {
139 if (err) throw err
140
0c1cbbfe 141 async.each(servers, function (server, callback) {
bc503c2a 142 let baseMagnet = null
9f10b292 143
0c1cbbfe 144 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
145 if (err) throw err
146
f0f5567b 147 const videos = res.body
9f10b292
C
148 expect(videos).to.be.an('array')
149 expect(videos.length).to.equal(2)
f0f5567b 150 const video = videos[1]
9f10b292
C
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
3a8a8b51 155 expect(video.duration).to.equal(5)
9f10b292 156
6d8ada5f
C
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
9f10b292 163 // All pods should have the same magnet Uri
bc503c2a
C
164 if (baseMagnet === null) {
165 baseMagnet = video.magnetUri
9f10b292
C
166 } else {
167 expect(video.magnetUri).to.equal.magnetUri
168 }
169
36d56024 170 utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
9e5f3740
C
171 if (err) throw err
172 expect(test).to.equal(true)
173
174 callback()
175 })
9f10b292
C
176 })
177 }, done)
ee66c593 178 }
9f10b292 179 )
8c308c2b
C
180 })
181
9f10b292
C
182 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
183 this.timeout(30000)
8c308c2b 184
9f10b292
C
185 async.series([
186 function (next) {
b6c6f935 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)
9f10b292
C
188 },
189 function (next) {
b6c6f935 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)
9f10b292
C
191 },
192 function (next) {
193 setTimeout(next, 22000)
194 }],
195 function (err) {
196 if (err) throw err
8c308c2b 197
bc503c2a 198 let baseMagnet = null
9f10b292 199 // All pods should have this video
0c1cbbfe
C
200 async.each(servers, function (server, callback) {
201 utils.getVideosList(server.url, function (err, res) {
9f10b292
C
202 if (err) throw err
203
f0f5567b 204 const videos = res.body
9f10b292
C
205 expect(videos).to.be.an('array')
206 expect(videos.length).to.equal(4)
3a8a8b51 207
cbe2f7c3
C
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
6d8ada5f
C
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
3a8a8b51 223 expect(video1.duration).to.equal(5)
6d8ada5f 224
6d8ada5f
C
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
3a8a8b51 229 expect(video2.duration).to.equal(5)
6d8ada5f
C
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 }
9f10b292
C
238
239 // All pods should have the same magnet Uri
bc503c2a
C
240 if (baseMagnet === null) {
241 baseMagnet = video2.magnetUri
9f10b292 242 } else {
6d8ada5f 243 expect(video2.magnetUri).to.equal.magnetUri
9f10b292
C
244 }
245
36d56024 246 utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
9e5f3740
C
247 if (err) throw err
248 expect(test).to.equal(true)
249
36d56024 250 utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
9e5f3740
C
251 if (err) throw err
252 expect(test).to.equal(true)
253
254 callback()
255 })
256 })
9f10b292
C
257 })
258 }, done)
259 }
260 )
8c308c2b 261 })
9f10b292 262 })
8c308c2b 263
9f10b292
C
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)
8c308c2b 268
0c1cbbfe 269 utils.getVideosList(servers[2].url, function (err, res) {
9f10b292 270 if (err) throw err
8c308c2b 271
f0f5567b 272 const video = res.body[0]
bc503c2a
C
273 toRemove.push(res.body[2].id)
274 toRemove.push(res.body[3].id)
8c308c2b 275
9f10b292
C
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('')
8c308c2b 280
9f10b292
C
281 done()
282 })
8c308c2b
C
283 })
284 })
285
9f10b292
C
286 it('Should add the file 2 by asking pod 1', function (done) {
287 // Yes, this could be long
288 this.timeout(200000)
8c308c2b 289
0c1cbbfe 290 utils.getVideosList(servers[0].url, function (err, res) {
9f10b292 291 if (err) throw err
8c308c2b 292
f0f5567b 293 const video = res.body[1]
0b697522 294
9f10b292
C
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('')
8c308c2b 299
9f10b292 300 done()
8c308c2b
C
301 })
302 })
9f10b292 303 })
8c308c2b 304
9f10b292
C
305 it('Should add the file 3 by asking pod 2', function (done) {
306 // Yes, this could be long
307 this.timeout(200000)
8c308c2b 308
0c1cbbfe 309 utils.getVideosList(servers[1].url, function (err, res) {
9f10b292 310 if (err) throw err
8c308c2b 311
f0f5567b 312 const video = res.body[2]
8c308c2b 313
9f10b292
C
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('')
8c308c2b 318
bc59d9e0 319 webtorrent.remove(video.magnetUri, done)
8c308c2b
C
320 })
321 })
9f10b292 322 })
8c308c2b 323
9f10b292
C
324 it('Should add the file 3-2 by asking pod 1', function (done) {
325 // Yes, this could be long
326 this.timeout(200000)
8c308c2b 327
0c1cbbfe 328 utils.getVideosList(servers[0].url, function (err, res) {
9f10b292 329 if (err) throw err
8c308c2b 330
f0f5567b 331 const video = res.body[3]
8c308c2b 332
9f10b292
C
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('')
8c308c2b 337
9f10b292 338 done()
8c308c2b
C
339 })
340 })
9f10b292 341 })
8c308c2b 342
9f10b292
C
343 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
344 this.timeout(15000)
0b697522 345
9f10b292
C
346 async.series([
347 function (next) {
bc503c2a 348 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
9f10b292
C
349 },
350 function (next) {
bc503c2a 351 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
9f10b292
C
352 }],
353 function (err) {
0b697522 354 if (err) throw err
9f10b292
C
355 setTimeout(done, 11000)
356 }
357 )
358 })
0b697522 359
9f10b292 360 it('Should have videos 1 and 3 on each pod', function (done) {
0c1cbbfe
C
361 async.each(servers, function (server, callback) {
362 utils.getVideosList(server.url, function (err, res) {
9f10b292 363 if (err) throw err
0b697522 364
f0f5567b 365 const videos = res.body
9f10b292
C
366 expect(videos).to.be.an('array')
367 expect(videos.length).to.equal(2)
2df82d42 368 expect(videos[0].id).not.to.equal(videos[1].id)
bc503c2a
C
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])
0b697522 373
9f10b292 374 callback()
0b697522 375 })
9f10b292 376 }, done)
8c308c2b 377 })
9f10b292 378 })
8c308c2b 379
9f10b292 380 after(function (done) {
0c1cbbfe
C
381 servers.forEach(function (server) {
382 process.kill(-server.app.pid)
8c308c2b 383 })
9f10b292
C
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 }
8c308c2b 392 })
9f10b292 393})