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