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