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