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