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