]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/multiplePods.js
Add tags support to server
[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 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)
83 },
84 function (next) {
85 setTimeout(next, 11000)
86 }],
87 // All pods should have this video
88 function (err) {
89 if (err) throw err
90
91 async.each(servers, function (server, callback) {
92 let baseMagnet = null
93
94 utils.getVideosList(server.url, function (err, res) {
95 if (err) throw err
96
97 const videos = res.body.data
98 expect(videos).to.be.an('array')
99 expect(videos.length).to.equal(1)
100 const video = videos[0]
101 expect(video.name).to.equal('my super name for pod 1')
102 expect(video.description).to.equal('my super description for pod 1')
103 expect(video.podUrl).to.equal('localhost:9001')
104 expect(video.magnetUri).to.exist
105 expect(video.duration).to.equal(10)
106 expect(video.tags).to.deep.equal([ 'tag1p1', 'tag2p1' ])
107 expect(utils.dateIsValid(video.createdDate)).to.be.true
108
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
115 // All pods should have the same magnet Uri
116 if (baseMagnet === null) {
117 baseMagnet = video.magnetUri
118 } else {
119 expect(video.magnetUri).to.equal.magnetUri
120 }
121
122 utils.testImage(server.url, 'video_short1.webm', video.thumbnailPath, function (err, test) {
123 if (err) throw err
124 expect(test).to.equal(true)
125
126 callback()
127 })
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([
138 function (next) {
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)
144 },
145 function (next) {
146 setTimeout(next, 11000)
147 }],
148 // All pods should have this video
149 function (err) {
150 if (err) throw err
151
152 async.each(servers, function (server, callback) {
153 let baseMagnet = null
154
155 utils.getVideosList(server.url, function (err, res) {
156 if (err) throw err
157
158 const videos = res.body.data
159 expect(videos).to.be.an('array')
160 expect(videos.length).to.equal(2)
161 const video = videos[1]
162 expect(video.name).to.equal('my super name for pod 2')
163 expect(video.description).to.equal('my super description for pod 2')
164 expect(video.podUrl).to.equal('localhost:9002')
165 expect(video.magnetUri).to.exist
166 expect(video.duration).to.equal(5)
167 expect(video.tags).to.deep.equal([ 'tag1p2', 'tag2p2', 'tag3p2' ])
168 expect(utils.dateIsValid(video.createdDate)).to.be.true
169
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
176 // All pods should have the same magnet Uri
177 if (baseMagnet === null) {
178 baseMagnet = video.magnetUri
179 } else {
180 expect(video.magnetUri).to.equal.magnetUri
181 }
182
183 utils.testImage(server.url, 'video_short2.webm', video.thumbnailPath, function (err, test) {
184 if (err) throw err
185 expect(test).to.equal(true)
186
187 callback()
188 })
189 })
190 }, done)
191 }
192 )
193 })
194
195 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
196 this.timeout(30000)
197
198 async.series([
199 function (next) {
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)
205 },
206 function (next) {
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)
212 },
213 function (next) {
214 setTimeout(next, 22000)
215 }],
216 function (err) {
217 if (err) throw err
218
219 let baseMagnet = null
220 // All pods should have this video
221 async.each(servers, function (server, callback) {
222 utils.getVideosList(server.url, function (err, res) {
223 if (err) throw err
224
225 const videos = res.body.data
226 expect(videos).to.be.an('array')
227 expect(videos.length).to.equal(4)
228
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
240 expect(video1.name).to.equal('my super name for pod 3')
241 expect(video1.description).to.equal('my super description for pod 3')
242 expect(video1.podUrl).to.equal('localhost:9003')
243 expect(video1.magnetUri).to.exist
244 expect(video1.duration).to.equal(5)
245 expect(video1.tags).to.deep.equal([ 'tag1p3' ])
246 expect(utils.dateIsValid(video1.createdDate)).to.be.true
247
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')
250 expect(video2.podUrl).to.equal('localhost:9003')
251 expect(video2.magnetUri).to.exist
252 expect(video2.duration).to.equal(5)
253 expect(video2.tags).to.deep.equal([ 'tag2p3', 'tag3p3', 'tag4p3' ])
254 expect(utils.dateIsValid(video2.createdDate)).to.be.true
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 }
263
264 // All pods should have the same magnet Uri
265 if (baseMagnet === null) {
266 baseMagnet = video2.magnetUri
267 } else {
268 expect(video2.magnetUri).to.equal.magnetUri
269 }
270
271 utils.testImage(server.url, 'video_short3.webm', video1.thumbnailPath, function (err, test) {
272 if (err) throw err
273 expect(test).to.equal(true)
274
275 utils.testImage(server.url, 'video_short.webm', video2.thumbnailPath, function (err, test) {
276 if (err) throw err
277 expect(test).to.equal(true)
278
279 callback()
280 })
281 })
282 })
283 }, done)
284 }
285 )
286 })
287 })
288
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)
293
294 utils.getVideosList(servers[2].url, function (err, res) {
295 if (err) throw err
296
297 const video = res.body.data[0]
298 toRemove.push(res.body.data[2].id)
299 toRemove.push(res.body.data[3].id)
300
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('')
305
306 done()
307 })
308 })
309 })
310
311 it('Should add the file 2 by asking pod 1', function (done) {
312 // Yes, this could be long
313 this.timeout(200000)
314
315 utils.getVideosList(servers[0].url, function (err, res) {
316 if (err) throw err
317
318 const video = res.body.data[1]
319
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('')
324
325 done()
326 })
327 })
328 })
329
330 it('Should add the file 3 by asking pod 2', function (done) {
331 // Yes, this could be long
332 this.timeout(200000)
333
334 utils.getVideosList(servers[1].url, function (err, res) {
335 if (err) throw err
336
337 const video = res.body.data[2]
338
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('')
343
344 webtorrent.remove(video.magnetUri, done)
345 })
346 })
347 })
348
349 it('Should add the file 3-2 by asking pod 1', function (done) {
350 // Yes, this could be long
351 this.timeout(200000)
352
353 utils.getVideosList(servers[0].url, function (err, res) {
354 if (err) throw err
355
356 const video = res.body.data[3]
357
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('')
362
363 done()
364 })
365 })
366 })
367
368 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
369 this.timeout(15000)
370
371 async.series([
372 function (next) {
373 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[0], next)
374 },
375 function (next) {
376 utils.removeVideo(servers[2].url, servers[2].accessToken, toRemove[1], next)
377 }],
378 function (err) {
379 if (err) throw err
380 setTimeout(done, 11000)
381 }
382 )
383 })
384
385 it('Should have videos 1 and 3 on each pod', function (done) {
386 async.each(servers, function (server, callback) {
387 utils.getVideosList(server.url, function (err, res) {
388 if (err) throw err
389
390 const videos = res.body.data
391 expect(videos).to.be.an('array')
392 expect(videos.length).to.equal(2)
393 expect(videos[0].id).not.to.equal(videos[1].id)
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])
398
399 callback()
400 })
401 }, done)
402 })
403 })
404
405 after(function (done) {
406 servers.forEach(function (server) {
407 process.kill(-server.app.pid)
408 })
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 }
417 })
418 })