]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/multiplePods.js
Refractoring and add thumbnails support (without tests)
[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 to_remove = []
15
16 before(function (done) {
17 this.timeout(30000)
18
19 async.series([
20 // Run servers
21 function (next) {
22 utils.flushAndRunMultipleServers(3, function (servers_run) {
23 servers = servers_run
24 next()
25 })
26 },
27 // Get the access tokens
28 function (next) {
29 async.each(servers, function (server, callback_each) {
30 utils.loginAndGetAccessToken(server, function (err, access_token) {
31 if (err) return callback_each(err)
32
33 server.access_token = access_token
34 callback_each()
35 })
36 }, next)
37 },
38 // The second pod make friend with the third
39 function (next) {
40 utils.makeFriends(servers[1].url, next)
41 },
42 // Wait for the request between pods
43 function (next) {
44 setTimeout(next, 10000)
45 },
46 // Pod 1 make friends too
47 function (next) {
48 utils.makeFriends(servers[0].url, next)
49 },
50 function (next) {
51 webtorrent.create({ host: 'client', port: '1' }, next)
52 }
53 ], done)
54 })
55
56 it('Should not have videos for all pods', function (done) {
57 async.each(servers, function (server, callback) {
58 utils.getVideosList(server.url, function (err, res) {
59 if (err) throw err
60
61 expect(res.body).to.be.an('array')
62 expect(res.body.length).to.equal(0)
63
64 callback()
65 })
66 }, done)
67 })
68
69 describe('Should upload the video and propagate on each pod', function () {
70 it('Should upload the video on pod 1 and propagate on each pod', function (done) {
71 this.timeout(15000)
72
73 async.series([
74 function (next) {
75 utils.uploadVideo(servers[0].url, servers[0].access_token, 'my super name for pod 1', 'my super description for pod 1', 'video_short1.webm', next)
76 },
77 function (next) {
78 setTimeout(next, 11000)
79 }],
80 // All pods should have this video
81 function (err) {
82 if (err) throw err
83
84 async.each(servers, function (server, callback) {
85 let base_magnet = null
86
87 utils.getVideosList(server.url, function (err, res) {
88 if (err) throw err
89
90 const videos = res.body
91 expect(videos).to.be.an('array')
92 expect(videos.length).to.equal(1)
93 const video = videos[0]
94 expect(video.name).to.equal('my super name for pod 1')
95 expect(video.description).to.equal('my super description for pod 1')
96 expect(video.podUrl).to.equal('http://localhost:9001')
97 expect(video.magnetUri).to.exist
98 expect(video.duration).to.equal(10)
99
100 if (server.url !== 'http://localhost:9001') {
101 expect(video.isLocal).to.be.false
102 } else {
103 expect(video.isLocal).to.be.true
104 }
105
106 // All pods should have the same magnet Uri
107 if (base_magnet === null) {
108 base_magnet = video.magnetUri
109 } else {
110 expect(video.magnetUri).to.equal.magnetUri
111 }
112
113 callback()
114 })
115 }, done)
116 }
117 )
118 })
119
120 it('Should upload the video on pod 2 and propagate on each pod', function (done) {
121 this.timeout(15000)
122
123 async.series([
124 function (next) {
125 utils.uploadVideo(servers[1].url, servers[1].access_token, 'my super name for pod 2', 'my super description for pod 2', 'video_short2.webm', next)
126 },
127 function (next) {
128 setTimeout(next, 11000)
129 }],
130 // All pods should have this video
131 function (err) {
132 if (err) throw err
133
134 async.each(servers, function (server, callback) {
135 let base_magnet = null
136
137 utils.getVideosList(server.url, function (err, res) {
138 if (err) throw err
139
140 const videos = res.body
141 expect(videos).to.be.an('array')
142 expect(videos.length).to.equal(2)
143 const video = videos[1]
144 expect(video.name).to.equal('my super name for pod 2')
145 expect(video.description).to.equal('my super description for pod 2')
146 expect(video.podUrl).to.equal('http://localhost:9002')
147 expect(video.magnetUri).to.exist
148 expect(video.duration).to.equal(5)
149
150 if (server.url !== 'http://localhost:9002') {
151 expect(video.isLocal).to.be.false
152 } else {
153 expect(video.isLocal).to.be.true
154 }
155
156 // All pods should have the same magnet Uri
157 if (base_magnet === null) {
158 base_magnet = video.magnetUri
159 } else {
160 expect(video.magnetUri).to.equal.magnetUri
161 }
162
163 callback()
164 })
165 }, done)
166 }
167 )
168 })
169
170 it('Should upload two videos on pod 3 and propagate on each pod', function (done) {
171 this.timeout(30000)
172
173 async.series([
174 function (next) {
175 utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3', 'my super description for pod 3', 'video_short3.webm', next)
176 },
177 function (next) {
178 utils.uploadVideo(servers[2].url, servers[2].access_token, 'my super name for pod 3-2', 'my super description for pod 3-2', 'video_short.webm', next)
179 },
180 function (next) {
181 setTimeout(next, 22000)
182 }],
183 function (err) {
184 if (err) throw err
185
186 let base_magnet = null
187 // All pods should have this video
188 async.each(servers, function (server, callback) {
189 utils.getVideosList(server.url, function (err, res) {
190 if (err) throw err
191
192 const videos = res.body
193 expect(videos).to.be.an('array')
194 expect(videos.length).to.equal(4)
195
196 // We not sure about the order of the two last uploads
197 let video1 = null
198 let video2 = null
199 if (videos[2].name === 'my super name for pod 3') {
200 video1 = videos[2]
201 video2 = videos[3]
202 } else {
203 video1 = videos[3]
204 video2 = videos[2]
205 }
206
207 expect(video1.name).to.equal('my super name for pod 3')
208 expect(video1.description).to.equal('my super description for pod 3')
209 expect(video1.podUrl).to.equal('http://localhost:9003')
210 expect(video1.magnetUri).to.exist
211 expect(video1.duration).to.equal(5)
212
213 expect(video2.name).to.equal('my super name for pod 3-2')
214 expect(video2.description).to.equal('my super description for pod 3-2')
215 expect(video2.podUrl).to.equal('http://localhost:9003')
216 expect(video2.magnetUri).to.exist
217 expect(video2.duration).to.equal(5)
218
219 if (server.url !== 'http://localhost:9003') {
220 expect(video1.isLocal).to.be.false
221 expect(video2.isLocal).to.be.false
222 } else {
223 expect(video1.isLocal).to.be.true
224 expect(video2.isLocal).to.be.true
225 }
226
227 // All pods should have the same magnet Uri
228 if (base_magnet === null) {
229 base_magnet = video2.magnetUri
230 } else {
231 expect(video2.magnetUri).to.equal.magnetUri
232 }
233
234 callback()
235 })
236 }, done)
237 }
238 )
239 })
240 })
241
242 describe('Should seed the uploaded video', function () {
243 it('Should add the file 1 by asking pod 3', function (done) {
244 // Yes, this could be long
245 this.timeout(200000)
246
247 utils.getVideosList(servers[2].url, function (err, res) {
248 if (err) throw err
249
250 const video = res.body[0]
251 to_remove.push(res.body[2].id)
252 to_remove.push(res.body[3].id)
253
254 webtorrent.add(video.magnetUri, function (torrent) {
255 expect(torrent.files).to.exist
256 expect(torrent.files.length).to.equal(1)
257 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
258
259 done()
260 })
261 })
262 })
263
264 it('Should add the file 2 by asking pod 1', function (done) {
265 // Yes, this could be long
266 this.timeout(200000)
267
268 utils.getVideosList(servers[0].url, function (err, res) {
269 if (err) throw err
270
271 const video = res.body[1]
272
273 webtorrent.add(video.magnetUri, function (torrent) {
274 expect(torrent.files).to.exist
275 expect(torrent.files.length).to.equal(1)
276 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
277
278 done()
279 })
280 })
281 })
282
283 it('Should add the file 3 by asking pod 2', function (done) {
284 // Yes, this could be long
285 this.timeout(200000)
286
287 utils.getVideosList(servers[1].url, function (err, res) {
288 if (err) throw err
289
290 const video = res.body[2]
291
292 webtorrent.add(video.magnetUri, function (torrent) {
293 expect(torrent.files).to.exist
294 expect(torrent.files.length).to.equal(1)
295 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
296
297 done()
298 })
299 })
300 })
301
302 it('Should add the file 3-2 by asking pod 1', function (done) {
303 // Yes, this could be long
304 this.timeout(200000)
305
306 utils.getVideosList(servers[0].url, function (err, res) {
307 if (err) throw err
308
309 const video = res.body[3]
310
311 webtorrent.add(video.magnetUri, function (torrent) {
312 expect(torrent.files).to.exist
313 expect(torrent.files.length).to.equal(1)
314 expect(torrent.files[0].path).to.exist.and.to.not.equal('')
315
316 done()
317 })
318 })
319 })
320
321 it('Should remove the file 3 and 3-2 by asking pod 3', function (done) {
322 this.timeout(15000)
323
324 async.series([
325 function (next) {
326 utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[0], next)
327 },
328 function (next) {
329 utils.removeVideo(servers[2].url, servers[2].access_token, to_remove[1], next)
330 }],
331 function (err) {
332 if (err) throw err
333 setTimeout(done, 11000)
334 }
335 )
336 })
337
338 it('Should have videos 1 and 3 on each pod', function (done) {
339 async.each(servers, function (server, callback) {
340 utils.getVideosList(server.url, function (err, res) {
341 if (err) throw err
342
343 const videos = res.body
344 expect(videos).to.be.an('array')
345 expect(videos.length).to.equal(2)
346 expect(videos[0].id).not.to.equal(videos[1].id)
347 expect(videos[0].id).not.to.equal(to_remove[0])
348 expect(videos[1].id).not.to.equal(to_remove[0])
349 expect(videos[0].id).not.to.equal(to_remove[1])
350 expect(videos[1].id).not.to.equal(to_remove[1])
351
352 callback()
353 })
354 }, done)
355 })
356 })
357
358 after(function (done) {
359 servers.forEach(function (server) {
360 process.kill(-server.app.pid)
361 })
362 process.kill(-webtorrent.app.pid)
363
364 // Keep the logs if the test failed
365 if (this.ok) {
366 utils.flushTests(done)
367 } else {
368 done()
369 }
370 })
371 })